Skip to content

Commit

Permalink
confdb,asserts,daemon: add confdb-control api
Browse files Browse the repository at this point in the history
  • Loading branch information
st3v3nmw committed Dec 18, 2024
1 parent d916b68 commit ea1a5cf
Show file tree
Hide file tree
Showing 7 changed files with 640 additions and 53 deletions.
63 changes: 62 additions & 1 deletion asserts/confdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ type ConfdbControl struct {
operators map[string]*confdb.Operator
}

func NewConfdbControl(serial *Serial) *ConfdbControl {
return &ConfdbControl{
assertionBase: assertionBase{
headers: map[string]interface{}{
"type": "confdb-control",
"brand-id": serial.BrandID(),
"model": serial.Model(),
"serial": serial.Serial(),
"groups": []any{},
},
},
operators: map[string]*confdb.Operator{},
}

Check warning on line 134 in asserts/confdb.go

View check run for this annotation

Codecov / codecov/patch

asserts/confdb.go#L122-L134

Added lines #L122 - L134 were not covered by tests
}

// BrandID returns the brand identifier of the device.
func (cc *ConfdbControl) BrandID() string {
return cc.HeaderString("brand-id")
Expand All @@ -135,6 +150,52 @@ func (cc *ConfdbControl) Serial() string {
return cc.HeaderString("serial")
}

// IsDelegated checks if <accountID>/<registry>/<view> is delegated to
// <operatorID> under the authentication method <auth>.
func (cc *ConfdbControl) IsDelegated(operatorID, view string, auth []string) (bool, error) {
operator, ok := cc.operators[operatorID]
if !ok {
// nothing is delegated to this operator
return false, nil
}

Check warning on line 160 in asserts/confdb.go

View check run for this annotation

Codecov / codecov/patch

asserts/confdb.go#L155-L160

Added lines #L155 - L160 were not covered by tests

return operator.IsDelegated(view, auth)

Check warning on line 162 in asserts/confdb.go

View check run for this annotation

Codecov / codecov/patch

asserts/confdb.go#L162

Added line #L162 was not covered by tests
}

// Delegate delegates the given views under the provided authentication methods to the operator.
func (cc *ConfdbControl) Delegate(operatorID string, views []string, auth []string) error {
operator, ok := cc.operators[operatorID]
if !ok {
operator = &confdb.Operator{ID: operatorID}
}

Check warning on line 170 in asserts/confdb.go

View check run for this annotation

Codecov / codecov/patch

asserts/confdb.go#L166-L170

Added lines #L166 - L170 were not covered by tests

err := operator.Delegate(views, auth)
if err != nil {
return err
}

Check warning on line 175 in asserts/confdb.go

View check run for this annotation

Codecov / codecov/patch

asserts/confdb.go#L172-L175

Added lines #L172 - L175 were not covered by tests

cc.operators[operatorID] = operator
return nil

Check warning on line 178 in asserts/confdb.go

View check run for this annotation

Codecov / codecov/patch

asserts/confdb.go#L177-L178

Added lines #L177 - L178 were not covered by tests
}

// Revoke withdraws remote access to the views that have been delegated under
// the authentication methods.
func (cc *ConfdbControl) Revoke(operatorID string, views []string, auth []string) error {
operator, ok := cc.operators[operatorID]
if !ok {
// nothing is delegated to this operator
return nil
}

Check warning on line 188 in asserts/confdb.go

View check run for this annotation

Codecov / codecov/patch

asserts/confdb.go#L183-L188

Added lines #L183 - L188 were not covered by tests

if len(views) == 0 && len(auth) == 0 {
// completely revoke access from this operator
delete(cc.operators, operatorID)
return nil
}

Check warning on line 194 in asserts/confdb.go

View check run for this annotation

Codecov / codecov/patch

asserts/confdb.go#L190-L194

Added lines #L190 - L194 were not covered by tests

return operator.Revoke(views, auth)

Check warning on line 196 in asserts/confdb.go

View check run for this annotation

Codecov / codecov/patch

asserts/confdb.go#L196

Added line #L196 was not covered by tests
}

// assembleConfdbControl creates a new confdb-control assertion after validating
// all required fields and constraints.
func assembleConfdbControl(assert assertionBase) (Assertion, error) {
Expand Down Expand Up @@ -209,7 +270,7 @@ func parseConfdbControlGroups(rawGroups []interface{}) (map[string]*confdb.Opera
return nil, fmt.Errorf(`%s: "views" must be provided`, errPrefix)
}

if err := operator.AddControlGroup(views, auth); err != nil {
if err := operator.Delegate(views, auth); err != nil {
return nil, fmt.Errorf(`%s: %w`, errPrefix, err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions asserts/confdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,12 @@ func (s *confdbCtrlSuite) TestDecodeInvalid(c *C) {
{
" - operator-key",
" - foo-bar",
"cannot parse group at position 1: cannot add group: invalid authentication method: foo-bar",
"cannot parse group at position 1: cannot delegate: invalid authentication method: foo-bar",
},
{
"canonical/network/control-interfaces",
"canonical",
`cannot parse group at position 2: view "canonical" must be in the format account/confdb/view`,
`cannot parse group at position 2: cannot delegate: view "canonical" must be in the format account/confdb/view`,
},
}

Expand Down
Loading

0 comments on commit ea1a5cf

Please sign in to comment.