From 751bb810d2c2a396f171bc834d9ffbbb7797c27d Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Mon, 28 Aug 2023 11:37:24 +0200 Subject: [PATCH] fix(dockerCompose): protect of empty attributes --- pkg/domain/dockerCompose/config.go | 36 ++++++++ pkg/domain/dockerCompose/config_test.go | 105 ++++++++++++++++++++++++ 2 files changed, 141 insertions(+) diff --git a/pkg/domain/dockerCompose/config.go b/pkg/domain/dockerCompose/config.go index 18e846c..1b363d0 100644 --- a/pkg/domain/dockerCompose/config.go +++ b/pkg/domain/dockerCompose/config.go @@ -844,6 +844,10 @@ func (s *Service) mergeExistingWinPorts(ports []string) { return default: for _, port := range ports { + if len(port) <= 0 { + continue + } + src, dest, protocol := splitStringInPort(port) if !s.ExistsDestinationPort(dest) { s.SetPort(src, dest, protocol) @@ -893,6 +897,10 @@ func (s *Service) mergeExistingWinVolumes(volumes []string) { func (s *Service) mergeLastWinCapabilitiesAdd(capabilitiesAdd []string) { for _, capabilityAdd := range capabilitiesAdd { + if len(capabilityAdd) <= 0 { + continue + } + if !existsInSlice(s.CapabilitiesAdd, capabilityAdd) { s.CapabilitiesAdd = append(s.CapabilitiesAdd, capabilityAdd) } @@ -901,6 +909,10 @@ func (s *Service) mergeLastWinCapabilitiesAdd(capabilitiesAdd []string) { func (s *Service) mergeLastWinCapabilitiesDrop(capabilitiesDrop []string) { for _, capabilityDrop := range capabilitiesDrop { + if len(capabilityDrop) <= 0 { + continue + } + if !existsInSlice(s.CapabilitiesAdd, capabilityDrop) { s.CapabilitiesDrop = append(s.CapabilitiesDrop, capabilityDrop) } @@ -930,6 +942,10 @@ func (s *Service) mergeLastWinEnvironments(environments []string) { return default: for _, environment := range environments { + if len(environment) <= 0 { + continue + } + key, value := splitStringInKeyValue(environment, environmentDelimiter) s.SetEnvironment(key, value) } @@ -953,6 +969,10 @@ func (s *Service) mergeLastWinImage(image string) { func (s *Service) mergeLastWinExtraHosts(extraHosts []string) { for _, extraHost := range extraHosts { + if len(extraHost) <= 0 { + continue + } + if !existsInSlice(s.ExtraHosts, extraHost) { s.ExtraHosts = append(s.ExtraHosts, extraHost) } @@ -969,6 +989,10 @@ func (s *Service) mergeLastWinLabels(labels []string) { return default: for _, label := range labels { + if len(label) <= 0 { + continue + } + key, value := splitStringInKeyValue(label, labelDelimiter) s.SetLabel(key, value) } @@ -1004,6 +1028,10 @@ func (s *Service) mergeLastWinPorts(ports []string) { return default: for _, port := range ports { + if len(port) <= 0 { + continue + } + src, dest, protocol := splitStringInPort(port) s.SetPort(src, dest, protocol) } @@ -1012,6 +1040,10 @@ func (s *Service) mergeLastWinPorts(ports []string) { func (s *Service) mergeLastWinSecrets(secrets []string) { for _, secret := range secrets { + if len(secret) <= 0 { + continue + } + if !existsInSlice(s.Secrets, secret) { s.Secrets = append(s.Secrets, secret) } @@ -1041,6 +1073,10 @@ func (s *Service) mergeLastWinVolumes(volumes []string) { return default: for _, volume := range volumes { + if len(volume) <= 0 { + continue + } + src, dest, perm := splitStringInVolume(volume) s.SetVolume(src, dest, perm) } diff --git a/pkg/domain/dockerCompose/config_test.go b/pkg/domain/dockerCompose/config_test.go index 25daa45..72dd533 100644 --- a/pkg/domain/dockerCompose/config_test.go +++ b/pkg/domain/dockerCompose/config_test.go @@ -1418,6 +1418,17 @@ func TestService_MergeLastWin(t *testing.T) { CapabilitiesAdd: []string{"NET_RAW"}, }, }, + { + serviceDeploymentA: &dockerCompose.Service{ + CapabilitiesAdd: []string{"NET_RAW"}, + }, + serviceDeploymentB: &dockerCompose.Service{ + CapabilitiesAdd: []string{""}, + }, + expectedService: &dockerCompose.Service{ + CapabilitiesAdd: []string{"NET_RAW"}, + }, + }, // CapabilitiesDrop { @@ -1453,6 +1464,17 @@ func TestService_MergeLastWin(t *testing.T) { CapabilitiesDrop: []string{"NET_RAW"}, }, }, + { + serviceDeploymentA: &dockerCompose.Service{ + CapabilitiesDrop: []string{"NET_RAW"}, + }, + serviceDeploymentB: &dockerCompose.Service{ + CapabilitiesDrop: []string{""}, + }, + expectedService: &dockerCompose.Service{ + CapabilitiesDrop: []string{"NET_RAW"}, + }, + }, // Deploy { @@ -1567,6 +1589,17 @@ func TestService_MergeLastWin(t *testing.T) { Environments: []string{"PROXY_HOST=u.example.local"}, }, }, + { + serviceDeploymentA: &dockerCompose.Service{ + Environments: []string{"PROXY_HOST=u.example.com"}, + }, + serviceDeploymentB: &dockerCompose.Service{ + Environments: []string{""}, + }, + expectedService: &dockerCompose.Service{ + Environments: []string{"PROXY_HOST=u.example.com"}, + }, + }, // ExtraHosts { @@ -1635,6 +1668,17 @@ func TestService_MergeLastWin(t *testing.T) { ExtraHosts: []string{"extra.host.com", "extra.host.local"}, }, }, + { + serviceDeploymentA: &dockerCompose.Service{ + ExtraHosts: []string{"extra.host.local"}, + }, + serviceDeploymentB: &dockerCompose.Service{ + ExtraHosts: []string{""}, + }, + expectedService: &dockerCompose.Service{ + ExtraHosts: []string{"extra.host.local"}, + }, + }, // Image { @@ -1738,6 +1782,17 @@ func TestService_MergeLastWin(t *testing.T) { Labels: []string{"prometheus.io/scrape=true"}, }, }, + { + serviceDeploymentA: &dockerCompose.Service{ + Labels: []string{"prometheus.io/scrape=true"}, + }, + serviceDeploymentB: &dockerCompose.Service{ + Labels: []string{""}, + }, + expectedService: &dockerCompose.Service{ + Labels: []string{"prometheus.io/scrape=true"}, + }, + }, // Networks { @@ -1817,6 +1872,23 @@ func TestService_MergeLastWin(t *testing.T) { }, }, }, + { + serviceDeploymentA: &dockerCompose.Service{ + Networks: map[string]*dockerCompose.ServiceNetwork{ + "proxy": {Aliases: []string{"app.proxy.network"}}, + }, + }, + serviceDeploymentB: &dockerCompose.Service{ + Networks: map[string]*dockerCompose.ServiceNetwork{ + "proxy": {Aliases: []string{""}}, + }, + }, + expectedService: &dockerCompose.Service{ + Networks: map[string]*dockerCompose.ServiceNetwork{ + "proxy": {Aliases: []string{"app.proxy.network"}}, + }, + }, + }, // Ports { @@ -1907,6 +1979,17 @@ func TestService_MergeLastWin(t *testing.T) { Ports: []string{"10080:80/udp"}, }, }, + { + serviceDeploymentA: &dockerCompose.Service{ + Ports: []string{"80:80"}, + }, + serviceDeploymentB: &dockerCompose.Service{ + Ports: []string{""}, + }, + expectedService: &dockerCompose.Service{ + Ports: []string{"80:80"}, + }, + }, // Secrets { @@ -1975,6 +2058,17 @@ func TestService_MergeLastWin(t *testing.T) { Secrets: []string{"db_pass_credentials", "oauth2_pass_credentials"}, }, }, + { + serviceDeploymentA: &dockerCompose.Service{ + Secrets: []string{"db_pass_credentials"}, + }, + serviceDeploymentB: &dockerCompose.Service{ + Secrets: []string{""}, + }, + expectedService: &dockerCompose.Service{ + Secrets: []string{"db_pass_credentials"}, + }, + }, // ULimits { @@ -2147,6 +2241,17 @@ func TestService_MergeLastWin(t *testing.T) { Volumes: []string{"/usr/share/zoneinfo/Europe/Berlin:/etc/localtime"}, }, }, + { + serviceDeploymentA: &dockerCompose.Service{ + Volumes: []string{"/etc/localtime:/etc/localtime"}, + }, + serviceDeploymentB: &dockerCompose.Service{ + Volumes: []string{""}, + }, + expectedService: &dockerCompose.Service{ + Volumes: []string{"/etc/localtime:/etc/localtime"}, + }, + }, } for i, testCase := range testCases {