-
Notifications
You must be signed in to change notification settings - Fork 571
/
auth_context.go
217 lines (177 loc) · 6.91 KB
/
auth_context.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
// Contains types needed to start up a standalone OAuth2 Authorization Server or delegate authentication to an external
// provider. It supports OpenId connect for user authentication.
package auth
import (
"context"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/coreos/go-oidc"
"golang.org/x/oauth2"
"github.com/flyteorg/flyte/flyteadmin/auth/config"
"github.com/flyteorg/flyte/flyteadmin/auth/interfaces"
"github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/service"
"github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery/core"
"github.com/flyteorg/flyte/flytestdlib/errors"
"github.com/flyteorg/flyte/flytestdlib/logger"
)
const (
IdpConnectionTimeout = 10 * time.Second
ErrauthCtx errors.ErrorCode = "AUTH_CONTEXT_SETUP_FAILED"
ErrConfigFileRead errors.ErrorCode = "CONFIG_OPTION_FILE_READ_FAILED"
)
var (
callbackRelativeURL = config.MustParseURL("/callback")
rootRelativeURL = config.MustParseURL("/")
)
// Please see the comment on the corresponding AuthenticationContext for more information.
type Context struct {
oauth2Client *oauth2.Config
cookieManager interfaces.CookieHandler
oidcProvider *oidc.Provider
options *config.Config
oauth2Provider interfaces.OAuth2Provider
oauth2ResourceServer interfaces.OAuth2ResourceServer
authServiceImpl service.AuthMetadataServiceServer
identityServiceIml service.IdentityServiceServer
userInfoURL *url.URL
oauth2MetadataURL *url.URL
oidcMetadataURL *url.URL
httpClient *http.Client
}
func (c Context) OAuth2Provider() interfaces.OAuth2Provider {
return c.oauth2Provider
}
func (c Context) OAuth2ClientConfig(requestURL *url.URL) *oauth2.Config {
if requestURL == nil || strings.HasPrefix(c.oauth2Client.RedirectURL, requestURL.ResolveReference(rootRelativeURL).String()) {
return c.oauth2Client
}
return &oauth2.Config{
RedirectURL: requestURL.ResolveReference(callbackRelativeURL).String(),
ClientID: c.oauth2Client.ClientID,
ClientSecret: c.oauth2Client.ClientSecret,
Scopes: c.oauth2Client.Scopes,
Endpoint: c.oauth2Client.Endpoint,
}
}
func (c Context) OidcProvider() *oidc.Provider {
return c.oidcProvider
}
func (c Context) CookieManager() interfaces.CookieHandler {
return c.cookieManager
}
func (c Context) Options() *config.Config {
return c.options
}
func (c Context) GetUserInfoURL() *url.URL {
return c.userInfoURL
}
func (c Context) GetHTTPClient() *http.Client {
return c.httpClient
}
func (c Context) GetOAuth2MetadataURL() *url.URL {
return c.oauth2MetadataURL
}
func (c Context) GetOIdCMetadataURL() *url.URL {
return c.oidcMetadataURL
}
func (c Context) AuthMetadataService() service.AuthMetadataServiceServer {
return c.authServiceImpl
}
func (c Context) IdentityService() service.IdentityServiceServer {
return c.identityServiceIml
}
func (c Context) OAuth2ResourceServer() interfaces.OAuth2ResourceServer {
return c.oauth2ResourceServer
}
func NewAuthenticationContext(ctx context.Context, sm core.SecretManager, oauth2Provider interfaces.OAuth2Provider,
oauth2ResourceServer interfaces.OAuth2ResourceServer, authMetadataService service.AuthMetadataServiceServer,
identityService service.IdentityServiceServer, options *config.Config) (Context, error) {
// Construct the cookie manager object.
hashKeyBase64, err := sm.Get(ctx, options.UserAuth.CookieHashKeySecretName)
if err != nil {
return Context{}, errors.Wrapf(ErrConfigFileRead, err, "Could not read hash key file")
}
blockKeyBase64, err := sm.Get(ctx, options.UserAuth.CookieBlockKeySecretName)
if err != nil {
return Context{}, errors.Wrapf(ErrConfigFileRead, err, "Could not read hash key file")
}
cookieManager, err := NewCookieManager(ctx, hashKeyBase64, blockKeyBase64, options.UserAuth.CookieSetting)
if err != nil {
logger.Errorf(ctx, "Error creating cookie manager %s", err)
return Context{}, errors.Wrapf(ErrauthCtx, err, "Error creating cookie manager")
}
// Construct an http client for interacting with the IDP if necessary.
httpClient := &http.Client{
Timeout: IdpConnectionTimeout,
}
if len(options.UserAuth.HTTPProxyURL.String()) > 0 {
logger.Infof(ctx, "HTTPProxy URL for OAuth2 is: %s", options.UserAuth.HTTPProxyURL.String())
httpClient.Transport = &http.Transport{Proxy: http.ProxyURL(&options.UserAuth.HTTPProxyURL.URL)}
}
// Construct an oidc Provider, which needs its own http Client.
oidcCtx := oidc.ClientContext(ctx, httpClient)
baseURL := options.UserAuth.OpenID.BaseURL.String()
provider, err := oidc.NewProvider(oidcCtx, baseURL)
if err != nil {
return Context{}, errors.Wrapf(ErrauthCtx, err, "Error creating oidc provider w/ issuer [%v]", baseURL)
}
// Construct the golang OAuth2 library's own internal configuration object from this package's config
oauth2Config, err := GetOAuth2ClientConfig(ctx, options.UserAuth.OpenID, provider.Endpoint(), sm)
if err != nil {
return Context{}, errors.Wrapf(ErrauthCtx, err, "Error creating OAuth2 library configuration")
}
logger.Infof(ctx, "Base IDP URL is %s", options.UserAuth.OpenID.BaseURL)
oauth2MetadataURL, err := url.Parse(OAuth2MetadataEndpoint)
if err != nil {
logger.Errorf(ctx, "Error parsing oauth2 metadata URL %s", err)
return Context{}, errors.Wrapf(ErrauthCtx, err, "Error parsing metadata URL")
}
logger.Infof(ctx, "Metadata endpoint is %s", oauth2MetadataURL)
oidcMetadataURL, err := url.Parse(OIdCMetadataEndpoint)
if err != nil {
logger.Errorf(ctx, "Error parsing oidc metadata URL %s", err)
return Context{}, errors.Wrapf(ErrauthCtx, err, "Error parsing metadata URL")
}
logger.Infof(ctx, "Metadata endpoint is %s", oidcMetadataURL)
authCtx := Context{
options: options,
oidcMetadataURL: oidcMetadataURL,
oauth2MetadataURL: oauth2MetadataURL,
oauth2Client: &oauth2Config,
oidcProvider: provider,
httpClient: httpClient,
cookieManager: cookieManager,
oauth2Provider: oauth2Provider,
oauth2ResourceServer: oauth2ResourceServer,
}
authCtx.authServiceImpl = authMetadataService
authCtx.identityServiceIml = identityService
return authCtx, nil
}
// This creates a oauth2 library config object, with values from the Flyte Admin config
func GetOAuth2ClientConfig(ctx context.Context, options config.OpenIDOptions, providerEndpoints oauth2.Endpoint, sm core.SecretManager) (cfg oauth2.Config, err error) {
var secret string
if len(options.DeprecatedClientSecretFile) > 0 {
secretBytes, err := ioutil.ReadFile(options.DeprecatedClientSecretFile)
if err != nil {
return oauth2.Config{}, err
}
secret = string(secretBytes)
} else {
secret, err = sm.Get(ctx, options.ClientSecretName)
if err != nil {
return oauth2.Config{}, err
}
}
secret = strings.TrimSuffix(secret, "\n")
return oauth2.Config{
RedirectURL: callbackRelativeURL.String(),
ClientID: options.ClientID,
ClientSecret: secret,
Scopes: options.Scopes,
Endpoint: providerEndpoints,
}, nil
}