-
Notifications
You must be signed in to change notification settings - Fork 29
/
iamrolebinding.go
95 lines (78 loc) · 3.17 KB
/
iamrolebinding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2023 Cisco Systems, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package iamrolebinding
import (
"encoding/json"
"github.com/apex/log"
"github.com/spf13/cobra"
"github.com/cisco-open/fsoc/platform/api"
)
// iamRbCmd represents the role binding command group
var iamRbCmd = &cobra.Command{
Use: "iam-role-binding",
Short: "Manage IAM role bindings",
Long: `Manage role bindings for a principal, as part of identity and access management (IAM)
Principals can be user principals, service principals or agent principals. For user principals, use the email
address of the user; for service and agent principal, use the principal's ID (client ID).
Roles are usually prefixed with the domain, e.g., "iam:observer". Standard roles include "iam:observer",
"iam:tenantAdmin" and "iam:configManager". Solutions often define their own roles that can be bound to principals
in order to access solution's functionality.
To see and manage roles, use the "iam-role" commands.
Commands from this group require a principal with tenant administrator access.`,
Aliases: []string{"iam-role-bindings", "role-binding", "role-bindings", "rb"},
Example: `
fsoc iam-role-bindings list john@example.com
fsoc rb list john@example.com
fsoc rb add jill@example.com iam:configManager optimize:optimizationManager
fsoc rb remove jay@example.com iam:tenantAdmin
`,
TraverseChildren: true,
}
// Package registration function for the iam-role-binding command root
func NewSubCmd() *cobra.Command {
cmd := iamRbCmd
cmd.AddCommand(newCmdRbList())
cmd.AddCommand(newCmdRbAdd())
cmd.AddCommand(newCmdRbRemove())
return cmd
}
func getIamRoleBindingsUrl() string {
return "iam/policy-admin/v1beta2/principals/roles"
}
func patchRoles(principal string, roles []string, is_add bool) error {
// choose value to use for roles in the request
var roleValue any
if is_add {
roleValue = true
} else {
roleValue = nil
}
// prepare request parameter
requestParams := ManageParameter{Principal: PrincipalParameter{ID: principal}, Roles: map[string]any{}}
for _, role := range roles {
requestParams.Roles[role] = roleValue
}
// request operation, the API requires the application/json type (bug?)
// JSONPatch converts to JSON with application/json-patch+json; to override this
// we must pre-marshal to JSON here
body, err := json.Marshal(requestParams) // must marshal here in order to be able to supply content-type
if err != nil {
log.Fatalf("(bug) failed to marshal to json: %v", err)
}
options := api.Options{Headers: map[string]string{"Content-Type": "application/json"}}
if err := api.JSONPatch(getIamRoleBindingsUrl(), body, nil, &options); err != nil {
return err
}
return nil
}