-
Notifications
You must be signed in to change notification settings - Fork 61
/
path_config.go
295 lines (268 loc) · 9.96 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
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package kubeauth
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const (
localCACertPath = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
localJWTPath = "/var/run/secrets/kubernetes.io/serviceaccount/token"
)
// pathConfig returns the path configuration for CRUD operations on the backend
// configuration.
func pathConfig(b *kubeAuthBackend) *framework.Path {
return &framework.Path{
Pattern: "config$",
DisplayAttrs: &framework.DisplayAttributes{
OperationPrefix: operationPrefixKubernetes,
},
Fields: map[string]*framework.FieldSchema{
"kubernetes_host": {
Type: framework.TypeString,
Description: "Host must be a host string, a host:port pair, or a URL to the base of the Kubernetes API server.",
},
"kubernetes_ca_cert": {
Type: framework.TypeString,
Description: `Optional PEM encoded CA cert for use by the TLS client used to talk with the API.
If it is not set and disable_local_ca_jwt is true, the system's trusted CA certificate pool will be used.`,
DisplayAttrs: &framework.DisplayAttributes{
Name: "Kubernetes CA Certificate",
},
},
"token_reviewer_jwt": {
Type: framework.TypeString,
Description: `A service account JWT (or other token) used as a bearer token to access the
TokenReview API to validate other JWTs during login. If not set
the JWT used for login will be used to access the API.`,
DisplayAttrs: &framework.DisplayAttributes{
Name: "Token Reviewer JWT",
},
},
"pem_keys": {
Type: framework.TypeCommaStringSlice,
Description: `Optional list of PEM-formated public keys or certificates
used to verify the signatures of kubernetes service account
JWTs. If a certificate is given, its public key will be
extracted. Not every installation of Kubernetes exposes these keys.`,
DisplayAttrs: &framework.DisplayAttributes{
Name: "Service account verification keys",
},
},
"issuer": {
Type: framework.TypeString,
Deprecated: true,
Description: `Optional JWT issuer. If no issuer is specified,
then this plugin will use kubernetes.io/serviceaccount as the default issuer.
(Deprecated, will be removed in a future release)`,
DisplayAttrs: &framework.DisplayAttributes{
Name: "JWT Issuer",
},
},
"disable_iss_validation": {
Type: framework.TypeBool,
Deprecated: true,
Description: `Disable JWT issuer validation (Deprecated, will be removed in a future release)`,
Default: true,
DisplayAttrs: &framework.DisplayAttributes{
Name: "Disable JWT Issuer Validation",
},
},
"disable_local_ca_jwt": {
Type: framework.TypeBool,
Description: "Disable defaulting to the local CA cert 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",
},
},
"use_annotations_as_alias_metadata": {
Type: framework.TypeBool,
Description: `Use annotations from the client token's
associated service account as alias metadata for the Vault entity.
Only annotations with the prefix "vault.hashicorp.com/alias-metadata-"
will be used. Note that Vault will need permission to read service
accounts from the Kubernetes API.`,
Default: false,
DisplayAttrs: &framework.DisplayAttributes{
Name: "Use annotations of JWT service account as alias metadata",
},
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathConfigWrite,
DisplayAttrs: &framework.DisplayAttributes{
OperationVerb: "configure",
OperationSuffix: "auth",
},
},
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathConfigRead,
DisplayAttrs: &framework.DisplayAttributes{
OperationVerb: "read",
OperationSuffix: "auth-configuration",
},
},
},
HelpSynopsis: confHelpSyn,
HelpDescription: confHelpDesc,
}
}
// pathConfigWrite handles create and update commands to the config
func (b *kubeAuthBackend) pathConfigRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
if config, err := b.config(ctx, req.Storage); err != nil {
return nil, err
} else if config == nil {
return nil, nil
} else {
// Create a map of data to be returned
resp := &logical.Response{
Data: map[string]interface{}{
"kubernetes_host": config.Host,
"kubernetes_ca_cert": config.CACert,
"pem_keys": config.PEMKeys,
"issuer": config.Issuer,
"disable_iss_validation": config.DisableISSValidation,
"disable_local_ca_jwt": config.DisableLocalCAJwt,
"token_reviewer_jwt_set": config.TokenReviewerJWT != "",
"use_annotations_as_alias_metadata": config.UseAnnotationsAsAliasMetadata,
},
}
return resp, nil
}
}
// pathConfigWrite handles create and update commands to the config
func (b *kubeAuthBackend) pathConfigWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
b.l.Lock()
defer b.l.Unlock()
host := data.Get("kubernetes_host").(string)
if host == "" {
return logical.ErrorResponse("no host provided"), nil
}
disableLocalCAJwt := data.Get("disable_local_ca_jwt").(bool)
pemList := data.Get("pem_keys").([]string)
caCert := data.Get("kubernetes_ca_cert").(string)
issuer := data.Get("issuer").(string)
disableIssValidation := data.Get("disable_iss_validation").(bool)
tokenReviewer := data.Get("token_reviewer_jwt").(string)
useAnnotationsAsAliasMetadata := data.Get("use_annotations_as_alias_metadata").(bool)
// hasCerts returns true if caCert contains at least one valid certificate. It
// does not check if any of the certificates from caCert are CAs, although that
// might be something that we want in the future.
hasCerts := func(certBundle string) bool {
var b *pem.Block
rest := []byte(certBundle)
for {
b, rest = pem.Decode(rest)
if b == nil {
break
}
if pem.EncodeToMemory(b) != nil {
return true
}
}
return false
}
if caCert != "" && !hasCerts(caCert) {
return logical.ErrorResponse(
"The provided CA PEM data contains no valid certificates",
), nil
}
config := &kubeConfig{
PublicKeys: make([]crypto.PublicKey, len(pemList)),
PEMKeys: pemList,
Host: host,
CACert: caCert,
TokenReviewerJWT: tokenReviewer,
Issuer: issuer,
DisableISSValidation: disableIssValidation,
DisableLocalCAJwt: disableLocalCAJwt,
UseAnnotationsAsAliasMetadata: useAnnotationsAsAliasMetadata,
}
var err error
for i, pem := range pemList {
config.PublicKeys[i], err = parsePublicKeyPEM([]byte(pem))
if err != nil {
return logical.ErrorResponse(err.Error()), nil
}
}
if err := b.updateTLSConfig(config); err != nil {
return logical.ErrorResponse(err.Error()), nil
}
entry, err := logical.StorageEntryJSON(configPath, config)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
return nil, nil
}
// kubeConfig contains the public key certificate used to verify the signature
// on the service account JWTs
type kubeConfig struct {
// PublicKeys is the list of public key objects used to verify JWTs
PublicKeys []crypto.PublicKey `json:"-"`
// PEMKeys is the list of public key PEMs used to store the keys
// in storage.
PEMKeys []string `json:"pem_keys"`
// Host is the url string for the kubernetes API
Host string `json:"host"`
// CACert is the CA Cert to use to call into the kubernetes API
CACert string `json:"ca_cert"`
// TokenReviewJWT is the bearer to use during the TokenReview API call
TokenReviewerJWT string `json:"token_reviewer_jwt"`
// Issuer is the claim that specifies who issued the token
Issuer string `json:"issuer"`
// DisableISSValidation is optional parameter to allow to skip ISS validation
DisableISSValidation bool `json:"disable_iss_validation"`
// 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"`
// UseAnnotationsAsAliasMetadata is an optional parameter to enable using
// annotations from the client token's associated service account as
// alias metadata for the Vault entity. Only annotations with the prefix
// "vault.hashicorp.com/alias-metadata-" will be used. Note that Vault will
// need permission to read service accounts from the Kubernetes API.
UseAnnotationsAsAliasMetadata bool `json:"use_annotations_as_alias_metadata"`
}
// PasrsePublicKeyPEM is used to parse RSA and ECDSA public keys from PEMs
func parsePublicKeyPEM(data []byte) (crypto.PublicKey, error) {
block, data := pem.Decode(data)
if block != nil {
var rawKey interface{}
var err error
if rawKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil {
if cert, err := x509.ParseCertificate(block.Bytes); err == nil {
rawKey = cert.PublicKey
} else {
return nil, err
}
}
if rsaPublicKey, ok := rawKey.(*rsa.PublicKey); ok {
return rsaPublicKey, nil
}
if ecPublicKey, ok := rawKey.(*ecdsa.PublicKey); ok {
return ecPublicKey, nil
}
}
return nil, errors.New("data does not contain any valid RSA or ECDSA public keys")
}
const (
confHelpSyn = `Configures the JWT Public Key and Kubernetes API information.`
confHelpDesc = `
The Kubernetes Auth backend validates service account JWTs and verifies their
existence with the Kubernetes TokenReview API. This endpoint configures the
public key used to validate the JWT signature and the necessary information to
access the Kubernetes API.
`
)