-
Notifications
You must be signed in to change notification settings - Fork 19
/
vault.go
172 lines (141 loc) · 3.22 KB
/
vault.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package testutils
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"io/ioutil"
"time"
"bytes"
"io"
"regexp"
"strings"
"github.com/onsi/ginkgo"
"github.com/rotisserie/eris"
)
const defaultVaultDockerImage = "vault:0.9.2"
type VaultFactory struct {
vaultpath string
tmpdir string
}
func NewVaultFactory() (*VaultFactory, error) {
envoypath := os.Getenv("VAULT_BINARY")
if envoypath != "" {
return &VaultFactory{
vaultpath: envoypath,
}, nil
}
// try to grab one form docker...
tmpdir, err := ioutil.TempDir(os.Getenv("HELPER_TMP"), "vault")
if err != nil {
return nil, err
}
bash := fmt.Sprintf(`
set -ex
CID=$(docker run -d %s /bin/sh -c exit)
# just print the image sha for repoducibility
echo "Using Vault Image:"
docker inspect %s -f "{{.RepoDigests}}"
docker cp $CID:/bin/vault .
docker rm -f $CID
`, defaultVaultDockerImage, defaultVaultDockerImage)
scriptfile := filepath.Join(tmpdir, "getvault.sh")
ioutil.WriteFile(scriptfile, []byte(bash), 0755)
cmd := exec.Command("bash", scriptfile)
cmd.Dir = tmpdir
cmd.Stdout = ginkgo.GinkgoWriter
cmd.Stderr = ginkgo.GinkgoWriter
if err := cmd.Run(); err != nil {
return nil, err
}
return &VaultFactory{
vaultpath: filepath.Join(tmpdir, "vault"),
tmpdir: tmpdir,
}, nil
}
func (ef *VaultFactory) Clean() error {
if ef == nil {
return nil
}
if ef.tmpdir != "" {
os.RemoveAll(ef.tmpdir)
}
return nil
}
type VaultInstance struct {
vaultpath string
tmpdir string
cmd *exec.Cmd
token string
}
func (ef *VaultFactory) NewVaultInstance() (*VaultInstance, error) {
// try to grab one form docker...
tmpdir, err := ioutil.TempDir(os.Getenv("HELPER_TMP"), "vault")
if err != nil {
return nil, err
}
return &VaultInstance{
vaultpath: ef.vaultpath,
tmpdir: tmpdir,
}, nil
}
func (i *VaultInstance) Run() error {
return i.RunWithPort()
}
func (i *VaultInstance) Token() string {
return i.token
}
func (i *VaultInstance) RunWithPort() error {
cmd := exec.Command(i.vaultpath,
"server",
"-dev",
"-dev-root-token-id=root",
"-dev-listen-address=0.0.0.0:8200",
)
buf := &bytes.Buffer{}
w := io.MultiWriter(ginkgo.GinkgoWriter, buf)
cmd.Dir = i.tmpdir
cmd.Stdout = w
cmd.Stderr = w
err := cmd.Start()
if err != nil {
return err
}
time.Sleep(time.Millisecond * 1500)
i.cmd = cmd
tokenSlice := regexp.MustCompile("Root Token: ([\\-[:word:]]+)").FindAllString(buf.String(), 1)
if len(tokenSlice) < 1 {
return eris.Errorf("%s did not contain root token", buf.String())
}
i.token = strings.TrimPrefix(tokenSlice[0], "Root Token: ")
return nil
}
func (i *VaultInstance) Binary() string {
return i.vaultpath
}
func (i *VaultInstance) Clean() error {
if i.cmd != nil {
i.cmd.Process.Kill()
i.cmd.Wait()
}
if i.tmpdir != "" {
os.RemoveAll(i.tmpdir)
}
return nil
}
func (i *VaultInstance) Exec(args ...string) (string, error) {
cmd := exec.Command(i.vaultpath, args...)
cmd.Env = os.Environ()
// disable DEBUG=1 from getting through to nomad
for i, pair := range cmd.Env {
if strings.HasPrefix(pair, "DEBUG") {
cmd.Env = append(cmd.Env[:i], cmd.Env[i+1:]...)
break
}
}
out, err := cmd.CombinedOutput()
if err != nil {
err = fmt.Errorf("%s (%v)", out, err)
}
return string(out), err
}