@@ -18,7 +18,9 @@ import (
18
18
"bytes"
19
19
"errors"
20
20
"fmt"
21
+ "io/ioutil"
21
22
"net/http"
23
+ "os"
22
24
"regexp"
23
25
"strconv"
24
26
"strings"
@@ -39,7 +41,10 @@ type rotationCandidate struct {
39
41
rotationThresholdMins int
40
42
}
41
43
42
- var logger = log .StdoutLogger ().Sugar ()
44
+ var (
45
+ logger = log .StdoutLogger ().Sugar ()
46
+ provisionedGoogleAppCreds = false
47
+ )
43
48
44
49
const (
45
50
datadogURL = "https://api.datadoghq.com/api/v1/series?api_key="
@@ -117,6 +122,9 @@ func Rotate(account, provider, project string, c config.Config) (err error) {
117
122
func rotateKey (rotationCandidate rotationCandidate , creds cred.Credentials ) (err error ) {
118
123
key := rotationCandidate .key
119
124
keyProvider := key .Provider .Provider
125
+ if keyProvider == "gcp" {
126
+ ensureGoogleAppCreds ()
127
+ }
120
128
var newKeyID string
121
129
var newKey string
122
130
if newKeyID , newKey , err = createKey (key , keyProvider ); err != nil {
@@ -234,17 +242,43 @@ func accountKeyLocation(account string,
234
242
return
235
243
}
236
244
245
+ //InLambda returns true if the AWS_LAMBDA_FUNCTION_NAME env var is set
246
+ func InLambda () (isLambda bool ) {
247
+ return len (os .Getenv ("AWS_LAMBDA_FUNCTION_NAME" )) > 0
248
+ }
249
+
250
+ //ensureGoogleAppCreds helps to provision a GCP service account key when running in a Lambda.
251
+ //The key could be used for various purposes, e.g. rotating a service account's key, writing
252
+ //a new key to GCS, or writing a new key to a Secret in GKE.
253
+ func ensureGoogleAppCreds () (err error ) {
254
+ if InLambda () && ! provisionedGoogleAppCreds {
255
+ var secretValue string
256
+ if secretValue , err = config .GetSecret ("ckr-gcp-key" ); err != nil {
257
+ return
258
+ }
259
+ keyFilePath := "/tmp/key.json"
260
+ if err = ioutil .WriteFile (keyFilePath , []byte (secretValue ), 0644 ); err == nil {
261
+ os .Setenv ("GOOGLE_APPLICATION_CREDENTIALS" , keyFilePath )
262
+ provisionedGoogleAppCreds = true
263
+ }
264
+ }
265
+ return
266
+ }
267
+
237
268
//locationsToUpdate return a slice of structs that implement the keyWriter
238
269
// interface, based on the keyLocations supplied
239
270
func locationsToUpdate (keyLocation config.KeyLocations ) (kws []location.KeyWriter ) {
240
271
272
+ var googleAppCredsRequired bool
273
+
241
274
// read locations
242
275
for _ , circleCI := range keyLocation .CircleCI {
243
276
kws = append (kws , circleCI )
244
277
}
245
278
246
279
for _ , gcs := range keyLocation .GCS {
247
280
kws = append (kws , gcs )
281
+ googleAppCredsRequired = true
248
282
}
249
283
250
284
if len (keyLocation .GitHub .OrgRepo ) > 0 {
@@ -257,6 +291,11 @@ func locationsToUpdate(keyLocation config.KeyLocations) (kws []location.KeyWrite
257
291
258
292
for _ , k8s := range keyLocation .K8s {
259
293
kws = append (kws , k8s )
294
+ googleAppCredsRequired = true
295
+ }
296
+
297
+ if googleAppCredsRequired {
298
+ ensureGoogleAppCreds ()
260
299
}
261
300
262
301
return
0 commit comments