secretstruct

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2021 License: MIT Imports: 7 Imported by: 0

README

Build Status Go Reference Coverage

secretstruct

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).

Usage

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:"-"`
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidInputParamType indicates that an input param is of the wrong type.
	ErrInvalidInputParamType = errors.New("input param must be a struct pointer")

	// ErrUnsupportedFieldType indicates that a field annotated with `secretstruct` tag is of the wrong type.
	// Could be a string or *string.
	ErrUnsupportedFieldType = errors.New("unsupported field type")

	// ErrInvalidLatestValue indicates that a call to `gocloud.dev/runtimevar#Variable.Latest`
	// method returned nil.
	ErrInvalidLatestValue = errors.New("latest value is nil")

	// ErrTypeMismatch indicates that a call to `gocloud.dev/runtimevar#Variable.Latest`
	// method returned something different from []byte or string.
	ErrTypeMismatch = errors.New("type mismatch")
)

Functions

func Process

func Process(ctx context.Context, in interface{}) error

Process populates the specified struct based on struct field tags.

Types

This section is empty.

Jump to

Keyboard shortcuts

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