Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce resset Action permissions for KMS key ops. #34

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@btoews are you OK having this sort of KMS-specific detail in the macaroon library?
Would it be better to have some generic names here like "ActionOp1", "ActionOp2" and then in petsem authority define those names as "ActionSign", "ActionVerify"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to avoid adding KMS-specific values to this package, especially since we've only given ourselves 16 bits for Action. I couldn't put my finger on a better solution, so I spent some time experimenting. The best I came up with was to make ResourceSet take a second type parameter for the Action. This seems to work pretty cleanly. Let me know what you think #35 https://github.com/superfly/nomad-firecracker/pull/2667

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing that works well with having a second, extended Action type is that it's more backwards compatible with existing macaroons. For Encrypt, for example, we can require ActionControl | ActionEncrypt. When checking the Apps caveat, we'll only look for the ActionControl part, but we'll look for ActionControl and ActionEncrypt when verifying Secrets or SecretTypes caveats.

)

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)
}
}
Loading