forked from sspinc/terraform-provider-credstash
-
Notifications
You must be signed in to change notification settings - Fork 7
/
datasource_secrets.go
77 lines (66 loc) · 1.72 KB
/
datasource_secrets.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
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/sspinc/terraform-provider-credstash/credstash"
)
func dataSourceSecret() *schema.Resource {
return &schema.Resource{
Read: dataSourceSecretRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "name of the secret",
},
"version": {
Type: schema.TypeString,
Optional: true,
Description: "version of the secrets",
Default: "",
},
"table": {
Type: schema.TypeString,
Optional: true,
Description: "name of DynamoDB table where the secrets are stored",
Default: "",
},
"context": {
Type: schema.TypeMap,
Optional: true,
Description: "encryption context for the secret",
},
"value": {
Type: schema.TypeString,
Computed: true,
Description: "value of the secret",
Sensitive: true,
},
},
}
}
func dataSourceSecretRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*credstash.Client)
name := d.Get("name").(string)
version := d.Get("version").(string)
table := d.Get("table").(string)
context := make(map[string]string)
for k, v := range d.Get("context").(map[string]interface{}) {
context[k] = fmt.Sprintf("%v", v)
}
log.Printf("[DEBUG] Getting secret for name=%q table=%q version=%q context=%+v", name, table, version, context)
value, err := client.GetSecret(name, table, version, context)
if err != nil {
return err
}
d.Set("value", value)
d.SetId(hash(value))
return nil
}
func hash(s string) string {
sha := sha256.Sum256([]byte(s))
return hex.EncodeToString(sha[:])
}