-
Notifications
You must be signed in to change notification settings - Fork 21
/
client.go
213 lines (189 loc) · 5.8 KB
/
client.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
package gcloudiam
import (
"context"
"fmt"
"strings"
"github.com/raystack/guardian/domain"
"google.golang.org/api/cloudresourcemanager/v1"
"google.golang.org/api/iam/v1"
"google.golang.org/api/option"
)
const (
ResourceNameOrganizationPrefix = "organizations/"
ResourceNameProjectPrefix = "projects/"
)
type iamClient struct {
resourceName string
cloudResourceManagerService *cloudresourcemanager.Service
iamService *iam.Service
}
func newIamClient(credentialsJSON []byte, resourceName string) (*iamClient, error) {
ctx := context.Background()
cloudResourceManagerService, err := cloudresourcemanager.NewService(ctx, option.WithCredentialsJSON(credentialsJSON))
if err != nil {
return nil, err
}
iamService, err := iam.NewService(ctx, option.WithCredentialsJSON(credentialsJSON))
if err != nil {
return nil, err
}
return &iamClient{
resourceName: resourceName,
cloudResourceManagerService: cloudResourceManagerService,
iamService: iamService,
}, nil
}
func (c *iamClient) GetRoles() ([]*Role, error) {
var roles []*Role
ctx := context.TODO()
req := c.iamService.Roles.List()
if err := req.Pages(ctx, func(page *iam.ListRolesResponse) error {
for _, role := range page.Roles {
roles = append(roles, c.fromIamRole(role))
}
return nil
}); err != nil {
return nil, err
}
if strings.HasPrefix(c.resourceName, ResourceNameProjectPrefix) {
projectRolesReq := c.iamService.Projects.Roles.List(c.resourceName)
if err := projectRolesReq.Pages(ctx, func(page *iam.ListRolesResponse) error {
for _, role := range page.Roles {
roles = append(roles, c.fromIamRole(role))
}
return nil
}); err != nil {
return nil, err
}
} else if strings.HasPrefix(c.resourceName, ResourceNameOrganizationPrefix) {
orgRolesReq := c.iamService.Organizations.Roles.List(c.resourceName)
if err := orgRolesReq.Pages(ctx, func(page *iam.ListRolesResponse) error {
for _, role := range page.Roles {
roles = append(roles, c.fromIamRole(role))
}
return nil
}); err != nil {
return nil, err
}
} else {
return nil, ErrInvalidResourceName
}
return roles, nil
}
func (c *iamClient) GrantAccess(accountType, accountID, role string) error {
ctx := context.TODO()
policy, err := c.getIamPolicy(ctx)
if err != nil {
return err
}
member := fmt.Sprintf("%s:%s", accountType, accountID)
roleExists := false
for _, b := range policy.Bindings {
if b.Role == role {
roleExists = true
if containsString(b.Members, member) {
return ErrPermissionAlreadyExists
}
b.Members = append(b.Members, member)
}
}
if !roleExists {
policy.Bindings = append(policy.Bindings, &cloudresourcemanager.Binding{
Role: role,
Members: []string{member},
})
}
_, err = c.setIamPolicy(ctx, policy)
return err
}
func (c *iamClient) RevokeAccess(accountType, accountID, role string) error {
ctx := context.TODO()
policy, err := c.getIamPolicy(ctx)
if err != nil {
return err
}
member := fmt.Sprintf("%s:%s", accountType, accountID)
for _, b := range policy.Bindings {
if b.Role == role {
removeIndex := -1
for i, m := range b.Members {
if m == member {
removeIndex = i
}
}
if removeIndex == -1 {
return ErrPermissionNotFound
}
b.Members = append(b.Members[:removeIndex], b.Members[removeIndex+1:]...)
}
}
c.setIamPolicy(ctx, policy)
return err
}
func (c *iamClient) ListAccess(ctx context.Context, resources []*domain.Resource) (domain.MapResourceAccess, error) {
policy, err := c.getIamPolicy(ctx)
if err != nil {
return nil, fmt.Errorf("getting IAM policy: %w", err)
}
access := make(domain.MapResourceAccess)
for _, resource := range resources {
for _, binding := range policy.Bindings {
for _, member := range binding.Members {
account := strings.Split(member, ":")
ae := domain.AccessEntry{
AccountType: account[0],
AccountID: account[1],
Permission: binding.Role,
}
access[resource.URN] = append(access[resource.URN], ae)
}
}
}
return access, nil
}
func (c *iamClient) getIamPolicy(ctx context.Context) (*cloudresourcemanager.Policy, error) {
if strings.HasPrefix(c.resourceName, ResourceNameProjectPrefix) {
projectID := strings.Replace(c.resourceName, ResourceNameProjectPrefix, "", 1)
return c.cloudResourceManagerService.Projects.
GetIamPolicy(projectID, &cloudresourcemanager.GetIamPolicyRequest{}).
Context(ctx).Do()
} else if strings.HasPrefix(c.resourceName, ResourceNameOrganizationPrefix) {
orgID := strings.Replace(c.resourceName, ResourceNameOrganizationPrefix, "", 1)
return c.cloudResourceManagerService.Organizations.
GetIamPolicy(orgID, &cloudresourcemanager.GetIamPolicyRequest{}).
Context(ctx).Do()
}
return nil, ErrInvalidResourceName
}
func (c *iamClient) setIamPolicy(ctx context.Context, policy *cloudresourcemanager.Policy) (*cloudresourcemanager.Policy, error) {
setIamPolicyRequest := &cloudresourcemanager.SetIamPolicyRequest{
Policy: policy,
}
if strings.HasPrefix(c.resourceName, ResourceNameProjectPrefix) {
projectID := strings.Replace(c.resourceName, ResourceNameProjectPrefix, "", 1)
return c.cloudResourceManagerService.Projects.
SetIamPolicy(projectID, setIamPolicyRequest).
Context(ctx).Do()
} else if strings.HasPrefix(c.resourceName, ResourceNameOrganizationPrefix) {
orgID := strings.Replace(c.resourceName, ResourceNameOrganizationPrefix, "", 1)
return c.cloudResourceManagerService.Organizations.
SetIamPolicy(orgID, setIamPolicyRequest).
Context(ctx).Do()
}
return nil, ErrInvalidResourceName
}
func (c *iamClient) fromIamRole(r *iam.Role) *Role {
return &Role{
Name: r.Name,
Title: r.Title,
Description: r.Description,
}
}
func containsString(arr []string, v string) bool {
for _, item := range arr {
if item == v {
return true
}
}
return false
}