This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
kuberos.go
380 lines (330 loc) · 10.7 KB
/
kuberos.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package kuberos
import (
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"github.com/negz/kuberos/extractor"
oidc "github.com/coreos/go-oidc"
"github.com/gorilla/schema"
"github.com/pkg/errors"
"github.com/spf13/afero"
"go.uber.org/zap"
"golang.org/x/oauth2"
"k8s.io/api/core/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)
const (
// DefaultKubeCfgEndpoint is the default endpoint to which clients should
// be redirected after authentication.
DefaultKubeCfgEndpoint = "ui"
// DefaultAPITokenMountPath is the default mount path for API tokens
DefaultAPITokenMountPath = "/var/run/secrets/kubernetes.io/serviceaccount"
schemeHTTP = "http"
schemeHTTPS = "https"
headerForwardedProto = "X-Forwarded-Proto"
headerForwardedFor = "X-Forwarded-For"
headerForwardedPrefix = "X-Forwarded-Prefix"
urlParamState = "state"
urlParamCode = "code"
urlParamError = "error"
urlParamErrorDescription = "error_description"
urlParamErrorURI = "error_uri"
templateAuthProvider = "oidc"
templateOIDCClientID = "client-id"
templateOIDCClientSecret = "client-secret"
templateOIDCIDToken = "id-token"
templateOIDCIssuer = "idp-issuer-url"
templateOIDCRefreshToken = "refresh-token"
templateFormParseMemory = 32 << 20 // 32MB
)
var (
// DefaultScopes are the minimum required oauth2 scopes for every
// authentication request.
DefaultScopes = []string{oidc.ScopeOpenID}
// ErrInvalidKubeCfgEndpoint indicates an unparseable redirect endpoint.
ErrInvalidKubeCfgEndpoint = errors.New("invalid redirect endpoint")
// ErrInvalidState indicates the provided state param was not as expected.
ErrInvalidState = errors.New("invalid state parameter: user agent or IP address changed between requests")
// ErrMissingCode indicates a response without an OAuth 2.0 authorization
// code
ErrMissingCode = errors.New("response missing authorization code")
// ErrNoYAMLSerializer indicates we're unable to serialize Kubernetes
// objects as YAML.
ErrNoYAMLSerializer = errors.New("no YAML serializer registered")
decoder = schema.NewDecoder()
appFs = afero.NewOsFs()
approvalConsent = oauth2.SetAuthURLParam("prompt", "consent")
)
// A StateFn should take an HTTP request and return a difficult to predict yet
// deterministic state string.
type StateFn func(*http.Request) string
func defaultStateFn(secret []byte) StateFn {
// Writing to a hash never returns an error.
// nolint: errcheck, gas
return func(r *http.Request) string {
h := sha256.New()
h.Write(secret)
h.Write([]byte(r.Host))
h.Write([]byte(r.UserAgent()))
return fmt.Sprintf("%x", h.Sum(nil))
}
}
// OfflineAsScope determines whether an offline refresh token is requested via
// a scope per the spec or via Google's custom access_type=offline method.
//
// See http://openid.net/specs/openid-connect-core-1_0.html#OfflineAccess and
// https://developers.google.com/identity/protocols/OAuth2WebServer#offline
func OfflineAsScope(p *oidc.Provider) bool {
var s struct {
Scopes []string `json:"scopes_supported"`
}
if err := p.Claims(&s); err != nil {
return true
}
if len(s.Scopes) == 0 {
return true
}
for _, scope := range s.Scopes {
if scope == oidc.ScopeOfflineAccess {
return true
}
}
return false
}
// ScopeRequests configures the oauth2 scopes to request during authentication.
type ScopeRequests struct {
OfflineAsScope bool
Scopes []string
}
// Get the scopes to request during authentication.
func (r *ScopeRequests) Get() []string {
scopes := DefaultScopes
if r.OfflineAsScope {
scopes = append(scopes, oidc.ScopeOfflineAccess)
}
return append(scopes, r.Scopes...)
}
// Handlers provides HTTP handlers for the Kubernary service.
type Handlers struct {
log *zap.Logger
cfg *oauth2.Config
e extractor.OIDC
oo []oauth2.AuthCodeOption
state StateFn
httpClient *http.Client
endpoint *url.URL
}
// An Option represents a Handlers option.
type Option func(*Handlers) error
// StateFunction allows the use of a bespoke state generator function.
func StateFunction(fn StateFn) Option {
return func(h *Handlers) error {
h.state = fn
return nil
}
}
// HTTPClient allows the use of a bespoke HTTP client for OIDC requests.
func HTTPClient(c *http.Client) Option {
return func(h *Handlers) error {
h.httpClient = c
return nil
}
}
// AuthCodeOptions allows the use of bespoke OAuth2 options.
func AuthCodeOptions(oo []oauth2.AuthCodeOption) Option {
return func(h *Handlers) error {
h.oo = oo
return nil
}
}
// Logger allows the use of a bespoke Zap logger.
func Logger(l *zap.Logger) Option {
return func(h *Handlers) error {
h.log = l
return nil
}
}
// NewHandlers returns a new set of Kuberos HTTP handlers.
func NewHandlers(c *oauth2.Config, e extractor.OIDC, ho ...Option) (*Handlers, error) {
l, err := zap.NewProduction()
if err != nil {
return nil, errors.Wrap(err, "cannot create default logger")
}
h := &Handlers{
log: l,
cfg: c,
e: e,
oo: []oauth2.AuthCodeOption{oauth2.AccessTypeOffline, approvalConsent},
state: defaultStateFn([]byte(c.ClientSecret)),
httpClient: http.DefaultClient,
endpoint: &url.URL{Path: DefaultKubeCfgEndpoint},
}
// Assume we're using a Googley request for offline access.
for _, s := range c.Scopes {
// ...Unless we find an offline scope
if s == oidc.ScopeOfflineAccess {
h.oo = []oauth2.AuthCodeOption{approvalConsent}
}
}
for _, o := range ho {
if err := o(h); err != nil {
return nil, errors.Wrap(err, "cannot apply handlers option")
}
}
return h, nil
}
// Login redirects to an OIDC provider per the supplied oauth2 config.
func (h *Handlers) Login(w http.ResponseWriter, r *http.Request) {
c := &oauth2.Config{
ClientID: h.cfg.ClientID,
ClientSecret: h.cfg.ClientSecret,
Endpoint: h.cfg.Endpoint,
Scopes: h.cfg.Scopes,
RedirectURL: redirectURL(r, h.endpoint),
}
u := c.AuthCodeURL(h.state(r), h.oo...)
h.log.Debug("redirect", zap.String("url", u))
http.Redirect(w, r, u, http.StatusSeeOther)
}
// KubeCfg returns a handler that forms helpers for kubecfg authentication.
func (h *Handlers) KubeCfg(w http.ResponseWriter, r *http.Request) {
if r.FormValue(urlParamState) != h.state(r) {
http.Error(w, ErrInvalidState.Error(), http.StatusForbidden)
return
}
if e := r.FormValue(urlParamError); e != "" {
msg := e
if desc := r.FormValue(urlParamErrorDescription); desc != "" {
msg = fmt.Sprintf("%s: %s", msg, desc)
}
if uri := r.FormValue(urlParamErrorURI); uri != "" {
msg = fmt.Sprintf("%s (see %s)", msg, uri)
}
http.Error(w, msg, http.StatusForbidden)
return
}
code := r.FormValue(urlParamCode)
if code == "" {
http.Error(w, ErrMissingCode.Error(), http.StatusBadRequest)
return
}
c := &oauth2.Config{
ClientID: h.cfg.ClientID,
ClientSecret: h.cfg.ClientSecret,
Endpoint: h.cfg.Endpoint,
Scopes: h.cfg.Scopes,
RedirectURL: redirectURL(r, h.endpoint),
}
rsp, err := h.e.Process(r.Context(), c, code)
if err != nil {
http.Error(w, errors.Wrap(err, "cannot process OAuth2 code").Error(), http.StatusForbidden)
return
}
j, err := json.Marshal(rsp)
if err != nil {
http.Error(w, errors.Wrap(err, "cannot marshal JSON").Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if _, err := w.Write(j); err != nil {
http.Error(w, errors.Wrap(err, "cannot write response").Error(), http.StatusInternalServerError)
}
}
func redirectURL(r *http.Request, endpoint *url.URL) string {
if r.URL.IsAbs() {
return fmt.Sprint(r.URL.ResolveReference(endpoint))
}
u := &url.URL{}
u.Scheme = schemeHTTP
if r.TLS != nil {
u.Scheme = schemeHTTPS
}
for h, v := range r.Header {
switch h {
case headerForwardedProto:
// Redirect to HTTPS if we're listening on HTTP behind an HTTPS ELB.
for _, proto := range v {
if proto == schemeHTTPS {
u.Scheme = schemeHTTPS
}
}
case headerForwardedPrefix:
// Redirect includes X-Forwarded-Prefix if exists
for _, prefix := range v {
u.Path = prefix
}
}
}
// TODO(negz): Set port if X-Forwarded-Port exists?
u.Host = r.Host
return fmt.Sprint(u.ResolveReference(endpoint))
}
// Template returns an HTTP handler that returns a new kubecfg by taking a
// template with existing clusters and adding a user and context for each based
// on the URL parameters passed to it.
func Template(cfg *api.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(templateFormParseMemory) //nolint:errcheck
p := &extractor.OIDCAuthenticationParams{}
// TODO(negz): Return an error if any required parameter is absent.
if err := decoder.Decode(p, r.Form); err != nil {
http.Error(w, errors.Wrap(err, "cannot parse URL parameter").Error(), http.StatusBadRequest)
return
}
y, err := clientcmd.Write(populateUser(cfg, p))
if err != nil {
http.Error(w, errors.Wrap(err, "cannot marshal template to YAML").Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/x-yaml; charset=utf-8")
w.Header().Set("Content-Disposition", "attachment")
if _, err := w.Write(y); err != nil {
http.Error(w, errors.Wrap(err, "cannot write response").Error(), http.StatusInternalServerError)
}
}
}
func populateUser(cfg *api.Config, p *extractor.OIDCAuthenticationParams) api.Config {
c := api.Config{}
c.AuthInfos = make(map[string]*api.AuthInfo)
c.Clusters = make(map[string]*api.Cluster)
c.Contexts = make(map[string]*api.Context)
c.CurrentContext = cfg.CurrentContext
c.AuthInfos[p.Username] = &api.AuthInfo{
AuthProvider: &api.AuthProviderConfig{
Name: templateAuthProvider,
Config: map[string]string{
templateOIDCClientID: p.ClientID,
templateOIDCClientSecret: p.ClientSecret,
templateOIDCIDToken: p.IDToken,
templateOIDCRefreshToken: p.RefreshToken,
templateOIDCIssuer: p.IssuerURL,
},
},
}
for name, cluster := range cfg.Clusters {
// If the cluster definition does not come with certificate-authority-data nor
// certificate-authority, then check if kuberos has access to the cluster's CA
// certificate and include it when possible. Assume all errors are non-fatal.
if len(cluster.CertificateAuthorityData) == 0 && cluster.CertificateAuthority == "" {
caPath := filepath.Join(DefaultAPITokenMountPath, v1.ServiceAccountRootCAKey)
if caFile, err := appFs.Open(caPath); err == nil {
if caCert, err := ioutil.ReadAll(caFile); err == nil {
cluster.CertificateAuthorityData = caCert
}
} else {
fmt.Printf("Error: %+v\n", err)
}
}
c.Clusters[name] = cluster
c.Contexts[name] = &api.Context{
Cluster: name,
AuthInfo: p.Username,
}
}
return c
}