Skip to content

Commit 6d16149

Browse files
fix: remove identifier limits
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
1 parent 5b8cda9 commit 6d16149

File tree

8 files changed

+70
-28
lines changed

8 files changed

+70
-28
lines changed

cmd/nerdctl/network_create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ package main
1919
import (
2020
"fmt"
2121

22-
"github.com/containerd/containerd/identifiers"
2322
"github.com/containerd/nerdctl/v2/pkg/api/types"
2423
"github.com/containerd/nerdctl/v2/pkg/cmd/network"
2524
"github.com/containerd/nerdctl/v2/pkg/strutil"
25+
"github.com/containerd/nerdctl/v2/pkg/validator"
2626

2727
"github.com/spf13/cobra"
2828
)
@@ -57,8 +57,8 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
5757
return err
5858
}
5959
name := args[0]
60-
if err := identifiers.Validate(name); err != nil {
61-
return fmt.Errorf("malformed name %s: %w", name, err)
60+
if err := validator.Validate(name); err != nil {
61+
return fmt.Errorf("invalid network name: %w", err)
6262
}
6363
driver, err := cmd.Flags().GetString("driver")
6464
if err != nil {

pkg/composer/composer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import (
2626
composecli "github.com/compose-spec/compose-go/v2/cli"
2727
compose "github.com/compose-spec/compose-go/v2/types"
2828
"github.com/containerd/containerd"
29-
"github.com/containerd/containerd/identifiers"
3029
"github.com/containerd/log"
3130
"github.com/containerd/nerdctl/v2/pkg/composer/serviceparser"
3231
"github.com/containerd/nerdctl/v2/pkg/reflectutil"
32+
"github.com/containerd/nerdctl/v2/pkg/validator"
3333
)
3434

3535
// Options groups the command line options recommended for a Compose implementation (ProjectOptions) and extra options for nerdctl
@@ -61,8 +61,8 @@ func New(o Options, client *containerd.Client) (*Composer, error) {
6161
}
6262

6363
if o.Project != "" {
64-
if err := identifiers.Validate(o.Project); err != nil {
65-
return nil, fmt.Errorf("got invalid project name %q: %w", o.Project, err)
64+
if err := validator.Validate(o.Project); err != nil {
65+
return nil, fmt.Errorf("invalid project name: %w", err)
6666
}
6767
}
6868

pkg/composer/serviceparser/build.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import (
2323
"strings"
2424

2525
"github.com/compose-spec/compose-go/v2/types"
26-
"github.com/containerd/containerd/identifiers"
2726
"github.com/containerd/errdefs"
2827
"github.com/containerd/log"
2928
"github.com/containerd/nerdctl/v2/pkg/reflectutil"
29+
"github.com/containerd/nerdctl/v2/pkg/validator"
3030

3131
securejoin "github.com/cyphar/filepath-securejoin"
3232
)
@@ -83,9 +83,11 @@ func parseBuildConfig(c *types.BuildConfig, project *types.Project, imageName st
8383

8484
for _, s := range c.Secrets {
8585
fileRef := types.FileReferenceConfig(s)
86-
if err := identifiers.Validate(fileRef.Source); err != nil {
87-
return nil, fmt.Errorf("secret source %q is invalid: %w", fileRef.Source, err)
86+
87+
if err := validator.Validate(fileRef.Source); err != nil {
88+
return nil, fmt.Errorf("invalid secret source name: %w", err)
8889
}
90+
8991
projectSecret, ok := project.Secrets[fileRef.Source]
9092
if !ok {
9193
return nil, fmt.Errorf("build: secret %s is undefined", fileRef.Source)

pkg/composer/serviceparser/serviceparser.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ import (
3030

3131
"github.com/compose-spec/compose-go/v2/types"
3232
"github.com/containerd/containerd/contrib/nvidia"
33-
"github.com/containerd/containerd/identifiers"
3433
"github.com/containerd/log"
3534
"github.com/containerd/nerdctl/v2/pkg/reflectutil"
35+
"github.com/containerd/nerdctl/v2/pkg/validator"
3636
)
3737

3838
// ComposeExtensionKey defines fields used to implement extension features.
@@ -846,8 +846,8 @@ func fileReferenceConfigToFlagV(c types.FileReferenceConfig, project *types.Proj
846846
log.L.Warnf("Ignoring: %s: %+v", objType, unknown)
847847
}
848848

849-
if err := identifiers.Validate(c.Source); err != nil {
850-
return "", fmt.Errorf("%s source %q is invalid: %w", objType, c.Source, err)
849+
if err := validator.Validate(c.Source); err != nil {
850+
return "", fmt.Errorf("invalid source name for %s: %w", objType, err)
851851
}
852852

853853
var obj types.FileObjectConfig

pkg/mountutil/mountutil.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ import (
2323
"runtime"
2424
"strings"
2525

26-
"github.com/containerd/containerd/identifiers"
2726
"github.com/containerd/containerd/oci"
2827
"github.com/containerd/containerd/pkg/userns"
2928
"github.com/containerd/log"
3029
"github.com/containerd/nerdctl/v2/pkg/idgen"
3130
"github.com/containerd/nerdctl/v2/pkg/mountutil/volumestore"
3231
"github.com/containerd/nerdctl/v2/pkg/strutil"
32+
"github.com/containerd/nerdctl/v2/pkg/validator"
3333
"github.com/opencontainers/runtime-spec/specs-go"
3434
)
3535

@@ -258,7 +258,7 @@ func createDirOnHost(src string, createDir bool) error {
258258
}
259259

260260
func isNamedVolume(s string) bool {
261-
err := identifiers.Validate(s)
261+
err := validator.Validate(s)
262262

263263
// If the volume name is invalid, we assume it is a path
264264
return err == nil

pkg/mountutil/volumestore/volumestore.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ import (
2323
"os"
2424
"path/filepath"
2525

26-
"github.com/containerd/containerd/identifiers"
2726
"github.com/containerd/errdefs"
2827
"github.com/containerd/log"
2928
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
3029
"github.com/containerd/nerdctl/v2/pkg/lockutil"
3130
"github.com/containerd/nerdctl/v2/pkg/strutil"
31+
"github.com/containerd/nerdctl/v2/pkg/validator"
3232
)
3333

3434
const (
@@ -108,9 +108,10 @@ func (vs *volumeStore) Unlock() error {
108108
// Create will create a new volume, or return an existing one if there is one already by that name
109109
// Besides a possible locking error, it might return ErrInvalidArgument, hard filesystem errors, json errors
110110
func (vs *volumeStore) Create(name string, labels []string) (*native.Volume, error) {
111-
if err := identifiers.Validate(name); err != nil {
112-
return nil, fmt.Errorf("malformed volume name: %w (%w)", err, errdefs.ErrInvalidArgument)
111+
if err := validator.Validate(name); err != nil {
112+
return nil, fmt.Errorf("invalid volume name: %w", err)
113113
}
114+
114115
volPath := filepath.Join(vs.dir, name)
115116
volDataPath := filepath.Join(volPath, dataDirName)
116117
volFilePath := filepath.Join(volPath, volumeJSONFileName)
@@ -177,8 +178,8 @@ func (vs *volumeStore) Create(name string, labels []string) (*native.Volume, err
177178
// Get retrieves a native volume from the store
178179
// Besides a possible locking error, it might return ErrInvalidArgument, ErrNotFound, or a filesystem error
179180
func (vs *volumeStore) Get(name string, size bool) (*native.Volume, error) {
180-
if err := identifiers.Validate(name); err != nil {
181-
return nil, fmt.Errorf("malformed volume name %q: %w", name, err)
181+
if err := validator.Validate(name); err != nil {
182+
return nil, fmt.Errorf("invalid volume name: %w", err)
182183
}
183184
volPath := filepath.Join(vs.dir, name)
184185
volDataPath := filepath.Join(volPath, dataDirName)
@@ -273,8 +274,8 @@ func (vs *volumeStore) Remove(names []string) (removed []string, warns []error,
273274
fn := func() error {
274275
for _, name := range names {
275276
// Invalid name, soft error
276-
if err := identifiers.Validate(name); err != nil {
277-
warns = append(warns, fmt.Errorf("malformed volume name: %w (%w)", err, errdefs.ErrInvalidArgument))
277+
if err := validator.Validate(name); err != nil {
278+
warns = append(warns, fmt.Errorf("invalid volume name: %w", err))
278279
continue
279280
}
280281
dir := filepath.Join(vs.dir, name)

pkg/namestore/namestore.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"path/filepath"
2323
"strings"
2424

25-
"github.com/containerd/containerd/identifiers"
2625
"github.com/containerd/nerdctl/v2/pkg/lockutil"
26+
"github.com/containerd/nerdctl/v2/pkg/validator"
2727
)
2828

2929
func New(dataStore, ns string) (NameStore, error) {
@@ -48,8 +48,8 @@ type nameStore struct {
4848
}
4949

5050
func (x *nameStore) Acquire(name, id string) error {
51-
if err := identifiers.Validate(name); err != nil {
52-
return fmt.Errorf("invalid name %q: %w", name, err)
51+
if err := validator.Validate(name); err != nil {
52+
return fmt.Errorf("invalid name: %w", err)
5353
}
5454
if strings.TrimSpace(id) != id {
5555
return fmt.Errorf("untrimmed ID %q", id)
@@ -68,8 +68,8 @@ func (x *nameStore) Release(name, id string) error {
6868
if name == "" {
6969
return nil
7070
}
71-
if err := identifiers.Validate(name); err != nil {
72-
return fmt.Errorf("invalid name %q: %w", name, err)
71+
if err := validator.Validate(name); err != nil {
72+
return fmt.Errorf("invalid name: %w", err)
7373
}
7474
if strings.TrimSpace(id) != id {
7575
return fmt.Errorf("untrimmed ID %q", id)
@@ -95,8 +95,8 @@ func (x *nameStore) Rename(oldName, id, newName string) error {
9595
if oldName == "" || newName == "" {
9696
return nil
9797
}
98-
if err := identifiers.Validate(newName); err != nil {
99-
return fmt.Errorf("invalid name %q: %w", oldName, err)
98+
if err := validator.Validate(newName); err != nil {
99+
return fmt.Errorf("invalid new name: %w", err)
100100
}
101101
fn := func() error {
102102
oldFileName := filepath.Join(x.dir, oldName)

pkg/validator/validate.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package validator
18+
19+
import (
20+
"fmt"
21+
"regexp"
22+
23+
"github.com/containerd/errdefs"
24+
)
25+
26+
const AllowedIdentfierChars = `[a-zA-Z0-9][a-zA-Z0-9_.-]`
27+
28+
var AllowedIdentifierPattern = regexp.MustCompile(`^` + AllowedIdentfierChars + `+$`)
29+
30+
func Validate(s string) error {
31+
if len(s) == 0 {
32+
return fmt.Errorf("identifier must not be empty %w", errdefs.ErrInvalidArgument)
33+
}
34+
35+
if !AllowedIdentifierPattern.MatchString(s) {
36+
return fmt.Errorf("identifier %q must match pattern %q: %w", s, AllowedIdentfierChars, errdefs.ErrInvalidArgument)
37+
}
38+
return nil
39+
}

0 commit comments

Comments
 (0)