secretmanagerenv

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2023 License: MIT Imports: 8 Imported by: 0

README

gcp-secretmanagerenv

Detect variable from environment variable or GCP Secret Manager

You can access Secret Manager with a syntax similar to os.Getenv

Latest Version Build Status Coverage Status Maintainability GoDoc Go Report Card

Requirements

Add IAM role roles/secretmanager.secretAccessor to service account if necessary.

Usage

package main

import (
    "context"
    "github.com/sue445/gcp-secretmanagerenv"
)

func main() {
    projectID := "gcp-project-id"
    c, err := secretmanagerenv.NewClient(context.Background(), projectID)
    if err != nil {
        panic(err)
    }

    // get from environment variable
    value, err := c.GetValueFromEnvOrSecretManager("SOME_KEY", true)
    // => return value from environment variable or Secret Manager

    // When key is not found in both environment variable and Secret Manager, returned empty string (not error)
    value, err := c.GetValueFromEnvOrSecretManager("INVALID_KEY", false)
    // => ""

    // When key is not found in both environment variable and Secret Manager, returned error
    value, err := c.GetValueFromEnvOrSecretManager("INVALID_KEY", true)
    // => error
}
Specification

When c.GetValueFromEnvOrSecretManager(key, required) is called, processing is performed in the following order

  1. Returns environment variable if key is found
  2. Returns latest version value of Secret Manager if projectID isn't empty and key is found
  3. Returns "" if required == false
  4. Returns error if required == true

Development

Setup

requires https://github.com/direnv/direnv

cp .envrc.example
vi .envrc
direnv allow

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client represents secretmanagerenv client

func NewClient

func NewClient(ctx context.Context, projectID string) (*Client, error)

NewClient creates a new Client instance

func (*Client) GetSecretManagerValue

func (c *Client) GetSecretManagerValue(key string, version string) (string, error)

GetSecretManagerValue returns value from SecretManager

func (*Client) GetValueFromEnvOrSecretManager

func (c *Client) GetValueFromEnvOrSecretManager(key string, required bool) (string, error)

GetValueFromEnvOrSecretManager returns value from environment variable or Secret Manager

Directories

Path Synopsis
Package mock_secretmanagerenv is a generated GoMock package.
Package mock_secretmanagerenv is a generated GoMock package.

Jump to

Keyboard shortcuts

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