Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for new OKTA OIE and retry 429 error code #731

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions authokta.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type authOKTARequest struct {
}

type authOKTAResponse struct {
CookieToken string `json:"cookieToken"`
CookieToken string `json:"cookieToken"`
SessionToken string `json:"sessionToken"`
}

/*
Expand Down Expand Up @@ -137,7 +138,13 @@ func authenticateBySAML(
logger.WithContext(ctx).Info("step 4: query IDP URL snowflake app to get SAML response")
params = &url.Values{}
params.Add("RelayState", "/some/deep/link")
params.Add("onetimetoken", respa.CookieToken)
var oneTimeToken string
if respa.SessionToken != "" {
oneTimeToken = respa.SessionToken
} else {
oneTimeToken = respa.CookieToken
}
params.Add("onetimetoken", oneTimeToken)

headers = make(map[string]string)
headers[httpHeaderAccept] = "*/*"
Expand Down
2 changes: 1 addition & 1 deletion retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (r *retryHTTP) execute() (res *http.Response, err error) {
logger.WithContext(r.ctx).Warningf(
"failed http connection. no response is returned. err: %v. retrying...\n", err)
} else {
if res.StatusCode == http.StatusOK || r.raise4XX && res != nil && res.StatusCode >= 400 && res.StatusCode < 500 {
if res.StatusCode == http.StatusOK || r.raise4XX && res != nil && res.StatusCode >= 400 && res.StatusCode < 500 && res.StatusCode != 429 {
// exit if success
// or
// abort connection if raise4XX flag is enabled and the range of HTTP status code are 4XX.
Expand Down
41 changes: 36 additions & 5 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ func (b *fakeResponseBody) Close() error {
}

type fakeHTTPClient struct {
cnt int // number of retry
success bool // return success after retry in cnt times
timeout bool // timeout
body []byte // return body
cnt int // number of retry
success bool // return success after retry in cnt times
timeout bool // timeout
body []byte // return body
statusCode int // status code
}

func (c *fakeHTTPClient) Do(req *http.Request) (*http.Response, error) {
Expand All @@ -71,7 +72,11 @@ func (c *fakeHTTPClient) Do(req *http.Request) (*http.Response, error) {
timeout: true,
}
}
retcode = 0
if c.statusCode != 0 {
retcode = c.statusCode
} else {
retcode = 0
}
}

ret := &http.Response{
Expand Down Expand Up @@ -263,3 +268,29 @@ func TestRetryLoginRequest(t *testing.T) {
t.Fatalf("no retry counter should be attached: %v", retryCounterKey)
}
}

func TestLoginRetry429(t *testing.T) {
client := &fakeHTTPClient{
cnt: 3,
success: true,
statusCode: 429,
}
urlPtr, err := url.Parse("https://fakeaccountretrylogin.snowflakecomputing.com:443/login-request?request_id=testid")
if err != nil {
t.Fatal("failed to parse the test URL")
}
_, err = newRetryHTTP(context.TODO(),
client,
fakeRequestFunc, urlPtr, make(map[string]string), 60*time.Second).doRaise4XX(true).doPost().setBody([]byte{0}).execute() // enable doRaise4XXX
if err != nil {
t.Fatal("failed to run retry")
}
var values url.Values
values, err = url.ParseQuery(urlPtr.RawQuery)
if err != nil {
t.Fatalf("failed to fail to parse the URL: %v", err)
}
if values.Get(retryCounterKey) != "" {
t.Fatalf("no retry counter should be attached: %v", retryCounterKey)
}
}