-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
sso.go
375 lines (340 loc) · 12 KB
/
sso.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
package sso
import (
"context"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"strings"
"time"
pkgrand "github.com/argoproj/pkg/rand"
"github.com/coreos/go-oidc/v3/oidc"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"gopkg.in/square/go-jose.v2"
"gopkg.in/square/go-jose.v2/jwt"
apiv1 "k8s.io/api/core/v1"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"github.com/argoproj/argo-workflows/v3/server/auth/rbac"
"github.com/argoproj/argo-workflows/v3/server/auth/types"
)
const (
Prefix = "Bearer v2:"
issuer = "argo-server" // the JWT issuer
secretName = "sso" // where we store SSO secret
cookieEncryptionPrivateKeySecretKey = "cookieEncryptionPrivateKey" // the key name for the private key in the secret
)
//go:generate mockery -name Interface
type Interface interface {
Authorize(authorization string) (*types.Claims, error)
HandleRedirect(writer http.ResponseWriter, request *http.Request)
HandleCallback(writer http.ResponseWriter, request *http.Request)
IsRBACEnabled() bool
}
var _ Interface = &sso{}
type sso struct {
config *oauth2.Config
issuer string
idTokenVerifier *oidc.IDTokenVerifier
httpClient *http.Client
baseHRef string
secure bool
privateKey crypto.PrivateKey
encrypter jose.Encrypter
rbacConfig *rbac.Config
expiry time.Duration
customClaimName string
userInfoPath string
}
func (s *sso) IsRBACEnabled() bool {
return s.rbacConfig.IsEnabled()
}
type Config struct {
Issuer string `json:"issuer"`
IssuerAlias string `json:"issuerAlias,omitempty"`
ClientID apiv1.SecretKeySelector `json:"clientId"`
ClientSecret apiv1.SecretKeySelector `json:"clientSecret"`
RedirectURL string `json:"redirectUrl"`
RBAC *rbac.Config `json:"rbac,omitempty"`
// additional scopes (on top of "openid")
Scopes []string `json:"scopes,omitempty"`
SessionExpiry metav1.Duration `json:"sessionExpiry,omitempty"`
// customGroupClaimName will override the groups claim name
CustomGroupClaimName string `json:"customGroupClaimName,omitempty"`
UserInfoPath string `json:"userInfoPath,omitempty"`
InsecureSkipVerify bool `json:"insecureSkipVerify,omitempty"`
}
func (c Config) GetSessionExpiry() time.Duration {
if c.SessionExpiry.Duration > 0 {
return c.SessionExpiry.Duration
}
return 10 * time.Hour
}
// Abstract methods of oidc.Provider that our code uses into an interface. That
// will allow us to implement a stub for unit testing. If you start using more
// oidc.Provider methods in this file, add them here and provide a stub
// implementation in test.
type providerInterface interface {
Endpoint() oauth2.Endpoint
Verifier(config *oidc.Config) *oidc.IDTokenVerifier
}
type providerFactory func(ctx context.Context, issuer string) (providerInterface, error)
func providerFactoryOIDC(ctx context.Context, issuer string) (providerInterface, error) {
return oidc.NewProvider(ctx, issuer)
}
func New(c Config, secretsIf corev1.SecretInterface, baseHRef string, secure bool) (Interface, error) {
return newSso(providerFactoryOIDC, c, secretsIf, baseHRef, secure)
}
func newSso(
factory providerFactory,
c Config,
secretsIf corev1.SecretInterface,
baseHRef string,
secure bool,
) (Interface, error) {
if c.Issuer == "" {
return nil, fmt.Errorf("issuer empty")
}
if c.ClientID.Name == "" || c.ClientID.Key == "" {
return nil, fmt.Errorf("clientID empty")
}
if c.ClientSecret.Name == "" || c.ClientSecret.Key == "" {
return nil, fmt.Errorf("clientSecret empty")
}
ctx := context.Background()
clientSecretObj, err := secretsIf.Get(ctx, c.ClientSecret.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
// Create http client with TLSConfig to allow skipping of CA validation if InsecureSkipVerify is set.
httpClient := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: c.InsecureSkipVerify}}}
oidcContext := oidc.ClientContext(ctx, httpClient)
// Some offspec providers like Azure, Oracle IDCS have oidc discovery url different from issuer url which causes issuerValidation to fail
// This providerCtx will allow the Verifier to succeed if the alternate/alias URL is in the config
if c.IssuerAlias != "" {
oidcContext = oidc.InsecureIssuerURLContext(oidcContext, c.IssuerAlias)
}
provider, err := factory(oidcContext, c.Issuer)
if err != nil {
return nil, err
}
var clientIDObj *apiv1.Secret
if c.ClientID.Name == c.ClientSecret.Name {
clientIDObj = clientSecretObj
} else {
clientIDObj, err = secretsIf.Get(ctx, c.ClientID.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
}
generatedKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, fmt.Errorf("failed to generate key: %w", err)
}
// whoa - are you ignoring errors - yes - we don't care if it fails -
// if it fails, then the get will fail, and the pod restart
// it may fail due to race condition with another pod - which is fine,
// when it restart it'll get the new key
_, err = secretsIf.Create(ctx, &apiv1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: secretName},
Data: map[string][]byte{cookieEncryptionPrivateKeySecretKey: x509.MarshalPKCS1PrivateKey(generatedKey)},
}, metav1.CreateOptions{})
if err != nil && !apierr.IsAlreadyExists(err) {
return nil, fmt.Errorf("failed to create secret: %w", err)
}
secret, err := secretsIf.Get(ctx, secretName, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("failed to read secret: %w", err)
}
privateKey, err := x509.ParsePKCS1PrivateKey(secret.Data[cookieEncryptionPrivateKeySecretKey])
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %w", err)
}
clientID := clientIDObj.Data[c.ClientID.Key]
if clientID == nil {
return nil, fmt.Errorf("key %s missing in secret %s", c.ClientID.Key, c.ClientID.Name)
}
clientSecret := clientSecretObj.Data[c.ClientSecret.Key]
if clientSecret == nil {
return nil, fmt.Errorf("key %s missing in secret %s", c.ClientSecret.Key, c.ClientSecret.Name)
}
config := &oauth2.Config{
ClientID: string(clientID),
ClientSecret: string(clientSecret),
RedirectURL: c.RedirectURL,
Endpoint: provider.Endpoint(),
Scopes: append(c.Scopes, oidc.ScopeOpenID),
}
idTokenVerifier := provider.Verifier(&oidc.Config{ClientID: config.ClientID})
encrypter, err := jose.NewEncrypter(jose.A256GCM, jose.Recipient{Algorithm: jose.RSA_OAEP_256, Key: privateKey.Public()}, &jose.EncrypterOptions{Compression: jose.DEFLATE})
if err != nil {
return nil, fmt.Errorf("failed to create JWT encrpytor: %w", err)
}
lf := log.Fields{"redirectUrl": config.RedirectURL, "issuer": c.Issuer, "issuerAlias": "DISABLED", "clientId": c.ClientID, "scopes": config.Scopes, "insecureSkipVerify": c.InsecureSkipVerify}
if c.IssuerAlias != "" {
lf["issuerAlias"] = c.IssuerAlias
}
log.WithFields(lf).Info("SSO configuration")
return &sso{
config: config,
idTokenVerifier: idTokenVerifier,
baseHRef: baseHRef,
httpClient: httpClient,
secure: secure,
privateKey: privateKey,
encrypter: encrypter,
rbacConfig: c.RBAC,
expiry: c.GetSessionExpiry(),
customClaimName: c.CustomGroupClaimName,
userInfoPath: c.UserInfoPath,
issuer: c.Issuer,
}, nil
}
func (s *sso) HandleRedirect(w http.ResponseWriter, r *http.Request) {
redirectUrl := r.URL.Query().Get("redirect")
state, err := pkgrand.RandString(10)
if err != nil {
log.WithError(err).Error("failed to create state")
w.WriteHeader(500)
return
}
http.SetCookie(w, &http.Cookie{
Name: state,
Value: redirectUrl,
Expires: time.Now().Add(3 * time.Minute),
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
Secure: s.secure,
})
redirectOption := oauth2.SetAuthURLParam("redirect_uri", s.getRedirectUrl(r))
http.Redirect(w, r, s.config.AuthCodeURL(state, redirectOption), http.StatusFound)
}
func (s *sso) HandleCallback(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
state := r.URL.Query().Get("state")
cookie, err := r.Cookie(state)
http.SetCookie(w, &http.Cookie{Name: state, MaxAge: 0})
if err != nil {
w.WriteHeader(400)
_, _ = w.Write([]byte(fmt.Sprintf("invalid state: %v", err)))
return
}
redirectOption := oauth2.SetAuthURLParam("redirect_uri", s.getRedirectUrl(r))
// Use sso.httpClient in order to respect TLSOptions
oauth2Context := context.WithValue(ctx, oauth2.HTTPClient, s.httpClient)
oauth2Token, err := s.config.Exchange(oauth2Context, r.URL.Query().Get("code"), redirectOption)
if err != nil {
w.WriteHeader(401)
_, _ = w.Write([]byte(fmt.Sprintf("failed to exchange token: %v", err)))
return
}
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
w.WriteHeader(401)
_, _ = w.Write([]byte("failed to get id_token"))
return
}
idToken, err := s.idTokenVerifier.Verify(ctx, rawIDToken)
if err != nil {
w.WriteHeader(401)
_, _ = w.Write([]byte(fmt.Sprintf("failed to verify token: %v", err)))
return
}
c := &types.Claims{}
if err := idToken.Claims(c); err != nil {
w.WriteHeader(401)
_, _ = w.Write([]byte(fmt.Sprintf("failed to get claims: %v", err)))
return
}
// Default to groups claim but if customClaimName is set
// extract groups based on that claim key
groups := c.Groups
if s.customClaimName != "" {
groups, err = c.GetCustomGroup(s.customClaimName)
if err != nil {
w.WriteHeader(401)
_, _ = w.Write([]byte(fmt.Sprintf("failed to get custom claim: %v", err)))
return
}
}
// Some SSO implementations (Okta) require a call to
// the OIDC user info path to get attributes like groups
if s.userInfoPath != "" {
groups, err = c.GetUserInfoGroups(oauth2Token.AccessToken, s.issuer, s.userInfoPath)
if err != nil {
w.WriteHeader(401)
_, _ = w.Write([]byte(fmt.Sprintf("failed to get groups claim: %v", err)))
return
}
}
argoClaims := &types.Claims{
Claims: jwt.Claims{
Issuer: issuer,
Subject: c.Subject,
Expiry: jwt.NewNumericDate(time.Now().Add(s.expiry)),
},
Groups: groups,
RawClaim: c.RawClaim,
Email: c.Email,
EmailVerified: c.EmailVerified,
ServiceAccountName: c.ServiceAccountName,
}
raw, err := jwt.Encrypted(s.encrypter).Claims(argoClaims).CompactSerialize()
if err != nil {
w.WriteHeader(401)
_, _ = w.Write([]byte(fmt.Sprintf("failed to encode claims: %v", err)))
return
}
value := Prefix + raw
log.Debugf("handing oauth2 callback %v", value)
http.SetCookie(w, &http.Cookie{
Value: value,
Name: "authorization",
Path: s.baseHRef,
Expires: time.Now().Add(s.expiry),
SameSite: http.SameSiteStrictMode,
Secure: s.secure,
})
redirect := s.baseHRef
proto := "http"
if s.secure {
proto = "https"
}
prefix := fmt.Sprintf("%s://%s%s", proto, r.Host, s.baseHRef)
if strings.HasPrefix(cookie.Value, prefix) {
redirect = cookie.Value
}
http.Redirect(w, r, redirect, 302)
}
// authorize verifies a bearer token and pulls user information form the claims.
func (s *sso) Authorize(authorization string) (*types.Claims, error) {
tok, err := jwt.ParseEncrypted(strings.TrimPrefix(authorization, Prefix))
if err != nil {
return nil, fmt.Errorf("failed to parse encrypted token %v", err)
}
c := &types.Claims{}
if err := tok.Claims(s.privateKey, c); err != nil {
return nil, fmt.Errorf("failed to parse claims: %v", err)
}
if err := c.Validate(jwt.Expected{Issuer: issuer}); err != nil {
return nil, fmt.Errorf("failed to validate claims: %v", err)
}
return c, nil
}
func (s *sso) getRedirectUrl(r *http.Request) string {
if s.config.RedirectURL != "" {
return s.config.RedirectURL
}
proto := "http"
if r.URL.Scheme != "" {
proto = r.URL.Scheme
} else if s.secure {
proto = "https"
}
return fmt.Sprintf("%s://%s%soauth2/callback", proto, r.Host, s.baseHRef)
}