forked from sofixa/nomad_follower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaultTokenBackend.go
135 lines (119 loc) · 3.66 KB
/
vaultTokenBackend.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
package main
import (
"time"
nomadApi "github.com/hashicorp/nomad/api"
vaultApi "github.com/hashicorp/vault/api"
)
// NomadConfig is a common interface to use a Nomad client + optionally renew tokens.
type NomadConfig interface {
Client() *nomadApi.Client
RenewToken()
}
// NomadEnvAuth uses the standard $NOMAD_TOKEN to auth to Nomad, if ACLs are enabled.
type NomadEnvAuth struct {
client *nomadApi.Client
config *nomadApi.Config
log Logger
}
// Client returns a pointer to the internal Nomad client struct.
func (n *NomadEnvAuth) Client() *nomadApi.Client {
return n.client
}
// RenewToken is a no-operation call to meet the NomadConfig interface.
func (n *NomadEnvAuth) RenewToken() {
// nothing to do $NOMAD_TOKEN is used or cluster does not use ACLs
}
// NewNomadEnvAuth returns a new standard-auth config relying on $NOMAD_TOKEN.
func NewNomadEnvAuth(nomadConfig *nomadApi.Config, logger Logger) *NomadEnvAuth {
var n = NomadEnvAuth{}
var err error
if nomadConfig == nil {
n.config = nomadApi.DefaultConfig()
} else {
n.config = nomadConfig
}
n.client, err = nomadApi.NewClient(n.config)
if err != nil {
logger.Error("NewNomadEnvAuth", "Failed to create Nomad client.")
return nil
}
n.log = logger
return &n
}
// NomadRenewableAuth uses a Vault Nomad backend to auth to Nomad and can renew tokens.
type NomadRenewableAuth struct {
client *nomadApi.Client
config *nomadApi.Config
log Logger
vaultClient *vaultApi.Client
vaultConfig *vaultApi.Config
nomadTokenBackend string
circuitBreak time.Duration
lastRenewTime time.Time
}
// Client returns a pointer to the internal Nomad client struct.
func (n *NomadRenewableAuth) Client() *nomadApi.Client {
return n.client
}
// RenewToken uses a Vault Nomad secrets backend to create a new Nomad token.
func (n *NomadRenewableAuth) RenewToken() {
logContext := "NomadRenewableAuth.RenewToken"
// check if circuit breaker tripped on token renewal
t := time.Now()
past := t.Sub(n.lastRenewTime)
if past < n.circuitBreak {
n.log.Trace(logContext, "Circuit breaker already tripped, skipping request")
return
}
vaultReader := n.vaultClient.Logical()
resp, err := vaultReader.Read(n.nomadTokenBackend)
if err != nil {
n.log.Errorf(logContext, "Error reading Nomad token backend: %s", err)
return
}
if resp == nil || resp.Data == nil || len(resp.Data) == 0 {
n.log.Error(logContext, "Error no secret data at path")
return
}
// pull out nomad token
token, ok := resp.Data["secret_id"].(string)
if !ok {
n.log.Error(logContext, "Nomad token not string in Vault response")
return
}
// set in client
n.log.Debug(logContext, "Refreshed Nomad token from Vault")
n.client.SetSecretID(token)
n.lastRenewTime = time.Now()
}
// NewNomadRenewableAuth returns a config reliant on a Vault Nomad backend to provide auth tokens.
func NewNomadRenewableAuth(nomadConfig *nomadApi.Config, vaultConfig *vaultApi.Config, tokenBackend string, circuitBreak time.Duration, logger Logger) *NomadRenewableAuth {
var n = NomadRenewableAuth{}
var err error
logContext := "NewNomadRenewableAuth"
if nomadConfig == nil {
n.config = nomadApi.DefaultConfig()
} else {
n.config = nomadConfig
}
if vaultConfig == nil {
n.vaultConfig = vaultApi.DefaultConfig()
n.vaultConfig.ReadEnvironment()
} else {
n.vaultConfig = vaultConfig
}
n.vaultClient, err = vaultApi.NewClient(n.vaultConfig)
if err != nil {
logger.Error(logContext, "Failed to create Vault client")
return nil
}
n.client, err = nomadApi.NewClient(n.config)
if err != nil {
logger.Error(logContext, "Failed to create Nomad client")
return nil
}
n.nomadTokenBackend = tokenBackend
n.circuitBreak = circuitBreak
n.log = logger
return &n
}