diff --git a/bakery/dbrootkeystore/rootkey.go b/bakery/dbrootkeystore/rootkey.go index 0dfa930..3b72e86 100644 --- a/bakery/dbrootkeystore/rootkey.go +++ b/bakery/dbrootkeystore/rootkey.go @@ -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, diff --git a/bakery/dbrootkeystore/rootkey_test.go b/bakery/dbrootkeystore/rootkey_test.go index 2a33633..b7be453 100644 --- a/bakery/dbrootkeystore/rootkey_test.go +++ b/bakery/dbrootkeystore/rootkey_test.go @@ -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, @@ -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) } diff --git a/httpbakery/keyring_test.go b/httpbakery/keyring_test.go index cfae4f8..dc62508 100644 --- a/httpbakery/keyring_test.go +++ b/httpbakery/keyring_test.go @@ -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) {