-
Notifications
You must be signed in to change notification settings - Fork 2
fix: surface DPoP proof rejections instead of "unknown error" (CXP-1002) #202
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
Open
agustin-conductor
wants to merge
1
commit into
main
Choose a base branch
from
bugfix/dpop-nonce-retry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package connector | ||
|
|
||
| import ( | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
| "testing" | ||
| "unicode/utf8" | ||
|
|
||
| "github.com/okta/okta-sdk-golang/v2/okta" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| // getError's five call sites all return its error verbatim, so it has to carry | ||
| // the status and the connector prefix itself. An empty body used to surface as a | ||
| // bare "unexpected end of JSON input". | ||
| func TestGetError(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| oktaResp := func(statusCode int, body string) *okta.Response { | ||
| return &okta.Response{Response: &http.Response{ | ||
| StatusCode: statusCode, | ||
| Status: http.StatusText(statusCode), | ||
| Body: io.NopCloser(strings.NewReader(body)), | ||
| }} | ||
| } | ||
|
|
||
| t.Run("empty body reports the status", func(t *testing.T) { | ||
| t.Parallel() | ||
| _, err := getError(oktaResp(http.StatusBadRequest, "")) | ||
| if err == nil { | ||
| t.Fatal("expected an error") | ||
| } | ||
| for _, want := range []string{"okta-connectorv2:", http.StatusText(http.StatusBadRequest)} { | ||
| if !strings.Contains(err.Error(), want) { | ||
| t.Errorf("error = %q, want it to contain %q", err, want) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| t.Run("non-JSON body is excerpted, not dropped", func(t *testing.T) { | ||
| t.Parallel() | ||
| _, err := getError(oktaResp(http.StatusBadGateway, "<html>gateway</html>")) | ||
| if err == nil { | ||
| t.Fatal("expected an error") | ||
| } | ||
| if !strings.Contains(err.Error(), "gateway") { | ||
| t.Errorf("error = %q, want the body excerpt included", err) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("decodable body still parses", func(t *testing.T) { | ||
| t.Parallel() | ||
| got, err := getError(oktaResp(http.StatusForbidden, `{"errorCode":"E0000006","errorSummary":"denied"}`)) | ||
| if err != nil { | ||
| t.Fatalf("getError: %v", err) | ||
| } | ||
| if got.ErrorCode != AccessDeniedErrorCode { | ||
| t.Errorf("ErrorCode = %q, want %q", got.ErrorCode, AccessDeniedErrorCode) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // A fixed byte offset can split a multi-byte rune; the excerpt lands in an error | ||
| // message and in log fields, so it has to stay valid UTF-8. | ||
| func TestBodyExcerpt_TruncatesOnRuneBoundary(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // A three-byte rune so the byte limit does not divide evenly and the cut lands | ||
| // mid-rune. Two-byte runes would tile it exactly and prove nothing. | ||
| body := strings.Repeat("€", errorBodyExcerptLimit) | ||
| got := bodyExcerpt([]byte(body)) | ||
|
|
||
| if !utf8.ValidString(got) { | ||
| t.Errorf("excerpt is not valid UTF-8: %q", got) | ||
| } | ||
| if !strings.HasSuffix(got, "...") { | ||
| t.Errorf("excerpt = %q, want it marked as truncated", got) | ||
| } | ||
| if short := bodyExcerpt([]byte("€")); short != "€" { | ||
| t.Errorf("short body = %q, want it returned whole", short) | ||
| } | ||
| } | ||
|
|
||
| // Both getError paths must classify by HTTP status, so the same upstream failure | ||
| // gets the same gRPC code whether or not Okta sent a parseable body. The five call | ||
| // sites return this error verbatim, so an unparseable body used to reach the sync | ||
| // as codes.Unknown while a parseable one became PermissionDenied. | ||
| func TestGetError_ClassifiesByHTTPStatus(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| statusCode int | ||
| want codes.Code | ||
| }{ | ||
| {name: "forbidden", statusCode: http.StatusForbidden, want: codes.PermissionDenied}, | ||
| {name: "rate limited", statusCode: http.StatusTooManyRequests, want: codes.Unavailable}, | ||
| {name: "not found", statusCode: http.StatusNotFound, want: codes.NotFound}, | ||
| {name: "bad request", statusCode: http.StatusBadRequest, want: codes.InvalidArgument}, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| resp := &okta.Response{Response: &http.Response{ | ||
| StatusCode: tc.statusCode, | ||
| Status: http.StatusText(tc.statusCode), | ||
| Body: io.NopCloser(strings.NewReader("<html>not json</html>")), | ||
| }} | ||
| _, err := getError(resp) | ||
| if status.Code(err) != tc.want { | ||
| t.Errorf("code = %s, want %s (error: %v)", status.Code(err), tc.want, err) | ||
| } | ||
| // The message still has to carry the prefix and status for the logs. | ||
| for _, sub := range []string{"okta-connectorv2:", http.StatusText(tc.statusCode)} { | ||
| if !strings.Contains(err.Error(), sub) { | ||
| t.Errorf("error = %q, want it to contain %q", err, sub) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Suggestion: the new messages add the status text and the prefix, but still carry no gRPC code. All five call sites (
group.go:211,role.go:456/498,app.go:407/479) return this verbatim, so an unparseable 403 body surfaces ascodes.Unknown, while a parseable one reacheshandleOktaResponseError→WrapErrorsWithRateLimitInfo(GrpcCodeFromHTTPStatus(...))and getsPermissionDenied. Same upstream failure, two different codes depending only on whether Okta sent a body. Consider wrapping withuhttp.WrapErrors(uhttp.GrpcCodeFromHTTPStatus(response.StatusCode), ...)here so the retry/surface decision is driven by the status either way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in fffa61c — the asymmetry was real: an unparseable 403 reached the sync as
codes.Unknownwhile a parseable one becamePermissionDeniedfor the same upstream failure, and a 429 was retryable only if Okta happened to send a body.getErrornow routes both failure paths throughuhttp.WrapErrors(uhttp.GrpcCodeFromHTTPStatus(response.StatusCode), ...)in a smallbodyReadErrorhelper, keeping the connector prefix, the status text, and the body excerpt in the message.TestGetError_ClassifiesByHTTPStatuscovers 403/429/404/400, asserting both the code and that the message still carries prefix and status.