Skip to content

Commit 2811285

Browse files
author
Chris Every
authored
Merge pull request #140 from ovotech/add-lambda-gcp-key-provision
Add lambda gcp key provision
2 parents cc7ce94 + 21dcb2a commit 2811285

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

main.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,13 @@ func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
4646
}
4747

4848
func main() {
49-
if isLambda() {
49+
if rotate.InLambda() {
5050
lambda.Start(HandleRequest)
5151
} else {
5252
cmd.Execute()
5353
}
5454
}
5555

56-
//isLambda returns true if the AWS_LAMBDA_FUNCTION_NAME env var is set
57-
func isLambda() (isLambda bool) {
58-
return len(os.Getenv("AWS_LAMBDA_FUNCTION_NAME")) > 0
59-
}
60-
6156
//getEnv returns the value of the env var matching the key, if it exists, and
6257
// the value of fallback otherwise
6358
func getEnv(key, fallback string) string {

pkg/config/config.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func GetConfig(configPath string) (c Config, err error) {
103103
// AWS Secret Manager
104104
func GetConfigFromAWSSecretManager(secretName, configType string) (c Config, err error) {
105105
var secret string
106-
if secret, err = getSecret(secretName); err != nil {
106+
if secret, err = GetSecret(secretName); err != nil {
107107
return
108108
}
109109
if len(secret) == 0 {
@@ -115,7 +115,8 @@ func GetConfigFromAWSSecretManager(secretName, configType string) (c Config, err
115115
return
116116
}
117117

118-
func getSecret(secretName string) (secretString string, err error) {
118+
//GetSecret gets the value of the secret in AWS SecretsManager with the specified name
119+
func GetSecret(secretName string) (secretString string, err error) {
119120
//Create a Secrets Manager client
120121
svc := secretsmanager.New(session.New())
121122
input := &secretsmanager.GetSecretValueInput{

pkg/rotate/rotatekeys.go

+40-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import (
1818
"bytes"
1919
"errors"
2020
"fmt"
21+
"io/ioutil"
2122
"net/http"
23+
"os"
2224
"regexp"
2325
"strconv"
2426
"strings"
@@ -39,7 +41,10 @@ type rotationCandidate struct {
3941
rotationThresholdMins int
4042
}
4143

42-
var logger = log.StdoutLogger().Sugar()
44+
var (
45+
logger = log.StdoutLogger().Sugar()
46+
provisionedGoogleAppCreds = false
47+
)
4348

4449
const (
4550
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) {
117122
func rotateKey(rotationCandidate rotationCandidate, creds cred.Credentials) (err error) {
118123
key := rotationCandidate.key
119124
keyProvider := key.Provider.Provider
125+
if keyProvider == "gcp" {
126+
ensureGoogleAppCreds()
127+
}
120128
var newKeyID string
121129
var newKey string
122130
if newKeyID, newKey, err = createKey(key, keyProvider); err != nil {
@@ -234,17 +242,43 @@ func accountKeyLocation(account string,
234242
return
235243
}
236244

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+
237268
//locationsToUpdate return a slice of structs that implement the keyWriter
238269
// interface, based on the keyLocations supplied
239270
func locationsToUpdate(keyLocation config.KeyLocations) (kws []location.KeyWriter) {
240271

272+
var googleAppCredsRequired bool
273+
241274
// read locations
242275
for _, circleCI := range keyLocation.CircleCI {
243276
kws = append(kws, circleCI)
244277
}
245278

246279
for _, gcs := range keyLocation.GCS {
247280
kws = append(kws, gcs)
281+
googleAppCredsRequired = true
248282
}
249283

250284
if len(keyLocation.GitHub.OrgRepo) > 0 {
@@ -257,6 +291,11 @@ func locationsToUpdate(keyLocation config.KeyLocations) (kws []location.KeyWrite
257291

258292
for _, k8s := range keyLocation.K8s {
259293
kws = append(kws, k8s)
294+
googleAppCredsRequired = true
295+
}
296+
297+
if googleAppCredsRequired {
298+
ensureGoogleAppCreds()
260299
}
261300

262301
return

0 commit comments

Comments
 (0)