-
Notifications
You must be signed in to change notification settings - Fork 3
/
datasource_secret.go
87 lines (76 loc) · 2.12 KB
/
datasource_secret.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/DelineaXPM/dsv-sdk-go/v2/vault"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceSecretRead(d *schema.ResourceData, meta interface{}) error {
path := d.Get("path").(string)
dsv, err := vault.New(meta.(vault.Configuration))
if err != nil {
log.Printf("[DEBUG] configuration error: %s", err)
return err
}
log.Printf("[DEBUG] getting secret %s", path)
secret, err := dsv.Secret(path)
if err != nil {
log.Printf("[DEBUG] unable to get secret: %s", err)
return err
}
d.SetId(secret.ID)
if element := d.Get("element"); element != "" {
// if element is defined, extract it from the secrets data map and return it
switch v := secret.Data[element.(string)].(type) {
case string:
log.Printf("[DEBUG] returning string %s from .data as the secret", v)
d.Set("contents", v)
case map[string]interface{}:
s, _ := json.Marshal(v)
log.Printf("[DEBUG] returning JSON %s from .data as the secret", s)
d.Set("contents", string(s[:]))
default:
return fmt.Errorf("element %s is unexpected data type %T", v, v)
}
} else {
// just marshal the whole thing back into JSON and return that
s, _ := json.Marshal(secret.Data)
log.Printf("[DEBUG] returning .data as the secret")
d.Set("contents", string(s[:]))
}
return nil
}
func dataSourceSecret() *schema.Resource {
return &schema.Resource{
Read: dataSourceSecretRead,
Schema: map[string]*schema.Schema{
"contents": {
Computed: true,
Description: "the contents of the secret",
Sensitive: true,
Type: schema.TypeString,
},
"element": {
Description: "the element to extract from the secret",
Optional: true,
Type: schema.TypeString,
},
"id": {
Computed: true,
Description: "the (UUID) identifier of the secret",
Type: schema.TypeString,
},
"path": {
Description: "the path of the secret",
Required: true,
Type: schema.TypeString,
},
"version": {
Computed: true,
Description: "the version of the secret",
Type: schema.TypeInt,
},
},
}
}