Skip to content

Commit 9ab8ae3

Browse files
authored
Fix issue #1201, dedupe fulcio certs. (#539)
Signed-off-by: Ville Aikas <[email protected]>
1 parent 242c520 commit 9ab8ae3

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

pkg/ctlog/config.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,28 @@ func Unmarshal(ctx context.Context, in map[string][]byte) (*Config, error) {
218218
if err != nil {
219219
return nil, fmt.Errorf("decrypting existing private key: %w", err)
220220
}
221-
// If there's legacy rootCA entry, check it first.
221+
// Make sure to dedupe along the way just to make sure we do not have
222+
// duplicate entries.
223+
uniqueFulcioCerts := map[string][]byte{}
224+
225+
// If there's legacy rootCA entry, check it first. This will get converted
226+
// to fulcio-0 when marshaling, but we just want to make sure it's there
227+
// when we're converting from ConfigMap based configuration into secret
228+
// based one.
222229
if legacyRoot, ok := in[LegacyRootCAKey]; ok && len(legacyRoot) > 0 {
223-
ret.FulcioCerts = append(ret.FulcioCerts, legacyRoot)
230+
uniqueFulcioCerts[string(legacyRoot)] = legacyRoot
224231
}
225-
// Then loop through Fulcio roots
232+
226233
for k, v := range in {
227234
if strings.HasPrefix(k, "fulcio-") {
228-
ret.FulcioCerts = append(ret.FulcioCerts, v)
235+
uniqueFulcioCerts[string(v)] = v
229236
}
230237
}
238+
239+
// Then loop through Fulcio roots that have been deduped above
240+
for _, v := range uniqueFulcioCerts {
241+
ret.FulcioCerts = append(ret.FulcioCerts, v)
242+
}
231243
return &ret, nil
232244
}
233245

@@ -244,7 +256,7 @@ func (c *Config) MarshalConfig(ctx context.Context) (map[string][]byte, error) {
244256
// of files containing them for the RootsPemFile. Names don't matter
245257
// so we just call them fulcio-%
246258
// What matters however is to ensure that the filenames match the keys
247-
// in the configmap / secret that we construc so they get properly mounted.
259+
// in the configmap / secret that we construct so they get properly mounted.
248260
rootPems := make([]string, 0, len(c.FulcioCerts))
249261
for i := range c.FulcioCerts {
250262
rootPems = append(rootPems, fmt.Sprintf("%sfulcio-%d", rootsPemFileDir, i))

pkg/ctlog/config_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,39 @@ func TestDecrypteExistingPrivateKey(t *testing.T) {
361361
t.Fatalf("got back a nil public key")
362362
}
363363
}
364+
365+
func TestDedupeUnmarshaling(t *testing.T) {
366+
for k, v := range testConfigs {
367+
t.Logf("testing with: %s", k)
368+
cm, err := createBaseConfig(t, v)
369+
if err != nil {
370+
t.Fatalf("Failed to create base config: %v", err)
371+
}
372+
// Override the legacy rootca entry with our own for ease of testing. It
373+
// doesn't really matter what it is for this test.
374+
cm["fulcio-0"] = []byte("this is a test cert")
375+
cm["fulcio-1"] = []byte("this is a test cert")
376+
cm["fulcio-99"] = []byte("this is a different test cert")
377+
config, err := Unmarshal(context.Background(), cm)
378+
if err != nil {
379+
t.Fatalf("failed to Unmarshal: %v", err)
380+
}
381+
// We should have original root cert, 0&1 deduped into one and fulcio-99
382+
if len(config.FulcioCerts) != 3 {
383+
t.Errorf("wanted 3 fulcio certs, got: %d", len(config.FulcioCerts))
384+
}
385+
checkContains(t, config.FulcioCerts, []byte("this is a test cert"))
386+
checkContains(t, config.FulcioCerts, []byte("this is a different test cert"))
387+
checkContains(t, config.FulcioCerts, cm["rootca"])
388+
}
389+
}
390+
391+
func checkContains(t *testing.T, fulcioCerts [][]byte, cert []byte) {
392+
t.Helper()
393+
for i := range fulcioCerts {
394+
if bytes.Equal(fulcioCerts[i], cert) {
395+
return
396+
}
397+
}
398+
t.Errorf("did not find %s in fulcioCerts", string(cert))
399+
}

0 commit comments

Comments
 (0)