Skip to content

Commit 127c1fe

Browse files
authored
CLI support for role config assignment (#1238)
1 parent 2b58b1e commit 127c1fe

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

pkg/cli/cli.go

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/noobaa/noobaa-operator/v5/pkg/operator"
2222
"github.com/noobaa/noobaa-operator/v5/pkg/options"
2323
"github.com/noobaa/noobaa-operator/v5/pkg/pvstore"
24+
"github.com/noobaa/noobaa-operator/v5/pkg/sts"
2425
"github.com/noobaa/noobaa-operator/v5/pkg/system"
2526
"github.com/noobaa/noobaa-operator/v5/pkg/util"
2627
"github.com/noobaa/noobaa-operator/v5/pkg/version"
@@ -132,6 +133,7 @@ Load noobaa completion to bash:
132133
diagnostics.CmdDiagnoseDeprecated(),
133134
diagnostics.CmdDbDumpDeprecated(),
134135
diagnostics.Cmd(),
136+
sts.Cmd(),
135137
},
136138
}, {
137139
Message: "Advanced:",

pkg/nb/api.go

+7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type Client interface {
4343
DeletePoolAPI(DeletePoolParams) error
4444
DeleteNamespaceResourceAPI(DeleteNamespaceResourceParams) error
4545

46+
UpdateAccount(UpdateAccountParams) error
4647
UpdateAccountS3Access(UpdateAccountS3AccessParams) error
4748
UpdateAllBucketsDefaultPool(UpdateDefaultResourceParams) error
4849
UpdateBucketClass(UpdateBucketClassParams) (BucketClassInfo, error)
@@ -326,6 +327,12 @@ func (c *RPCClient) DeletePoolAPI(params DeletePoolParams) error {
326327
return c.Call(req, nil)
327328
}
328329

330+
// UpdateAccount calls account_api.update_account()
331+
func (c *RPCClient) UpdateAccount(params UpdateAccountParams) error {
332+
req := &RPCMessage{API: "account_api", Method: "update_account", Params: params}
333+
return c.Call(req, nil)
334+
}
335+
329336
// UpdateAccountS3Access calls account_api.update_account_s3_access()
330337
func (c *RPCClient) UpdateAccountS3Access(params UpdateAccountS3AccessParams) error {
331338
req := &RPCMessage{API: "account_api", Method: "update_account_s3_access", Params: params}

pkg/nb/types.go

+13
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,19 @@ type DeleteNamespaceResourceParams struct {
571571
Name string `json:"name"`
572572
}
573573

574+
// UpdateAccountParams is the params of account_api.update_account_s3_access()
575+
type UpdateAccountParams struct {
576+
Name *string `json:"username,omitempty"`
577+
Email string `json:"email"`
578+
NewEmail *string `json:"new_email,omitempty"`
579+
AllowedIPs *[]struct {
580+
Start string `json:"start"`
581+
End string `json:"end"`
582+
} `json:"ips,omitempty"`
583+
RoleConfig interface{} `json:"role_config,omitempty"`
584+
RemoveRoleConfig bool `json:"remove_role_config,omitempty"`
585+
}
586+
574587
// UpdateAccountS3AccessParams is the params of account_api.update_account_s3_access()
575588
type UpdateAccountS3AccessParams struct {
576589
Email string `json:"email"`

pkg/sts/sts.go

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package sts
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
7+
"github.com/noobaa/noobaa-operator/v5/pkg/nb"
8+
"github.com/noobaa/noobaa-operator/v5/pkg/system"
9+
"github.com/noobaa/noobaa-operator/v5/pkg/util"
10+
11+
"github.com/spf13/cobra"
12+
)
13+
14+
// Cmd returns a CLI command
15+
func Cmd() *cobra.Command {
16+
cmd := &cobra.Command{
17+
Use: "sts",
18+
Short: "Manage the NooBaa Security Token Service",
19+
Long: "Manage the NooBaa Security Token Service by assigning, updating or removing a NooBaa account's role config.\n" +
20+
"The role config object must contain the keys 'role_name' and 'assume_role_policy', with their respective values.",
21+
}
22+
cmd.AddCommand(
23+
CmdAssignRole(),
24+
CmdRemoveRole(),
25+
)
26+
return cmd
27+
}
28+
29+
// CmdAssignRole returns a CLI command
30+
func CmdAssignRole() *cobra.Command {
31+
cmd := &cobra.Command{
32+
Use: "assign-role <noobaa-account-name> <role-config>",
33+
Short: "Assign a role config to a NooBaa account - note that this will override the existing role config",
34+
Run: RunAssign,
35+
}
36+
cmd.Flags().String("email", "", "The email of the account that will be updated")
37+
err := cmd.MarkFlagRequired("email")
38+
if err != nil {
39+
log.Fatalf(`❌ Failed to mark email flag as required - %s`, err)
40+
}
41+
cmd.Flags().String("role_config", "", "The new value that the account's role_config should be set to")
42+
err = cmd.MarkFlagRequired("role_config")
43+
if err != nil {
44+
log.Fatalf(`❌ Failed to mark role_config flag as required - %s`, err)
45+
}
46+
return cmd
47+
}
48+
49+
// CmdRemoveRole returns a CLI command
50+
func CmdRemoveRole() *cobra.Command {
51+
cmd := &cobra.Command{
52+
Use: "remove-role <noobaa-account-name>",
53+
Short: "Remove a NooBaa account's role config",
54+
Run: RunRemove,
55+
}
56+
cmd.Flags().String("email", "", "The email of the account that will be updated")
57+
err := cmd.MarkFlagRequired("email")
58+
if err != nil {
59+
log.Fatalf(`❌ Failed to mark email flag as required - %s`, err)
60+
}
61+
return cmd
62+
}
63+
64+
// RunAssign runs a CLI command
65+
func RunAssign(cmd *cobra.Command, args []string) {
66+
log := util.Logger()
67+
email, _ := cmd.Flags().GetString("email")
68+
roleConfig, _ := cmd.Flags().GetString("role_config")
69+
70+
if !json.Valid([]byte(roleConfig)) {
71+
log.Fatalf(`❌ The provided role configuration is not valid JSON`)
72+
}
73+
74+
sysClient, err := system.Connect(true)
75+
if err != nil {
76+
log.Fatalf(`❌ Failed to create RPC client %s`, err)
77+
}
78+
NBClient := sysClient.NBClient
79+
80+
var roleConfigObject interface{}
81+
err = json.Unmarshal([]byte(roleConfig), &roleConfigObject)
82+
if err != nil {
83+
log.Fatalf("❌ Failed to parse role config - %s", err)
84+
}
85+
if err != nil {
86+
log.Fatalf(`❌ Failed to read account - %s`, err)
87+
}
88+
UpdateAccountParams := nb.UpdateAccountParams{
89+
Email: email,
90+
RoleConfig: roleConfigObject,
91+
}
92+
93+
err = NBClient.UpdateAccount(UpdateAccountParams)
94+
if err != nil {
95+
log.Fatalf(`❌ Failed to update account - %s`, err)
96+
}
97+
}
98+
99+
// RunRemove runs a CLI command
100+
func RunRemove(cmd *cobra.Command, args []string) {
101+
email, _ := cmd.Flags().GetString("email")
102+
103+
sysClient, err := system.Connect(true)
104+
if err != nil {
105+
log.Fatalf(`❌ Failed to create RPC client %s`, err)
106+
}
107+
NBClient := sysClient.NBClient
108+
109+
UpdateAccountParams := nb.UpdateAccountParams{
110+
Email: email,
111+
RemoveRoleConfig: true,
112+
}
113+
114+
err = NBClient.UpdateAccount(UpdateAccountParams)
115+
if err != nil {
116+
log.Fatalf(`❌ Failed to remove the requested role config - %s`, err)
117+
}
118+
}

0 commit comments

Comments
 (0)