Skip to content

Commit

Permalink
Add --api-key-id to grant command
Browse files Browse the repository at this point in the history
  • Loading branch information
telyn committed Nov 21, 2018
1 parent aa1e94e commit 2fa92bf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cmd/bytemark/commands/grant.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package commands

import (
"errors"

"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app"
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args"
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with"
Expand Down Expand Up @@ -30,6 +32,10 @@ func init() {
UsageText: "grant privilege <privilege> [on] <object> [to] <user>\r\nbytemark grant cluster_admin [to] <user>",
Description: "Grant a privilege to a user for a particular bytemark object\r\n\r\n" + privilegeText,
Flags: []cli.Flag{
cli.IntFlag{
Name: "api-key-id",
Usage: "ID of an API key that should be allowed to use this privilege. Leave blank when not using api keys",
},
cli.BoolFlag{
Name: "yubikey-required",
Usage: "Set if the privilege should require a yubikey.",
Expand All @@ -42,6 +48,10 @@ func init() {
},
Action: app.Action(args.Join("privilege"), with.RequiredFlags("privilege"), with.Privilege("privilege"), func(c *app.Context) (err error) {
c.Privilege.YubikeyRequired = c.Bool("yubikey-required")
if c.Bool("yubikey-required") && c.IsSet("api-key-id") {
return errors.New("Only one of --api-key-id and --yubikey-required may be set at a time")
}
c.Privilege.ApiKeyID = c.Int("api-key-id")

err = c.Client().GrantPrivilege(c.Privilege)
if err == nil {
Expand Down
14 changes: 14 additions & 0 deletions cmd/bytemark/commands/grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ func TestGrantPrivilege(t *testing.T) {
},
ShouldErr: false,
Input: "bytemark grant privilege account_admin on test-account to test-user",
}, {
Name: "ApiKey",
Setup: func(config *mocks.Config, c *mocks.Client) {
config.When("GetIgnoreErr", "account").Return("default-account")
c.When("GetAccount", "account").Return(lib.Account{BrainID: 32310})
c.When("GrantPrivilege", brain.Privilege{
Username: "user",
AccountID: 32310,
Level: brain.AccountAdminPrivilege,
ApiKeyID: 4,
}).Return(nil).Times(1)
},
ShouldErr: false,
Input: "bytemark grant privilege --api-key-id 4 account_admin on account to user",
},
}
for i, test := range tests {
Expand Down

0 comments on commit 2fa92bf

Please sign in to comment.