Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for buildkite token from ssm #76

Merged
merged 3 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions backend/ssm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package backend

import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"os"
"sync"
)

var ssmClient *ssm.SSM
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think it's worth caching this client between lambda invocations?

var once sync.Once

func GetSsmClient() *ssm.SSM {
once.Do(func() {
ssmClient = ssm.New(session.Must(session.NewSession()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps move this into RetrieveFromParameterStore and use an if ssmClient != nil check vs sync.once.

})
return ssmClient
}

func RetrieveFromParameterStore(ssmClient *ssm.SSM, key string) string {
output, err := ssmClient.GetParameter(&ssm.GetParameterInput{
Name: &key,
WithDecryption: aws.Bool(true),
})

if err != nil {
fmt.Printf("Error retrieving SSM (%s): %v", key, err)
os.Exit(1)
}

return *output.Parameter.Value
}
7 changes: 7 additions & 0 deletions lambda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,25 @@ func main() {

func Handler(ctx context.Context, evt json.RawMessage) (string, error) {
token := os.Getenv("BUILDKITE_AGENT_TOKEN")
useSsmString := os.Getenv("BUILDKITE_TOKEN_IN_SSM")
backendOpt := os.Getenv("BUILDKITE_BACKEND")
queue := os.Getenv("BUILDKITE_QUEUE")
clwDimensions := os.Getenv("BUILDKITE_CLOUDWATCH_DIMENSIONS")
quietString := os.Getenv("BUILDKITE_QUIET")
quiet := quietString == "1" || quietString == "true"
useSsm := useSsmString == "true"

if quiet {
log.SetOutput(ioutil.Discard)
}

t := time.Now()

if useSsm {
ssmClient := backend.GetSsmClient()
token = backend.RetrieveFromParameterStore(ssmClient, "buildkite_agent_token")
}

userAgent := fmt.Sprintf("buildkite-agent-metrics/%s buildkite-agent-metrics-lambda", version.Version)

c := collector.Collector{
Expand Down