Skip to content

Commit

Permalink
Reduced length of IMDS errors to shorter format
Browse files Browse the repository at this point in the history
  • Loading branch information
kvincao authored and Aperocky committed Oct 21, 2024
1 parent ab4144f commit 8b5d4e3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions agent/sdkutil/awserr.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ func GetAwsErrorCode(err error) string {
return errorCode
}

// GetAwsError tries to return AwsError
func GetAwsError(err error) awserr.Error {
var awsErr awserr.Error
if ok := errors.As(err, &awsErr); ok {
return awsErr
}
return nil
}

// resetStopPolicy will reset the stoppolicy error count
func resetStopPolicy(stopPolicy *StopPolicy) {
if stopPolicy != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ func (p *EC2RoleProvider) RetrieveWithContext(ctx context.Context) (credentials.

iprCredentials, err := p.InnerProviders.IPRProvider.RetrieveWithContext(ctx)
if err != nil {
if awsErr := sdkutil.GetAwsError(err); awsErr != nil {
err = awserr.New(awsErr.Code(), awsErr.Message(), nil)
}
err = fmt.Errorf("failed to retrieve instance profile role credentials. Err: %w", err)
p.Log.Error(err)
return iprEmptyCredential, err
Expand Down Expand Up @@ -200,6 +203,11 @@ func (p *EC2RoleProvider) updateEmptyInstanceInformation(ctx context.Context, ss
}

_, err := ssmClient.UpdateInstanceInformationWithContext(ctx, input)
if err != nil {
if awsErr := sdkutil.GetAwsError(err); awsErr != nil {
err = awserr.New(awsErr.Code(), awsErr.Message(), nil)
}
}
return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ func TestEC2RoleProvider_UpdateEmptyInstanceInformation_Success(t *testing.T) {
assert.NoError(t, err)
}

func TestEC2RoleProvider_UpdateEmptyInstanceInformation_ReturnsNestedError(t *testing.T) {
// Arrange
_, ec2RoleProvider := arrangeUpdateInstanceInformation(awserr.New(ErrCodeEC2RoleRequestError, "Instance profile role not found", genericAwsClientError))
defaultEndpoint := "ssm.amazon.com"

// Act
err := ec2RoleProvider.updateEmptyInstanceInformation(context.Background(), defaultEndpoint, &credentials.Credentials{})

// Assert
assert.Error(t, err)
assert.Contains(t, err.Error(), ErrCodeEC2RoleRequestError)
assert.NotContains(t, err.Error(), genericAwsClientError.Error())
}

func TestEC2RoleProvider_IPRCredentials_ReturnsIPRCredentials_With1HrSession(t *testing.T) {
// Arrange
_, ec2RoleProvider := arrangeUpdateInstanceInformation(nil)
Expand Down Expand Up @@ -339,6 +353,30 @@ func TestEC2RoleProvider_Retrieve_ReturnsEmptyCredentials(t *testing.T) {
}
}

func TestEC2RoleProvider_Retrieve_ReturnsNestedErr(t *testing.T) {
testCases := []testCase{
{
testName: "InstanceProfileRoleRetrieveErrorWithOrigError_ReturnsNoOrigError",
iprRetrieveErr: awserr.New(ErrCodeEC2RoleRequestError, "Instance profile role not found", genericAwsClientError),
},
}

for _, j := range testCases {
t.Run(j.testName, func(t *testing.T) {
// Arrange
ec2RoleProvider := arrangeRetrieveEmptyTest(j)

// Act
_, err := ec2RoleProvider.Retrieve()

//Assert
assert.Error(t, err)
assert.Contains(t, err.Error(), ErrCodeEC2RoleRequestError)
assert.NotContains(t, err.Error(), genericAwsClientError.Error())
})
}
}

func TestEC2RoleProvider_RetrieveRemote_ReturnsEmptyCredentials(t *testing.T) {
testCases := []testCase{
{
Expand Down

0 comments on commit 8b5d4e3

Please sign in to comment.