-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathroles.go
243 lines (218 loc) · 9.76 KB
/
roles.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package roles
import (
"fmt"
"log"
"regexp"
"github.com/google/go-cmp/cmp"
"google.golang.org/api/cloudresourcemanager/v1"
)
//TODO: Add handling of policy version according to the comments in Policy type, see: https://godoc.org/google.golang.org/api/cloudresourcemanager/v1#Policy
// projects management object.
type Client struct {
crmservice CRM
}
type CRM interface {
GetPolicy(projectname string, getiampolicyrequest *cloudresourcemanager.GetIamPolicyRequest) (*cloudresourcemanager.Policy, error)
SetPolicy(projectname string, setiampolicyrequest *cloudresourcemanager.SetIamPolicyRequest) (*cloudresourcemanager.Policy, error)
}
//Custom Errors
type PolicyModifiedError struct {
msg string //description of error
}
type BindingNotFoundError struct {
msg string //description of error
}
// Implementation of Error interface.
func (e *PolicyModifiedError) Error() string { return e.msg }
func (e *BindingNotFoundError) Error() string { return e.msg }
// New return new Client and error object. Error is not used at present. Added it for future use and to support common error handling.
func New(crmservice CRM) (*Client, error) {
return &Client{
crmservice: crmservice,
}, nil
}
//TODO: Change method signature to accept condition expression string instead *cloudresourcemanager.Expr object. Align cmd main.go code to pass expression string.
//AddSAtoRole will fetch policy from GCP, assign serviceaccount to roles and send policy back to GCP.
//If role binding doesn't exist it will be added to the policy.
//Check in caller if returned error is PolicyModifiedError. If yes, GCP policy was changed by other caller in the meantime.
func (client *Client) AddSAtoRole(saname string, roles []string, projectname string, condition *cloudresourcemanager.Expr) (*cloudresourcemanager.Policy, error) {
//Test if mandatory input values are not empty
if saname == "" || projectname == "" || len(roles) == 0 || func(roles []string) bool {
for _, role := range roles {
if role == "" {
return true
}
}
return false
}(roles) {
return nil, fmt.Errorf("One of mandatory method arguments saname, projectname ,role can not be empty. Got values. saname: %s projectname: %s roles: %v.", saname, projectname, roles)
}
match, err := regexp.MatchString(`^.+@.+\.iam\.gserviceaccount\.com$`, saname)
if err != nil {
//TODO: How to test this return?
return nil, fmt.Errorf("When checking if provided saname match safqdn regex got error: %w.", err)
}
if match {
return nil, fmt.Errorf("saname argument can not be serviceaccount fqdn. Provide only name, without domain part. Got value: %s.", saname)
}
//Getting current policy from GCP
policy, err := client.getPolicy(projectname)
if err != nil {
return nil, fmt.Errorf("When adding role for serviceaccount %s got error: %w.", saname, err)
}
//
safqdn := client.MakeSafqdn(saname, projectname)
//Go over roles to assign
for _, role := range roles {
//Make valid rolename string
rolefullname := client.makeRoleFullname(role)
//Add service account to role binding
err = client.addToRole(policy, safqdn, rolefullname, projectname, condition)
if err != nil {
if _, ok := err.(*BindingNotFoundError); ok {
//If role binding was not found create it and add serviceacount to it.
client.addRole(policy, safqdn, rolefullname, projectname, condition)
} else {
return nil, fmt.Errorf("When adding role for serviceaccount %s got error: %w.", saname, err)
}
}
}
//Send policy back to GCP
err = client.setPolicy(policy, projectname)
if err != nil {
return nil, fmt.Errorf("When adding roles for serviceaccount %s got error: %w", safqdn, err)
}
log.Printf("Assigned %s to roles: %v", safqdn, roles)
return policy, nil
}
//TODO: Change method signature to accept condition expression string instead *cloudresourcemanager.Expr object. Align cmd main.go code to pass expression string.
//AddSAtoRole will fetch policy from GCP, assign serviceaccount to roles and send policy back to GCP.
//If role binding doesn't exist it will be added to the policy.
//Check in caller if returned error is PolicyModifiedError. If yes, GCP policy was changed by other caller in the meantime.
func (client *Client) RemoveSaRole(saname string, roles []string, projectname string, condition *cloudresourcemanager.Expr) (*cloudresourcemanager.Policy, error) {
//Test if mandatory input values are not empty
if saname == "" || projectname == "" || len(roles) == 0 || func(roles []string) bool {
for _, role := range roles {
if role == "" {
return true
}
}
return false
}(roles) {
return nil, fmt.Errorf("One of mandatory method arguments saname, projectname ,role can not be empty. Got values. saname: %s projectname: %s roles: %v.", saname, projectname, roles)
}
match, err := regexp.MatchString(`^.+@.+\.iam\.gserviceaccount\.com$`, saname)
if err != nil {
//TODO: How to test this return?
return nil, fmt.Errorf("When checking if provided saname match safqdn regex got error: %w.", err)
}
if match {
return nil, fmt.Errorf("saname argument can not be serviceaccount fqdn. Provide only name, without domain part. Got value: %s.", saname)
}
//Getting current policy from GCP
policy, err := client.getPolicy(projectname)
if err != nil {
return nil, fmt.Errorf("When removing role for serviceaccount %s got error: %w.", saname, err)
}
//
safqdn := client.MakeSafqdn(saname, projectname)
//Go over roles to assign
for _, role := range roles {
//Make valid rolename string
rolefullname := client.makeRoleFullname(role)
//Add service account to role binding
err = client.removeFromRole(policy, safqdn, rolefullname, projectname, condition)
if err != nil {
if _, ok := err.(*BindingNotFoundError); ok {
//If role binding was not found create it and add serviceacount to it.
continue
} else {
return nil, fmt.Errorf("When removing role for serviceaccount %s got error: %w.", saname, err)
}
}
}
//Send policy back to GCP
err = client.setPolicy(policy, projectname)
if err != nil {
return nil, fmt.Errorf("When removing roles for serviceaccount %s got error: %w", safqdn, err)
}
log.Printf("Removed %s from roles: %v", safqdn, roles)
return policy, nil
}
//getPolicy will fetch policy from GCP
func (client *Client) getPolicy(projectname string) (*cloudresourcemanager.Policy, error) {
iampolicyrequest := &cloudresourcemanager.GetIamPolicyRequest{}
policy, err := client.crmservice.GetPolicy(projectname, iampolicyrequest)
if err != nil {
return nil, fmt.Errorf("When downloading policy for %s project got error: %w.", projectname, err)
}
return policy, nil
}
//addToRole will search role binding and add serviceaccount to the role binding members list.
func (client *Client) addToRole(policy *cloudresourcemanager.Policy, safqdn string, rolefullname string, projectname string, condition *cloudresourcemanager.Expr) error {
for index, binding := range policy.Bindings {
if binding.Role == rolefullname && cmp.Equal(binding.Condition, condition) {
policy.Bindings[index].Members = append(policy.Bindings[index].Members, safqdn)
return nil
}
}
return &BindingNotFoundError{msg: fmt.Sprintf("Binding for role %s not found in %s project policy.", rolefullname, projectname)}
}
func (client *Client) removeFromRole(policy *cloudresourcemanager.Policy, safqdn string, rolefullname string, projectname string, condition *cloudresourcemanager.Expr) error {
for index, binding := range policy.Bindings {
if binding.Role == rolefullname && cmp.Equal(binding.Condition, condition) {
for i, v := range binding.Members {
if v == safqdn {
a := binding.Members
a[i] = a[len(a)-1]
a[len(a)-1] = ""
a = a[:len(a)-1]
policy.Bindings[index].Members = a
}
}
return nil
}
}
return &BindingNotFoundError{msg: fmt.Sprintf("Binding for role %s not found in %s project policy.", rolefullname, projectname)}
}
//TODO: This should be renamed to make sa resource string. It should not be exported. Revert it to client private method.
//makeSafqdn will create serviceaccount fully qualified valid name, accepted by GCP API.
func (client *Client) MakeSafqdn(saname string, projectname string) string {
return fmt.Sprintf("serviceAccount:%s@%s.iam.gserviceaccount.com", saname, projectname)
}
//makeRoleFullname will create role name valid string, accepted by GCP API.
func (client *Client) makeRoleFullname(role string) string {
if prefixed, _ := regexp.MatchString("^(roles|organizations)/.*", role); !prefixed {
return fmt.Sprintf("roles/%s", role)
}
return role
}
//addRole will create new binding for role and add serviceaccount to members list.
func (client *Client) addRole(policy *cloudresourcemanager.Policy, safqdn string, rolefullname string, projectname string, condition *cloudresourcemanager.Expr) {
policy.Bindings = append(policy.Bindings, &cloudresourcemanager.Binding{
Role: rolefullname,
Members: []string{safqdn},
Condition: condition,
})
}
//setPolicy will send policy back to GCP.
//It will check if policy was not modified and differ from the one which was downloaded and modified.
//Policy modification is detected by comparing policy resource etag.
func (client *Client) setPolicy(policy *cloudresourcemanager.Policy, projectname string) error {
currentpolicy, err := client.getPolicy(projectname)
if err == nil {
if currentpolicy.Etag != policy.Etag {
return &PolicyModifiedError{msg: fmt.Sprintf("When checking if policy was modified for %s project got: Policy was modified.", projectname)}
}
} else {
return fmt.Errorf("When sending new policy, failed download current policy from GCP. Got error: %w", err)
}
iampolicyrequest := &cloudresourcemanager.SetIamPolicyRequest{
Policy: policy,
}
policy, err = client.crmservice.SetPolicy(projectname, iampolicyrequest)
if err != nil {
return fmt.Errorf("When setting new policy for %s project got error: %w.", projectname, err)
}
return nil
}