-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
coap-gateway: check if oauth2 errors are temporary
jwt package doesn't propagate correctly error: golang/oauth2#635
- Loading branch information
Showing
9 changed files
with
188 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 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,110 @@ | ||
package message | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/oauth2" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type testError struct { | ||
isTemporary bool | ||
isTimeout bool | ||
grpcStatus status.Status | ||
oauthErr *oauth2.RetrieveError | ||
} | ||
|
||
type oauth2RetrieveError struct { | ||
oauthErr *oauth2.RetrieveError | ||
} | ||
|
||
func (e *oauth2RetrieveError) Unwrap() error { | ||
return e.oauthErr | ||
} | ||
|
||
func (e *oauth2RetrieveError) Error() string { | ||
return "test oauth error" | ||
} | ||
|
||
func (e *testError) Error() string { | ||
return "test error" | ||
} | ||
|
||
func (e *testError) Temporary() bool { | ||
return e.isTemporary | ||
} | ||
|
||
func (e *testError) Timeout() bool { | ||
return e.isTimeout | ||
} | ||
|
||
func (e *testError) GRPCStatus() *status.Status { | ||
return &e.grpcStatus | ||
} | ||
|
||
func (e *testError) Unwrap() error { | ||
return e.oauthErr | ||
} | ||
|
||
func TestIsTempError(t *testing.T) { | ||
type args struct { | ||
err error | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
name: "nil", | ||
args: args{}, | ||
want: false, | ||
}, | ||
{ | ||
name: "not temporary", | ||
args: args{err: fmt.Errorf("err: %w", &testError{grpcStatus: *status.Convert(status.Error(codes.Unauthenticated, "unauthenticated"))})}, | ||
want: false, | ||
}, | ||
{ | ||
name: "any error as temporary", | ||
args: args{err: fmt.Errorf("any error")}, | ||
want: true, | ||
}, | ||
{ | ||
name: "temporary", | ||
args: args{err: fmt.Errorf("err: %w", &testError{isTemporary: true})}, | ||
want: true, | ||
}, | ||
{ | ||
name: "timeout", | ||
args: args{err: fmt.Errorf("err: %w", &testError{isTimeout: true})}, | ||
want: true, | ||
}, | ||
{ | ||
name: "grpcTemporary", | ||
args: args{err: fmt.Errorf("err: %w", &testError{grpcStatus: *status.FromContextError(context.DeadlineExceeded)})}, | ||
want: true, | ||
}, | ||
{ | ||
name: "oauth2Temporary", | ||
args: args{err: fmt.Errorf("err: %w", &oauth2RetrieveError{oauthErr: &oauth2.RetrieveError{Response: &http.Response{StatusCode: http.StatusServiceUnavailable}}})}, | ||
want: true, | ||
}, | ||
{ | ||
name: "oauth2NotTemporary", | ||
args: args{err: fmt.Errorf("err: %w", &oauth2RetrieveError{oauthErr: &oauth2.RetrieveError{Response: &http.Response{StatusCode: http.StatusUnauthorized}}})}, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := IsTempError(tt.args.err) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
This file contains 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 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 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 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 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 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
26 changes: 26 additions & 0 deletions
26
tools/docker/patches/golang_org_x_oauth2_propagate_error.patch
This file contains 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,26 @@ | ||
diff --git a/internal/oauth2.go b/internal/oauth2.go | ||
index c0ab196..cad4b1f 100644 | ||
--- a/internal/oauth2.go | ||
+++ b/internal/oauth2.go | ||
@@ -26,7 +26,7 @@ func ParseKey(key []byte) (*rsa.PrivateKey, error) { | ||
if err != nil { | ||
parsedKey, err = x509.ParsePKCS1PrivateKey(key) | ||
if err != nil { | ||
- return nil, fmt.Errorf("private key should be a PEM or plain PKCS1 or PKCS8; parse error: %v", err) | ||
+ return nil, fmt.Errorf("private key should be a PEM or plain PKCS1 or PKCS8; parse error: %w", err) | ||
} | ||
} | ||
parsed, ok := parsedKey.(*rsa.PrivateKey) | ||
diff --git a/internal/token.go b/internal/token.go | ||
index b4723fc..7b96171 100644 | ||
--- a/internal/token.go | ||
+++ b/internal/token.go | ||
@@ -234,7 +234,7 @@ func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) { | ||
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1<<20)) | ||
r.Body.Close() | ||
if err != nil { | ||
- return nil, fmt.Errorf("oauth2: cannot fetch token: %v", err) | ||
+ return nil, fmt.Errorf("oauth2: cannot fetch token: %w", err) | ||
} | ||
if code := r.StatusCode; code < 200 || code > 299 { | ||
return nil, &RetrieveError{ |