|
| 1 | +package adapters |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/aws/aws-sdk-go-v2/service/apigateway" |
| 9 | + "github.com/aws/aws-sdk-go-v2/service/apigateway/types" |
| 10 | + "github.com/overmindtech/aws-source/adapterhelpers" |
| 11 | + "github.com/overmindtech/sdp-go" |
| 12 | +) |
| 13 | + |
| 14 | +// convertGetAuthorizerOutputToAuthorizer converts a GetAuthorizerOutput to an Authorizer |
| 15 | +func convertGetAuthorizerOutputToAuthorizer(output *apigateway.GetAuthorizerOutput) *types.Authorizer { |
| 16 | + return &types.Authorizer{ |
| 17 | + Id: output.Id, |
| 18 | + Name: output.Name, |
| 19 | + Type: output.Type, |
| 20 | + ProviderARNs: output.ProviderARNs, |
| 21 | + AuthType: output.AuthType, |
| 22 | + AuthorizerUri: output.AuthorizerUri, |
| 23 | + AuthorizerCredentials: output.AuthorizerCredentials, |
| 24 | + IdentitySource: output.IdentitySource, |
| 25 | + IdentityValidationExpression: output.IdentityValidationExpression, |
| 26 | + AuthorizerResultTtlInSeconds: output.AuthorizerResultTtlInSeconds, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func authorizerOutputMapper(scope string, awsItem *types.Authorizer) (*sdp.Item, error) { |
| 31 | + attributes, err := adapterhelpers.ToAttributesWithExclude(awsItem, "tags") |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + |
| 36 | + item := sdp.Item{ |
| 37 | + Type: "apigateway-authorizer", |
| 38 | + UniqueAttribute: "Id", |
| 39 | + Attributes: attributes, |
| 40 | + Scope: scope, |
| 41 | + } |
| 42 | + |
| 43 | + return &item, nil |
| 44 | +} |
| 45 | + |
| 46 | +func NewAPIGatewayAuthorizerAdapter(client *apigateway.Client, accountID string, region string) *adapterhelpers.GetListAdapter[*types.Authorizer, *apigateway.Client, *apigateway.Options] { |
| 47 | + return &adapterhelpers.GetListAdapter[*types.Authorizer, *apigateway.Client, *apigateway.Options]{ |
| 48 | + ItemType: "apigateway-authorizer", |
| 49 | + Client: client, |
| 50 | + AccountID: accountID, |
| 51 | + Region: region, |
| 52 | + AdapterMetadata: authorizerAdapterMetadata, |
| 53 | + GetFunc: func(ctx context.Context, client *apigateway.Client, scope, query string) (*types.Authorizer, error) { |
| 54 | + f := strings.Split(query, "/") |
| 55 | + if len(f) != 2 { |
| 56 | + return nil, &sdp.QueryError{ |
| 57 | + ErrorType: sdp.QueryError_NOTFOUND, |
| 58 | + ErrorString: fmt.Sprintf("query must be in the format of: the rest-api-id/authorizer-id, but found: %s", query), |
| 59 | + } |
| 60 | + } |
| 61 | + out, err := client.GetAuthorizer(ctx, &apigateway.GetAuthorizerInput{ |
| 62 | + RestApiId: &f[0], |
| 63 | + AuthorizerId: &f[1], |
| 64 | + }) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + return convertGetAuthorizerOutputToAuthorizer(out), nil |
| 69 | + }, |
| 70 | + DisableList: true, |
| 71 | + SearchFunc: func(ctx context.Context, client *apigateway.Client, scope string, query string) ([]*types.Authorizer, error) { |
| 72 | + f := strings.Split(query, "/") |
| 73 | + var restAPIID string |
| 74 | + var name string |
| 75 | + |
| 76 | + switch len(f) { |
| 77 | + case 1: |
| 78 | + restAPIID = f[0] |
| 79 | + case 2: |
| 80 | + restAPIID = f[0] |
| 81 | + name = f[1] |
| 82 | + default: |
| 83 | + return nil, &sdp.QueryError{ |
| 84 | + ErrorType: sdp.QueryError_NOTFOUND, |
| 85 | + ErrorString: fmt.Sprintf( |
| 86 | + "query must be in the format of: the rest-api-id/authorizer-id or rest-api-id, but found: %s", |
| 87 | + query, |
| 88 | + ), |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + out, err := client.GetAuthorizers(ctx, &apigateway.GetAuthorizersInput{ |
| 93 | + RestApiId: &restAPIID, |
| 94 | + }) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + var items []*types.Authorizer |
| 100 | + for _, authorizer := range out.Items { |
| 101 | + if name != "" && strings.Contains(*authorizer.Name, name) { |
| 102 | + items = append(items, &authorizer) |
| 103 | + } else { |
| 104 | + items = append(items, &authorizer) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return items, nil |
| 109 | + }, |
| 110 | + ItemMapper: func(_, scope string, awsItem *types.Authorizer) (*sdp.Item, error) { |
| 111 | + return authorizerOutputMapper(scope, awsItem) |
| 112 | + }, |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +var authorizerAdapterMetadata = Metadata.Register(&sdp.AdapterMetadata{ |
| 117 | + Type: "apigateway-authorizer", |
| 118 | + DescriptiveName: "API Gateway Authorizer", |
| 119 | + Category: sdp.AdapterCategory_ADAPTER_CATEGORY_SECURITY, |
| 120 | + SupportedQueryMethods: &sdp.AdapterSupportedQueryMethods{ |
| 121 | + Get: true, |
| 122 | + List: true, |
| 123 | + Search: true, |
| 124 | + GetDescription: "Get an API Gateway Authorizer by its rest API ID and ID: rest-api-id/authorizer-id", |
| 125 | + ListDescription: "List all API Gateway Authorizers", |
| 126 | + SearchDescription: "Search for API Gateway Authorizers by their rest API ID or with rest API ID and their name: rest-api-id/authorizer-name", |
| 127 | + }, |
| 128 | +}) |
0 commit comments