Skip to content

Commit

Permalink
Introduce resset Action permissions for KMS key ops.
Browse files Browse the repository at this point in the history
  • Loading branch information
timflyio committed Sep 9, 2024
1 parent 43a0ea3 commit eb5c49b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
37 changes: 35 additions & 2 deletions resset/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,20 @@ const (
// not modifying other attributes. In practice, this mostly applies to
// starting/stopping/signaling machines.
ActionControl

// ActionSign, ActionVerify, ActionEncrypt, and ActionDecrypt indicate
// operations using keys.
ActionSign
ActionVerify
ActionEncrypt
ActionDecrypt
)

const (
ActionAll = ActionRead | ActionWrite | ActionCreate | ActionDelete | ActionControl
ActionNone = Action(0)
ActionAll = ActionRead | ActionWrite | ActionCreate | ActionDelete | ActionControl |
ActionSign | ActionVerify | ActionEncrypt | ActionDecrypt
ActionAllKeyOps = ActionSign | ActionVerify | ActionEncrypt | ActionDecrypt
ActionNone = Action(0)
)

func ActionFromString(ms string) Action {
Expand All @@ -71,6 +80,14 @@ func ActionFromString(ms string) Action {
ret |= ActionDelete
case 'C':
ret |= ActionControl
case 'S':
ret |= ActionSign
case 'V':
ret |= ActionVerify
case 'E':
ret |= ActionEncrypt
case 'D':
ret |= ActionDecrypt
}
}

Expand Down Expand Up @@ -100,6 +117,22 @@ func (a Action) String() string {
str = append(str, 'C')
}

if a&ActionSign != 0 {
str = append(str, 'S')
}

if a&ActionVerify != 0 {
str = append(str, 'V')
}

if a&ActionEncrypt != 0 {
str = append(str, 'E')
}

if a&ActionDecrypt != 0 {
str = append(str, 'D')
}

return string(str)
}

Expand Down
14 changes: 14 additions & 0 deletions resset/action_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resset

import (
"encoding/json"
"testing"

"github.com/alecthomas/assert/v2"
Expand All @@ -17,3 +18,16 @@ func TestActionCaveat(t *testing.T) {
ErrUnauthorizedForAction,
)
}

func TestActionSerialization(t *testing.T) {
highest := ActionDecrypt << 1
for act := Action(0); act < highest; act += 1 {
bs, err := json.Marshal(&act)
assert.NoError(t, err)

var act2 Action
err = json.Unmarshal(bs, &act2)
assert.NoError(t, err)
assert.Equal(t, act, act2)
}
}

0 comments on commit eb5c49b

Please sign in to comment.