-
Notifications
You must be signed in to change notification settings - Fork 21
/
provider.go
203 lines (165 loc) · 4.93 KB
/
provider.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
package gcloudiam
import (
"errors"
"fmt"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/raystack/guardian/core/provider"
"github.com/raystack/guardian/domain"
"golang.org/x/net/context"
)
//go:generate mockery --name=GcloudIamClient --exported --with-expecter
type GcloudIamClient interface {
GetRoles() ([]*Role, error)
GrantAccess(accountType, accountID, role string) error
RevokeAccess(accountType, accountID, role string) error
ListAccess(ctx context.Context, resources []*domain.Resource) (domain.MapResourceAccess, error)
}
//go:generate mockery --name=encryptor --exported --with-expecter
type encryptor interface {
domain.Crypto
}
type Provider struct {
provider.PermissionManager
provider.UnimplementedClient
typeName string
Clients map[string]GcloudIamClient
crypto encryptor
}
func NewProvider(typeName string, crypto encryptor) *Provider {
return &Provider{
typeName: typeName,
Clients: map[string]GcloudIamClient{},
crypto: crypto,
}
}
func (p *Provider) GetType() string {
return p.typeName
}
func (p *Provider) CreateConfig(pc *domain.ProviderConfig) error {
c := NewConfig(pc, p.crypto)
if err := c.ParseAndValidate(); err != nil {
return err
}
client, err := p.getIamClient(pc)
if err != nil {
return err
}
for _, r := range c.ProviderConfig.Resources {
if err := c.validatePermissions(r, client); err != nil {
return err
}
}
return c.EncryptCredentials()
}
func (p *Provider) GetResources(pc *domain.ProviderConfig) ([]*domain.Resource, error) {
var creds Credentials
if err := mapstructure.Decode(pc.Credentials, &creds); err != nil {
return nil, err
}
var t string
if strings.HasPrefix(creds.ResourceName, "project") {
t = ResourceTypeProject
} else if strings.HasPrefix(creds.ResourceName, "organization") {
t = ResourceTypeOrganization
}
return []*domain.Resource{
{
ProviderType: pc.Type,
ProviderURN: pc.URN,
Type: t,
URN: creds.ResourceName,
Name: fmt.Sprintf("%s - GCP IAM", creds.ResourceName),
},
}, nil
}
func (p *Provider) GrantAccess(pc *domain.ProviderConfig, a domain.Grant) error {
// TODO: validate provider config and appeal
var creds Credentials
if err := mapstructure.Decode(pc.Credentials, &creds); err != nil {
return err
}
client, err := p.getIamClient(pc)
if err != nil {
return err
}
if a.Resource.Type == ResourceTypeProject || a.Resource.Type == ResourceTypeOrganization {
for _, p := range a.Permissions {
permission := fmt.Sprint(p)
if err := client.GrantAccess(a.AccountType, a.AccountID, permission); err != nil {
if !errors.Is(err, ErrPermissionAlreadyExists) {
return err
}
}
}
return nil
}
return ErrInvalidResourceType
}
func (p *Provider) RevokeAccess(pc *domain.ProviderConfig, a domain.Grant) error {
var creds Credentials
if err := mapstructure.Decode(pc.Credentials, &creds); err != nil {
return err
}
client, err := p.getIamClient(pc)
if err != nil {
return err
}
if a.Resource.Type == ResourceTypeProject || a.Resource.Type == ResourceTypeOrganization {
for _, p := range a.Permissions {
permission := fmt.Sprint(p)
if err := client.RevokeAccess(a.AccountType, a.AccountID, permission); err != nil {
if !errors.Is(err, ErrPermissionNotFound) {
return err
}
}
}
return nil
}
return ErrInvalidResourceType
}
func (p *Provider) GetRoles(pc *domain.ProviderConfig, resourceType string) ([]*domain.Role, error) {
if resourceType != ResourceTypeProject && resourceType != ResourceTypeOrganization {
return nil, ErrInvalidResourceType
}
return provider.GetRoles(pc, resourceType)
}
func (p *Provider) GetPermissions(_pc *domain.ProviderConfig, _resourceType, role string) ([]interface{}, error) {
// TODO: validate if role is a valid gcloud iam role
return p.PermissionManager.GetPermissions(_pc, _resourceType, role)
}
func (p *Provider) GetAccountTypes() []string {
return []string{
AccountTypeUser,
AccountTypeServiceAccount,
AccountTypeGroup,
}
}
func (p *Provider) ListAccess(ctx context.Context, pc domain.ProviderConfig, resources []*domain.Resource) (domain.MapResourceAccess, error) {
var creds Credentials
if err := mapstructure.Decode(pc.Credentials, &creds); err != nil {
return nil, fmt.Errorf("parsing credentials: %w", err)
}
client, err := p.getIamClient(&pc)
if err != nil {
return nil, fmt.Errorf("initializing iam client: %w", err)
}
return client.ListAccess(ctx, resources)
}
func (p *Provider) getIamClient(pc *domain.ProviderConfig) (GcloudIamClient, error) {
var credentials Credentials
if err := mapstructure.Decode(pc.Credentials, &credentials); err != nil {
return nil, err
}
providerURN := pc.URN
if p.Clients[providerURN] != nil {
return p.Clients[providerURN], nil
}
credentials.Decrypt(p.crypto)
client, err := newIamClient([]byte(credentials.ServiceAccountKey), credentials.ResourceName)
if err != nil {
return nil, err
}
p.Clients[providerURN] = client
return client, nil
}