-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #245 from xmidt-org/feature/error-interfaces
Feature/error interfaces
- Loading branch information
Showing
7 changed files
with
197 additions
and
84 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// SPDX-FileCopyrightText: 2021 Comcast Cable Communications Management, LLC | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package bascule | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
// ErrorType is an enumeration type for various types of security errors. | ||
// This type can be used to determine more detail about the context of an error. | ||
type ErrorType int | ||
|
||
const ( | ||
// ErrorTypeUnknown indicates an error that didn't specify an ErrorType, | ||
// possibly because the error didn't implement the Error interface in this package. | ||
ErrorTypeUnknown ErrorType = iota | ||
|
||
// ErrorTypeMissingCredentials indicates that no credentials could be found. | ||
// For example, this is the type used when no credentials are present in an HTTP request. | ||
ErrorTypeMissingCredentials | ||
|
||
// ErrorTypeBadCredentials indcates that credentials exist, but they were badly formatted. | ||
// In other words, bascule could not parse the credentials. | ||
ErrorTypeBadCredentials | ||
|
||
// ErrorTypeInvalidCredentials indicates that credentials exist and are properly formatted, | ||
// but they failed validation. Typically, this is due to failed authentication. It can also | ||
// mean that a token's fields are invalid, such as the exp field of a JWT. | ||
ErrorTypeInvalidCredentials | ||
|
||
// ErrorTypeForbidden indicates that a token did not have sufficient privileges to | ||
// perform an operation. | ||
ErrorTypeForbidden | ||
) | ||
|
||
// Error is an optional interface that errors may implement to expose security | ||
// metadata about the error. | ||
type Error interface { | ||
// Type is the ErrorType describing this error. | ||
Type() ErrorType | ||
} | ||
|
||
type typedError struct { | ||
error | ||
et ErrorType | ||
} | ||
|
||
func (te *typedError) Unwrap() error { return te.error } | ||
|
||
func (te *typedError) Type() ErrorType { return te.et } | ||
|
||
// NewTypedError wraps a given error and associates an ErrorType with it. | ||
// The returned error will implement the Error interface in this package, | ||
// and will have an Unwrap method that returns err. | ||
func NewTypedError(err error, et ErrorType) error { | ||
return &typedError{ | ||
error: err, | ||
et: et, | ||
} | ||
} | ||
|
||
// GetErrorType examines err to determine its associated metadata type. If err | ||
// does not implement Error, this function returns ErrorTypeUnknown. | ||
func GetErrorType(err error) ErrorType { | ||
var e Error | ||
if errors.As(err, &e) { | ||
return e.Type() | ||
} | ||
|
||
return ErrorTypeUnknown | ||
} | ||
|
||
// UnsupportedSchemeError indicates that a credentials scheme was not supported | ||
// by a TokenParser. | ||
type UnsupportedSchemeError struct { | ||
// Scheme is the unsupported credential scheme. | ||
Scheme Scheme | ||
} | ||
|
||
// Type tags errors of this type as ErrorTypeBadCredentials. | ||
func (err *UnsupportedSchemeError) Type() ErrorType { return ErrorTypeBadCredentials } | ||
|
||
func (err *UnsupportedSchemeError) Error() string { | ||
var o strings.Builder | ||
o.WriteString(`Unsupported scheme: "`) | ||
o.WriteString(string(err.Scheme)) | ||
o.WriteRune('"') | ||
return o.String() | ||
} | ||
|
||
// BadCredentialsError is a general-purpose error indicating that credentials | ||
// could not be parsed. | ||
type BadCredentialsError struct { | ||
// Raw is the raw value of the credentials that could not be parsed. | ||
Raw string | ||
} | ||
|
||
// Type tags errors of this type as ErrorTypeBadCredentials. | ||
func (err *BadCredentialsError) Type() ErrorType { return ErrorTypeBadCredentials } | ||
|
||
func (err *BadCredentialsError) Error() string { | ||
var o strings.Builder | ||
o.WriteString(`Bad credentials: "`) | ||
o.WriteString(err.Raw) | ||
o.WriteRune('"') | ||
return o.String() | ||
} |
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,65 @@ | ||
package bascule | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type ErrorSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (suite *ErrorSuite) TestUnsupportedSchemeError() { | ||
err := UnsupportedSchemeError{ | ||
Scheme: Scheme("scheme"), | ||
} | ||
|
||
suite.Contains(err.Error(), "scheme") | ||
suite.Equal(ErrorTypeBadCredentials, err.Type()) | ||
} | ||
|
||
func (suite *ErrorSuite) TestBadCredentialsError() { | ||
err := BadCredentialsError{ | ||
Raw: "these are an unparseable, raw credentials", | ||
} | ||
|
||
suite.Contains(err.Error(), "these are an unparseable, raw credentials") | ||
suite.Equal(ErrorTypeBadCredentials, err.Type()) | ||
} | ||
|
||
func (suite *ErrorSuite) TestNewTypedError() { | ||
original := errors.New("original error") | ||
typed := NewTypedError(original, ErrorTypeBadCredentials) | ||
|
||
suite.ErrorIs(typed, original) | ||
suite.Require().Implements((*Error)(nil), typed) | ||
|
||
var e Error | ||
suite.Require().ErrorAs(typed, &e) | ||
suite.Equal( | ||
ErrorTypeBadCredentials, | ||
e.Type(), | ||
) | ||
} | ||
|
||
func (suite *ErrorSuite) TestGetErrorType() { | ||
suite.Run("Unknown", func() { | ||
suite.Equal( | ||
ErrorTypeUnknown, | ||
GetErrorType(errors.New("this is an error that is unknown to bascule")), | ||
) | ||
}) | ||
|
||
suite.Run("ImplementsError", func() { | ||
suite.Equal( | ||
ErrorTypeBadCredentials, | ||
GetErrorType(new(BadCredentialsError)), | ||
) | ||
}) | ||
} | ||
|
||
func TestError(t *testing.T) { | ||
suite.Run(t, new(ErrorSuite)) | ||
} |