forked from GoogleCloudPlatform/professional-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
433 lines (382 loc) · 11 KB
/
main.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"encoding/json"
"github.com/s12v/go-jwks"
"github.com/square/go-jose"
"cloud.google.com/go/storage"
"github.com/form3tech-oss/jwt-go"
"golang.org/x/oauth2"
"google.golang.org/api/impersonate"
"google.golang.org/api/iterator"
"gopkg.in/yaml.v3"
)
type Branch struct {
Ref string `yaml:"ref"`
ServiceAccounts []string `yaml:"service_accounts"`
AllowedScopes []string `yaml:"allowed_scopes"`
}
type Pipeline struct {
Name string `yaml:"name"`
DefaultServiceAccount string `yaml:"default_sa"`
Branches []Branch `yaml:"branches"`
}
type Config struct {
Issuers []struct {
Name string `yaml:"name"`
JwksUrl string `yaml:"jwks_url"`
} `yaml:"issuers"`
Pipelines []Pipeline `yaml:"pipelines"`
}
var config *Config
func main() {
var err error
var port int64
if os.Getenv("PORT") != "" {
port, err = strconv.ParseInt(os.Getenv("PORT"), 10, 64)
if err != nil {
log.Panicf("Can't parse value of PORT env variable %s %v", os.Getenv("PORT"), err)
}
} else {
port = 8080
}
err = getConfig()
if err != nil {
fmt.Printf("Unable to read config %v", err)
log.Panicf("Unable to read config %v", err)
}
go scheduleConfigRefresh()
http.HandleFunc("/access", handler)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}
func handler(w http.ResponseWriter, r *http.Request) {
sa := r.URL.Query().Get("sa")
scopesString := r.URL.Query().Get("scopes")
if scopesString == "" {
scopesString = "https://www.googleapis.com/auth/cloud-platform"
}
scopes := strings.Split(scopesString, ",")
var err error
var lifetime float64
lifetimeString := r.URL.Query().Get("lifetime")
if lifetimeString == "" {
lifetime = 60
} else {
lifetime, err = strconv.ParseFloat(lifetimeString, 32)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "%v", err)
return
}
}
jwt := r.Header.Get("Gitlab-Token")
if jwt != "" {
claims, err := validateJwtAndExtractClaims(jwt)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "The provided jwt is invalid %v", err)
return
}
if sa == "" {
pipeline, err := retrievePipeline(claims)
if err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "no default SA for the project path defined %s, provide one via sa query attribute", claims.ProjectPath)
return
}
sa = pipeline.DefaultServiceAccount
if sa == "" {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "no default SA for the project path defined %s, provide one via sa query attribute", claims.ProjectPath)
return
}
}
if hasAccess(sa, claims) {
token, err := getAccesToken(sa,
scopes,
time.Duration(lifetime)*time.Minute)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "%v", err)
} else {
fmt.Fprintf(w, "%s", token.AccessToken)
}
} else {
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "JWT not allowed to access sa")
}
} else {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "No jwt provided")
}
}
func retrievePipeline(claims *GitlabClaims) (*Pipeline, error) {
current := -1
var currentPipeline *Pipeline
for i := 0; i < len(config.Pipelines); i++ {
if strings.HasPrefix(claims.ProjectPath, config.Pipelines[i].Name) {
if current < len(config.Pipelines[i].Name) {
current = len(config.Pipelines[i].Name)
currentPipeline = &config.Pipelines[i]
}
}
}
if current == -1 {
return nil, fmt.Errorf("no pipeline for project path %s", claims.ProjectPath)
} else {
return currentPipeline, nil
}
}
func hasAccess(serviceAccount string, claims *GitlabClaims) bool {
for i := 0; i < len(config.Pipelines); i++ {
if strings.HasPrefix(claims.ProjectPath, config.Pipelines[i].Name) {
for j := 0; j < len(config.Pipelines[i].Branches); j++ {
if config.Pipelines[i].Branches[j].Ref == "*" || config.Pipelines[i].Branches[j].Ref == claims.Ref {
if arrayContains(config.Pipelines[i].Branches[j].ServiceAccounts, serviceAccount) {
return true
}
}
}
}
}
return false
}
func arrayContains(array []string, element string) bool {
for _, ele := range array {
if ele == element {
return true
}
}
return false
}
type GitlabClaims struct {
NamespaceId string `json:"namespace_id"`
NamespacePath string `json:"namespace_path"`
ProjectId string `json:"project_id"`
ProjectPath string `json:"project_path"`
UserId string `json:"user_id"`
UserLogin string `json:"user_login"`
UserEmail string `json:"user_email"`
PipelineId string `json:"pipeline_id"`
JobId string `json:"job_id"`
Ref string `json:"ref"`
RefType string `json:"ref_type"`
RefProtected string `json:"ref_protected"`
jwt.StandardClaims
}
func validateJwtAndExtractClaims(tokenString string) (*GitlabClaims, error) {
log.Printf("token %v", tokenString)
var claims jwt.Claims = &GitlabClaims{}
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
claims, ok := token.Claims.(*GitlabClaims)
if claims.RefProtected != "true" {
return nil, fmt.Errorf("unprotected refs are not allowed to access service accounts ref=%s", claims.Ref)
}
if ok {
for i := 0; len(config.Issuers) > i; i++ {
if claims.Issuer == config.Issuers[i].Name {
var jwksClient jwks.JWKSClient
if strings.HasPrefix(config.Issuers[i].JwksUrl, "http") {
jwksSource := jwks.NewWebSource(config.Issuers[i].JwksUrl)
jwksClient = jwks.NewDefaultClient(
jwksSource,
time.Hour, // Refresh keys every 1 hour
12*time.Hour, // Expire keys after 12 hours
)
} else {
jwksSource := NewFileSource(config.Issuers[i].JwksUrl)
jwksClient = jwks.NewDefaultClient(
jwksSource,
time.Hour, // Refresh keys every 1 hour
12*time.Hour, // Expire keys after 12 hours
)
}
var jwk *jose.JSONWebKey
kid := token.Header["kid"]
kidString := fmt.Sprintf("%v", kid)
jwk, err := jwksClient.GetEncryptionKey(kidString)
if err != nil {
log.Fatal(err)
}
return jwk.Public().Key, nil
}
}
return nil, fmt.Errorf("issuer not configured %s", claims.Issuer)
}
return nil, fmt.Errorf("token not okay %s", tokenString)
})
if claims, ok := token.Claims.(*GitlabClaims); ok && token.Valid {
return claims, nil
} else {
return nil, err
}
}
func getAccesToken(sa string, scopes []string, lifetime time.Duration) (*oauth2.Token, error) {
ctx := context.Background()
ts, err := impersonate.CredentialsTokenSource(ctx, impersonate.CredentialsConfig{
TargetPrincipal: sa,
Scopes: scopes,
Lifetime: lifetime,
})
if err != nil {
return nil, err
}
var token *oauth2.Token
token, err = ts.Token()
if err != nil {
return nil, err
}
return token, nil
}
func getConfig() error {
config = &Config{}
configFileLocation := os.Getenv("GCS_CONFIG_LINK") // gs://bucketname/objectname
if configFileLocation == "" {
return fmt.Errorf("Environment Variable GCS_CONFIG_LINK not set")
}
u, err := url.Parse(configFileLocation)
if err != nil {
return fmt.Errorf("Environment Variable GCS_CONFIG_LINK not properly formated %v", err)
}
if u.Scheme == "gs" {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
defer client.Close()
query := &storage.Query{Prefix: u.Path[1:]}
bucket := client.Bucket(u.Host)
it := bucket.Objects(ctx, query)
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
log.Printf("Loading config %s", attrs.Name)
rc, err := bucket.Object(attrs.Name).NewReader(ctx)
if err != nil {
return fmt.Errorf("Object(%q).NewReader: %v", attrs.Name, err)
}
defer rc.Close()
dat, err := ioutil.ReadAll(rc)
if err != nil {
return fmt.Errorf("ioutil.ReadAll: %v", err)
}
var singleConfig Config
err = yaml.Unmarshal(dat, &singleConfig)
if err != nil {
log.Fatalf("error parsing config file %s %v", attrs.Name, err)
}
mergeConfig(config, &singleConfig)
//names = append(names, attrs.Name)
}
return nil
} else {
return fmt.Errorf("Schema of provided location unsupported (currently only gs://)")
}
}
func mergeConfig(target *Config, source *Config) {
target.Issuers = append(target.Issuers, source.Issuers...)
for _, sourcePipeline := range source.Pipelines {
merged := false
for _, targetPipeline := range target.Pipelines {
if targetPipeline.Name == sourcePipeline.Name {
mergePipelines(&targetPipeline, &sourcePipeline)
merged = true
}
}
if !merged {
target.Pipelines = append(target.Pipelines, sourcePipeline)
}
}
}
func mergePipelines(target *Pipeline, source *Pipeline) {
target.DefaultServiceAccount = source.DefaultServiceAccount
for _, sourceBranch := range source.Branches {
merged := false
for _, targetBranch := range target.Branches {
if targetBranch.Ref == sourceBranch.Ref {
mergeBranches(&targetBranch, &sourceBranch)
merged = true
}
}
if !merged {
target.Branches = append(target.Branches, sourceBranch)
}
}
}
func mergeBranches(target *Branch, source *Branch) {
target.AllowedScopes = append(target.AllowedScopes, source.AllowedScopes...)
target.ServiceAccounts = append(target.ServiceAccounts, source.ServiceAccounts...)
}
/*
Refresh
*/
func scheduleConfigRefresh() {
var err error
configRefreshIntervalString := os.Getenv("CONFIG_REFRESH_INTERVAL")
if configRefreshIntervalString == "0" || configRefreshIntervalString == "-1" {
return
} else {
var configRefreshInterval int64 = 5
if configRefreshIntervalString != "" {
configRefreshInterval, err = strconv.ParseInt(configRefreshIntervalString, 0, 64)
if err != nil {
log.Panicf("Unable to parse config refresh interval %v", err)
}
}
ticker := time.NewTicker(time.Duration(configRefreshInterval) * time.Minute)
for {
select {
case <-ticker.C:
getConfig()
}
}
}
}
func NewFileSource(filePath string) *FileSource {
return &FileSource{
FilePath: filePath,
}
}
type FileSource struct {
FilePath string
}
func (s *FileSource) JSONWebKeySet() (*jose.JSONWebKeySet, error) {
dat, err := ioutil.ReadFile(s.FilePath)
if err != nil {
return nil, err
}
jsonWebKeySet := new(jose.JSONWebKeySet)
if err = json.NewDecoder(bytes.NewReader(dat)).Decode(jsonWebKeySet); err != nil {
return nil, err
}
return jsonWebKeySet, err
}