-
Notifications
You must be signed in to change notification settings - Fork 125
/
authokta_test.go
327 lines (292 loc) · 12.2 KB
/
authokta_test.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
// Copyright (c) 2017-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"context"
"errors"
"net/http"
"net/url"
"strconv"
"testing"
"time"
)
func TestUnitPostBackURL(t *testing.T) {
c := `<html><form id="1" action="https://abc.com/"></form></html>`
pbURL, err := postBackURL([]byte(c))
if err != nil {
t.Fatalf("failed to get URL. err: %v, %v", err, c)
}
if pbURL.String() != "https://abc.com/" {
t.Errorf("failed to get URL. got: %v, %v", pbURL, c)
}
c = `<html></html>`
_, err = postBackURL([]byte(c))
if err == nil {
t.Fatalf("should have failed")
}
c = `<html><form id="1"/></html>`
_, err = postBackURL([]byte(c))
if err == nil {
t.Fatalf("should have failed")
}
c = `<html><form id="1" action="https://abc.com//></html>`
_, err = postBackURL([]byte(c))
if err == nil {
t.Fatalf("should have failed")
}
}
func getTestError(_ context.Context, _ *snowflakeRestful, _ *url.URL, _ map[string]string, _ time.Duration) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: &fakeResponseBody{body: []byte{0x12, 0x34}},
}, errors.New("failed to run post method")
}
func getTestAppBadGatewayError(_ context.Context, _ *snowflakeRestful, _ *url.URL, _ map[string]string, _ time.Duration) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusBadGateway,
Body: &fakeResponseBody{body: []byte{0x12, 0x34}},
}, nil
}
func getTestHTMLSuccess(_ context.Context, _ *snowflakeRestful, _ *url.URL, _ map[string]string, _ time.Duration) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: &fakeResponseBody{body: []byte("<htm></html>")},
}, nil
}
func TestUnitPostAuthSAML(t *testing.T) {
sr := &snowflakeRestful{
FuncPost: postTestError,
TokenAccessor: getSimpleTokenAccessor(),
}
var err error
_, err = postAuthSAML(context.Background(), sr, make(map[string]string), []byte{}, 0)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncPost = postTestAppBadGatewayError
_, err = postAuthSAML(context.Background(), sr, make(map[string]string), []byte{}, 0)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncPost = postTestSuccessButInvalidJSON
_, err = postAuthSAML(context.Background(), sr, make(map[string]string), []byte{0x12, 0x34}, 0)
if err == nil {
t.Fatalf("should have failed to post")
}
}
func TestUnitPostAuthOKTA(t *testing.T) {
sr := &snowflakeRestful{
FuncPost: postTestError,
TokenAccessor: getSimpleTokenAccessor(),
}
var err error
_, err = postAuthOKTA(context.Background(), sr, make(map[string]string), []byte{}, "hahah", 0)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncPost = postTestAppBadGatewayError
_, err = postAuthOKTA(context.Background(), sr, make(map[string]string), []byte{}, "hahah", 0)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncPost = postTestSuccessButInvalidJSON
_, err = postAuthOKTA(context.Background(), sr, make(map[string]string), []byte{0x12, 0x34}, "haha", 0)
if err == nil {
t.Fatal("should have failed to run post request after the renewal")
}
}
func TestUnitGetSSO(t *testing.T) {
sr := &snowflakeRestful{
FuncGet: getTestError,
TokenAccessor: getSimpleTokenAccessor(),
}
var err error
_, err = getSSO(context.Background(), sr, &url.Values{}, make(map[string]string), "hahah", 0)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncGet = getTestAppBadGatewayError
_, err = getSSO(context.Background(), sr, &url.Values{}, make(map[string]string), "hahah", 0)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncGet = getTestHTMLSuccess
_, err = getSSO(context.Background(), sr, &url.Values{}, make(map[string]string), "hahah", 0)
if err != nil {
t.Fatalf("failed to get HTML content. err: %v", err)
}
_, err = getSSO(context.Background(), sr, &url.Values{}, make(map[string]string), "invalid!@url$%^", 0)
if err == nil {
t.Fatal("should have failed to parse URL.")
}
}
func postAuthSAMLError(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ time.Duration) (*authResponse, error) {
return &authResponse{}, errors.New("failed to get SAML response")
}
func postAuthSAMLAuthFail(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ time.Duration) (*authResponse, error) {
return &authResponse{
Success: false,
Message: "SAML auth failed",
}, nil
}
func postAuthSAMLAuthFailWithCode(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ time.Duration) (*authResponse, error) {
return &authResponse{
Success: false,
Code: strconv.Itoa(ErrCodeIdpConnectionError),
Message: "SAML auth failed",
}, nil
}
func postAuthSAMLAuthSuccessButInvalidURL(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ time.Duration) (*authResponse, error) {
return &authResponse{
Success: true,
Message: "",
Data: authResponseMain{
TokenURL: "https://1abc.com/token",
SSOURL: "https://2abc.com/sso",
},
}, nil
}
func postAuthSAMLAuthSuccessButInvalidTokenURL(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ time.Duration) (*authResponse, error) {
return &authResponse{
Success: true,
Message: "",
Data: authResponseMain{
TokenURL: "invalid!@url$%^",
SSOURL: "https://abc.com/sso",
},
}, nil
}
func postAuthSAMLAuthSuccessButInvalidSSOURL(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ time.Duration) (*authResponse, error) {
return &authResponse{
Success: true,
Message: "",
Data: authResponseMain{
TokenURL: "https://abc.com/token",
SSOURL: "invalid!@url$%^",
},
}, nil
}
func postAuthSAMLAuthSuccess(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ time.Duration) (*authResponse, error) {
return &authResponse{
Success: true,
Message: "",
Data: authResponseMain{
TokenURL: "https://abc.com/token",
SSOURL: "https://abc.com/sso",
},
}, nil
}
func postAuthOKTAError(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ string, _ time.Duration) (*authOKTAResponse, error) {
return &authOKTAResponse{}, errors.New("failed to get SAML response")
}
func postAuthOKTASuccess(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ string, _ time.Duration) (*authOKTAResponse, error) {
return &authOKTAResponse{}, nil
}
func getSSOError(_ context.Context, _ *snowflakeRestful, _ *url.Values, _ map[string]string, _ string, _ time.Duration) ([]byte, error) {
return []byte{}, errors.New("failed to get SSO html")
}
func getSSOSuccessButInvalidURL(_ context.Context, _ *snowflakeRestful, _ *url.Values, _ map[string]string, _ string, _ time.Duration) ([]byte, error) {
return []byte(`<html><form id="1"/></html>`), nil
}
func getSSOSuccess(_ context.Context, _ *snowflakeRestful, _ *url.Values, _ map[string]string, _ string, _ time.Duration) ([]byte, error) {
return []byte(`<html><form id="1" action="https://abc.com/"></form></html>`), nil
}
func getSSOSuccessButWrongPrefixURL(_ context.Context, _ *snowflakeRestful, _ *url.Values, _ map[string]string, _ string, _ time.Duration) ([]byte, error) {
return []byte(`<html><form id="1" action="https://1abc.com/"></form></html>`), nil
}
func TestUnitAuthenticateBySAML(t *testing.T) {
authenticator := &url.URL{
Scheme: "https",
Host: "abc.com",
}
application := "testapp"
account := "testaccount"
user := "u"
password := "p"
sr := &snowflakeRestful{
Protocol: "https",
Host: "abc.com",
Port: 443,
FuncPostAuthSAML: postAuthSAMLError,
TokenAccessor: getSimpleTokenAccessor(),
}
var err error
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password, ConfigBoolFalse)
assertNotNilF(t, err, "should have failed at FuncPostAuthSAML.")
assertEqualE(t, err.Error(), "failed to get SAML response")
sr.FuncPostAuthSAML = postAuthSAMLAuthFail
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password, ConfigBoolFalse)
assertNotNilF(t, err, "should have failed at FuncPostAuthSAML.")
assertEqualE(t, err.Error(), "strconv.Atoi: parsing \"\": invalid syntax")
sr.FuncPostAuthSAML = postAuthSAMLAuthFailWithCode
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password, ConfigBoolFalse)
assertNotNilF(t, err, "should have failed at FuncPostAuthSAML.")
driverErr, ok := err.(*SnowflakeError)
assertTrueF(t, ok, "should be a SnowflakeError")
assertEqualE(t, driverErr.Number, ErrCodeIdpConnectionError)
sr.FuncPostAuthSAML = postAuthSAMLAuthSuccessButInvalidURL
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password, ConfigBoolFalse)
assertNotNilF(t, err, "should have failed at FuncPostAuthSAML.")
driverErr, ok = err.(*SnowflakeError)
assertTrueF(t, ok, "should be a SnowflakeError")
assertEqualE(t, driverErr.Number, ErrCodeIdpConnectionError)
sr.FuncPostAuthSAML = postAuthSAMLAuthSuccessButInvalidTokenURL
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password, ConfigBoolFalse)
assertNotNilF(t, err, "should have failed at FuncPostAuthSAML.")
assertEqualE(t, err.Error(), "failed to parse token URL. invalid!@url$%^")
sr.FuncPostAuthSAML = postAuthSAMLAuthSuccessButInvalidSSOURL
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password, ConfigBoolFalse)
assertNotNilF(t, err, "should have failed at FuncPostAuthSAML.")
assertEqualE(t, err.Error(), "failed to parse SSO URL. invalid!@url$%^")
sr.FuncPostAuthSAML = postAuthSAMLAuthSuccess
sr.FuncPostAuthOKTA = postAuthOKTAError
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password, ConfigBoolFalse)
assertNotNilF(t, err, "should have failed at FuncPostAuthOKTA.")
assertEqualE(t, err.Error(), "failed to get SAML response")
sr.FuncPostAuthOKTA = postAuthOKTASuccess
sr.FuncGetSSO = getSSOError
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password, ConfigBoolFalse)
assertNotNilF(t, err, "should have failed at FuncGetSSO.")
assertEqualE(t, err.Error(), "failed to get SSO html")
sr.FuncGetSSO = getSSOSuccessButInvalidURL
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password, ConfigBoolFalse)
assertNotNilF(t, err, "should have failed at FuncGetSSO.")
assertHasPrefixE(t, err.Error(), "failed to find action field in HTML response")
sr.FuncGetSSO = getSSOSuccess
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password, ConfigBoolFalse)
assertNilF(t, err, "should have succeeded at FuncGetSSO.")
sr.FuncGetSSO = getSSOSuccessButWrongPrefixURL
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password, ConfigBoolFalse)
assertNotNilF(t, err, "should have failed at FuncGetSSO.")
driverErr, ok = err.(*SnowflakeError)
assertTrueF(t, ok, "should be a SnowflakeError")
assertEqualE(t, driverErr.Number, ErrCodeSSOURLNotMatch)
}
func TestDisableSamlURLCheck(t *testing.T) {
authenticator := &url.URL{
Scheme: "https",
Host: "abc.com",
}
application := "testapp"
account := "testaccount"
user := "u"
password := "p"
sr := &snowflakeRestful{
Protocol: "https",
Host: "abc.com",
Port: 443,
FuncPostAuthSAML: postAuthSAMLAuthSuccess,
FuncPostAuthOKTA: postAuthOKTASuccess,
FuncGetSSO: getSSOSuccessButWrongPrefixURL,
TokenAccessor: getSimpleTokenAccessor(),
}
var err error
// Test for disabled SAML URL check
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password, ConfigBoolTrue)
assertNilF(t, err, "SAML URL check should have disabled.")
// Test for enabled SAML URL check
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password, ConfigBoolFalse)
assertNotNilF(t, err, "should have failed at FuncGetSSO.")
driverErr, ok := err.(*SnowflakeError)
assertTrueF(t, ok, "should be a SnowflakeError")
assertEqualE(t, driverErr.Number, ErrCodeSSOURLNotMatch)
}