-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpath_config.go
251 lines (220 loc) · 7.87 KB
/
path_config.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package kubesecrets
import (
"context"
"errors"
"fmt"
"os"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const (
configPath = "config"
localCACertPath = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
localJWTPath = "/var/run/secrets/kubernetes.io/serviceaccount/token"
k8sServiceHostEnv = "KUBERNETES_SERVICE_HOST"
k8sServicePortEnv = "KUBERNETES_SERVICE_PORT_HTTPS"
)
// kubeConfig contains the public key certificate used to verify the signature
// on the service account JWTs
type kubeConfig struct {
// Host is the url string for the kubernetes API
Host string `json:"kubernetes_host"`
// CACert is the CA Cert to use to call into the kubernetes API
CACert string `json:"kubernetes_ca_cert"`
// ServiceAccountJwt is the bearer token to use when authenticating to the
// kubernetes API
ServiceAccountJwt string `json:"service_account_jwt"`
// DisableLocalJWT is an optional parameter to disable defaulting to using
// the local CA cert and service account jwt when running in a Kubernetes
// pod
DisableLocalCAJwt bool `json:"disable_local_ca_jwt"`
}
func (b *backend) pathConfig() *framework.Path {
return &framework.Path{
Pattern: configPath,
DisplayAttrs: &framework.DisplayAttributes{
OperationPrefix: operationPrefixKubernetes,
},
Fields: map[string]*framework.FieldSchema{
"disable_local_ca_jwt": {
Type: framework.TypeBool,
Description: "Disable defaulting to the local CA certificate and service account JWT when running in a Kubernetes pod.",
Default: false,
DisplayAttrs: &framework.DisplayAttributes{
Name: "Disable use of local CA and service account JWT",
},
},
"kubernetes_ca_cert": {
Type: framework.TypeString,
Description: "PEM encoded CA certificate to use to verify the Kubernetes API server certificate. Defaults to the local pod's CA if found.",
DisplayAttrs: &framework.DisplayAttributes{
Name: "Kubernetes CA Certificate",
},
},
"kubernetes_host": {
Type: framework.TypeString,
Description: "Kubernetes API URL to connect to. Defaults to https://$KUBERNETES_SERVICE_HOST:KUBERNETES_SERVICE_PORT if those environment variables are set.",
DisplayAttrs: &framework.DisplayAttributes{
Name: "Kubernetes API URL",
},
},
"service_account_jwt": {
Type: framework.TypeString,
Description: "The JSON web token of the service account used by the secret engine to manage Kubernetes credentials. Defaults to the local pod's JWT if found.",
DisplayAttrs: &framework.DisplayAttributes{
Name: "Kubernetes API JWT",
},
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathConfigWrite,
DisplayAttrs: &framework.DisplayAttributes{
OperationVerb: "configure",
},
},
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathConfigRead,
DisplayAttrs: &framework.DisplayAttributes{
OperationSuffix: "configuration",
},
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathConfigDelete,
DisplayAttrs: &framework.DisplayAttributes{
OperationSuffix: "configuration",
},
},
},
HelpSynopsis: "Configure the Kubernetes secret engine plugin.",
HelpDescription: "This path configures the Kubernetes secret engine plugin. See the documentation for the " +
"plugin specified for a full list of accepted connection details.",
}
}
// pathConfigWrite handles create and update commands to the config
func (b *backend) pathConfigRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
if config, err := getConfig(ctx, req.Storage); err != nil {
return nil, err
} else if config == nil {
return nil, nil
} else {
// Create a map of data to be returned. Note that these reflect just the
// values that the user set, not what the defaults will be if they
// aren't set (see configWithDynamicValues() for those defaults). And
// the service account jwt is omitted as sensitive data.
resp := &logical.Response{
Data: map[string]interface{}{
"disable_local_ca_jwt": config.DisableLocalCAJwt,
"kubernetes_ca_cert": config.CACert,
"kubernetes_host": config.Host,
},
}
return resp, nil
}
}
func (b *backend) pathConfigWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
config, err := getConfig(ctx, req.Storage)
if err != nil {
return nil, err
}
if config == nil {
config = &kubeConfig{}
}
if host, ok := data.GetOk("kubernetes_host"); ok {
config.Host = host.(string)
} else if _, err := getK8sURLFromEnv(); err != nil {
return nil, errors.New("kubernetes_host was unset and could not be determined from environment variables")
}
if disableLocalJWT, ok := data.GetOk("disable_local_ca_jwt"); ok {
config.DisableLocalCAJwt = disableLocalJWT.(bool)
}
if caCert, ok := data.GetOk("kubernetes_ca_cert"); ok {
config.CACert = caCert.(string)
}
if serviceAccountJWT, ok := data.GetOk("service_account_jwt"); ok {
config.ServiceAccountJwt = serviceAccountJWT.(string)
}
entry, err := logical.StorageEntryJSON(configPath, config)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
// reset the client so the next invocation will pick up the new configuration
b.reset()
return nil, nil
}
func (b *backend) pathConfigDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
err := req.Storage.Delete(ctx, configPath)
if err == nil {
b.reset()
}
return nil, err
}
// configWithDynamicValues fetches the kubeConfig from storage and sets any
// runtime defaults for host, local token, and local CA certificate.
func (b *backend) configWithDynamicValues(ctx context.Context, s logical.Storage) (*kubeConfig, error) {
config, err := getConfig(ctx, s)
if err != nil {
return nil, err
}
if config == nil {
return nil, errors.New("could not load backend configuration")
}
// If host is blank, default to reading from env
if config.Host == "" {
config.Host, err = getK8sURLFromEnv()
if err != nil {
return nil, errors.New("kubernetes_host was unset and could not determine it from environment variables")
}
}
// Nothing more to do if loading local CA cert and JWT token is disabled.
if config.DisableLocalCAJwt {
return config, nil
}
// Read local JWT token unless it was not stored in config.
if config.ServiceAccountJwt == "" {
jwtBytes, err := b.localSATokenReader.ReadFile()
if err != nil {
// Ignore error: make best effort trying to load local JWT,
// otherwise the JWT submitted in login payload will be used.
b.Logger().Debug("failed to read local service account token, will use client token", "error", err)
}
config.ServiceAccountJwt = string(jwtBytes)
}
// Read local CA cert unless it was stored in config.
if config.CACert == "" {
caBytes, err := b.localCACertReader.ReadFile()
if err != nil {
return nil, err
}
config.CACert = string(caBytes)
}
return config, nil
}
func getConfig(ctx context.Context, s logical.Storage) (*kubeConfig, error) {
entry, err := s.Get(ctx, configPath)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
config := new(kubeConfig)
if err := entry.DecodeJSON(&config); err != nil {
return nil, fmt.Errorf("error reading root configuration: %w", err)
}
// return the config, we are done
return config, nil
}
func getK8sURLFromEnv() (string, error) {
host := os.Getenv(k8sServiceHostEnv)
port := os.Getenv(k8sServicePortEnv)
if host == "" || port == "" {
return "", fmt.Errorf("failed to find k8s API host variables %q and %q in env", k8sServiceHostEnv, k8sServicePortEnv)
}
return fmt.Sprintf("https://%s:%s", host, port), nil
}