This content originally appeared on DEV Community and was authored by Ankit malik
Hi, This is my first post on this portal.
What is Vault:
Vault is a tool by which you can securely access you credentials. It is developed by Hashicorp. It is similar to AWS Parameter store. It helps in managing credentials effectively.
Sample Code for CRUD Operations in Vault:
package main
import (
"fmt"
"net/http"
"time"
"github.com/hashicorp/vault/api"
)
var httpClient = &http.Client{
Timeout: 10 * time.Second,
}
func main() {
token := "your token"
vaultAddr := "your url"
client, err := api.NewClient(&api.Config{Address: vaultAddr, HttpClient: httpClient})
if err != nil {
panic(err)
}
client.SetToken(token)
//writing the data
inputData := map[string]interface{}{
"data": map[string]interface{}{
"first": "ankit",
},
}
output, err := client.Logical().Write("secret/data/abd", inputData)
fmt.Println(output)
if err != nil {
panic(err)
}
//deleting the data
data, err := client.Logical().Read("secret/data/hello")
if err != nil {
panic(err)
}
fmt.Println(data.Data)
//deleting the data
output, err = client.Logical().Delete("secret/metadata/abd")
fmt.Println(output)
if err != nil {
panic(err)
}
}
Things to Focus here is code
If we look at in this code then it is very easy to miss the write operation. We need to check inputData
and how it is structured because according to golang object type it seems that we should use in this way rather than of how we used it.
inputData := map[string]interface{}{
"first": "ankit",
}
Where should I use Vault?
- It should be used when we are initialising the project.
- All the configurations should be read from Vault or any other secret manager.
- There should be no configurations saved on server.
This content originally appeared on DEV Community and was authored by Ankit malik

Ankit malik | Sciencx (2021-09-01T13:36:06+00:00) Working with Vault and Golang. Retrieved from https://www.scien.cx/2021/09/01/working-with-vault-and-golang/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.