-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
212 lines (167 loc) · 5.4 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
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
package main
import (
"sort"
"regexp"
log "github.com/sirupsen/logrus"
)
type EnvVars struct {
Host string `default:"0.0.0.0" envconfig:"BWC_HOST"`
Port string `default:"8080" envconfig:"BWC_PORT"`
Log string `default:"Info" envconfig:"BWC_LOG_LEVEL"`
Title string `default:"Bucket Web Client" envconfig:"BWC_TITLE"`
ClientID string `required:"true" envconfig:"BWC_CLIENT_ID"`
ClientSecret string `required:"true" envconfig:"BWC_CLIENT_SECRET"`
RedirectURL string `required:"true" envconfig:"BWC_REDIRECT_URL"`
AuthFile string `required:"true" envconfig:"BWC_AUTH_FILE"`
GoogleFile string `default:"" envconfig:"BWC_GOOGLE_APPLICATION_CREDENTIALS"`
TimeoutCache int64 `default:"60" envconfig:"BWC_TIMEOUT_CACHE"`
}
type AuthRules struct {
AuthRules []AuthRule `json:"auth_rules"`
BucketNames []BucketNaming `json:"bucket_friendly_naming"`
}
type AuthRule struct {
Emails []string `json:"emails"`
AwsBuckets []string `json:"aws_buckets"`
GcpBuckets []string `json:"gcp_buckets"`
}
type BucketNaming struct {
RealName string `json:"real_name"`
FriendlyName string `json:"friendly_name"`
}
func sortAndValidateAuthRules (authRules []AuthRule) (error) {
for _,rule := range authRules {
sort.Strings(rule.Emails)
for _,r := range rule.Emails {
_, err := regexp.Compile(r)
if err != nil {
return err
}
}
if len(rule.AwsBuckets) > 0 {
sort.Strings(rule.AwsBuckets)
for _,r := range rule.AwsBuckets {
_, err := regexp.Compile(r)
if err != nil {
return err
}
}
}
if len(rule.GcpBuckets) > 0 {
sort.Strings(rule.GcpBuckets)
for _,r := range rule.GcpBuckets {
_, err := regexp.Compile(r)
if err != nil {
return err
}
}
}
}
return nil
}
func removeDuplicateStrings(slice []string) ([]string) {
keys := map[string]bool{}
list := []string{}
for _, entry := range slice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func getListBucketUserConfig(userEmail string) ([]string, []string) {
var bucketsAws, bucketsGcp []string
for _,rule := range authRules.AuthRules {
for _,user := range rule.Emails {
found, err := regexp.MatchString(user, userEmail)
if err != nil {
log.Error(err)
continue
}
if found {
bucketsAws = append (bucketsAws, rule.AwsBuckets...)
bucketsGcp = append (bucketsGcp, rule.GcpBuckets...)
}
}
}
uniqueBucketsAws := removeDuplicateStrings(bucketsAws)
uniqueBucketsGcp := removeDuplicateStrings(bucketsGcp)
return uniqueBucketsAws, uniqueBucketsGcp
}
func checkUserAuth(userEmail string) (bool) {
for _,rule := range authRules.AuthRules {
for _,user := range rule.Emails {
found, err := regexp.MatchString(user, userEmail)
if err != nil {
log.Error(err)
continue
}
if found {
log.Info("User ", userEmail, " is allowed")
return true
}
}
}
return false
}
func checkUserAuthBucket(userEmail, userBucket string) (bool) {
for _,rule := range authRules.AuthRules {
for _,user := range rule.Emails {
found, err := regexp.MatchString(user, userEmail)
if err != nil {
log.Error(err)
continue
}
if found {
for _,bucket := range rule.AwsBuckets {
found, err := regexp.MatchString(bucket, userBucket)
if err != nil {
log.Error(err)
continue
}
if found {
log.Info("User ", userEmail, " accessing Aws bucket ", userBucket)
return true
}
}
for _,bucket := range rule.GcpBuckets {
found, err := regexp.MatchString(bucket, userBucket)
if err != nil {
log.Error(err)
continue
}
if found {
log.Info("User ", userEmail, " accessing Gcp bucket ", userBucket)
return true
}
}
}
}
}
return false
}
// FriendlyName related functions
func getRealBucketName(friendlyName string) string {
for _,bucket := range authRules.BucketNames {
if bucket.FriendlyName == friendlyName {
return bucket.RealName
}
}
return friendlyName
}
func getFriendlyBucketName(realName string) string {
for _,bucket := range authRules.BucketNames {
if bucket.RealName == realName {
return bucket.FriendlyName
}
}
return realName
}
func changeRealToFriendlyBuckets (realName []string) []string {
var buckets []string
for _,bucket := range realName {
buckets = append(buckets, getFriendlyBucketName(bucket))
}
return buckets
}