Skip to content

Commit

Permalink
Merge pull request #301 from jack-w-shaw/JUJU-5885_add_context_baking…
Browse files Browse the repository at this point in the history
…_constructor

feat(rootkeys): add new constructor for root key store
  • Loading branch information
alesstimec authored Jul 12, 2024
2 parents f9b21e1 + c31a632 commit 7e8973a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
19 changes: 16 additions & 3 deletions bakery/dbrootkeystore/rootkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,26 @@ type Policy struct {
// It is expected that all Backing instances passed to a given Store's
// NewStore method should refer to the same underlying database.
func (s *RootKeys) NewStore(b Backing, policy Policy) bakery.RootKeyStore {
if policy.GenerateInterval == 0 {
policy.GenerateInterval = policy.ExpiryDuration
}
cb, ok := b.(ContextBacking)
if !ok {
cb = backingWrapper{b: b}
}
return s.NewContextStore(cb, policy)
}

// NewContextStore returns a new RootKeyStore implementation that stores
// and obtains root keys from the given ContextBacking.
//
// Root keys will be generated and stored following the
// given store policy.
//
// It is expected that all ContextBacking instances passed to a given
// Store's NewContextStore method should refer to the same underlying
// database.
func (s *RootKeys) NewContextStore(cb ContextBacking, policy Policy) bakery.RootKeyStore {
if policy.GenerateInterval == 0 {
policy.GenerateInterval = policy.ExpiryDuration
}
return &store{
keys: s,
backing: cb,
Expand Down
50 changes: 43 additions & 7 deletions bakery/dbrootkeystore/rootkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func TestGetExpiredItemFromCache(t *testing.T) {
func TestContextBackingTakesPrecedence(t *testing.T) {
c := qt.New(t)

b := contextBacking{make(memBacking)}
b := fullContextBacking{make(memBacking)}
store := dbrootkeystore.NewRootKeys(5, nil).NewStore(b, dbrootkeystore.Policy{
GenerateInterval: 1 * time.Minute,
ExpiryDuration: 30 * time.Minute,
Expand All @@ -422,30 +422,66 @@ func TestContextBackingTakesPrecedence(t *testing.T) {
c.Assert(key1, qt.DeepEquals, key2)
}

type contextBacking struct {
type fullContextBacking struct {
b dbrootkeystore.Backing
}

func (b contextBacking) GetKey(id []byte) (dbrootkeystore.RootKey, error) {
func (b fullContextBacking) GetKey(id []byte) (dbrootkeystore.RootKey, error) {
return dbrootkeystore.RootKey{}, errgo.Newf("Unexected call to GetKey")
}

func (b contextBacking) GetKeyContext(_ context.Context, id []byte) (dbrootkeystore.RootKey, error) {
func (b fullContextBacking) GetKeyContext(_ context.Context, id []byte) (dbrootkeystore.RootKey, error) {
return b.b.GetKey(id)
}

func (b contextBacking) FindLatestKey(createdAfter, expiresAfter, expiresBefore time.Time) (dbrootkeystore.RootKey, error) {
func (b fullContextBacking) FindLatestKey(createdAfter, expiresAfter, expiresBefore time.Time) (dbrootkeystore.RootKey, error) {
return dbrootkeystore.RootKey{}, errgo.Newf("Unexected call to FindLatestKey")
}

func (b contextBacking) FindLatestKeyContext(_ context.Context, createdAfter, expiresAfter, expiresBefore time.Time) (dbrootkeystore.RootKey, error) {
func (b fullContextBacking) FindLatestKeyContext(_ context.Context, createdAfter, expiresAfter, expiresBefore time.Time) (dbrootkeystore.RootKey, error) {
return b.b.FindLatestKey(createdAfter, expiresAfter, expiresBefore)
}

func (b contextBacking) InsertKey(_ dbrootkeystore.RootKey) error {
func (b fullContextBacking) InsertKey(_ dbrootkeystore.RootKey) error {
return errgo.Newf("Unexected call to FindLatestKey")
}

func (b fullContextBacking) InsertKeyContext(_ context.Context, key dbrootkeystore.RootKey) error {
return b.b.InsertKey(key)
}

func TestContextBackingStore(t *testing.T) {
c := qt.New(t)

b := contextBacking{make(memBacking)}
store := dbrootkeystore.NewRootKeys(5, nil).NewContextStore(b, dbrootkeystore.Policy{
GenerateInterval: 1 * time.Minute,
ExpiryDuration: 30 * time.Minute,
})

ctx := context.Background()

key1, id, err := store.RootKey(ctx)
c.Assert(err, qt.Equals, nil)

key2, err := store.Get(ctx, id)
c.Assert(err, qt.Equals, nil)

c.Assert(key1, qt.DeepEquals, key2)
}

type contextBacking struct {
b dbrootkeystore.Backing
}

func (b contextBacking) GetKeyContext(_ context.Context, id []byte) (dbrootkeystore.RootKey, error) {
return b.b.GetKey(id)
}

func (b contextBacking) FindLatestKeyContext(_ context.Context, createdAfter, expiresAfter, expiresBefore time.Time) (dbrootkeystore.RootKey, error) {
return b.b.FindLatestKey(createdAfter, expiresAfter, expiresBefore)
}

func (b contextBacking) InsertKeyContext(_ context.Context, key dbrootkeystore.RootKey) error {
return b.b.InsertKey(key)
}
Expand Down
2 changes: 1 addition & 1 deletion httpbakery/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestThirdPartyInfoForLocationWrongURL(t *testing.T) {
_, err := httpbakery.ThirdPartyInfoForLocation(testContext, client, "http://localhost:0")
c.Logf("%v", errgo.Details(err))
c.Assert(err, qt.ErrorMatches,
`(Get|GET) ["]?http://localhost:0/discharge/info["]?: dial tcp 127.0.0.1:0: .*connection refused`)
`(Get|GET) ["]?http://localhost:0/discharge/info["]?: dial tcp (127.0.0.1|\[::1\]):0: .*connection refused`)
}

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

0 comments on commit 7e8973a

Please sign in to comment.