-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
hashicorpvault_handler.go
178 lines (146 loc) · 4.15 KB
/
hashicorpvault_handler.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
/*
Copyright 2021 The KEDA Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package resolver
import (
"errors"
"fmt"
"io/ioutil"
"github.com/go-logr/logr"
vaultapi "github.com/hashicorp/vault/api"
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
)
// HashicorpVaultHandler is specification of Hashi Corp Vault
type HashicorpVaultHandler struct {
vault *kedav1alpha1.HashiCorpVault
client *vaultapi.Client
stopCh chan struct{}
}
// NewHashicorpVaultHandler creates a HashicorpVaultHandler object
func NewHashicorpVaultHandler(v *kedav1alpha1.HashiCorpVault) *HashicorpVaultHandler {
return &HashicorpVaultHandler{
vault: v,
}
}
// Initialize the Vault client
func (vh *HashicorpVaultHandler) Initialize(logger logr.Logger) error {
config := vaultapi.DefaultConfig()
client, err := vaultapi.NewClient(config)
if err != nil {
return err
}
err = client.SetAddress(vh.vault.Address)
if err != nil {
return err
}
if len(vh.vault.Namespace) > 0 {
client.SetNamespace(vh.vault.Namespace)
}
token, err := vh.token(client)
if err != nil {
return err
}
if len(token) > 0 {
client.SetToken(token)
}
lookup, err := client.Auth().Token().LookupSelf()
// If token is not valid so get out of here early
if err != nil {
return err
}
renew := lookup.Data["renewable"].(bool)
if renew {
vh.stopCh = make(chan struct{})
go vh.renewToken(logger)
}
vh.client = client
return nil
}
func (vh *HashicorpVaultHandler) token(client *vaultapi.Client) (string, error) {
var token string
switch vh.vault.Authentication {
case kedav1alpha1.VaultAuthenticationToken:
// Got token from VAULT_TOKEN env variable
switch {
case len(client.Token()) > 0:
break
case len(vh.vault.Credential.Token) > 0:
token = vh.vault.Credential.Token
default:
return token, errors.New("could not get Vault token")
}
case kedav1alpha1.VaultAuthenticationKubernetes:
if len(vh.vault.Mount) == 0 {
return token, errors.New("auth mount not in config")
}
if len(vh.vault.Role) == 0 {
return token, errors.New("k8s role not in config")
}
if len(vh.vault.Credential.ServiceAccount) == 0 {
return token, errors.New("k8s SA file not in config")
}
// Get the JWT from POD
jwt, err := ioutil.ReadFile(vh.vault.Credential.ServiceAccount)
if err != nil {
return token, err
}
data := map[string]interface{}{"jwt": string(jwt), "role": vh.vault.Role}
secret, err := client.Logical().Write(fmt.Sprintf("auth/%s/login", vh.vault.Mount), data)
if err != nil {
return token, err
}
token = secret.Auth.ClientToken
default:
return token, fmt.Errorf("vault auth method %s is not supported", vh.vault.Authentication)
}
return token, nil
}
func (vh *HashicorpVaultHandler) renewToken(logger logr.Logger) {
secret, err := vh.client.Auth().Token().RenewSelf(0)
if err != nil {
logger.Error(err, "Vault renew token: failed to create the payload")
}
renewer, err := vh.client.NewLifetimeWatcher(&vaultapi.RenewerInput{
Secret: secret,
//Grace: time.Duration(15 * time.Second),
//Increment: 60,
})
if err != nil {
logger.Error(err, "Vault renew token: cannot create the renewer")
}
go renewer.Renew()
defer func() {
renewer.Stop()
close(vh.stopCh)
}()
RenewWatcherLoop:
for {
select {
case <-vh.stopCh:
break RenewWatcherLoop
case err := <-renewer.DoneCh():
if err != nil {
logger.Error(err, "error renewing token")
}
break RenewWatcherLoop
}
}
}
func (vh *HashicorpVaultHandler) Read(path string) (*vaultapi.Secret, error) {
return vh.client.Logical().Read(path)
}
// Stop is responsible for stoping the renew token process
func (vh *HashicorpVaultHandler) Stop() {
if vh.stopCh != nil {
vh.stopCh <- struct{}{}
}
}