forked from hashicorp/vault-plugin-auth-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_config.go
262 lines (236 loc) · 8.39 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
package kubeauth
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"net/http"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
josejwt "gopkg.in/square/go-jose.v2/jwt"
)
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$",
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: "PEM encoded CA cert for use by the TLS client used to talk with the API.",
DisplayAttrs: &framework.DisplayAttributes{
Name: "Kubernetes CA Certificate",
},
},
"token_reviewer_jwt": {
Type: framework.TypeString,
Description: `A service account JWT used 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",
},
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathConfigWrite,
logical.CreateOperation: b.pathConfigWrite,
logical.ReadOperation: b.pathConfigRead,
},
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,
},
}
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) {
host := data.Get("kubernetes_host").(string)
if host == "" {
return logical.ErrorResponse("no host provided"), nil
}
disableLocalJWT := 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)
if tokenReviewer != "" {
// Validate it's a JWT, but don't verify the signature, since we may not have the right cert.
_, err := josejwt.ParseSigned(tokenReviewer)
if err != nil {
return nil, err
}
}
if disableLocalJWT && caCert == "" {
return logical.ErrorResponse("kubernetes_ca_cert must be given when disable_local_ca_jwt is true"), nil
}
config := &kubeConfig{
PublicKeys: make([]crypto.PublicKey, len(pemList)),
PEMKeys: pemList,
Host: host,
CACert: caCert,
TokenReviewerJWT: tokenReviewer,
Issuer: issuer,
DisableISSValidation: disableIssValidation,
DisableLocalCAJwt: disableLocalJWT,
}
b.l.Lock()
defer b.l.Unlock()
// Determine if we load the local CA cert or the CA cert provided
// by the kubernetes_ca_cert path into the backend's HTTP client
certPool := x509.NewCertPool()
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
}
if disableLocalJWT || len(caCert) > 0 {
certPool.AppendCertsFromPEM([]byte(config.CACert))
tlsConfig.RootCAs = certPool
b.httpClient.Transport.(*http.Transport).TLSClientConfig = tlsConfig
} else {
localCACert, err := b.localCACertReader.ReadFile()
if err != nil {
return nil, err
}
certPool.AppendCertsFromPEM([]byte(localCACert))
tlsConfig.RootCAs = certPool
b.httpClient.Transport.(*http.Transport).TLSClientConfig = tlsConfig
}
var err error
for i, pem := range pemList {
config.PublicKeys[i], err = parsePublicKeyPEM([]byte(pem))
if 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"`
}
// 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.
`
)