Skip to content

Commit 9237191

Browse files
committed
o/devicemgmtstate,o/assertstate: address import cycle
1 parent 256fde5 commit 9237191

4 files changed

Lines changed: 23 additions & 7 deletions

File tree

overlord/assertstate/assertstate.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/snapcore/snapd/httputil"
3535
"github.com/snapcore/snapd/logger"
3636
"github.com/snapcore/snapd/overlord/confdbstate"
37+
"github.com/snapcore/snapd/overlord/devicemgmtstate"
3738
"github.com/snapcore/snapd/overlord/snapstate"
3839
"github.com/snapcore/snapd/overlord/state"
3940
"github.com/snapcore/snapd/release"
@@ -406,6 +407,11 @@ func delayedCrossMgrInit() {
406407
// wire confdbstate helpers that look up confdb-schema assertions
407408
confdbstate.AssertstateFetchConfdbSchemaAssertion = FetchConfdbSchemaAssertion
408409
confdbstate.AssertstateConfdbSchema = ConfdbSchema
410+
// wire devicemgmtstate helpers that look up the assertion database and
411+
// account-key assertions
412+
devicemgmtstate.AssertstateDB = DB
413+
devicemgmtstate.AssertstateAccountKey = AccountKey
414+
devicemgmtstate.AssertstateFetchAccountKey = FetchAccountKey
409415
}
410416

411417
// AutoRefreshAssertions tries to refresh all assertions

overlord/devicemgmtstate/devicemgmtmgr.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
"github.com/snapcore/snapd/asserts"
3737
"github.com/snapcore/snapd/features"
3838
"github.com/snapcore/snapd/logger"
39-
"github.com/snapcore/snapd/overlord/assertstate"
4039
"github.com/snapcore/snapd/overlord/configstate/config"
4140
"github.com/snapcore/snapd/overlord/snapstate"
4241
"github.com/snapcore/snapd/overlord/state"
@@ -55,15 +54,22 @@ const (
5554
)
5655

5756
var (
58-
timeNow = time.Now
59-
assertstateFetchAccountKey = assertstate.FetchAccountKey
57+
timeNow = time.Now
6058

6159
maxSequences = 256
6260
maxBlockedMessagesPerSequence = 8
6361

6462
awaitSubsystemRetryInterval = 30 * time.Second
6563

6664
deviceMgmtExchangeChangeKind = swfeats.RegisterChangeKind("device-management-exchange")
65+
66+
// AssertstateDB, AssertstateAccountKey and AssertstateFetchAccountKey are
67+
// wired to the corresponding assertstate helpers by
68+
// assertstate.delayedCrossMgrInit. They exist to break the import cycle
69+
// assertstate -> confdbstate -> devicemgmtstate -> assertstate.
70+
AssertstateDB func(s *state.State) asserts.RODatabase
71+
AssertstateAccountKey func(st *state.State, signKeyID string) (*asserts.AccountKey, error)
72+
AssertstateFetchAccountKey func(st *state.State, userID int, signKeyID string) error
6773
)
6874

6975
// deviceBackend provides device identity and response message signing.
@@ -660,7 +666,7 @@ func (m *DeviceMgmtManager) doValidateMessage(t *state.Task, _ *tomb.Tomb) error
660666
}
661667
}
662668

663-
err = assertstate.DB(m.state).Check(a)
669+
err = AssertstateDB(m.state).Check(a)
664670
if err != nil {
665671
rejectMsg(fmt.Sprintf("cannot verify message signature: %v", err))
666672
return nil
@@ -720,15 +726,15 @@ func (m *DeviceMgmtManager) doValidateMessage(t *state.Task, _ *tomb.Tomb) error
720726
// ensureAccountKey fetches the account-key assertion for signKeyID from the
721727
// store if it is not already in the local database.
722728
func (m *DeviceMgmtManager) ensureAccountKey(signKeyID string) (fetched bool, err error) {
723-
_, err = assertstate.AccountKey(m.state, signKeyID)
729+
_, err = AssertstateAccountKey(m.state, signKeyID)
724730
if err == nil {
725731
return false, nil
726732
}
727733
if !errors.Is(err, &asserts.NotFoundError{}) {
728734
return false, err
729735
}
730736

731-
err = assertstateFetchAccountKey(m.state, 0, signKeyID)
737+
err = AssertstateFetchAccountKey(m.state, 0, signKeyID)
732738
if err != nil && !errors.Is(err, &asserts.NotFoundError{}) {
733739
return true, err
734740
}

overlord/devicemgmtstate/devicemgmtmgr_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ func (s *deviceMgmtMgrSuite) SetUpTest(c *C) {
161161
c.Assert(db.Add(s.storeStack.StoreAccountKey("")), IsNil)
162162
assertstate.ReplaceDB(s.st, db)
163163

164+
devicemgmtstate.AssertstateDB = assertstate.DB
165+
devicemgmtstate.AssertstateAccountKey = assertstate.AccountKey
166+
devicemgmtstate.AssertstateFetchAccountKey = assertstate.FetchAccountKey
167+
164168
s.runner = s.o.TaskRunner()
165169
s.o.AddManager(s.runner)
166170

overlord/devicemgmtstate/export_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var (
3838
)
3939

4040
func MockFetchAccountKey(f func(st *state.State, userID int, signKeyID string) error) func() {
41-
return testutil.Mock(&assertstateFetchAccountKey, f)
41+
return testutil.Mock(&AssertstateFetchAccountKey, f)
4242
}
4343

4444
func MockMaxSequences(n int) func() {

0 commit comments

Comments
 (0)