/
config.go
175 lines (142 loc) · 4.28 KB
/
config.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
package dataplex
import (
"encoding/base64"
"errors"
"fmt"
"strings"
"github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure"
"github.com/raystack/guardian/domain"
"github.com/raystack/guardian/utils"
)
const (
AccountTypeUser = "user"
AccountTypeServiceAccount = "serviceAccount"
)
// Credentials is the authentication configuration used by the policy_tag client
type Credentials struct {
ServiceAccountKey string `mapstructure:"service_account_key" json:"service_account_key" validate:"required,base64"`
ResourceName string `mapstructure:"resource_name" json:"resource_name" validate:"startswith=projects/"`
}
// Encrypt encrypts GCP credentials
func (c *Credentials) Encrypt(encryptor domain.Encryptor) error {
if c == nil {
return ErrUnableToEncryptNilCredentials
}
encryptedCredentials, err := encryptor.Encrypt(c.ServiceAccountKey)
if err != nil {
return err
}
c.ServiceAccountKey = encryptedCredentials
return nil
}
// Decrypt decrypts GCP credentials
func (c *Credentials) Decrypt(decryptor domain.Decryptor) error {
if c == nil {
return ErrUnableToDecryptNilCredentials
}
decryptedCredentials, err := decryptor.Decrypt(c.ServiceAccountKey)
if err != nil {
return err
}
c.ServiceAccountKey = decryptedCredentials
return nil
}
// Permission is for mapping role into policy tag fine-grained-reader permissions
type Permission string
// Config for policy_tag provider
type Config struct {
ProviderConfig *domain.ProviderConfig
valid bool
crypto domain.Crypto
validator *validator.Validate
}
// NewConfig returns policy_tag config struct
func NewConfig(pc *domain.ProviderConfig, crypto domain.Crypto) *Config {
return &Config{
ProviderConfig: pc,
validator: validator.New(),
crypto: crypto,
}
}
// EncryptCredentials encrypts the policy_tag credentials config
func (c *Config) EncryptCredentials() error {
if err := c.parseAndValidate(); err != nil {
return err
}
credentials, ok := c.ProviderConfig.Credentials.(*Credentials)
if !ok {
return ErrInvalidCredentialsType
}
if err := credentials.Encrypt(c.crypto); err != nil {
return err
}
c.ProviderConfig.Credentials = credentials
return nil
}
// ParseAndValidate validates policy_tag config within provider config and make the interface{} config value castable into the expected policy_tag config value
func (c *Config) ParseAndValidate() error {
return c.parseAndValidate()
}
func (c *Config) parseAndValidate() error {
if c.valid {
return nil
}
credentials, err := c.validateCredentials(c.ProviderConfig.Credentials)
if err != nil {
return err
} else {
c.ProviderConfig.Credentials = credentials
}
permissionValidationErrors := []error{}
for _, resource := range c.ProviderConfig.Resources {
for _, role := range resource.Roles {
for i, permission := range role.Permissions {
if permissionConfig, err := c.validatePermission(permission, resource.Type); err != nil {
permissionValidationErrors = append(permissionValidationErrors, err)
} else {
role.Permissions[i] = permissionConfig
}
}
}
}
if len(permissionValidationErrors) > 0 {
errorStrings := []string{}
for _, err := range permissionValidationErrors {
errorStrings = append(errorStrings, err.Error())
}
return errors.New(strings.Join(errorStrings, "\n"))
}
c.valid = true
return nil
}
func (c *Config) validateCredentials(value interface{}) (*Credentials, error) {
var credentials Credentials
if err := mapstructure.Decode(value, &credentials); err != nil {
return nil, err
}
if err := c.validator.Struct(credentials); err != nil {
return nil, err
}
saKeyJson, err := base64.StdEncoding.DecodeString(credentials.ServiceAccountKey)
if err != nil {
return nil, err
}
credentials.ServiceAccountKey = string(saKeyJson)
return &credentials, nil
}
func (c *Config) validatePermission(value interface{}, resourceType string) (*Permission, error) {
permision, ok := value.(string)
if !ok {
return nil, ErrInvalidPermissionConfig
}
if resourceType == ResourceTypeTag {
if !utils.ContainsString([]string{FineGrainReaderPermission}, permision) {
return nil, fmt.Errorf("%v: %v", ErrInvalidDatasetPermission, permision)
}
} else {
return nil, ErrInvalidResourceType
}
configValue := Permission(permision)
return &configValue, nil
}