forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
465 lines (428 loc) · 14.3 KB
/
auth.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Copyright (c) 2017-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"context"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"runtime"
"strconv"
"strings"
"time"
"github.com/form3tech-oss/jwt-go"
)
const (
clientType = "Go"
)
// AuthType indicates the type of authentication in Snowflake
type AuthType int
const (
// AuthTypeSnowflake is the general username password authentication
AuthTypeSnowflake AuthType = iota
// AuthTypeOAuth is the OAuth authentication
AuthTypeOAuth
// AuthTypeExternalBrowser is to use a browser to access an Fed and perform SSO authentication
AuthTypeExternalBrowser
// AuthTypeOkta is to use a native okta URL to perform SSO authentication on Okta
AuthTypeOkta
// AuthTypeJwt is to use Jwt to perform authentication
AuthTypeJwt
// AuthTypeTokenAccessor is to use the provided token accessor and bypass authentication
AuthTypeTokenAccessor
)
func determineAuthenticatorType(cfg *Config, value string) error {
upperCaseValue := strings.ToUpper(value)
lowerCaseValue := strings.ToLower(value)
if strings.Trim(value, " ") == "" || upperCaseValue == AuthTypeSnowflake.String() {
cfg.Authenticator = AuthTypeSnowflake
return nil
} else if upperCaseValue == AuthTypeOAuth.String() {
cfg.Authenticator = AuthTypeOAuth
return nil
} else if upperCaseValue == AuthTypeJwt.String() {
cfg.Authenticator = AuthTypeJwt
return nil
} else if upperCaseValue == AuthTypeExternalBrowser.String() {
cfg.Authenticator = AuthTypeExternalBrowser
return nil
} else {
// possibly Okta case
oktaURLString, err := url.QueryUnescape(lowerCaseValue)
if err != nil {
return &SnowflakeError{
Number: ErrCodeFailedToParseAuthenticator,
Message: errMsgFailedToParseAuthenticator,
MessageArgs: []interface{}{lowerCaseValue},
}
}
oktaURL, err := url.Parse(oktaURLString)
if err != nil {
return &SnowflakeError{
Number: ErrCodeFailedToParseAuthenticator,
Message: errMsgFailedToParseAuthenticator,
MessageArgs: []interface{}{oktaURLString},
}
}
if oktaURL.Scheme != "https" || !strings.HasSuffix(oktaURL.Host, "okta.com") {
return &SnowflakeError{
Number: ErrCodeFailedToParseAuthenticator,
Message: errMsgFailedToParseAuthenticator,
MessageArgs: []interface{}{oktaURLString},
}
}
cfg.OktaURL = oktaURL
cfg.Authenticator = AuthTypeOkta
}
return nil
}
func (authType AuthType) String() string {
switch authType {
case AuthTypeSnowflake:
return "SNOWFLAKE"
case AuthTypeOAuth:
return "OAUTH"
case AuthTypeExternalBrowser:
return "EXTERNALBROWSER"
case AuthTypeOkta:
return "OKTA"
case AuthTypeJwt:
return "SNOWFLAKE_JWT"
case AuthTypeTokenAccessor:
return "TOKENACCESSOR"
default:
return "UNKNOWN"
}
}
// platform consists of compiler and architecture type in string
var platform = fmt.Sprintf("%v-%v", runtime.Compiler, runtime.GOARCH)
// operatingSystem is the runtime operating system.
var operatingSystem = runtime.GOOS
// userAgent shows up in User-Agent HTTP header
var userAgent = fmt.Sprintf("%v/%v (%v-%v) %v/%v",
clientType,
SnowflakeGoDriverVersion,
operatingSystem,
runtime.GOARCH,
runtime.Compiler,
runtime.Version())
type authRequestClientEnvironment struct {
Application string `json:"APPLICATION"`
Os string `json:"OS"`
OsVersion string `json:"OS_VERSION"`
OCSPMode string `json:"OCSP_MODE"`
}
type authRequestData struct {
ClientAppID string `json:"CLIENT_APP_ID"`
ClientAppVersion string `json:"CLIENT_APP_VERSION"`
SvnRevision string `json:"SVN_REVISION"`
AccountName string `json:"ACCOUNT_NAME"`
LoginName string `json:"LOGIN_NAME,omitempty"`
Password string `json:"PASSWORD,omitempty"`
RawSAMLResponse string `json:"RAW_SAML_RESPONSE,omitempty"`
ExtAuthnDuoMethod string `json:"EXT_AUTHN_DUO_METHOD,omitempty"`
Passcode string `json:"PASSCODE,omitempty"`
Authenticator string `json:"AUTHENTICATOR,omitempty"`
SessionParameters map[string]interface{} `json:"SESSION_PARAMETERS,omitempty"`
ClientEnvironment authRequestClientEnvironment `json:"CLIENT_ENVIRONMENT"`
BrowserModeRedirectPort string `json:"BROWSER_MODE_REDIRECT_PORT,omitempty"`
ProofKey string `json:"PROOF_KEY,omitempty"`
Token string `json:"TOKEN,omitempty"`
}
type authRequest struct {
Data authRequestData `json:"data"`
}
type nameValueParameter struct {
Name string `json:"name"`
Value interface{} `json:"value"`
}
type authResponseSessionInfo struct {
DatabaseName string `json:"databaseName"`
SchemaName string `json:"schemaName"`
WarehouseName string `json:"warehouseName"`
RoleName string `json:"roleName"`
}
type authResponseMain struct {
Token string `json:"token,omitempty"`
Validity time.Duration `json:"validityInSeconds,omitempty"`
MasterToken string `json:"masterToken,omitempty"`
MasterValidity time.Duration `json:"masterValidityInSeconds"`
DisplayUserName string `json:"displayUserName"`
ServerVersion string `json:"serverVersion"`
FirstLogin bool `json:"firstLogin"`
RemMeToken string `json:"remMeToken"`
RemMeValidity time.Duration `json:"remMeValidityInSeconds"`
HealthCheckInterval time.Duration `json:"healthCheckInterval"`
NewClientForUpgrade string `json:"newClientForUpgrade"`
SessionID int64 `json:"sessionId"`
Parameters []nameValueParameter `json:"parameters"`
SessionInfo authResponseSessionInfo `json:"sessionInfo"`
TokenURL string `json:"tokenUrl,omitempty"`
SSOURL string `json:"ssoUrl,omitempty"`
ProofKey string `json:"proofKey,omitempty"`
}
type authResponse struct {
Data authResponseMain `json:"data"`
Message string `json:"message"`
Code string `json:"code"`
Success bool `json:"success"`
}
func postAuth(
ctx context.Context,
sr *snowflakeRestful,
params *url.Values,
headers map[string]string,
body []byte,
timeout time.Duration) (
data *authResponse, err error) {
params.Add(requestIDKey, getOrGenerateRequestIDFromContext(ctx).String())
params.Add(requestGUIDKey, NewUUID().String())
fullURL := sr.getFullURL(loginRequestPath, params)
logger.Infof("full URL: %v", fullURL)
resp, err := sr.FuncPost(ctx, sr, fullURL, headers, body, timeout, true)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
var respd authResponse
err = json.NewDecoder(resp.Body).Decode(&respd)
if err != nil {
logger.Error("failed to decode JSON. err: %v", err)
return nil, err
}
return &respd, nil
}
switch resp.StatusCode {
case http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout:
// service availability or connectivity issue. Most likely server side issue.
return nil, &SnowflakeError{
Number: ErrCodeServiceUnavailable,
SQLState: SQLStateConnectionWasNotEstablished,
Message: errMsgServiceUnavailable,
MessageArgs: []interface{}{resp.StatusCode, fullURL},
}
case http.StatusUnauthorized, http.StatusForbidden:
// failed to connect to db. account name may be wrong
return nil, &SnowflakeError{
Number: ErrCodeFailedToConnect,
SQLState: SQLStateConnectionRejected,
Message: errMsgFailedToConnect,
MessageArgs: []interface{}{resp.StatusCode, fullURL},
}
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Errorf("failed to extract HTTP response body. err: %v", err)
return nil, err
}
logger.Infof("HTTP: %v, URL: %v, Body: %v", resp.StatusCode, fullURL, b)
logger.Infof("Header: %v", resp.Header)
return nil, &SnowflakeError{
Number: ErrFailedToAuth,
SQLState: SQLStateConnectionRejected,
Message: errMsgFailedToAuth,
MessageArgs: []interface{}{resp.StatusCode, fullURL},
}
}
// Generates a map of headers needed to authenticate
// with Snowflake.
func getHeaders() map[string]string {
headers := make(map[string]string)
headers[httpHeaderContentType] = headerContentTypeApplicationJSON
headers[httpHeaderAccept] = headerAcceptTypeApplicationSnowflake
headers[httpHeaderUserAgent] = userAgent
return headers
}
// Used to authenticate the user with Snowflake.
func authenticate(
ctx context.Context,
sc *snowflakeConn,
samlResponse []byte,
proofKey []byte,
) (resp *authResponseMain, err error) {
headers := getHeaders()
clientEnvironment := authRequestClientEnvironment{
Application: sc.cfg.Application,
Os: operatingSystem,
OsVersion: platform,
OCSPMode: sc.cfg.ocspMode(),
}
sessionParameters := make(map[string]interface{})
for k, v := range sc.cfg.Params {
// upper casing to normalize keys
sessionParameters[strings.ToUpper(k)] = *v
}
sessionParameters[sessionClientValidateDefaultParameters] = sc.cfg.ValidateDefaultParameters != ConfigBoolFalse
requestMain := authRequestData{
ClientAppID: clientType,
ClientAppVersion: SnowflakeGoDriverVersion,
AccountName: sc.cfg.Account,
SessionParameters: sessionParameters,
ClientEnvironment: clientEnvironment,
}
switch sc.cfg.Authenticator {
case AuthTypeExternalBrowser:
requestMain.ProofKey = string(proofKey)
requestMain.Token = string(samlResponse)
requestMain.LoginName = sc.cfg.User
requestMain.Authenticator = AuthTypeExternalBrowser.String()
case AuthTypeOAuth:
requestMain.LoginName = sc.cfg.User
requestMain.Authenticator = AuthTypeOAuth.String()
requestMain.Token = sc.cfg.Token
case AuthTypeOkta:
requestMain.RawSAMLResponse = string(samlResponse)
case AuthTypeJwt:
requestMain.Authenticator = AuthTypeJwt.String()
jwtTokenString, err := prepareJWTToken(sc.cfg)
if err != nil {
return nil, err
}
requestMain.Token = jwtTokenString
case AuthTypeSnowflake:
logger.Info("Username and password")
requestMain.LoginName = sc.cfg.User
requestMain.Password = sc.cfg.Password
switch {
case sc.cfg.PasscodeInPassword:
requestMain.ExtAuthnDuoMethod = "passcode"
case sc.cfg.Passcode != "":
requestMain.Passcode = sc.cfg.Passcode
requestMain.ExtAuthnDuoMethod = "passcode"
}
case AuthTypeTokenAccessor:
logger.Info("Bypass authentication using existing token from token accessor")
sessionInfo := authResponseSessionInfo{
DatabaseName: sc.cfg.Database,
SchemaName: sc.cfg.Schema,
WarehouseName: sc.cfg.Warehouse,
RoleName: sc.cfg.Role,
}
token, masterToken, sessionID := sc.cfg.TokenAccessor.GetTokens()
return &authResponseMain{
Token: token,
MasterToken: masterToken,
SessionID: sessionID,
SessionInfo: sessionInfo,
}, nil
}
authRequest := authRequest{
Data: requestMain,
}
params := &url.Values{}
if sc.cfg.Database != "" {
params.Add("databaseName", sc.cfg.Database)
}
if sc.cfg.Schema != "" {
params.Add("schemaName", sc.cfg.Schema)
}
if sc.cfg.Warehouse != "" {
params.Add("warehouse", sc.cfg.Warehouse)
}
if sc.cfg.Role != "" {
params.Add("roleName", sc.cfg.Role)
}
jsonBody, err := json.Marshal(authRequest)
if err != nil {
return
}
logger.WithContext(sc.ctx).Infof("PARAMS for Auth: %v, %v, %v, %v, %v, %v",
params, sc.rest.Protocol, sc.rest.Host, sc.rest.Port, sc.rest.LoginTimeout, sc.cfg.Authenticator.String())
respd, err := sc.rest.FuncPostAuth(ctx, sc.rest, params, headers, jsonBody, sc.rest.LoginTimeout)
if err != nil {
return nil, err
}
if !respd.Success {
logger.Errorln("Authentication FAILED")
sc.rest.TokenAccessor.SetTokens("", "", -1)
code, err := strconv.Atoi(respd.Code)
if err != nil {
code = -1
return nil, err
}
return nil, (&SnowflakeError{
Number: code,
SQLState: SQLStateConnectionRejected,
Message: respd.Message,
}).exceptionTelemetry(sc)
}
logger.Info("Authentication SUCCESS")
sc.rest.TokenAccessor.SetTokens(respd.Data.Token, respd.Data.MasterToken, respd.Data.SessionID)
return &respd.Data, nil
}
// Generate a JWT token in string given the configuration
func prepareJWTToken(config *Config) (string, error) {
pubBytes, err := x509.MarshalPKIXPublicKey(config.PrivateKey.Public())
if err != nil {
return "", err
}
hash := sha256.Sum256(pubBytes)
accountName := strings.ToUpper(config.Account)
userName := strings.ToUpper(config.User)
issueAtTime := time.Now().UTC()
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
"iss": fmt.Sprintf("%s.%s.%s", accountName, userName, "SHA256:"+base64.StdEncoding.EncodeToString(hash[:])),
"sub": fmt.Sprintf("%s.%s", accountName, userName),
"iat": issueAtTime.Unix(),
"nbf": time.Date(2015, 10, 10, 12, 0, 0, 0, time.UTC).Unix(),
"exp": issueAtTime.Add(config.JWTExpireTimeout).Unix(),
})
tokenString, err := token.SignedString(config.PrivateKey)
if err != nil {
return "", err
}
return tokenString, err
}
// Authenticate with sc.cfg
func authenticateWithConfig(sc *snowflakeConn) error {
var authData *authResponseMain
var samlResponse []byte
var proofKey []byte
var err error
logger.Infof("Authenticating via %v", sc.cfg.Authenticator.String())
switch sc.cfg.Authenticator {
case AuthTypeExternalBrowser:
samlResponse, proofKey, err = authenticateByExternalBrowser(
sc.ctx,
sc.rest,
sc.cfg.Authenticator.String(),
sc.cfg.Application,
sc.cfg.Account,
sc.cfg.User,
sc.cfg.Password)
if err != nil {
sc.cleanup()
return err
}
case AuthTypeOkta:
samlResponse, err = authenticateBySAML(
sc.ctx,
sc.rest,
sc.cfg.OktaURL,
sc.cfg.Application,
sc.cfg.Account,
sc.cfg.User,
sc.cfg.Password)
if err != nil {
sc.cleanup()
return err
}
}
authData, err = authenticate(
sc.ctx,
sc,
samlResponse,
proofKey)
if err != nil {
sc.cleanup()
return err
}
sc.populateSessionParameters(authData.Parameters)
sc.ctx = context.WithValue(sc.ctx, SFSessionIDKey, authData.SessionID)
return nil
}