-
Notifications
You must be signed in to change notification settings - Fork 444
/
vault.go
211 lines (172 loc) · 4.28 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package services
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/onsi/gomega/gexec"
"github.com/solo-io/go-utils/log"
"io/ioutil"
"time"
"regexp"
"strings"
"github.com/onsi/ginkgo"
errors "github.com/rotisserie/eris"
)
const defaultVaultDockerImage = "vault:1.1.3"
const defaultAddress = "127.0.0.1:8200"
type VaultFactory struct {
vaultPath string
tmpdir string
}
type VaultFactoryConfig struct {
PathPrefix string
}
func NewVaultFactory() (*VaultFactory, error) {
path := os.Getenv("VAULT_BINARY")
if path != "" {
return &VaultFactory{
vaultPath: path,
}, nil
}
vaultPath, err := exec.LookPath("vault")
if err == nil {
log.Printf("Using vault from PATH: %s", vaultPath)
return &VaultFactory{
vaultPath: vaultPath,
}, 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
session *gexec.Session
token string
address 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.RunWithAddress(defaultAddress)
}
func (i *VaultInstance) RunWithAddress(address string) error {
i.token = "root"
i.address = address
cmd := exec.Command(i.vaultpath,
"server",
// https://www.vaultproject.io/docs/concepts/dev-server
"-dev",
fmt.Sprintf("-dev-root-token-id=%s", i.token),
fmt.Sprintf("-dev-listen-address=%s", i.address),
)
cmd.Dir = i.tmpdir
cmd.Stdout = ginkgo.GinkgoWriter
cmd.Stderr = ginkgo.GinkgoWriter
session, err := gexec.Start(cmd, ginkgo.GinkgoWriter, ginkgo.GinkgoWriter)
if err != nil {
return err
}
time.Sleep(time.Millisecond * 1500)
i.cmd = cmd
i.session = session
out := string(session.Out.Contents())
tokenSlice := regexp.MustCompile("Root Token: ([\\-[:word:]]+)").FindAllString(out, 1)
if len(tokenSlice) < 1 {
return errors.Errorf("%s did not contain root token", out)
}
i.address = address
i.token = strings.TrimPrefix(tokenSlice[0], "Root Token: ")
return nil
}
func (i *VaultInstance) Token() string {
return i.token
}
func (i *VaultInstance) Address() string {
return fmt.Sprintf("http://%s", i.address)
}
func (i *VaultInstance) EnableSecretEngine(secretEngine string) error {
_, err := i.Exec("secrets", "enable", "-version=2", fmt.Sprintf("-path=%s", secretEngine), "kv")
return err
}
func (i *VaultInstance) Binary() string {
return i.vaultpath
}
func (i *VaultInstance) Clean() error {
if i.session != nil {
i.session.Kill()
}
if i.cmd != nil && i.cmd.Process != nil {
i.cmd.Process.Kill()
}
if i.tmpdir != "" {
return os.RemoveAll(i.tmpdir)
}
return nil
}
func (i *VaultInstance) Exec(args ...string) (string, error) {
cmd := exec.Command(i.vaultpath, args...)
cmd.Dir = i.tmpdir
cmd.Env = os.Environ()
// disable DEBUG=1 from getting through to nomad
for e, pair := range cmd.Env {
if strings.HasPrefix(pair, "DEBUG") {
cmd.Env = append(cmd.Env[:e], cmd.Env[e+1:]...)
break
}
}
cmd.Env = append(
cmd.Env,
fmt.Sprintf("VAULT_TOKEN=%s", i.Token()),
fmt.Sprintf("VAULT_ADDR=%s", i.Address()))
out, err := cmd.CombinedOutput()
if err != nil {
err = fmt.Errorf("%s (%v)", out, err)
}
return string(out), err
}