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

Adding support for retrieving specific secret versions #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/cyberark/summon-aws-secrets
require (
github.com/aws/aws-sdk-go v1.34.20
github.com/smartystreets/goconvey v1.6.4
github.com/stretchr/testify v1.5.1 // indirect
)

go 1.13
48 changes: 36 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,41 @@ package main

import (
"encoding/json"
"fmt"
"os"
"strings"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/secretsmanager"
)

func ExtractSecretPathAndArguments(input string) (secretPath string, keyName string, secretVersion string) {
keyName = ""
secretVersion = ""
secretPath = ""

if strings.ContainsRune(input, 35) && strings.ContainsRune(input, 94) {
fields := strings.FieldsFunc(input, func(r rune) bool {
return r == '#' || r == '^'
})
return fields[0], fields[1], fields[2]

} else if strings.ContainsRune(input, 35) {
fields := strings.SplitN(input, "#", 2)
return fields[0], fields[1], ""
} else if strings.ContainsRune(input, 94) {
fields := strings.SplitN(input, "^", 2)
return fields[0], "", fields[1]
}

return input, "", ""
}

func RetrieveSecret(variableName string) {
// By default get secrets that contain the version AWSCURRENT
secretVersion := "AWSCURRENT"
// All clients require a Session. The Session provides the client with
// shared configuration such as region, endpoint, and credentials. A
// Session should be shared where possible to take advantage of
Expand Down Expand Up @@ -53,20 +77,20 @@ func RetrieveSecret(variableName string) {

svc := secretsmanager.New(sess)

// Check if key has been specified
arguments := strings.SplitN(variableName, "#", 2)
//Check if arguments (key, version) have been specified
secretName, keyName, secretVersion := ExtractSecretPathAndArguments(variableName)

secretName := arguments[0]
var keyName string
secretValueInput := &secretsmanager.GetSecretValueInput{
SecretId: aws.String(secretName),
}

if len(arguments) > 1 {
keyName = arguments[1]
// If a secret version has been specified
if len(secretVersion) > 0 {
secretValueInput.VersionStage = aws.String(secretVersion)
}

// Get secret value
req, resp := svc.GetSecretValueRequest(&secretsmanager.GetSecretValueInput{
SecretId: aws.String(secretName),
})
req, resp := svc.GetSecretValueRequest(secretValueInput)

err = req.Send()
if err != nil { // resp is now filled
Expand All @@ -79,8 +103,8 @@ func RetrieveSecret(variableName string) {
} else {
secretBytes = resp.SecretBinary
}

if keyName != "" {
// If key has been specified for retrieval
if len(keyName) > 0 {
secretBytes, err = getValueByKey(keyName, secretBytes)
if err != nil {
printAndExit(err)
Expand Down