How do I store and retrieve secrets securely in Go?

Storing and retrieving secrets securely in Go is essential for maintaining the integrity and confidentiality of sensitive information, such as API keys, passwords, and other credentials. Various methods exist for managing secrets, including using environment variables, configuration files with encryption, and secret management systems like HashiCorp Vault or AWS Secrets Manager.

Using Environment Variables

One of the simplest ways to manage secrets is through environment variables. This method keeps sensitive information out of your source code.

// Reading an environment variable in Go package main import ( "fmt" "os" ) func main() { secret := os.Getenv("MY_SECRET_KEY") if secret == "" { fmt.Println("No secret found!") } else { fmt.Println("Secret retrieved successfully!") } }

Using a Configuration File

Another approach is to store secrets in a configuration file. This file can be encrypted to enhance security.

// Using a JSON config file package main import ( "encoding/json" "fmt" "io/ioutil" "log" ) type Config struct { SecretKey string `json:"secret_key"` } func main() { data, err := ioutil.ReadFile("config.json") if err != nil { log.Fatal(err) } var config Config err = json.Unmarshal(data, &config) if err != nil { log.Fatal(err) } fmt.Printf("Secret Key: %s\n", config.SecretKey) }

Using Secret Management Systems

For a production environment, consider using a dedicated secrets management system.

// Example using HashiCorp Vault (pseudo code) package main import ( "fmt" "github.com/hashicorp/vault/api" ) func main() { client, err := api.NewClient(api.DefaultConfig()) if err != nil { panic(err) } secret, err := client.Logical().Read("secret/myapp") if err != nil { panic(err) } fmt.Println("Retrieved secret:", secret.Data["value"]) }

Go Secrets Management Environment Variables Configuration Management HashiCorp Vault AWS Secrets Manager