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

Support for multiple secrets manager secrets #233

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ supported.
#### Option 3 - Retrieve token from AWS Secrets Manager

- `BUILDKITE_AGENT_SECRETS_MANAGER_SECRET_ID`: The id of the secret which
contains the token value in AWS Secrets Manager.
contains the token value in AWS Secrets Manager. You can supply
multiple secrets comma-separated.
Copy link
Contributor

Choose a reason for hiding this comment

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

Give you're making this change to SSM as well, can we get it documented there too pls?

- (Optional) `BUILDKITE_AGENT_SECRETS_MANAGER_JSON_KEY`: The JSON key containing
the token value in the secret JSON blob.

Expand Down
49 changes: 38 additions & 11 deletions lambda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,19 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) {
return "", nil
}

provider, err := initTokenProvider(awsRegion)
providers, err := initTokenProvider(awsRegion)
if err != nil {
return "", err
}

bkToken, err := provider.Get()
if err != nil {
return "", err
tokens := make([]string, 0)
for _, provider := range providers {
bkToken, err := provider.Get()
if err != nil {
return "", err
}
tokens = append(tokens, bkToken)
}
tokens := strings.Split(bkToken, ",")

queues := []string{}
if queue != "" {
Expand Down Expand Up @@ -173,7 +176,7 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) {
return "", nil
}

func initTokenProvider(awsRegion string) (token.Provider, error) {
func initTokenProvider(awsRegion string) ([]token.Provider, error) {
mutuallyExclusiveEnvVars := []string{
BKAgentTokenEnvVar,
BKAgentTokenSSMKeyEnvVar,
Expand All @@ -184,8 +187,16 @@ func initTokenProvider(awsRegion string) (token.Provider, error) {
return nil, err
}

if bkToken := os.Getenv(BKAgentTokenEnvVar); bkToken != "" {
return token.NewInMemory(bkToken)
providers := make([]token.Provider, 0)
if bkTokenEnvVar := os.Getenv(BKAgentTokenEnvVar); bkTokenEnvVar != "" {
bkTokens := strings.Split(bkTokenEnvVar, ",")
for _, bkToken := range bkTokens {
provider, err := token.NewInMemory(bkToken)
if err != nil {
return nil, err
}
providers = append(providers, provider)
}
}

if ssmKey := os.Getenv(BKAgentTokenSSMKeyEnvVar); ssmKey != "" {
Expand All @@ -194,7 +205,11 @@ func initTokenProvider(awsRegion string) (token.Provider, error) {
return nil, err
}
client := ssm.New(sess)
return token.NewSSM(client, ssmKey)
provider, err := token.NewSSM(client, ssmKey)
if err != nil {
return nil, err
}
providers = append(providers, provider)
}

if secretsManagerSecretID := os.Getenv(BKAgentTokenSecretsManagerSecretIDEnvVar); secretsManagerSecretID != "" {
Expand All @@ -205,10 +220,22 @@ func initTokenProvider(awsRegion string) (token.Provider, error) {
}
client := secretsmanager.New(sess)
if jsonKey == "" {
return token.NewSecretsManager(client, secretsManagerSecretID)
secretIDs := strings.Split(secretsManagerSecretID, ",")
for _, secretID := range secretIDs {
secretManager, err := token.NewSecretsManager(client, secretID)
if err != nil {
return nil, err
}
providers = append(providers, secretManager)
}
} else {
return token.NewSecretsManager(client, secretsManagerSecretID, token.WithSecretsManagerJSONSecret(jsonKey))
secretManager, err := token.NewSecretsManager(client, secretsManagerSecretID, token.WithSecretsManagerJSONSecret(jsonKey))
if err != nil {
return nil, err
}
providers = append(providers, secretManager)
}
return providers, nil
}

return nil, fmt.Errorf("failed to initialize Buildkite token provider: one of the [%s] environment variables "+
Expand Down