Skip to content

Commit

Permalink
asserts: default to brand ID when account ID is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
Meulengracht committed Mar 10, 2023
1 parent b18a7aa commit 1f903f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
23 changes: 18 additions & 5 deletions asserts/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,19 @@ func checkOptionalSystemUserAuthority(headers map[string]interface{}, brandID st
return nil, fmt.Errorf("%q header must be '*' or a list of account ids", name)
}

func checkModelValidationSetAccountID(headers map[string]interface{}, brandID string) (string, error) {
accountID, err := checkOptionalString(headers, "account-id")
if err != nil {
return "", err
}

// default to brand ID if account ID is not provided
if accountID == "" {
return brandID, nil
}
return accountID, nil
}

// checkModelValidationSetSequence reads the optional 'sequence' member, if
// not set, returns 0 as this means unpinned. Unfortunately we are not able
// to reuse `checkSequence` as it operates inside different parameters.
Expand Down Expand Up @@ -717,8 +730,8 @@ func checkModelValidationSetMode(headers map[string]interface{}) (ModelValidatio
return ModelValidationSetMode(modeStr), nil
}

func checkModelValidationSet(headers map[string]interface{}) (*ModelValidationSet, error) {
accountID, err := checkOptionalString(headers, "account-id")
func checkModelValidationSet(headers map[string]interface{}, brandID string) (*ModelValidationSet, error) {
accountID, err := checkModelValidationSetAccountID(headers, brandID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -746,7 +759,7 @@ func checkModelValidationSet(headers map[string]interface{}) (*ModelValidationSe
}, nil
}

func checkOptionalModelValidationSets(headers map[string]interface{}) ([]*ModelValidationSet, error) {
func checkOptionalModelValidationSets(headers map[string]interface{}, brandID string) ([]*ModelValidationSet, error) {
valSets, ok := headers["validation-sets"]
if !ok {
return nil, nil
Expand All @@ -772,7 +785,7 @@ func checkOptionalModelValidationSets(headers map[string]interface{}) ([]*ModelV
return nil, fmt.Errorf(`"validation-sets" must contain a list of validation sets`)
}

vs, err := checkModelValidationSet(data)
vs, err := checkModelValidationSet(data, brandID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1006,7 +1019,7 @@ func assembleModel(assert assertionBase) (Assertion, error) {

allSnaps, requiredWithEssentialSnaps, numEssentialSnaps := modSnaps.list()

valSets, err := checkOptionalModelValidationSets(assert.headers)
valSets, err := checkOptionalModelValidationSets(assert.headers, brandID)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions asserts/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1250,15 +1250,16 @@ func (mods *modelSuite) TestValidationSetsDecodeOK(c *C) {
frag string
expected []*asserts.ModelValidationSet
}{
// brand validation-set
// brand validation-set, this should instead use the brand specified
// by the core20ModelExample, as account-id is not set
{`validation-sets:
-
name: my-set
mode: prefer-enforce
`,
[]*asserts.ModelValidationSet{
{
AccountID: "",
AccountID: "brand-id1",
Name: "my-set",
Mode: asserts.ModelValidationSetModePreferEnforced,
},
Expand Down

0 comments on commit 1f903f7

Please sign in to comment.