Skip to content

Commit

Permalink
Pass sequence as int to SeqFormingAssertion.
Browse files Browse the repository at this point in the history
  • Loading branch information
stolowski committed Jan 25, 2021
1 parent c18a6a9 commit e2eed5f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion overlord/snapstate/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type StoreService interface {
DownloadStream(context.Context, string, *snap.DownloadInfo, int64, *auth.UserState) (r io.ReadCloser, status int, err error)

Assertion(assertType *asserts.AssertionType, primaryKey []string, user *auth.UserState) (asserts.Assertion, error)
SeqFormingAssertion(assertType *asserts.AssertionType, seqOrPrimaryKey []string, user *auth.UserState) (asserts.Assertion, error)
SeqFormingAssertion(assertType *asserts.AssertionType, sequenceKey []string, sequence int, user *auth.UserState) (asserts.Assertion, error)
DownloadAssertions([]string, *asserts.Batch, *auth.UserState) error

SuggestedCurrency() string
Expand Down
25 changes: 11 additions & 14 deletions store/store_asserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,21 @@ func (s *Store) Assertion(assertType *asserts.AssertionType, primaryKey []string
}

// SeqFormingAssertion retrieves the sequence-forming assertion for the given
// type (currently validation-set only) and primary or sequence key.
func (s *Store) SeqFormingAssertion(assertType *asserts.AssertionType, seqOrPrimaryKey []string, user *auth.UserState) (asserts.Assertion, error) {
// type (currently validation-set only).
func (s *Store) SeqFormingAssertion(assertType *asserts.AssertionType, sequenceKey []string, sequence int, user *auth.UserState) (asserts.Assertion, error) {
if !assertType.SequenceForming() {
return nil, fmt.Errorf("internal error: requested non sequence-forming assertion type %q", assertType.Name)
}
v := url.Values{}
v.Set("max-format", strconv.Itoa(assertType.MaxSupportedFormat()))

hasSequenceNumber := len(seqOrPrimaryKey) == len(assertType.PrimaryKey)

// sequence key is the primary key without sequence number element.
var sequenceKey []string
hasSequenceNumber := sequence > 0
if hasSequenceNumber {
// full primary key passed, query specific sequence number.
v.Set("sequence", seqOrPrimaryKey[len(seqOrPrimaryKey)-1])
// drop sequence number
sequenceKey = seqOrPrimaryKey[:len(seqOrPrimaryKey)-1]
v.Set("sequence", fmt.Sprintf("%d", sequence))
} else {
// sequence key passed, query for the latest sequence.
// query for the latest sequence.
v.Set("sequence", "latest")
sequenceKey = seqOrPrimaryKey
}
u := s.assertionsEndpointURL(path.Join(assertType.Name, path.Join(sequenceKey...)), v)

Expand All @@ -139,17 +133,20 @@ func (s *Store) SeqFormingAssertion(assertType *asserts.AssertionType, seqOrPrim
// more relaxed about key length, making sequence optional. Should
// we make it a helper on its own in store for the not-found-error
// handling?
if len(seqOrPrimaryKey) < len(assertType.PrimaryKey)-1 || len(seqOrPrimaryKey) > len(assertType.PrimaryKey) {
return fmt.Errorf("primary key has wrong length for %q assertion", assertType.Name)
if len(sequenceKey) != len(assertType.PrimaryKey)-1 {
return fmt.Errorf("sequence key has wrong length for %q assertion", assertType.Name)
}
headers := make(map[string]string)
for i, keyVal := range seqOrPrimaryKey {
for i, keyVal := range sequenceKey {
name := assertType.PrimaryKey[i]
if keyVal == "" {
return fmt.Errorf("primary key %q header cannot be empty", name)
}
headers[name] = keyVal
}
if hasSequenceNumber {
headers[assertType.PrimaryKey[len(assertType.PrimaryKey)-1]] = fmt.Sprintf("%d", sequence)
}
return &asserts.NotFoundError{
Type: assertType,
Headers: headers,
Expand Down
13 changes: 7 additions & 6 deletions store/store_asserts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,12 @@ func (s *storeAssertsSuite) TestSeqFormingAssertion(c *C) {
defer mockServer.Close()

for _, tc := range []struct {
primaryKey []string
sequenceKey []string
sequence int
expectedSeqArg string
}{
{[]string{"16", "account-foo", "set-bar", "2"}, "2"},
{[]string{"16", "account-foo", "set-bar"}, "latest"},
{[]string{"16", "account-foo", "set-bar"}, 2, "2"},
{[]string{"16", "account-foo", "set-bar"}, 0, "latest"},
} {
expectedSeqArg = tc.expectedSeqArg

Expand All @@ -470,7 +471,7 @@ func (s *storeAssertsSuite) TestSeqFormingAssertion(c *C) {
dauthCtx := &testDauthContext{c: c, device: s.device}
sto := store.New(&cfg, dauthCtx)

a, err := sto.SeqFormingAssertion(asserts.ValidationSetType, tc.primaryKey, nil)
a, err := sto.SeqFormingAssertion(asserts.ValidationSetType, tc.sequenceKey, tc.sequence, nil)
c.Assert(err, IsNil)
c.Check(a, NotNil)
c.Check(a.Type(), Equals, asserts.ValidationSetType)
Expand All @@ -496,7 +497,7 @@ func (s *storeAssertsSuite) TestSeqFormingAssertionNotFound(c *C) {
}
sto := store.New(&cfg, nil)

_, err := sto.SeqFormingAssertion(asserts.ValidationSetType, []string{"16", "account-foo", "set-bar", "1"}, nil)
_, err := sto.SeqFormingAssertion(asserts.ValidationSetType, []string{"16", "account-foo", "set-bar"}, 1, nil)
c.Check(asserts.IsNotFound(err), Equals, true)
c.Check(err, DeepEquals, &asserts.NotFoundError{
Type: asserts.ValidationSetType,
Expand All @@ -509,7 +510,7 @@ func (s *storeAssertsSuite) TestSeqFormingAssertionNotFound(c *C) {
})

// latest requested
_, err = sto.SeqFormingAssertion(asserts.ValidationSetType, []string{"16", "account-foo", "set-bar"}, nil)
_, err = sto.SeqFormingAssertion(asserts.ValidationSetType, []string{"16", "account-foo", "set-bar"}, 0, nil)
c.Check(asserts.IsNotFound(err), Equals, true)
c.Check(err, DeepEquals, &asserts.NotFoundError{
Type: asserts.ValidationSetType,
Expand Down
2 changes: 1 addition & 1 deletion store/storetest/storetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (Store) Assertion(*asserts.AssertionType, []string, *auth.UserState) (asser
panic("Store.Assertion not expected")
}

func (Store) SeqFormingAssertion(*asserts.AssertionType, []string, *auth.UserState) (asserts.Assertion, error) {
func (Store) SeqFormingAssertion(*asserts.AssertionType, []string, int, *auth.UserState) (asserts.Assertion, error) {
panic("Store.SeqFormingAssertion not expected")
}

Expand Down

0 comments on commit e2eed5f

Please sign in to comment.