gsm

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

README

gsm

Zero-dependency Go client for Google Cloud Secret Manager.

Why another Secret Manager client? The official Google SDK pulls in 90+ dependencies. This library uses the Secret Manager REST API directly with zero external dependencies - just the Go standard library.

Installation

go get github.com/codeGROOVE-dev/gsm

Quick Start

import "github.com/codeGROOVE-dev/gsm"

// Fetch a secret (auto-detects project from metadata server)
value, err := gsm.Fetch(ctx, "my-secret")

// Store a secret (creates if missing, adds version if exists)
err = gsm.Store(ctx, "my-secret", "secret-value")

// Or specify project explicitly
value, err = gsm.FetchFromProject(ctx, "my-project", "my-secret")
err = gsm.StoreInProject(ctx, "my-project", "my-secret", "secret-value")

Features

  • Zero dependencies - Uses only Go standard library (no protobuf, no gRPC, no bloat)
  • Production-ready - Automatic retries (3 attempts, 1s delay), context cancellation, 10MB response limits
  • Auto-auth - Authenticates via GCP metadata server (Cloud Run, GCE, GKE)
  • Idempotent writes - Store() creates secrets if missing, adds versions if they exist
  • Structured logging - Uses log/slog for observability

Permissions

Reading Secrets

Grant roles/secretmanager.secretAccessor:

gcloud projects add-iam-policy-binding PROJECT_ID \
    --member="serviceAccount:SERVICE_ACCOUNT" \
    --role="roles/secretmanager.secretAccessor"
Writing Secrets

For principle of least privilege, grant these specific permissions:

  1. Create a custom role for secret creation:
gcloud iam roles create secretCreator --project=PROJECT_ID \
    --title="Secret Creator" \
    --description="Can create new secrets" \
    --permissions=secretmanager.secrets.create

gcloud projects add-iam-policy-binding PROJECT_ID \
    --member="serviceAccount:SERVICE_ACCOUNT" \
    --role="projects/PROJECT_ID/roles/secretCreator"
  1. Grant version management (add new secret versions):
gcloud projects add-iam-policy-binding PROJECT_ID \
    --member="serviceAccount:SERVICE_ACCOUNT" \
    --role="roles/secretmanager.secretVersionAdder"
  1. Grant read access (required to check if secret exists):
gcloud projects add-iam-policy-binding PROJECT_ID \
    --member="serviceAccount:SERVICE_ACCOUNT" \
    --role="roles/secretmanager.secretAccessor"

Alternatively, for full control (not recommended for production):

gcloud projects add-iam-policy-binding PROJECT_ID \
    --member="serviceAccount:SERVICE_ACCOUNT" \
    --role="roles/secretmanager.admin"

Environment

Designed for GCP environments with metadata server access:

  • Cloud Run
  • Google Compute Engine (GCE)
  • Google Kubernetes Engine (GKE)

Why This Exists

Most projects don't need 90+ dependencies just to read a secret. The official SDK is great if you're using lots of GCP services, but if you just need Secret Manager, this gives you the same functionality with zero deps and a much smaller binary.

License

MIT

Documentation

Overview

Package gsm provides access to Google Cloud Secret Manager via REST API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fetch

func Fetch(ctx context.Context, name string) (string, error)

Fetch retrieves the latest version of a secret from the current project. The project ID is auto-detected from the GCP metadata server.

func FetchFromProject

func FetchFromProject(ctx context.Context, pid, name string) (string, error)

FetchFromProject retrieves the latest version of a secret from a specific project.

func Store

func Store(ctx context.Context, name, value string) error

Store creates or updates a secret in the current project. The project ID is auto-detected from the GCP metadata server. If the secret doesn't exist, it will be created. If it exists, a new version will be added.

func StoreInProject

func StoreInProject(ctx context.Context, pid, name, value string) error

StoreInProject creates or updates a secret in a specific project. If the secret doesn't exist, it will be created. If it exists, a new version will be added.

Types

This section is empty.

Directories

Path Synopsis
Package main demonstrates using the gcp-secret library.
Package main demonstrates using the gcp-secret library.

Jump to

Keyboard shortcuts

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