From 568dc6de2387f7c5d4993386433475c065b0ce0a Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Fri, 3 Jul 2020 10:53:50 +0200 Subject: [PATCH] Do not allow ACI single containers with name that includes separator used for compose services (this would breaks docker logs, docker exec, docker rm using ) --- azure/backend.go | 15 +++++++++++---- azure/backend_test.go | 10 ++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/azure/backend.go b/azure/backend.go index 6bb9f9200..34c5bdf4b 100644 --- a/azure/backend.go +++ b/azure/backend.go @@ -42,7 +42,10 @@ import ( "github.com/docker/api/errdefs" ) -const singleContainerName = "single--container--aci" +const ( + singleContainerName = "single--container--aci" + composeContainerSeparator = "_" +) // ErrNoSuchContainer is returned when the mentioned container does not exist var ErrNoSuchContainer = errors.New("no such container") @@ -135,7 +138,7 @@ func (cs *aciContainerService) List(ctx context.Context, _ bool) ([]containers.C if *container.Name == singleContainerName { containerID = *containerGroup.Name } else { - containerID = *containerGroup.Name + "_" + *container.Name + containerID = *containerGroup.Name + composeContainerSeparator + *container.Name } status := "Unknown" if container.InstanceView != nil && container.InstanceView.CurrentState != nil { @@ -155,6 +158,10 @@ func (cs *aciContainerService) List(ctx context.Context, _ bool) ([]containers.C } func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerConfig) error { + if strings.Contains(r.ID, composeContainerSeparator) { + return errors.New(fmt.Sprintf(`invalid container name. ACI container name cannot include "%s"`, composeContainerSeparator)) + } + var ports []types.ServicePortConfig for _, p := range r.Ports { ports = append(ports, types.ServicePortConfig{ @@ -204,7 +211,7 @@ func (cs *aciContainerService) Stop(ctx context.Context, containerName string, t } func getGroupAndContainerName(containerID string) (groupName string, containerName string) { - tokens := strings.Split(containerID, "_") + tokens := strings.Split(containerID, composeContainerSeparator) groupName = tokens[0] if len(tokens) > 1 { containerName = tokens[len(tokens)-1] @@ -322,7 +329,7 @@ func (cs *aciComposeService) Down(ctx context.Context, opts cli.ProjectOptions) var project types.Project if opts.Name != "" { - project = types.Project{Name:opts.Name} + project = types.Project{Name: opts.Name} } else { fullProject, err := cli.ProjectFromOptions(&opts) if err != nil { diff --git a/azure/backend_test.go b/azure/backend_test.go index 7b17f704f..c70904894 100644 --- a/azure/backend_test.go +++ b/azure/backend_test.go @@ -20,6 +20,8 @@ import ( "context" "testing" + "github.com/docker/api/containers" + "github.com/stretchr/testify/suite" . "github.com/onsi/gomega" @@ -51,6 +53,14 @@ func (suite *BackendSuiteTest) TestErrorMessageDeletingContainerFromComposeAppli Expect(err.Error()).To(Equal("cannot delete service \"service1\" from compose app \"compose-app\", you must delete the entire compose app with docker compose down")) } +func (suite *BackendSuiteTest) TestErrorMessageRunSingleContainerNameWithComposeSeparator() { + service := aciContainerService{} + err := service.Run(context.TODO(), containers.ContainerConfig{ID: "container_name"}) + + Expect(err).NotTo(BeNil()) + Expect(err.Error()).To(Equal("invalid container name. ACI container name cannot include \"_\"")) +} + func TestBackendSuite(t *testing.T) { RegisterTestingT(t) suite.Run(t, new(BackendSuiteTest))