secrets

package
v1.0.43 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2024 License: MIT Imports: 5 Imported by: 0

README

ca-go/secrets

The secrets package wraps the AWS SecretManager in a simple to use sington pattern that you can call directly.

Environment Variables

You MUST set these:

  • AWS_REGION = The AWS region this code is running in (eg. "us-west-1")

FAQ

Question: I need to load secrets from another region? How do I do that? Answer: You can create your own secrets with sm := NewAWSSecrets("region") and then call sm.Get("secret")

Examples

package cago

import (
	"fmt"

	"github.com/cultureamp/ca-go/secrets"
)

func BasicExamples() {
	ctx := context.Background()

	// this will automatically use the AWS Region as per the environment variable "AWS_REGION"
	answer, err := secrets.Get(ctx, "my-test-secret")
	fmt.Printf("The answer to the secret is '%s' (err='%v')\n", answer, err)

	// or if you need secrets from another region other than the one you are running in use
	sm, err := secrets.NewAWSSecretsManager(ctx, "a-different-region")
	answer, err = sm.Get(ctx, "my-test-secret2")
	fmt.Printf("The answer to the secret2 is '%s' (err='%v')\n", answer, err)

	// of if you want to have a custom client that requires a different region
	cfg, _ := config.LoadDefaultConfig(ctx, config.WithRegion("us-west-2"))
	smc := secretsmanager.NewFromConfig(cfg)
	sm = secrets.NewAWSSecretsManagerWithClient(smc)

	// or if you want to be able to mock the behavior
	mockSM := newTestRunner()
	oldSM := secrets.DefaultAWSSecretsManager
	defer func() { secrets.DefaultAWSSecretsManager = oldSM }()
	secrets.DefaultAWSSecretsManager = mockSM
}

type testRunner struct{}

func newTestRunner() *testRunner {
	return &testRunner{}
}

// Get on the test runner returns the key as the secret.
func (c *testRunner) Get(_ context.Context, key string) (string, error) {
	// do whatever you want here
	return key, nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(ctx context.Context, secretKey string) (string, error)

Get retrives the secret from AWS SecretsManager.

Types

type AWSSecretsManager added in v0.0.16

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

AWSSecretsManager supports wraps the Secrets interface.

func NewAWSSecretsManager added in v0.0.16

func NewAWSSecretsManager(ctx context.Context, region string) (*AWSSecretsManager, error)

NewAWSSecretsManager creates a new AWS Secret Manager for a given region.

func NewAWSSecretsManagerWithClient added in v0.0.16

func NewAWSSecretsManagerWithClient(client *secretsmanager.Client) *AWSSecretsManager

NewAWSSecretsManagerWithClient creates a new AWS Secret Manager with a custom client that supports the AWSSecretsManagerClient interface.

func (*AWSSecretsManager) Get added in v0.0.16

func (sm *AWSSecretsManager) Get(ctx context.Context, secretKey string) (string, error)

Get retrieves the secret from AWS SecretsManager.

type Secrets added in v1.0.17

type Secrets interface {
	Get(ctx context.Context, secretKey string) (string, error)
}

Secrets can be mocked by clients for testing purposes.

var DefaultAWSSecretsManager Secrets = nil //nolint:revive

DefaultAWSSecretsManager is a public *AWSSecretsManager used for package level methods.

Jump to

Keyboard shortcuts

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