-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
300 lines (264 loc) · 6.85 KB
/
main.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"time"
vault "github.com/hashicorp/vault/api"
)
var (
vaddr = flag.String("vault", "", "Vault address.")
vauth = flag.String("auth-strategy", "token", "Vault auth strategy.")
pkibackend = flag.String("pki-backend", "", "Vault PKI backend path.")
pkirole = flag.String("pki-role", "kubelet", "Vault PKI role.")
certdir = flag.String("cert-dir", "", "Certificate storage directory.")
hostname = flag.String("hostname", "", "Hostname - empty triggers hostname discovery.")
ipsan = flag.String("ip-san", "", "IP SAN - empty triggers ip discovery.")
localhost = flag.Bool("localhost", false, "Include localhost in IP SANs.")
authdir = flag.String("auth-dir", "", "Auth info (token, nonce) storage directory.")
)
type VaultAuthStrategy string
const (
AuthToken VaultAuthStrategy = "token"
AuthTokenFile VaultAuthStrategy = "token-file"
AuthAWSEC2 VaultAuthStrategy = "aws-ec2"
)
func main() {
flag.Parse()
log.Printf("kubelet-vault-registrar starting.")
vclient, err := newVaultClient(*vaddr)
if err != nil {
panic(err.Error())
}
if *certdir == "" {
panic("cert-dir is required")
}
if err := os.MkdirAll(*certdir, 0777); err != nil {
panic(err.Error())
}
if *authdir != "" {
if err := os.MkdirAll(*authdir, 0777); err != nil {
panic(err.Error())
}
}
if *hostname == "" {
name, err := os.Hostname()
if err != nil {
panic(err.Error())
}
hostname = &name
}
if *ipsan == "" {
ip, err := externalIP()
if err != nil {
panic(err.Error())
}
ipsan = &ip
}
if *localhost {
ip := *ipsan + ",localhost"
ipsan = &ip
}
info, err := getAuthInfo(*authdir)
if err != nil {
panic(err.Error())
}
if err := authenticate(vclient, VaultAuthStrategy(*vauth), info); err != nil {
panic(err.Error())
}
if err := writeAuthInfo(*authdir, info); err != nil {
panic(err.Error())
}
go func() {
path := fmt.Sprintf("%s/issue/%s", *pkibackend, *pkirole)
err := maintainCerts(vclient, path, *certdir, *hostname, *ipsan)
if err != nil {
log.Fatal(err.Error())
}
}()
<-make(chan struct{})
}
func getAuthInfo(dir string) (map[string]string, error) {
meta := map[string]string{}
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return filepath.SkipDir
}
content, err := ioutil.ReadFile(dir)
if err != nil {
return err
}
meta[info.Name()] = string(content)
return nil
})
return meta, err
}
func writeAuthInfo(dir string, info map[string]string) error {
for key, val := range info {
err := ioutil.WriteFile(filepath.Join(dir, key), []byte(val), 0600)
if err != nil {
return err
}
}
return nil
}
func authenticate(client *vault.Client, strategy VaultAuthStrategy, info map[string]string) error {
switch strategy {
case AuthToken:
if client.Token() == "" {
return errors.New("VAULT_TOKEN not set")
}
return nil
case AuthTokenFile:
if token, ok := info["token"]; ok && token != "" {
client.SetToken(token)
return nil
} else {
return errors.New("token file not found")
}
case AuthAWSEC2:
identity, err := getAWSIdentityDocument()
if err != nil {
return err
}
pkcs := strings.Replace(string(identity), "\n", "", -1)
payload := map[string]interface{}{
"role": os.Getenv("VAULT_AUTH_ROLE"),
"pkcs7": pkcs,
}
nonce, ok := info["nonce"]
if ok {
payload["nonce"] = string(nonce)
}
resp, err := client.Logical().Write("auth/aws-ec2/login", payload)
if err != nil {
return err
}
info["token"] = resp.Auth.ClientToken
info["nonce"] = resp.Auth.Metadata["nonce"]
client.SetToken(resp.Auth.ClientToken)
go func() {
half := (time.Duration(resp.Auth.LeaseDuration) * time.Second) / 2
for {
log.Printf("scheduling token renew in %s.", half)
select {
case <-time.After(half):
secret, err := client.Auth().Token().RenewSelf(0)
if err != nil {
log.Printf("failed to renew token: %s", err)
half = half / 2
} else {
half = (time.Duration(secret.Auth.LeaseDuration) * time.Second) / 2
}
}
}
}()
return nil
default:
return fmt.Errorf("unsupported auth strategy: %s", strategy)
}
}
func newVaultClient(flag string) (*vault.Client, error) {
vconfig := vault.DefaultConfig()
err := vconfig.ReadEnvironment()
if err != nil {
panic(err.Error())
}
if *vaddr != "" {
vconfig.Address = *vaddr
}
return vault.NewClient(vconfig)
}
func maintainCerts(client *vault.Client, path string, dir string, hostname, ipsans string) error {
secret, err := getAndWriteCerts(client, path, dir, hostname, ipsans)
if err != nil {
return err
}
half := (time.Duration(secret.LeaseDuration) * time.Second) / 2
for {
log.Printf("scheduling cert update after %s", half)
select {
case <-time.After(half):
secret, err := getAndWriteCerts(client, path, dir, hostname, ipsans)
if err != nil {
return err
}
half = (time.Duration(secret.LeaseDuration) * time.Second) / 2
}
}
}
func getAndWriteCerts(client *vault.Client, path string, dir string, hostname, ipsans string) (*vault.Secret, error) {
log.Printf("using hostname %s, ip addr %s.", hostname, ipsans)
secret, err := client.Logical().Write(path, map[string]interface{}{
"common_name": hostname,
"ip_sans": ipsans,
})
if err != nil {
return nil, err
}
log.Printf("got secret with ttl %s.", time.Duration(secret.LeaseDuration)*time.Second)
cert := secret.Data["certificate"].(string)
if err := ioutil.WriteFile(filepath.Join(dir, "cert.pem"), []byte(cert), 0600); err != nil {
return nil, err
}
key := secret.Data["private_key"].(string)
if err := ioutil.WriteFile(filepath.Join(dir, "key.pem"), []byte(key), 0600); err != nil {
return nil, err
}
ca := secret.Data["ca_chain"].(string)
if err := ioutil.WriteFile(filepath.Join(dir, "ca.pem"), []byte(ca), 0600); err != nil {
return nil, err
}
return secret, nil
}
func getAWSIdentityDocument() ([]byte, error) {
resp, err := http.Get("http://169.254.169.254/latest/dynamic/instance-identity/pkcs7")
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
func externalIP() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String(), nil
}
}
return "", errors.New("are you connected to the network?")
}