From 3c037baf079adf8a5534d75030a4963b89e7bb02 Mon Sep 17 00:00:00 2001 From: Jonathan Rudenberg Date: Mon, 16 Dec 2013 21:15:51 -0500 Subject: [PATCH 1/3] Add '.' to valid container name pattern Upstream-commit: 1940015824f5dabf1e8ffbd0c2b7c09f11f8cdf0 Component: engine --- components/engine/runtime.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/engine/runtime.go b/components/engine/runtime.go index 3268892d56..9adf40c5ee 100644 --- a/components/engine/runtime.go +++ b/components/engine/runtime.go @@ -32,7 +32,7 @@ const MaxImageDepth = 127 var ( defaultDns = []string{"8.8.8.8", "8.8.4.4"} - validContainerName = regexp.MustCompile(`^/?[a-zA-Z0-9_-]+$`) + validContainerName = regexp.MustCompile(`^/?[a-zA-Z0-9_.-]+$`) ) type Capabilities struct { From b6d7266c64831e97d6391cc925b6c25083c2beed Mon Sep 17 00:00:00 2001 From: Jonathan Rudenberg Date: Mon, 16 Dec 2013 21:17:22 -0500 Subject: [PATCH 2/3] DRY up valid container name pattern usage Upstream-commit: 3ec39ad01a5823acbb3c4ce49ce5c81258a60815 Component: engine --- components/engine/runtime.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/components/engine/runtime.go b/components/engine/runtime.go index 9adf40c5ee..f942d0b740 100644 --- a/components/engine/runtime.go +++ b/components/engine/runtime.go @@ -31,8 +31,9 @@ import ( const MaxImageDepth = 127 var ( - defaultDns = []string{"8.8.8.8", "8.8.4.4"} - validContainerName = regexp.MustCompile(`^/?[a-zA-Z0-9_.-]+$`) + defaultDns = []string{"8.8.8.8", "8.8.4.4"} + validContainerNameChars = `[a-zA-Z0-9_.-]` + validContainerNamePattern = regexp.MustCompile(`^/?` + validContainerNameChars + `+$`) ) type Capabilities struct { @@ -425,8 +426,8 @@ func (runtime *Runtime) Create(config *Config, name string) (*Container, []strin name = utils.TruncateID(id) } } else { - if !validContainerName.MatchString(name) { - return nil, nil, fmt.Errorf("Invalid container name (%s), only [a-zA-Z0-9_-] are allowed", name) + if !validContainerNamePattern.MatchString(name) { + return nil, nil, fmt.Errorf("Invalid container name (%s), only %s are allowed", name, validContainerNameChars) } } From a3e0fa246cc643930de38de4c5a983107eb66944 Mon Sep 17 00:00:00 2001 From: Jonathan Rudenberg Date: Tue, 17 Dec 2013 19:57:14 -0500 Subject: [PATCH 3/3] Add container name validation test Upstream-commit: c06ab5f9c237a647120b9b3cc433e094676fe75a Component: engine --- components/engine/integration/runtime_test.go | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/components/engine/integration/runtime_test.go b/components/engine/integration/runtime_test.go index cf4fcc1d61..cdd4818934 100644 --- a/components/engine/integration/runtime_test.go +++ b/components/engine/integration/runtime_test.go @@ -748,6 +748,54 @@ func TestRandomContainerName(t *testing.T) { } } +func TestContainerNameValidation(t *testing.T) { + eng := NewTestEngine(t) + runtime := mkRuntimeFromEngine(eng, t) + defer nuke(runtime) + + for _, test := range []struct { + Name string + Valid bool + }{ + {"abc-123_AAA.1", true}, + {"\000asdf", false}, + } { + config, _, _, err := docker.ParseRun([]string{unitTestImageID, "echo test"}, nil) + if err != nil { + if !test.Valid { + continue + } + t.Fatal(err) + } + + var shortID string + job := eng.Job("create", test.Name) + if err := job.ImportEnv(config); err != nil { + t.Fatal(err) + } + job.Stdout.AddString(&shortID) + if err := job.Run(); err != nil { + if !test.Valid { + continue + } + t.Fatal(err) + } + + container := runtime.Get(shortID) + + if container.Name != "/"+test.Name { + t.Fatalf("Expect /%s got %s", test.Name, container.Name) + } + + if c := runtime.Get("/" + test.Name); c == nil { + t.Fatalf("Couldn't retrieve test container as /%s", test.Name) + } else if c.ID != container.ID { + t.Fatalf("Container /%s has ID %s instead of %s", test.Name, c.ID, container.ID) + } + } + +} + func TestLinkChildContainer(t *testing.T) { eng := NewTestEngine(t) runtime := mkRuntimeFromEngine(eng, t)