Skip to content

Commit 48a591b

Browse files
authored
Merge pull request #3032 from crazy-max/bake-secrets-dupes
correctly remove duplicated secrets and ssh keys
2 parents 128acdb + fe17ebd commit 48a591b

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

util/buildflags/secrets.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (s Secrets) Normalize() Secrets {
2727
if len(s) == 0 {
2828
return nil
2929
}
30-
return removeDupes(s)
30+
return removeSecretDupes(s)
3131
}
3232

3333
func (s Secrets) ToPB() []*controllerapi.Secret {
@@ -155,3 +155,17 @@ func parseSecret(value string) (*controllerapi.Secret, error) {
155155
}
156156
return s.ToPB(), nil
157157
}
158+
159+
func removeSecretDupes(s []*Secret) []*Secret {
160+
var res []*Secret
161+
m := map[string]int{}
162+
for _, sec := range s {
163+
if i, ok := m[sec.ID]; ok {
164+
res[i] = sec
165+
} else {
166+
m[sec.ID] = len(res)
167+
res = append(res, sec)
168+
}
169+
}
170+
return res
171+
}

util/buildflags/secrets_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,17 @@ func TestSecrets(t *testing.T) {
8181
result := actual.Equals(expected)
8282
require.True(t, result.True())
8383
})
84+
85+
t.Run("RemoveDupes", func(t *testing.T) {
86+
secrets := Secrets{
87+
{ID: "mysecret", Env: "FOO"},
88+
{ID: "mysecret", Env: "BAR"},
89+
{ID: "mysecret2", Env: "BAZ"},
90+
}.Normalize()
91+
92+
expected := `[{"id":"mysecret","env":"BAR"},{"id":"mysecret2","env":"BAZ"}]`
93+
actual, err := json.Marshal(secrets)
94+
require.NoError(t, err)
95+
require.JSONEq(t, expected, string(actual))
96+
})
8497
}

util/buildflags/ssh.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (s SSHKeys) Normalize() SSHKeys {
2828
if len(s) == 0 {
2929
return nil
3030
}
31-
return removeDupes(s)
31+
return removeSSHDupes(s)
3232
}
3333

3434
func (s SSHKeys) ToPB() []*controllerapi.SSH {
@@ -131,3 +131,17 @@ func IsGitSSH(repo string) bool {
131131
}
132132
return url.Scheme == gitutil.SSHProtocol
133133
}
134+
135+
func removeSSHDupes(s []*SSH) []*SSH {
136+
var res []*SSH
137+
m := map[string]int{}
138+
for _, ssh := range s {
139+
if i, ok := m[ssh.ID]; ok {
140+
res[i] = ssh
141+
} else {
142+
m[ssh.ID] = len(res)
143+
res = append(res, ssh)
144+
}
145+
}
146+
return res
147+
}

util/buildflags/ssh_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,17 @@ func TestSSHKeys(t *testing.T) {
8282
result := actual.Equals(expected)
8383
require.True(t, result.True())
8484
})
85+
86+
t.Run("RemoveDupes", func(t *testing.T) {
87+
sshkeys := SSHKeys{
88+
{ID: "default"},
89+
{ID: "key", Paths: []string{"path/to/foo"}},
90+
{ID: "key", Paths: []string{"path/to/bar"}},
91+
}.Normalize()
92+
93+
expected := `[{"id":"default"},{"id":"key","paths":["path/to/bar"]}]`
94+
actual, err := json.Marshal(sshkeys)
95+
require.NoError(t, err)
96+
require.JSONEq(t, expected, string(actual))
97+
})
8598
}

0 commit comments

Comments
 (0)