Skip to content

Commit 9ca7a07

Browse files
author
Chris Every
authored
Merge pull request #154 from ovotech/add-gcf-entrypoint
Add gcf entrypoint
2 parents 7414a56 + 2283f64 commit 9ca7a07

File tree

9 files changed

+210
-242
lines changed

9 files changed

+210
-242
lines changed

.circleci/config.yml

+20-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,22 @@ jobs:
1515
- run:
1616
name: goreleaser
1717
command: |
18-
export GITHUB_TOKEN=$GITHUB
1918
export GO111MODULE=on
2019
go mod download
2120
curl -sL https://git.io/goreleaser | bash
21+
release_cloudfunction_zip:
22+
docker:
23+
- image: cibuilds/github
24+
steps:
25+
- checkout
26+
27+
- run:
28+
name: "Publish Release on GitHub"
29+
command: |
30+
VERSION=$(echo ${CIRCLE_TAG} | sed 's/^v//')
31+
ZIP_NAME=cloud-key-rotator_${VERSION}_cloudfunction
32+
zip -R ${ZIP_NAME} '*.go' 'go.mod'
33+
ghr -t ${GITHUB_TOKEN} -u ${CIRCLE_PROJECT_USERNAME} -r ${CIRCLE_PROJECT_REPONAME} ${CIRCLE_TAG} ${ZIP_NAME}.zip
2234
docker_build_and_push:
2335
<<: *defaults
2436

@@ -129,14 +141,20 @@ jobs:
129141
130142
workflows:
131143
version: 2
132-
goreleaser_pipeline:
144+
release_pipeline:
133145
jobs:
134146
- goreleaser:
135147
filters:
136148
tags:
137149
only: /v[0-9]+(\.[0-9]+)*(-.*)*/
138150
branches:
139151
ignore: /.*/
152+
- release_cloudfunction_zip:
153+
filters:
154+
tags:
155+
only: /v[0-9]+(\.[0-9]+)*(-.*)*/
156+
branches:
157+
ignore: /.*/
140158
- docker_build_and_push:
141159
filters:
142160
tags:

.goreleaser.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ archives:
1212
name_template: "{{ .ProjectName }}_{{ .Version }}_lambda"
1313
builds:
1414
- id: binary-build
15-
main: ./
15+
binary: cloud-key-rotator
16+
main: ./cmd/
1617
goos:
1718
- windows
1819
- darwin
1920
- linux
2021
goarch:
2122
- amd64
2223
- id: lambda-build
23-
main: ./
24+
binary: cloud-key-rotator
25+
main: ./cmd/
2426
goos:
2527
- linux
2628
goarch:

cloudfunction.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package cloudfunction
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"os"
7+
8+
"github.com/ovotech/cloud-key-rotator/pkg/config"
9+
"github.com/ovotech/cloud-key-rotator/pkg/log"
10+
"github.com/ovotech/cloud-key-rotator/pkg/rotate"
11+
)
12+
13+
var logger = log.StdoutLogger().Sugar()
14+
15+
// Request is the CloudFunction entrypoint
16+
func Request(w http.ResponseWriter, r *http.Request) {
17+
var c config.Config
18+
var err error
19+
var bucketName string
20+
var ok bool
21+
bucketEnvVarName := "CKR_BUCKET_NAME"
22+
if bucketName, ok = os.LookupEnv(bucketEnvVarName); !ok {
23+
logCloudFunctionError(w, fmt.Errorf("Env var: %s is required", bucketEnvVarName))
24+
return
25+
}
26+
if c, err = config.GetConfigFromGCS(
27+
bucketName,
28+
getEnv("CKR_SECRET_CONFIG_NAME", "ckr-config.json"),
29+
getEnv("CKR_CONFIG_TYPE", "json")); err != nil {
30+
logCloudFunctionError(w, err)
31+
return
32+
}
33+
if err = rotate.Rotate("", "", "", c); err != nil {
34+
logCloudFunctionError(w, err)
35+
return
36+
}
37+
}
38+
39+
func logCloudFunctionError(w http.ResponseWriter, err error) {
40+
w.WriteHeader(http.StatusInternalServerError)
41+
w.Write([]byte(err.Error()))
42+
logger.Error(err)
43+
}
44+
45+
//getEnv returns the value of the env var matching the key, if it exists, and
46+
// the value of fallback otherwise
47+
func getEnv(key, fallback string) string {
48+
if value, ok := os.LookupEnv(key); ok {
49+
return value
50+
}
51+
return fallback
52+
}

cmd/root.go cmd/cobra/root.go

File renamed without changes.

cmd/rotate.go cmd/cobra/rotate.go

File renamed without changes.

main.go cmd/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import (
1919
"os"
2020

2121
"github.com/aws/aws-lambda-go/lambda"
22-
"github.com/ovotech/cloud-key-rotator/cmd"
22+
cmd "github.com/ovotech/cloud-key-rotator/cmd/cobra"
2323
"github.com/ovotech/cloud-key-rotator/pkg/config"
24+
"github.com/ovotech/cloud-key-rotator/pkg/log"
2425
"github.com/ovotech/cloud-key-rotator/pkg/rotate"
2526
)
2627

@@ -29,6 +30,8 @@ type MyEvent struct {
2930
Name string `json:"name"`
3031
}
3132

33+
var logger = log.StdoutLogger().Sugar()
34+
3235
//HandleRequest allows cloud-key-rotator to be used in the Lambda program model
3336
func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
3437
var c config.Config

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.12
44

55
require (
66
cloud.google.com/go v0.46.3
7+
cloud.google.com/go/storage v1.0.0
78
github.com/aws/aws-lambda-go v1.13.2
89
github.com/aws/aws-sdk-go v1.25.0
910
github.com/beamly/go-gocd v0.0.0-20190719193049-383d56afbf92

go.sum

+100-235
Large diffs are not rendered by default.

pkg/config/config.go

+29-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ package config
1616

1717
import (
1818
"bytes"
19+
"context"
1920
"errors"
21+
"io/ioutil"
2022

23+
"cloud.google.com/go/storage"
2124
"github.com/aws/aws-sdk-go/aws"
2225
"github.com/aws/aws-sdk-go/aws/session"
2326
"github.com/aws/aws-sdk-go/service/secretsmanager"
@@ -100,8 +103,8 @@ func GetConfig(configPath string) (c Config, err error) {
100103
return
101104
}
102105

103-
// GetConfigFromAWSSecretManager grabs the cloud-key-rotator's config from
104-
// AWS Secret Manager
106+
//GetConfigFromAWSSecretManager grabs the cloud-key-rotator's config from
107+
//AWS Secret Manager
105108
func GetConfigFromAWSSecretManager(secretName, configType string) (c Config, err error) {
106109
var secret string
107110
if secret, err = GetSecret(secretName); err != nil {
@@ -116,6 +119,30 @@ func GetConfigFromAWSSecretManager(secretName, configType string) (c Config, err
116119
return
117120
}
118121

122+
//GetConfigFromGCS grabs the cloud-key-rotator's config from GCS
123+
func GetConfigFromGCS(bucketName, objectName, configType string) (c Config, err error) {
124+
ctx := context.Background()
125+
var client *storage.Client
126+
if client, err = storage.NewClient(ctx); err != nil {
127+
return
128+
}
129+
bkt := client.Bucket(bucketName)
130+
obj := bkt.Object(objectName)
131+
var rc *storage.Reader
132+
if rc, err = obj.NewReader(ctx); err != nil {
133+
return
134+
}
135+
defer rc.Close()
136+
var data []byte
137+
if data, err = ioutil.ReadAll(rc); err != nil {
138+
return
139+
}
140+
viper.SetConfigType(configType)
141+
viper.ReadConfig(bytes.NewReader(data))
142+
err = viper.Unmarshal(&c)
143+
return
144+
}
145+
119146
//GetSecret gets the value of the secret in AWS SecretsManager with the specified name
120147
func GetSecret(secretName string) (secretString string, err error) {
121148
//Create a Secrets Manager client

0 commit comments

Comments
 (0)