forked from camptocamp/terraform-provider-pass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_pass_password.go
78 lines (61 loc) · 1.82 KB
/
resource_pass_password.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
package pass
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"github.com/hashicorp/terraform/helper/schema"
)
func passPasswordResource() *schema.Resource {
return &schema.Resource{
Create: passPasswordResourceWrite,
Update: passPasswordResourceWrite,
Delete: passPasswordResourceDelete,
Read: passPasswordResourceRead,
Schema: map[string]*schema.Schema{
"path": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Full path where the pass password will be written.",
},
// Data is passed as JSON so that an arbitrary structure is
// possible, rather than forcing e.g. all values to be strings.
"data": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "JSON-encoded secret data to write.",
},
},
}
}
func passPasswordResourceWrite(d *schema.ResourceData, meta interface{}) error {
path := d.Get("path").(string)
log.Printf("[DEBUG] Writing pass Password secret to %s", path)
subProcess := exec.Command("pass", "insert", "-e", path)
stdin, err := subProcess.StdinPipe()
if err != nil {
return fmt.Errorf("Fail to acquire stdin : %v", err)
}
defer stdin.Close()
subProcess.Stdout = os.Stdout
subProcess.Stderr = os.Stderr
if err = subProcess.Start(); err != nil {
return fmt.Errorf("Fail to run command : %v", err)
}
io.WriteString(stdin, fmt.Sprintf("%s\n", d.Get("data")))
subProcess.Wait()
d.SetId(path)
return nil
}
func passPasswordResourceDelete(d *schema.ResourceData, meta interface{}) error {
path := d.Id()
log.Printf("[DEBUG] Deleting generic Vault from %s", path)
exec.Command("pass", "rm", path)
return nil
}
func passPasswordResourceRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[WARN] pass_password does not automatically refresh")
return nil
}