Skip to content

Commit

Permalink
Remove remaining db and discovery AWS SDK imports
Browse files Browse the repository at this point in the history
This eliminates all remaining references to AWS SDK v1 in the
lib/srv/db/... and lib/srv/discovery/... packages.
Most of the changes are simply to remove dead code.
  • Loading branch information
GavinFrazar committed Jan 30, 2025
1 parent b223177 commit c4c78e8
Show file tree
Hide file tree
Showing 37 changed files with 207 additions and 616 deletions.
22 changes: 4 additions & 18 deletions lib/cloud/aws/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,16 @@ import (
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
ecstypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/gravitational/trace"
)

// ConvertRequestFailureError converts `err` into AWS errors to trace errors.
// If the provided error is not a [awserr.RequestFailure] it delegates
// error conversion to [ConvertRequestFailureErrorV2] for SDK v2 compatibility.
// Prefer using [ConvertRequestFailureErrorV2] directly for AWS SDK v2 client
// errors.
func ConvertRequestFailureError(err error) error {
var requestErr awserr.RequestFailure
if errors.As(err, &requestErr) {
return convertRequestFailureErrorFromStatusCode(requestErr.StatusCode(), requestErr)
}
return ConvertRequestFailureErrorV2(err)
}

// ConvertRequestFailureErrorV2 converts AWS SDK v2 errors to trace errors.
// ConvertRequestFailureError converts AWS SDK v2 errors to trace errors.
// If the provided error is not a [awshttp.ResponseError] it returns the error
// without modifying it.
func ConvertRequestFailureErrorV2(err error) error {
func ConvertRequestFailureError(err error) error {
var re *awshttp.ResponseError
if errors.As(err, &re) {
return convertRequestFailureErrorFromStatusCode(re.HTTPStatusCode(), re.Err)
return convertRequestFailureErrorFromStatusCode(re.HTTPStatusCode(), re)
}
return err
}
Expand Down Expand Up @@ -107,5 +93,5 @@ func ConvertIAMError(err error) error {
return trace.BadParameter(*malformedPolicyDocument.Message)
}

return ConvertRequestFailureErrorV2(err)
return ConvertRequestFailureError(err)
}
48 changes: 28 additions & 20 deletions lib/cloud/aws/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ import (
"net/http"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
smithyhttp "github.com/aws/smithy-go/transport/http"
"github.com/gravitational/trace"
"github.com/stretchr/testify/require"
Expand All @@ -37,6 +36,18 @@ func TestConvertRequestFailureError(t *testing.T) {

fakeRequestID := "11111111-2222-3333-3333-333333333334"

newResponseError := func(code int) error {
return &awshttp.ResponseError{
RequestID: fakeRequestID,
ResponseError: &smithyhttp.ResponseError{
Response: &smithyhttp.Response{Response: &http.Response{
StatusCode: code,
}},
Err: trace.Errorf("inner"),
},
}
}

tests := []struct {
name string
inputError error
Expand All @@ -45,49 +56,46 @@ func TestConvertRequestFailureError(t *testing.T) {
}{
{
name: "StatusForbidden",
inputError: awserr.NewRequestFailure(awserr.New("code", "message", nil), http.StatusForbidden, fakeRequestID),
inputError: newResponseError(http.StatusForbidden),
wantIsError: trace.IsAccessDenied,
},
{
name: "StatusConflict",
inputError: awserr.NewRequestFailure(awserr.New("code", "message", nil), http.StatusConflict, fakeRequestID),
inputError: newResponseError(http.StatusConflict),
wantIsError: trace.IsAlreadyExists,
},
{
name: "StatusNotFound",
inputError: awserr.NewRequestFailure(awserr.New("code", "message", nil), http.StatusNotFound, fakeRequestID),
inputError: newResponseError(http.StatusNotFound),
wantIsError: trace.IsNotFound,
},
{
name: "StatusBadRequest",
inputError: awserr.NewRequestFailure(awserr.New("code", "message", nil), http.StatusBadRequest, fakeRequestID),
wantUnmodified: true,
},
{
name: "StatusBadRequest with AccessDeniedException",
inputError: awserr.NewRequestFailure(awserr.New("AccessDeniedException", "message", nil), http.StatusBadRequest, fakeRequestID),
wantIsError: trace.IsAccessDenied,
},
{
name: "not AWS error",
inputError: errors.New("not-aws-error"),
inputError: newResponseError(http.StatusBadRequest),
wantUnmodified: true,
},
{
name: "v2 sdk error",
name: "StatusBadRequest with AccessDeniedException",
inputError: &awshttp.ResponseError{
RequestID: fakeRequestID,
ResponseError: &smithyhttp.ResponseError{
Response: &smithyhttp.Response{Response: &http.Response{
StatusCode: http.StatusNotFound,
StatusCode: http.StatusBadRequest,
}},
Err: trace.Errorf(""),
Err: trace.Errorf("AccessDeniedException"),
},
},
wantIsError: trace.IsNotFound,
wantIsError: trace.IsAccessDenied,
},
{
name: "not AWS error",
inputError: errors.New("not-aws-error"),
wantUnmodified: true,
},
{
name: "v2 sdk error for ecs ClusterNotFoundException",
inputError: &awshttp.ResponseError{
RequestID: fakeRequestID,
ResponseError: &smithyhttp.ResponseError{
Response: &smithyhttp.Response{Response: &http.Response{
StatusCode: http.StatusBadRequest,
Expand Down
22 changes: 4 additions & 18 deletions lib/cloud/aws/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ import (
"strings"

awsv2 "github.com/aws/aws-sdk-go-v2/aws"
stsv2 "github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/gravitational/trace"
)

Expand Down Expand Up @@ -109,23 +106,12 @@ func (i identityBase) String() string {
}

type callerIdentityGetter interface {
GetCallerIdentity(ctx context.Context, params *stsv2.GetCallerIdentityInput, optFns ...func(*stsv2.Options)) (*stsv2.GetCallerIdentityOutput, error)
GetCallerIdentity(ctx context.Context, params *sts.GetCallerIdentityInput, optFns ...func(*sts.Options)) (*sts.GetCallerIdentityOutput, error)
}

// GetIdentityWithClient determines AWS identity of this Teleport process
// using the provided STS API client.
func GetIdentityWithClient(ctx context.Context, stsClient stsiface.STSAPI) (Identity, error) {
out, err := stsClient.GetCallerIdentityWithContext(ctx, &sts.GetCallerIdentityInput{})
if err != nil {
return nil, trace.Wrap(err)
}

return IdentityFromArn(aws.StringValue(out.Arn))
}

// GetIdentityWithClient determines AWS identity of this Teleport process
// using the provided STS API client.
func GetIdentityWithClientV2(ctx context.Context, clt callerIdentityGetter) (Identity, error) {
func GetIdentityWithClient(ctx context.Context, clt callerIdentityGetter) (Identity, error) {
out, err := clt.GetCallerIdentity(ctx, nil)
if err != nil {
return nil, trace.Wrap(err)
Expand Down
19 changes: 3 additions & 16 deletions lib/cloud/aws/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ import (
"context"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
"github.com/stretchr/testify/require"

"github.com/gravitational/teleport/lib/cloud/mocks"
)

// TestGetIdentity verifies parsing of AWS identity received from STS API.
Expand Down Expand Up @@ -79,7 +77,7 @@ func TestGetIdentity(t *testing.T) {
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
identity, err := GetIdentityWithClient(context.Background(), &stsMock{arn: test.inARN})
identity, err := GetIdentityWithClient(context.Background(), &mocks.STSClient{ARN: test.inARN})
require.NoError(t, err)
require.IsType(t, test.outIdentity, identity)
require.Equal(t, test.outName, identity.GetName())
Expand All @@ -89,14 +87,3 @@ func TestGetIdentity(t *testing.T) {
})
}
}

type stsMock struct {
stsiface.STSAPI
arn string
}

func (m *stsMock) GetCallerIdentityWithContext(aws.Context, *sts.GetCallerIdentityInput, ...request.Option) (*sts.GetCallerIdentityOutput, error) {
return &sts.GetCallerIdentityOutput{
Arn: aws.String(m.arn),
}, nil
}
2 changes: 1 addition & 1 deletion lib/cloud/aws/policy_statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package aws
import (
"fmt"

"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/gravitational/trace"

"github.com/gravitational/teleport/api/types"
Expand Down
2 changes: 1 addition & 1 deletion lib/cloud/aws/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/iam"
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/google/go-cmp/cmp"
"github.com/gravitational/trace"
"github.com/stretchr/testify/require"
Expand Down
105 changes: 0 additions & 105 deletions lib/cloud/mocks/aws.go

This file was deleted.

Loading

0 comments on commit c4c78e8

Please sign in to comment.