Skip to content

Commit

Permalink
fix(aws): Remove ARN helper usage (#4714)
Browse files Browse the repository at this point in the history
Fixes #4689
  • Loading branch information
disq committed Nov 17, 2022
1 parent 02b41f6 commit dde430f
Show file tree
Hide file tree
Showing 34 changed files with 411 additions and 163 deletions.
20 changes: 0 additions & 20 deletions plugins/source/aws/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,26 +141,6 @@ func (c *Client) Services() *Services {
return s
}

// ARN builds an ARN tied to current client's partition, accountID and region
func (c *Client) ARN(service AWSService, idParts ...string) string {
return makeARN(service, c.Partition, c.AccountID, c.Region, idParts...).String()
}

// AccountGlobalARN builds an ARN tied to current client's partition and accountID
func (c *Client) AccountGlobalARN(service AWSService, idParts ...string) string {
return makeARN(service, c.Partition, c.AccountID, "", idParts...).String()
}

// PartitionGlobalARN builds an ARN tied to current client's partition
func (c *Client) PartitionGlobalARN(service AWSService, idParts ...string) string {
return makeARN(service, c.Partition, "", "", idParts...).String()
}

// RegionGlobalARN builds an ARN tied to current client's partition and accountID
func (c *Client) RegionGlobalARN(service AWSService, idParts ...string) string {
return makeARN(service, c.Partition, "", c.Region, idParts...).String()
}

func (c *Client) withPartitionAccountIDAndRegion(partition, accountID, region string) *Client {
return &Client{
Partition: partition,
Expand Down
21 changes: 7 additions & 14 deletions plugins/source/aws/client/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,19 +230,6 @@ func IgnoreNotAvailableRegion(err error) bool {
return false
}

// makeARN creates an ARN using supplied service name, partition, account id, region name and resource id parts.
// Resource id parts are concatenated using forward slash (/).
// See https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html for more information.
func makeARN(service AWSService, partition, accountID, region string, idParts ...string) arn.ARN {
return arn.ARN{
Partition: partition,
Service: string(service),
Region: region,
AccountID: accountID,
Resource: strings.Join(idParts, "/"),
}
}

func resolveARN(service AWSService, resourceID func(resource *schema.Resource) ([]string, error), useRegion, useAccountID bool) schema.ColumnResolver {
return func(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
cl := meta.(*Client)
Expand All @@ -257,7 +244,13 @@ func resolveARN(service AWSService, resourceID func(resource *schema.Resource) (
if useRegion {
region = cl.Region
}
return resource.Set(c.Name, makeARN(service, cl.Partition, accountID, region, idParts...).String())
return resource.Set(c.Name, arn.ARN{
Partition: cl.Partition,
Service: string(service),
Region: region,
AccountID: accountID,
Resource: strings.Join(idParts, "/"),
}.String())
}
}

Expand Down
28 changes: 0 additions & 28 deletions plugins/source/aws/client/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,34 +72,6 @@ func TestResolveARN(t *testing.T) {
}
}

func TestMakeARN(t *testing.T) {
cases := []struct {
service AWSService
region string
idParts []string
expected string
}{
{
service: S3Service,
region: "us-east-1",
idParts: []string{"my-bucket"},
expected: `arn:aws:s3:us-east-1:12345:my-bucket`,
},
{
service: S3Service,
region: "cn-north-1",
//idParts: []string{"my-bucket"},
idParts: []string{"我的桶"},
expected: `arn:aws-cn:s3:cn-north-1:12345:我的桶`,
},
}
for _, tc := range cases {
p, _ := RegionsPartition(tc.region)
res := makeARN(tc.service, p, "12345", tc.region, tc.idParts...).String()
assert.Equal(t, tc.expected, res)
}
}

func TestTagsToMap(t *testing.T) {
type randomType struct {
Key string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package apigateway

import (
"context"
"fmt"

"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/apigateway"
"github.com/aws/aws-sdk-go-v2/service/apigateway/types"
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
Expand All @@ -29,6 +31,11 @@ func fetchApigatewayApiKeys(ctx context.Context, meta schema.ClientMeta, parent
func resolveApigatewayAPIKeyArn(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
cl := meta.(*client.Client)
ak := resource.Item.(types.ApiKey)
arn := cl.RegionGlobalARN(client.ApigatewayService, "/apikeys", *ak.Id)
return resource.Set(c.Name, arn)
return resource.Set(c.Name, arn.ARN{
Partition: cl.Partition,
Service: string(client.ApigatewayService),
Region: cl.Region,
AccountID: "",
Resource: fmt.Sprintf("/apikeys/%s", aws.ToString(ak.Id)),
}.String())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package apigateway

import (
"context"
"fmt"

"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/apigateway"
"github.com/aws/aws-sdk-go-v2/service/apigateway/types"
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
Expand All @@ -25,6 +28,11 @@ func fetchApigatewayClientCertificates(ctx context.Context, meta schema.ClientMe
func resolveApigatewayClientCertificateArn(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
cl := meta.(*client.Client)
cert := resource.Item.(types.ClientCertificate)
arn := cl.RegionGlobalARN(client.ApigatewayService, "/clientcertificates", *cert.ClientCertificateId)
return resource.Set(c.Name, arn)
return resource.Set(c.Name, arn.ARN{
Partition: cl.Partition,
Service: string(client.ApigatewayService),
Region: cl.Region,
AccountID: "",
Resource: fmt.Sprintf("/clientcertificates/%s", aws.ToString(cert.ClientCertificateId)),
}.String())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package apigateway

import (
"context"
"fmt"

"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/apigateway"
"github.com/aws/aws-sdk-go-v2/service/apigateway/types"
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
Expand All @@ -25,8 +28,13 @@ func fetchApigatewayDomainNames(ctx context.Context, meta schema.ClientMeta, par
func resolveApigatewayDomainNameArn(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
cl := meta.(*client.Client)
domain := resource.Item.(types.DomainName)
arn := cl.RegionGlobalARN(client.ApigatewayService, domainNameIDPart, *domain.DomainName)
return resource.Set(c.Name, arn)
return resource.Set(c.Name, arn.ARN{
Partition: cl.Partition,
Service: string(client.ApigatewayService),
Region: cl.Region,
AccountID: "",
Resource: fmt.Sprintf("/domainnames/%s", aws.ToString(domain.DomainName)),
}.String())
}
func fetchApigatewayDomainNameBasePathMappings(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
r := parent.Item.(types.DomainName)
Expand All @@ -46,6 +54,11 @@ func resolveApigatewayDomainNameBasePathMappingArn(ctx context.Context, meta sch
cl := meta.(*client.Client)
domain := resource.Parent.Item.(types.DomainName)
mapping := resource.Item.(types.BasePathMapping)
arn := cl.RegionGlobalARN(client.ApigatewayService, domainNameIDPart, *domain.DomainName, "basepathmappings", *mapping.BasePath)
return resource.Set(c.Name, arn)
return resource.Set(c.Name, arn.ARN{
Partition: cl.Partition,
Service: string(client.ApigatewayService),
Region: cl.Region,
AccountID: "",
Resource: fmt.Sprintf("/domainnames/%s/basepathmappings/%s", aws.ToString(domain.DomainName), aws.ToString(mapping.BasePath)),
}.String())
}
92 changes: 72 additions & 20 deletions plugins/source/aws/resources/services/apigateway/rest_apis_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package apigateway

import (
"context"
"fmt"

"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/apigateway"
"github.com/aws/aws-sdk-go-v2/service/apigateway/types"
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
Expand All @@ -26,8 +28,13 @@ func fetchApigatewayRestApis(ctx context.Context, meta schema.ClientMeta, parent
func resolveApigatewayRestAPIArn(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
cl := meta.(*client.Client)
rapi := resource.Item.(types.RestApi)
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id)
return resource.Set(c.Name, arn)
return resource.Set(c.Name, arn.ARN{
Partition: cl.Partition,
Service: string(client.ApigatewayService),
Region: cl.Region,
AccountID: "",
Resource: fmt.Sprintf("/restapis/%s", aws.ToString(rapi.Id)),
}.String())
}
func fetchApigatewayRestApiAuthorizers(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
r := parent.Item.(types.RestApi)
Expand All @@ -54,8 +61,13 @@ func resolveApigatewayRestAPIAuthorizerArn(ctx context.Context, meta schema.Clie
cl := meta.(*client.Client)
auth := resource.Item.(types.Authorizer)
rapi := resource.Parent.Item.(types.RestApi)
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "authorizers", *auth.Id)
return resource.Set(c.Name, arn)
return resource.Set(c.Name, arn.ARN{
Partition: cl.Partition,
Service: string(client.ApigatewayService),
Region: cl.Region,
AccountID: "",
Resource: fmt.Sprintf("/restapis/%s/authorizers/%s", aws.ToString(rapi.Id), aws.ToString(auth.Id)),
}.String())
}
func fetchApigatewayRestApiDeployments(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
r := parent.Item.(types.RestApi)
Expand All @@ -78,8 +90,13 @@ func resolveApigatewayRestAPIDeploymentArn(ctx context.Context, meta schema.Clie
cl := meta.(*client.Client)
d := resource.Item.(types.Deployment)
rapi := resource.Parent.Item.(types.RestApi)
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "deployments", *d.Id)
return resource.Set(c.Name, arn)
return resource.Set(c.Name, arn.ARN{
Partition: cl.Partition,
Service: string(client.ApigatewayService),
Region: cl.Region,
AccountID: "",
Resource: fmt.Sprintf("/restapis/%s/deployments/%s", aws.ToString(rapi.Id), aws.ToString(d.Id)),
}.String())
}
func fetchApigatewayRestApiDocumentationParts(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
r := parent.Item.(types.RestApi)
Expand All @@ -106,8 +123,13 @@ func resolveApigatewayRestAPIDocumentationPartArn(ctx context.Context, meta sche
cl := meta.(*client.Client)
d := resource.Item.(types.DocumentationPart)
rapi := resource.Parent.Item.(types.RestApi)
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "documentation/parts", *d.Id)
return resource.Set(c.Name, arn)
return resource.Set(c.Name, arn.ARN{
Partition: cl.Partition,
Service: string(client.ApigatewayService),
Region: cl.Region,
AccountID: "",
Resource: fmt.Sprintf("/restapis/%s/documentation/parts/%s", aws.ToString(rapi.Id), aws.ToString(d.Id)),
}.String())
}
func fetchApigatewayRestApiDocumentationVersions(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
r := parent.Item.(types.RestApi)
Expand All @@ -134,8 +156,13 @@ func resolveApigatewayRestAPIDocumentationVersionArn(ctx context.Context, meta s
cl := meta.(*client.Client)
v := resource.Item.(types.DocumentationVersion)
rapi := resource.Parent.Item.(types.RestApi)
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "documentation/versions", *v.Version)
return resource.Set(c.Name, arn)
return resource.Set(c.Name, arn.ARN{
Partition: cl.Partition,
Service: string(client.ApigatewayService),
Region: cl.Region,
AccountID: "",
Resource: fmt.Sprintf("/restapis/%s/documentation/versions/%s", aws.ToString(rapi.Id), aws.ToString(v.Version)),
}.String())
}
func fetchApigatewayRestApiGatewayResponses(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
r := parent.Item.(types.RestApi)
Expand All @@ -162,8 +189,13 @@ func resolveApigatewayRestAPIGatewayResponseArn(ctx context.Context, meta schema
cl := meta.(*client.Client)
r := resource.Item.(types.GatewayResponse)
rapi := resource.Parent.Item.(types.RestApi)
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "gatewayresponses", string(r.ResponseType))
return resource.Set(c.Name, arn)
return resource.Set(c.Name, arn.ARN{
Partition: cl.Partition,
Service: string(client.ApigatewayService),
Region: cl.Region,
AccountID: "",
Resource: fmt.Sprintf("/restapis/%s/gatewayresponses/%s", aws.ToString(rapi.Id), string(r.ResponseType)),
}.String())
}
func fetchApigatewayRestApiModels(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
r := parent.Item.(types.RestApi)
Expand All @@ -186,8 +218,13 @@ func resolveApigatewayRestAPIModelArn(ctx context.Context, meta schema.ClientMet
cl := meta.(*client.Client)
m := resource.Item.(types.Model)
rapi := resource.Parent.Item.(types.RestApi)
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "models", *m.Name)
return resource.Set(c.Name, arn)
return resource.Set(c.Name, arn.ARN{
Partition: cl.Partition,
Service: string(client.ApigatewayService),
Region: cl.Region,
AccountID: "",
Resource: fmt.Sprintf("/restapis/%s/models/%s", aws.ToString(rapi.Id), aws.ToString(m.Name)),
}.String())
}
func resolveApigatewayRestAPIModelModelTemplate(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
r := resource.Item.(types.Model)
Expand Down Expand Up @@ -245,8 +282,13 @@ func resolveApigatewayRestAPIRequestValidatorArn(ctx context.Context, meta schem
cl := meta.(*client.Client)
r := resource.Item.(types.RequestValidator)
rapi := resource.Parent.Item.(types.RestApi)
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "requestvalidators", *r.Id)
return resource.Set(c.Name, arn)
return resource.Set(c.Name, arn.ARN{
Partition: cl.Partition,
Service: string(client.ApigatewayService),
Region: cl.Region,
AccountID: "",
Resource: fmt.Sprintf("/restapis/%s/requestvalidators/%s", aws.ToString(rapi.Id), aws.ToString(r.Id)),
}.String())
}
func fetchApigatewayRestApiResources(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
r := parent.Item.(types.RestApi)
Expand All @@ -269,8 +311,13 @@ func resolveApigatewayRestAPIResourceArn(ctx context.Context, meta schema.Client
cl := meta.(*client.Client)
r := resource.Item.(types.Resource)
rapi := resource.Parent.Item.(types.RestApi)
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "resources", *r.Id)
return resource.Set(c.Name, arn)
return resource.Set(c.Name, arn.ARN{
Partition: cl.Partition,
Service: string(client.ApigatewayService),
Region: cl.Region,
AccountID: "",
Resource: fmt.Sprintf("/restapis/%s/resources/%s", aws.ToString(rapi.Id), aws.ToString(r.Id)),
}.String())
}
func fetchApigatewayRestApiStages(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
r := parent.Item.(types.RestApi)
Expand All @@ -293,6 +340,11 @@ func resolveApigatewayRestAPIStageArn(ctx context.Context, meta schema.ClientMet
cl := meta.(*client.Client)
s := resource.Item.(types.Stage)
rapi := resource.Parent.Item.(types.RestApi)
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "stages", *s.StageName)
return resource.Set(c.Name, arn)
return resource.Set(c.Name, arn.ARN{
Partition: cl.Partition,
Service: string(client.ApigatewayService),
Region: cl.Region,
AccountID: "",
Resource: fmt.Sprintf("/restapis/%s/stages/%s", aws.ToString(rapi.Id), aws.ToString(s.StageName)),
}.String())
}
7 changes: 0 additions & 7 deletions plugins/source/aws/resources/services/apigateway/types.go

This file was deleted.

0 comments on commit dde430f

Please sign in to comment.