|
| 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