Skip to content

Commit

Permalink
Fix consumed secret watcher to return initial data
Browse files Browse the repository at this point in the history
  • Loading branch information
wallyworld committed Mar 7, 2023
1 parent 18349be commit c4f5f6d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
18 changes: 14 additions & 4 deletions state/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -1336,16 +1336,26 @@ func (w *consumedSecretsWatcher) Changes() <-chan []string {
return w.out
}

func (w *consumedSecretsWatcher) initial() error {
func (w *consumedSecretsWatcher) initial() ([]string, error) {
var doc secretConsumerDoc
secretConsumersCollection, closer := w.db.GetCollection(secretConsumersC)
defer closer()

var ids []string
iter := secretConsumersCollection.Find(bson.D{{"consumer-tag", w.consumer}}).Iter()
for iter.Next(&doc) {
w.knownRevisions[doc.DocID] = doc.LatestRevision
if doc.LatestRevision < 2 {
continue
}
uriStr := strings.Split(w.backend.localID(doc.DocID), "#")[0]
uri, err := secrets.ParseURI(uriStr)
if err != nil {
return nil, errors.Trace(err)
}
ids = append(ids, uri.String())
}
return errors.Trace(iter.Close())
return ids, errors.Trace(iter.Close())
}

func (w *consumedSecretsWatcher) merge(currentChanges []string, change watcher.Change) ([]string, error) {
Expand Down Expand Up @@ -1403,11 +1413,11 @@ func (w *consumedSecretsWatcher) loop() (err error) {
w.watcher.WatchCollectionWithFilter(secretConsumersC, ch, filter)
defer w.watcher.UnwatchCollection(secretConsumersC, ch)

if err = w.initial(); err != nil {
changes, err := w.initial()
if err != nil {
return errors.Trace(err)
}

var changes []string
out := w.out
for {
select {
Expand Down
46 changes: 40 additions & 6 deletions state/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,15 @@ func (s *SecretsConsumedWatcherSuite) SetUpTest(c *gc.C) {
s.owner = s.Factory.MakeApplication(c, nil)
}

func (s *SecretsConsumedWatcherSuite) TestWatcherInitialEvent(c *gc.C) {
w, err := s.State.WatchConsumedSecretsChanges(names.NewUnitTag("mariadb/0"))
c.Assert(err, jc.ErrorIsNil)
wc := testing.NewStringsWatcherC(c, w)
wc.AssertChange()

testing.AssertStop(c, w)
}

func (s *SecretsConsumedWatcherSuite) setupWatcher(c *gc.C) (state.StringsWatcher, *secrets.URI) {
uri := secrets.NewURI()
cp := state.CreateSecretParams{
Expand All @@ -1977,16 +1986,41 @@ func (s *SecretsConsumedWatcherSuite) setupWatcher(c *gc.C) (state.StringsWatche
}
_, err := s.store.CreateSecret(uri, cp)
c.Assert(err, jc.ErrorIsNil)
w, err := s.State.WatchConsumedSecretsChanges(names.NewUnitTag("mariadb/0"))

uri2 := secrets.NewURI()
cp = state.CreateSecretParams{
Version: 1,
Owner: s.owner.Tag(),
UpdateSecretParams: state.UpdateSecretParams{
LeaderToken: &fakeToken{},
Data: map[string]string{"foo": "bar"},
},
}
_, err = s.store.CreateSecret(uri2, cp)
c.Assert(err, jc.ErrorIsNil)
_, err = s.store.UpdateSecret(uri2, state.UpdateSecretParams{
LeaderToken: &fakeToken{},
Data: secrets.SecretData{"foo": "bar2"},
})
c.Assert(err, jc.ErrorIsNil)

wc := testing.NewStringsWatcherC(c, w)
wc.AssertChange()
err = s.State.SaveSecretConsumer(uri, names.NewUnitTag("mariadb/0"), &secrets.SecretConsumerMetadata{
CurrentRevision: 1,
LatestRevision: 1,
})
c.Assert(err, jc.ErrorIsNil)
err = s.State.SaveSecretConsumer(uri2, names.NewUnitTag("mariadb/0"), &secrets.SecretConsumerMetadata{
CurrentRevision: 1,
LatestRevision: 2,
})
c.Assert(err, jc.ErrorIsNil)

err = s.State.SaveSecretConsumer(uri, names.NewUnitTag("mariadb/0"), &secrets.SecretConsumerMetadata{CurrentRevision: 1})
w, err := s.State.WatchConsumedSecretsChanges(names.NewUnitTag("mariadb/0"))
c.Assert(err, jc.ErrorIsNil)
// No event until rev > 1.
wc.AssertNoChange()
wc := testing.NewStringsWatcherC(c, w)

// No event until rev > 1, so just the one change.
wc.AssertChange(uri2.String())
return w, uri
}

Expand Down

0 comments on commit c4f5f6d

Please sign in to comment.