Created
June 27, 2019 23:11
-
-
Save xlyk/f2f2246ee259415c05f84eb21218ac73 to your computer and use it in GitHub Desktop.
golang and AWS secrets manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/secretsmanager" | |
) | |
type SecretData struct { | |
MongoUser string `json:"mongo_user"` | |
MongoPass string `json:"mongo_pass"` | |
} | |
var ( | |
secretName string = "name-goes-here" | |
region string = "us-west-2" | |
versionStage string = "AWSCURRENT" | |
) | |
func GetSecret() SecretData { | |
svc := secretsmanager.New( | |
session.New(), | |
aws.NewConfig().WithRegion(region), | |
) | |
input := &secretsmanager.GetSecretValueInput{ | |
SecretId: aws.String(secretName), | |
VersionStage: aws.String(versionStage), | |
} | |
result, err := svc.GetSecretValue(input) | |
if err != nil { | |
panic(err.Error()) | |
} | |
var secretString string | |
if result.SecretString != nil { | |
secretString = *result.SecretString | |
} | |
var secretData SecretData | |
err = json.Unmarshal([]byte(secretString), &secretData) | |
if err != nil { | |
panic(err.Error()) | |
} | |
return secretData | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment