Skip to content

Commit

Permalink
fix: Secret references must be sorted to ensure stable output
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgubler committed Jul 5, 2024
1 parent 6c9ed3e commit 5dda337
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pkg/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,19 @@ func composeServiceToContainer(
envFrom = append(envFrom, core.EnvFromSource{SecretRef: &core.SecretEnvSource{LocalObjectReference: core.LocalObjectReference{Name: secret.Name}}})
}
env := []core.EnvVar{}
for key, value := range workload.AsCompose().Environment {

// We need to iterate over the Environment map in an ordered way, because otherwise the resulting secret references will have a non-deterministic order.
// Hence we get the keys first and sort them.
keys := make([]string, len(workload.AsCompose().Environment))
i := 0
for k, _ := range workload.AsCompose().Environment {

Check failure on line 322 in pkg/converter/converter.go

View workflow job for this annotation

GitHub Actions / lint

S1005: unnecessary assignment to the blank identifier (gosimple)
keys[i] = k
i++
}
sort.Strings(keys)
// Now iterate over the sorted keys
for _, key := range keys {
value := workload.AsCompose().Environment[key]
if value != nil && strings.HasPrefix(*value, SecretRefMagic+":") {
// we've encountered a reference to another secret (starting with "$_ref_:" in the compose file)
refValue := (*value)[len(SecretRefMagic)+1:]
Expand All @@ -326,6 +338,7 @@ func composeServiceToContainer(
env = append(env, core.EnvVar{Name: key, ValueFrom: &core.EnvVarSource{SecretKeyRef: &core.SecretKeySelector{LocalObjectReference: core.LocalObjectReference{Name: refStrings[0]}, Key: refStrings[1]}}})
}
}

return core.Container{
Name: composeService.Name + refSlug,
Image: composeService.Image,
Expand Down
2 changes: 2 additions & 0 deletions tests/golden/env-vars/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ services:
- BAR=${BAR}
- something_else=${BAZ}
- "PASSWORD=$_ref_:mongodb-secret:password"
- "FOOREF=$_ref_:foo:fooooooo"
- "BARREF=$_ref_:bar:baaaaaar"
10 changes: 10 additions & 0 deletions tests/golden/env-vars/manifests/fooBar-oasp-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ spec:
topologyKey: kubernetes.io/hostname
containers:
- env:
- name: BARREF
valueFrom:
secretKeyRef:
key: baaaaaar
name: bar
- name: FOOREF
valueFrom:
secretKeyRef:
key: fooooooo
name: foo
- name: PASSWORD
valueFrom:
secretKeyRef:
Expand Down

0 comments on commit 5dda337

Please sign in to comment.