forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
283 lines (260 loc) · 9.27 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
// Copyright (c) 2017-2018 Snowflake Computing Inc. All right reserved.
package gosnowflake
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"runtime"
"strconv"
"strings"
"time"
"github.com/satori/go.uuid"
)
const (
clientType = "Go"
)
const (
authenticatorExternalBrowser = "EXTERNALBROWSER"
authenticatorOAuth = "OAUTH"
authenticatorSnowflake = "SNOWFLAKE"
authenticatorOkta = "OKTA"
)
// 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", clientType, SnowflakeGoDriverVersion, runtime.Version(), platform)
type authRequestClientEnvironment struct {
Application string `json:"APPLICATION"`
Os string `json:"OS"`
OsVersion string `json:"OS_VERSION"`
}
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]string `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"`
ValidityInSeconds time.Duration `json:"validityInSeconds,omitempty"`
MasterToken string `json:"masterToken,omitempty"`
MasterValidityInSeconds time.Duration `json:"masterValidityInSeconds"`
DisplayUserName string `json:"displayUserName"`
ServerVersion string `json:"serverVersion"`
FirstLogin bool `json:"firstLogin"`
RemMeToken string `json:"remMeToken"`
RemMeValidityInSeconds time.Duration `json:"remMeValidityInSeconds"`
HealthCheckInterval time.Duration `json:"healthCheckInterval"`
NewClientForUpgrade string `json:"newClientForUpgrade"`
SessionID int `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(
sr *snowflakeRestful,
params *url.Values,
headers map[string]string,
body []byte,
timeout time.Duration) (
data *authResponse, err error) {
params.Add("requestId", uuid.NewV4().String())
fullURL := fmt.Sprintf(
"%s://%s:%d%s", sr.Protocol, sr.Host, sr.Port,
"/session/v1/login-request?"+params.Encode())
glog.V(2).Infof("full URL: %v", fullURL)
resp, err := sr.FuncPost(context.TODO(), 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 {
glog.V(1).Infof("failed to decode JSON. err: %v", err)
glog.Flush()
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 {
glog.V(1).Infof("failed to extract HTTP response body. err: %v", err)
glog.Flush()
return nil, err
}
glog.V(1).Infof("HTTP: %v, URL: %v, Body: %v", resp.StatusCode, fullURL, b)
glog.V(1).Infof("Header: %v", resp.Header)
glog.Flush()
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["Content-Type"] = headerContentTypeApplicationJSON
headers["accept"] = headerAcceptTypeApplicationSnowflake
headers["User-Agent"] = userAgent
return headers
}
// Used to authenticate the user with Snowflake.
func authenticate(
sc *snowflakeConn,
samlResponse []byte,
proofKey []byte,
) (resp *authResponseMain, err error) {
headers := getHeaders()
clientEnvironment := authRequestClientEnvironment{
Application: sc.cfg.Application,
Os: operatingSystem,
OsVersion: platform,
}
sessionParameters := make(map[string]string)
for k, v := range sc.cfg.Params {
// upper casing to normalize keys
sessionParameters[strings.ToUpper(k)] = *v
}
requestMain := authRequestData{
ClientAppID: clientType,
ClientAppVersion: SnowflakeGoDriverVersion,
AccountName: sc.cfg.Account,
SessionParameters: sessionParameters,
ClientEnvironment: clientEnvironment,
}
authenticator := strings.ToUpper(sc.cfg.Authenticator)
switch authenticator {
case authenticatorExternalBrowser:
requestMain.ProofKey = string(proofKey)
requestMain.Token = string(samlResponse)
requestMain.LoginName = sc.cfg.User
requestMain.Authenticator = authenticatorExternalBrowser
case authenticatorOAuth:
requestMain.LoginName = sc.cfg.User
requestMain.Authenticator = authenticatorOAuth
requestMain.Token = sc.cfg.Token
case authenticatorOkta:
requestMain.RawSAMLResponse = string(samlResponse)
case authenticatorSnowflake:
fallthrough
default:
glog.V(2).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"
}
}
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
}
glog.V(2).Infof("PARAMS for Auth: %v, %v, %v, %v, %v, %v",
params, sc.rest.Protocol, sc.rest.Host, sc.rest.Port, sc.rest.LoginTimeout, sc.rest.Authenticator)
respd, err := sc.rest.FuncPostAuth(sc.rest, params, headers, jsonBody, sc.rest.LoginTimeout)
if err != nil {
return nil, err
}
if !respd.Success {
glog.V(1).Infoln("Authentication FAILED")
glog.Flush()
sc.rest.Token = ""
sc.rest.MasterToken = ""
sc.rest.SessionID = -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,
}
}
glog.V(2).Info("Authentication SUCCESS")
sc.rest.Token = respd.Data.Token
sc.rest.MasterToken = respd.Data.MasterToken
sc.rest.SessionID = respd.Data.SessionID
return &respd.Data, nil
}