Sobes.tech
Junior — Middle

How have you worked with key-value storage or key management systems in your projects?

sobes.tech AI

Answer from AI

In Go projects for working with key data stores or key management systems, I have used several approaches. For example, for secure storage of secrets and keys, HashiCorp Vault is often used, integrating it via HTTP API.

Example of using Vault to retrieve a secret:

package main

import (
    "fmt"
    "log"
    "github.com/hashicorp/vault/api"
)

func main() {
    config := api.DefaultConfig()
    client, err := api.NewClient(config)
    if err != nil {
        log.Fatal(err)
    }

    client.SetToken("your-vault-token")

    secret, err := client.Logical().Read("secret/data/myapp/config")
    if err != nil {
        log.Fatal(err)
    }

    if secret == nil || secret.Data == nil {
        log.Fatal("No data found")
    }

    data := secret.Data["data"].(map[string]interface{})
    apiKey := data["api_key"].(string)

    fmt.Println("API Key:", apiKey)
}

In some cases, I also used the built-in capabilities of cloud providers (AWS KMS, Google Cloud KMS) for encryption and key management, calling their SDKs from Go.

It is important to follow security principles when working with keys: do not store keys in code, use environment variables or secure storage, restrict access, and regularly rotate keys.