varsource

package
v0.0.0-...-9758eb9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

README

varsource

A package for sourcing variable values from multiple upstreams.

Currently supported upstreams include:

  • env: an environment variable value
  • file: the contents of a file
  • aws:ssm: an AWS Systems Manager Parameter Store parameter value
  • aws:secretsmanager: an AWS Secrets Manager secret value
Usage
vs := varsource.NewDefaultVariableSource()

vars, err := vs.GetVariables(ctx, map[string]string{
	"DB_USERNAME": "from:env:DB_USERNAME",
	"DB_PASSWORD": "from:aws:secretsmanager:my-password",
}
if err != nil {
	log.Fatalf("failed to fetch variables: %v", err)
}

username := vars["DB_USERNAME"]
password := vars["DB_PASSWORD"]

// ... do something useful ...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MultipleUpstreamVariableSource

type MultipleUpstreamVariableSource struct {
	// contains filtered or unexported fields
}

MultipleUpstreamVariableSource is a VariableSource with multiple possible upstreams

func NewMultipleUpstreamVariableSource

func NewMultipleUpstreamVariableSource(opts ...Option) *MultipleUpstreamVariableSource

NewMultipleUpstreamVariableSource is the MultipleUpstreamVariableSource constructor. It returns a newly-initialized MultipleUpstreamVariableSource with all the given upstream sources set.

func (*MultipleUpstreamVariableSource) GetVariable

func (vs *MultipleUpstreamVariableSource) GetVariable(ctx context.Context, varDefn string) (string, error)

GetVariable takes a single variable definition and returns the variable's value i.e. fetches the variable's value based on the variable definition.

Note that variables that begin with one of the prefixes can have fetching from upstream escaped using backslash ('\') e.g.: - from:aws:ssm:/my/path --> agdkylfuiaoeifas (value in ssm was fetched) - \from:aws:ssm:/my/path --> from:aws:ssm:/my/path - \\from:aws:ssm:/my/path --> \from:aws:ssm:/my/path

func (*MultipleUpstreamVariableSource) GetVariables

func (vs *MultipleUpstreamVariableSource) GetVariables(ctx context.Context, vars map[string]string) (map[string]string, error)

GetVariables takes a map of variable names to variable definitions and returns a map of variable name to variable values. i.e. returns fetches the variable values based on the variable definitions and returns the same map populated with values instead of definitions.

type Option

type Option func(muvs *MultipleUpstreamVariableSource)

Option represents a constructor option to set configuration settings (e.g. an entry in the upstreams map) for a new MultipleUpstreamVariableSource

func WithAWSSSMVariableUpstream

func WithAWSSSMVariableUpstream() Option

WithAWSSSMVariableUpstream is the Option to set the aws ssm parameter store upstream source in a new MultipleUpstreamVariableSource

func WithAWSSecretsManagerVariableUpstream

func WithAWSSecretsManagerVariableUpstream() Option

WithAWSSecretsManagerVariableUpstream is the Option to set the aws secrets manager upstream source in a new MultipleUpstreamVariableSource

func WithEnvVariableUpstream

func WithEnvVariableUpstream() Option

WithEnvVariableUpstream is the Option to set the environment variable upstream source in a new MultipleUpstreamVariableSource

func WithFileVariableUpstream

func WithFileVariableUpstream() Option

WithFileVariableUpstream is the Option to set the file contents source in a new MultipleUpstreamVariableSource

func WithTopLevelPrefix

func WithTopLevelPrefix(prefix string) Option

WithTopLevelPrefix is the Option to set the top level prefix string.

type VariableSource

type VariableSource interface {
	// GetVariables takes a map of variable-name to variable-definition
	// and returns a map of variable-name to variable-value. i.e. returns
	// the same map except that the variable definitions are replaced with
	// the actual variable values.
	//
	// For example:
	// {
	//   "DB_USERNAME": "from:env:DB_USERNAME",
	//   "DB_PASSWORD": "from:file:~/.creds/password.txt",
	// }
	// would be translated to:
	// {
	//   "DB_USERNAME": "database-user-xyz",
	//   "DB_PASSWORD": "df29^%qd3gs8&*&(asd8t\tqe=",
	// }
	GetVariables(ctx context.Context, vars map[string]string) (map[string]string, error)
	GetVariable(ctx context.Context, varDefn string) (string, error)
}

VariableSource represents the necessary functionality of a source of variables.

func NewDefaultVariableSource

func NewDefaultVariableSource() VariableSource

NewDefaultVariableSource returns the default VariableSource implementation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL