Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: fix corner cases in secret declaration #124

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions client/setec/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"log"
"math/rand"
"os"
"slices"
"sync"
"time"

Expand Down Expand Up @@ -855,10 +856,18 @@ func (c StoreConfig) secretNames() ([]string, []*Fields, error) {
return nil, nil, fmt.Errorf("parse struct fields: %w", err)
}

// We don't need to deduplicate here, the constructor merges all the
// names into a map.
sec = append(sec, fs.Secrets()...)
svs = append(svs, fs)
}
// Sort and compact (deduplicate) secret names.
// Although the constructor puts them into a map, duplicates can "poison"
// the initialization, so remove them up front.
slices.Sort(sec)
sec = slices.Compact(sec)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL about slices.Compact neat!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's relatively new!

for _, name := range sec {
if name == "" {
return nil, nil, errors.New("empty secret name not allowed")
}
}
return sec, svs, nil
}
26 changes: 26 additions & 0 deletions client/setec/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,32 @@ func TestStore(t *testing.T) {
t.Errorf("Lookup(bravo): got %q, want error", s.Get())
}
})

t.Run("NewStore_emptyName", func(t *testing.T) {
st, err := setec.NewStore(ctx, setec.StoreConfig{
Client: cli,
Secrets: []string{""},
Logf: logger.Discard,
AllowLookup: true,
})
if err == nil {
t.Fatalf("NewStore(empty): got %+v, want error", st)
}
})

t.Run("NewStore_dupName", func(t *testing.T) {
// Regression: A duplicate name can poison the initialization map, so we
// need to deduplicate as part of collection.
st, err := setec.NewStore(ctx, setec.StoreConfig{
Client: cli,
Secrets: []string{"alpha", "alpha"},
Logf: logger.Discard,
})
if err != nil {
t.Fatalf("NewStore(dup): unexpected error: %v", err)
}
t.Logf("NewStore: got secret %q", st.Secret("alpha").GetString())
})
}

func TestCachedStore(t *testing.T) {
Expand Down