-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathexample.go
57 lines (43 loc) · 1.37 KB
/
example.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package main
import (
"context"
"fmt"
"log"
vault "github.com/hashicorp/vault/api"
)
// This is the accompanying code for the Developer Quick Start.
// WARNING: Using root tokens is insecure and should never be done in production!
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
client.SetToken("dev-only-token")
secretData := map[string]interface{}{
"password": "Hashi123",
}
// Write a secret
_, err = client.KVv2("secret").Put(context.Background(), "my-secret-password", secretData)
if err != nil {
log.Fatalf("unable to write secret: %v", err)
}
fmt.Println("Secret written successfully.")
// Read a secret from the default mount path for KV v2 in dev mode, "secret"
secret, err := client.KVv2("secret").Get(context.Background(), "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)
}
fmt.Println("Access granted!")
}