fix(dockerCompose): protect of empty attributes

This commit is contained in:
Markus Pesch 2023-08-28 11:37:24 +02:00
parent 9b469bd8c6
commit 751bb810d2
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
2 changed files with 141 additions and 0 deletions

View File

@ -844,6 +844,10 @@ func (s *Service) mergeExistingWinPorts(ports []string) {
return return
default: default:
for _, port := range ports { for _, port := range ports {
if len(port) <= 0 {
continue
}
src, dest, protocol := splitStringInPort(port) src, dest, protocol := splitStringInPort(port)
if !s.ExistsDestinationPort(dest) { if !s.ExistsDestinationPort(dest) {
s.SetPort(src, dest, protocol) s.SetPort(src, dest, protocol)
@ -893,6 +897,10 @@ func (s *Service) mergeExistingWinVolumes(volumes []string) {
func (s *Service) mergeLastWinCapabilitiesAdd(capabilitiesAdd []string) { func (s *Service) mergeLastWinCapabilitiesAdd(capabilitiesAdd []string) {
for _, capabilityAdd := range capabilitiesAdd { for _, capabilityAdd := range capabilitiesAdd {
if len(capabilityAdd) <= 0 {
continue
}
if !existsInSlice(s.CapabilitiesAdd, capabilityAdd) { if !existsInSlice(s.CapabilitiesAdd, capabilityAdd) {
s.CapabilitiesAdd = append(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) { func (s *Service) mergeLastWinCapabilitiesDrop(capabilitiesDrop []string) {
for _, capabilityDrop := range capabilitiesDrop { for _, capabilityDrop := range capabilitiesDrop {
if len(capabilityDrop) <= 0 {
continue
}
if !existsInSlice(s.CapabilitiesAdd, capabilityDrop) { if !existsInSlice(s.CapabilitiesAdd, capabilityDrop) {
s.CapabilitiesDrop = append(s.CapabilitiesDrop, capabilityDrop) s.CapabilitiesDrop = append(s.CapabilitiesDrop, capabilityDrop)
} }
@ -930,6 +942,10 @@ func (s *Service) mergeLastWinEnvironments(environments []string) {
return return
default: default:
for _, environment := range environments { for _, environment := range environments {
if len(environment) <= 0 {
continue
}
key, value := splitStringInKeyValue(environment, environmentDelimiter) key, value := splitStringInKeyValue(environment, environmentDelimiter)
s.SetEnvironment(key, value) s.SetEnvironment(key, value)
} }
@ -953,6 +969,10 @@ func (s *Service) mergeLastWinImage(image string) {
func (s *Service) mergeLastWinExtraHosts(extraHosts []string) { func (s *Service) mergeLastWinExtraHosts(extraHosts []string) {
for _, extraHost := range extraHosts { for _, extraHost := range extraHosts {
if len(extraHost) <= 0 {
continue
}
if !existsInSlice(s.ExtraHosts, extraHost) { if !existsInSlice(s.ExtraHosts, extraHost) {
s.ExtraHosts = append(s.ExtraHosts, extraHost) s.ExtraHosts = append(s.ExtraHosts, extraHost)
} }
@ -969,6 +989,10 @@ func (s *Service) mergeLastWinLabels(labels []string) {
return return
default: default:
for _, label := range labels { for _, label := range labels {
if len(label) <= 0 {
continue
}
key, value := splitStringInKeyValue(label, labelDelimiter) key, value := splitStringInKeyValue(label, labelDelimiter)
s.SetLabel(key, value) s.SetLabel(key, value)
} }
@ -1004,6 +1028,10 @@ func (s *Service) mergeLastWinPorts(ports []string) {
return return
default: default:
for _, port := range ports { for _, port := range ports {
if len(port) <= 0 {
continue
}
src, dest, protocol := splitStringInPort(port) src, dest, protocol := splitStringInPort(port)
s.SetPort(src, dest, protocol) s.SetPort(src, dest, protocol)
} }
@ -1012,6 +1040,10 @@ func (s *Service) mergeLastWinPorts(ports []string) {
func (s *Service) mergeLastWinSecrets(secrets []string) { func (s *Service) mergeLastWinSecrets(secrets []string) {
for _, secret := range secrets { for _, secret := range secrets {
if len(secret) <= 0 {
continue
}
if !existsInSlice(s.Secrets, secret) { if !existsInSlice(s.Secrets, secret) {
s.Secrets = append(s.Secrets, secret) s.Secrets = append(s.Secrets, secret)
} }
@ -1041,6 +1073,10 @@ func (s *Service) mergeLastWinVolumes(volumes []string) {
return return
default: default:
for _, volume := range volumes { for _, volume := range volumes {
if len(volume) <= 0 {
continue
}
src, dest, perm := splitStringInVolume(volume) src, dest, perm := splitStringInVolume(volume)
s.SetVolume(src, dest, perm) s.SetVolume(src, dest, perm)
} }

View File

@ -1418,6 +1418,17 @@ func TestService_MergeLastWin(t *testing.T) {
CapabilitiesAdd: []string{"NET_RAW"}, CapabilitiesAdd: []string{"NET_RAW"},
}, },
}, },
{
serviceDeploymentA: &dockerCompose.Service{
CapabilitiesAdd: []string{"NET_RAW"},
},
serviceDeploymentB: &dockerCompose.Service{
CapabilitiesAdd: []string{""},
},
expectedService: &dockerCompose.Service{
CapabilitiesAdd: []string{"NET_RAW"},
},
},
// CapabilitiesDrop // CapabilitiesDrop
{ {
@ -1453,6 +1464,17 @@ func TestService_MergeLastWin(t *testing.T) {
CapabilitiesDrop: []string{"NET_RAW"}, CapabilitiesDrop: []string{"NET_RAW"},
}, },
}, },
{
serviceDeploymentA: &dockerCompose.Service{
CapabilitiesDrop: []string{"NET_RAW"},
},
serviceDeploymentB: &dockerCompose.Service{
CapabilitiesDrop: []string{""},
},
expectedService: &dockerCompose.Service{
CapabilitiesDrop: []string{"NET_RAW"},
},
},
// Deploy // Deploy
{ {
@ -1567,6 +1589,17 @@ func TestService_MergeLastWin(t *testing.T) {
Environments: []string{"PROXY_HOST=u.example.local"}, 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 // ExtraHosts
{ {
@ -1635,6 +1668,17 @@ func TestService_MergeLastWin(t *testing.T) {
ExtraHosts: []string{"extra.host.com", "extra.host.local"}, 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 // Image
{ {
@ -1738,6 +1782,17 @@ func TestService_MergeLastWin(t *testing.T) {
Labels: []string{"prometheus.io/scrape=true"}, 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 // 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 // Ports
{ {
@ -1907,6 +1979,17 @@ func TestService_MergeLastWin(t *testing.T) {
Ports: []string{"10080:80/udp"}, 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 // Secrets
{ {
@ -1975,6 +2058,17 @@ func TestService_MergeLastWin(t *testing.T) {
Secrets: []string{"db_pass_credentials", "oauth2_pass_credentials"}, 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 // ULimits
{ {
@ -2147,6 +2241,17 @@ func TestService_MergeLastWin(t *testing.T) {
Volumes: []string{"/usr/share/zoneinfo/Europe/Berlin:/etc/localtime"}, 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 { for i, testCase := range testCases {