-
Notifications
You must be signed in to change notification settings - Fork 36
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
Introduce Go 1.13 compatible error types #42
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,76 @@ | ||
package awsbase | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// CannotAssumeRoleError occurs when AssumeRole cannot complete. | ||
type CannotAssumeRoleError struct { | ||
Config *Config | ||
Err error | ||
} | ||
|
||
func (e CannotAssumeRoleError) Error() string { | ||
if e.Config == nil { | ||
return fmt.Sprintf("cannot assume role: %s", e.Err) | ||
} | ||
|
||
return fmt.Sprintf(`IAM Role (%s) cannot be assumed. | ||
There are a number of possible causes of this - the most common are: | ||
* The credentials used in order to assume the role are invalid | ||
* The credentials do not have appropriate permission to assume the role | ||
* The role ARN is not valid | ||
Error: %s | ||
`, e.Config.AssumeRoleARN, e.Err) | ||
} | ||
|
||
func (e CannotAssumeRoleError) Unwrap() error { | ||
return e.Err | ||
} | ||
|
||
// IsCannotAssumeRoleError returns true if the error contains the CannotAssumeRoleError type. | ||
func IsCannotAssumeRoleError(err error) bool { | ||
var e CannotAssumeRoleError | ||
return errors.As(err, &e) | ||
} | ||
|
||
func (c *Config) NewCannotAssumeRoleError(err error) CannotAssumeRoleError { | ||
return CannotAssumeRoleError{Config: c, Err: err} | ||
} | ||
|
||
// NoValidCredentialSourcesError occurs when all credential lookup methods have been exhausted without results. | ||
type NoValidCredentialSourcesError struct { | ||
Config *Config | ||
Err error | ||
} | ||
|
||
func (e NoValidCredentialSourcesError) Error() string { | ||
if e.Config == nil { | ||
return fmt.Sprintf("no valid credential sources found: %s", e.Err) | ||
} | ||
|
||
return fmt.Sprintf(`no valid credential sources for %s found. | ||
Please see %s | ||
for more information about providing credentials. | ||
Error: %s | ||
`, e.Config.CallerName, e.Config.CallerDocumentationURL, e.Err) | ||
} | ||
|
||
func (e NoValidCredentialSourcesError) Unwrap() error { | ||
return e.Err | ||
} | ||
|
||
// IsNoValidCredentialSourcesError returns true if the error contains the NoValidCredentialSourcesError type. | ||
func IsNoValidCredentialSourcesError(err error) bool { | ||
var e NoValidCredentialSourcesError | ||
return errors.As(err, &e) | ||
} | ||
|
||
func (c *Config) NewNoValidCredentialSourcesError(err error) NoValidCredentialSourcesError { | ||
return NoValidCredentialSourcesError{Config: c, Err: err} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package awsbase | ||
|
||
import ( | ||
"errors" | ||
"io/ioutil" | ||
"os" | ||
"reflect" | ||
|
@@ -79,7 +78,7 @@ func TestGetSession(t *testing.T) { | |
Config: &Config{}, | ||
Description: "no configuration or credentials", | ||
ExpectedError: func(err error) bool { | ||
return errors.Is(err, ErrNoValidCredentialSources) | ||
return IsNoValidCredentialSourcesError(err) | ||
}, | ||
}, | ||
{ | ||
|
@@ -885,7 +884,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey | |
}, | ||
Description: "assume role error", | ||
ExpectedError: func(err error) bool { | ||
return strings.Contains(err.Error(), "The role \"arn:aws:iam::555555555555:role/AssumeRole\" cannot be assumed.") | ||
return IsCannotAssumeRoleError(err) | ||
}, | ||
ExpectedRegion: "us-east-1", | ||
MockStsEndpoints: []*MockEndpoint{ | ||
|
@@ -923,9 +922,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey | |
}, | ||
Description: "session creation error", | ||
ExpectedError: func(err error) bool { | ||
// TODO: Return wrapped error | ||
//return IsAWSErr(err, "CredentialRequiresARNError", "") | ||
return err.Error() == errMsgNoValidCredentialSources | ||
return IsAWSErr(err, "NoCredentialProviders", "") | ||
}, | ||
SharedConfigurationFile: ` | ||
[profile SharedConfigurationProfile] | ||
|
@@ -1050,6 +1047,7 @@ source_profile = SourceSharedCredentials | |
t.Fatalf("unexpected GetSession() error: %s", err) | ||
} | ||
|
||
t.Logf("received expected error: %s", err) | ||
return | ||
} | ||
|
||
|
@@ -1091,23 +1089,23 @@ func TestGetSessionWithAccountIDAndPartition(t *testing.T) { | |
config *Config | ||
expectedAcctID string | ||
expectedPartition string | ||
expectedError string | ||
expectedError bool | ||
}{ | ||
{"StandardProvider_Config", &Config{ | ||
AccessKey: "MockAccessKey", | ||
SecretKey: "MockSecretKey", | ||
Region: "us-west-2", | ||
UserAgentProducts: []*UserAgentProduct{{}}, | ||
StsEndpoint: ts.URL}, | ||
"222222222222", "aws", ""}, | ||
"222222222222", "aws", false}, | ||
{"SkipCredsValidation_Config", &Config{ | ||
AccessKey: "MockAccessKey", | ||
SecretKey: "MockSecretKey", | ||
Region: "us-west-2", | ||
SkipCredsValidation: true, | ||
UserAgentProducts: []*UserAgentProduct{{}}, | ||
StsEndpoint: ts.URL}, | ||
"222222222222", "aws", ""}, | ||
"222222222222", "aws", false}, | ||
{"SkipRequestingAccountId_Config", &Config{ | ||
AccessKey: "MockAccessKey", | ||
SecretKey: "MockSecretKey", | ||
|
@@ -1116,7 +1114,7 @@ func TestGetSessionWithAccountIDAndPartition(t *testing.T) { | |
SkipRequestingAccountId: true, | ||
UserAgentProducts: []*UserAgentProduct{{}}, | ||
StsEndpoint: ts.URL}, | ||
"", "aws", ""}, | ||
"", "aws", false}, | ||
// {"WithAssumeRole", &Config{ | ||
// AccessKey: "MockAccessKey", | ||
// SecretKey: "MockSecretKey", | ||
|
@@ -1130,7 +1128,7 @@ func TestGetSessionWithAccountIDAndPartition(t *testing.T) { | |
Region: "us-west-2", | ||
UserAgentProducts: []*UserAgentProduct{{}}, | ||
StsEndpoint: ts.URL}, | ||
"", "", "No valid credential sources found for AWS."}, | ||
"", "", true}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
|
@@ -1139,27 +1137,28 @@ func TestGetSessionWithAccountIDAndPartition(t *testing.T) { | |
t.Run(tc.desc, func(t *testing.T) { | ||
sess, acctID, part, err := GetSessionWithAccountIDAndPartition(tc.config) | ||
if err != nil { | ||
if tc.expectedError == "" { | ||
t.Fatalf("GetSessionWithAccountIDAndPartition(&Config{...}) should return a valid session, but got the error %s", err) | ||
} else { | ||
if !strings.Contains(err.Error(), tc.expectedError) { | ||
t.Fatalf("GetSession(c) expected error %q and got %q", tc.expectedError, err) | ||
} else { | ||
t.Logf("Found error message %q", err) | ||
} | ||
} | ||
} else { | ||
if sess == nil { | ||
t.Error("GetSession(c) resulted in a nil session") | ||
if !tc.expectedError { | ||
t.Fatalf("expected no error, got: %s", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the more concise errors here |
||
} | ||
|
||
if acctID != tc.expectedAcctID { | ||
t.Errorf("GetSession(c) returned an incorrect AWS account ID, expected %q but got %q", tc.expectedAcctID, acctID) | ||
if !IsNoValidCredentialSourcesError(err) { | ||
t.Fatalf("expected no valid credential sources error, got: %s", err) | ||
} | ||
|
||
if part != tc.expectedPartition { | ||
t.Errorf("GetSession(c) returned an incorrect AWS partition, expected %q but got %q", tc.expectedPartition, part) | ||
} | ||
t.Logf("received expected error: %s", err) | ||
return | ||
} | ||
|
||
if sess == nil { | ||
t.Error("unexpected empty session") | ||
} | ||
|
||
if acctID != tc.expectedAcctID { | ||
t.Errorf("expected account ID (%s), got: %s", tc.expectedAcctID, acctID) | ||
} | ||
|
||
if part != tc.expectedPartition { | ||
t.Errorf("expected partition (%s), got: %s", tc.expectedPartition, part) | ||
} | ||
}) | ||
} | ||
|
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.
👍