-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore.go
45 lines (35 loc) · 1.09 KB
/
restore.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
package main
import (
"context"
"fmt"
"os"
"github.com/hashicorp/vault/api"
)
type VaultKvBackup struct {
Secrets map[string]map[string]interface{} `json:"secrets"`
}
func convertJSONToVaultKvBackup(JSONData []byte) (*VaultKvBackup, error) {
vaultKvBackup, err := fromJSON(JSONData)
if err != nil {
return nil, err
}
return vaultKvBackup, nil
}
func restoreVaultKvSecrets(client *api.Client, kvMountPath string, vaultKvBackup *VaultKvBackup, quietProgress bool) error {
kvV2Client := client.KVv2(kvMountPath)
for secretsPath, secrets := range vaultKvBackup.Secrets {
if quietProgress {
fmt.Fprintf(os.Stdout, ".")
} else {
fmt.Fprintf(os.Stdout, "\nrestoring secrets to `%s` secrets path in vault\n", secretsPath)
}
restoredKvSecret, err := kvV2Client.Put(context.TODO(), secretsPath, secrets)
if err != nil {
return fmt.Errorf("error occurred while putting/writing the secrets at path `%s` in vault: %v", secretsPath, err)
}
if restoredKvSecret == nil {
return fmt.Errorf("no secret at path `%s` in vault after write operation", secretsPath)
}
}
return nil
}