Golang package to parse secret values from secret providers to struct fields.
This package uses runtimevar
package from go-cloud library
to get secret values from supported stores (see docs).
Annotate a field with secretstruct
tag to fetch a variable from supported secret providers.
secretstruct
tag can contain either a URL of a format used by gocloud.dev/runtimevar
package
(see runtimevar docs) or self
which denotes that this field value
contains the URL.
package main
import (
"context"
"fmt"
"github.com/THE108/secretstruct"
// Use blank imports to init providers supported by `github.com/google/go-cloud/runtimevar`.
_ "gocloud.dev/runtimevar/awsparamstore"
_ "gocloud.dev/runtimevar/awssecretsmanager"
_ "gocloud.dev/runtimevar/constantvar"
_ "gocloud.dev/runtimevar/gcpsecretmanager"
)
type TestStruct struct {
// This field will be fetched from AWS Secrets Manager (see https://aws.amazon.com/en/secrets-manager/).
FieldAWSSecretsManager string `secretstruct:"awssecretsmanager://test-string-value-from-aws-secrets-manager"`
// This field will be fetched from GCP Secret Manager (see https://cloud.google.com/secret-manager).
FieldGCPSecretManager string `secretstruct:"gcpsecretmanager://test-string-value-from-gcp-secret-manager"`
// This field will be fetched using the URL from the current FieldAWSParamStore field value
// (see struct init below).
FieldAWSParamStore string `secretstruct:"self"`
}
func main() {
ctx := context.Background()
testStruct := TestStruct{
// This field will be fetched from AWS Param Store.
FieldAWSParamStore: "awsparamstore://test-string-value-from-aws-param-store",
}
// Call Process to fetch all string values marked with `secretstruct` tag.
if err := secretstruct.Process(ctx, &testStruct); err != nil {
fmt.Println(err)
return
}
fmt.Printf("testStruct: %+v\n", testStruct)
}
Embedded and internal structs are also supported:
type EmbeddedStruct struct {
EmbeddedField string `secretstruct:"awssecretsmanager://test-string-value-from-aws-secrets-manager"`
}
type TestStruct struct {
EmbeddedStruct
InnerStruct struct {
FieldAWSSecretsManager string `secretstruct:"awssecretsmanager://test-string-value-from-aws-secrets-manager"`
}
FieldGCPSecretManager string `secretstruct:"gcpsecretmanager://test-string-value-from-gcp-secret-manager"`
}
To ignore a field use -
tag value:
type TestStruct struct {
IgnoredField string `secretstruct:"-"`
}
MIT