-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmain.go
58 lines (43 loc) · 1.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package main
import (
"context"
"log"
vault "github.com/hashicorp/vault/api"
)
func main() {
config := vault.DefaultConfig()
config.Address = "http://127.0.0.1:8200"
client, err := vault.NewClient(config)
if err != nil {
log.Fatalf("unable to initialize Vault client: %v", err)
}
// Authenticate
// WARNING: This quickstart uses the root token for our Vault dev server.
// Don't do this in production!
client.SetToken("dev-only-token")
secretData := map[string]interface{}{
"password": "Hashi123",
}
ctx := context.Background()
// Write a secret
_, err = client.KVv2("secret").Put(ctx, "my-secret-password", secretData)
if err != nil {
log.Fatalf("unable to write secret: %v", err)
}
log.Println("Secret written successfully.")
// Read a secret
secret, err := client.KVv2("secret").Get(ctx, "my-secret-password")
if err != nil {
log.Fatalf("unable to read secret: %v", err)
}
value, ok := secret.Data["password"].(string)
if !ok {
log.Fatalf("value type assertion failed: %T %#v", secret.Data["password"], secret.Data["password"])
}
if value != "Hashi123" {
log.Fatalf("unexpected password value %q retrieved from vault", value)
}
log.Println("Access granted!")
}