Skip to content

Commit dde430f

Browse files
authored
fix(aws): Remove ARN helper usage (#4714)
Fixes #4689
1 parent 02b41f6 commit dde430f

34 files changed

Lines changed: 411 additions & 163 deletions

plugins/source/aws/client/client.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,26 +141,6 @@ func (c *Client) Services() *Services {
141141
return s
142142
}
143143

144-
// ARN builds an ARN tied to current client's partition, accountID and region
145-
func (c *Client) ARN(service AWSService, idParts ...string) string {
146-
return makeARN(service, c.Partition, c.AccountID, c.Region, idParts...).String()
147-
}
148-
149-
// AccountGlobalARN builds an ARN tied to current client's partition and accountID
150-
func (c *Client) AccountGlobalARN(service AWSService, idParts ...string) string {
151-
return makeARN(service, c.Partition, c.AccountID, "", idParts...).String()
152-
}
153-
154-
// PartitionGlobalARN builds an ARN tied to current client's partition
155-
func (c *Client) PartitionGlobalARN(service AWSService, idParts ...string) string {
156-
return makeARN(service, c.Partition, "", "", idParts...).String()
157-
}
158-
159-
// RegionGlobalARN builds an ARN tied to current client's partition and accountID
160-
func (c *Client) RegionGlobalARN(service AWSService, idParts ...string) string {
161-
return makeARN(service, c.Partition, "", c.Region, idParts...).String()
162-
}
163-
164144
func (c *Client) withPartitionAccountIDAndRegion(partition, accountID, region string) *Client {
165145
return &Client{
166146
Partition: partition,

plugins/source/aws/client/helpers.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -230,19 +230,6 @@ func IgnoreNotAvailableRegion(err error) bool {
230230
return false
231231
}
232232

233-
// makeARN creates an ARN using supplied service name, partition, account id, region name and resource id parts.
234-
// Resource id parts are concatenated using forward slash (/).
235-
// See https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html for more information.
236-
func makeARN(service AWSService, partition, accountID, region string, idParts ...string) arn.ARN {
237-
return arn.ARN{
238-
Partition: partition,
239-
Service: string(service),
240-
Region: region,
241-
AccountID: accountID,
242-
Resource: strings.Join(idParts, "/"),
243-
}
244-
}
245-
246233
func resolveARN(service AWSService, resourceID func(resource *schema.Resource) ([]string, error), useRegion, useAccountID bool) schema.ColumnResolver {
247234
return func(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
248235
cl := meta.(*Client)
@@ -257,7 +244,13 @@ func resolveARN(service AWSService, resourceID func(resource *schema.Resource) (
257244
if useRegion {
258245
region = cl.Region
259246
}
260-
return resource.Set(c.Name, makeARN(service, cl.Partition, accountID, region, idParts...).String())
247+
return resource.Set(c.Name, arn.ARN{
248+
Partition: cl.Partition,
249+
Service: string(service),
250+
Region: region,
251+
AccountID: accountID,
252+
Resource: strings.Join(idParts, "/"),
253+
}.String())
261254
}
262255
}
263256

plugins/source/aws/client/helpers_test.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,34 +72,6 @@ func TestResolveARN(t *testing.T) {
7272
}
7373
}
7474

75-
func TestMakeARN(t *testing.T) {
76-
cases := []struct {
77-
service AWSService
78-
region string
79-
idParts []string
80-
expected string
81-
}{
82-
{
83-
service: S3Service,
84-
region: "us-east-1",
85-
idParts: []string{"my-bucket"},
86-
expected: `arn:aws:s3:us-east-1:12345:my-bucket`,
87-
},
88-
{
89-
service: S3Service,
90-
region: "cn-north-1",
91-
//idParts: []string{"my-bucket"},
92-
idParts: []string{"我的桶"},
93-
expected: `arn:aws-cn:s3:cn-north-1:12345:我的桶`,
94-
},
95-
}
96-
for _, tc := range cases {
97-
p, _ := RegionsPartition(tc.region)
98-
res := makeARN(tc.service, p, "12345", tc.region, tc.idParts...).String()
99-
assert.Equal(t, tc.expected, res)
100-
}
101-
}
102-
10375
func TestTagsToMap(t *testing.T) {
10476
type randomType struct {
10577
Key string

plugins/source/aws/resources/services/apigateway/api_keys_fetch.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package apigateway
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/aws/aws-sdk-go-v2/aws"
8+
"github.com/aws/aws-sdk-go-v2/aws/arn"
79
"github.com/aws/aws-sdk-go-v2/service/apigateway"
810
"github.com/aws/aws-sdk-go-v2/service/apigateway/types"
911
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
@@ -29,6 +31,11 @@ func fetchApigatewayApiKeys(ctx context.Context, meta schema.ClientMeta, parent
2931
func resolveApigatewayAPIKeyArn(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
3032
cl := meta.(*client.Client)
3133
ak := resource.Item.(types.ApiKey)
32-
arn := cl.RegionGlobalARN(client.ApigatewayService, "/apikeys", *ak.Id)
33-
return resource.Set(c.Name, arn)
34+
return resource.Set(c.Name, arn.ARN{
35+
Partition: cl.Partition,
36+
Service: string(client.ApigatewayService),
37+
Region: cl.Region,
38+
AccountID: "",
39+
Resource: fmt.Sprintf("/apikeys/%s", aws.ToString(ak.Id)),
40+
}.String())
3441
}

plugins/source/aws/resources/services/apigateway/client_certificates_fetch.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package apigateway
22

33
import (
44
"context"
5+
"fmt"
56

7+
"github.com/aws/aws-sdk-go-v2/aws"
8+
"github.com/aws/aws-sdk-go-v2/aws/arn"
69
"github.com/aws/aws-sdk-go-v2/service/apigateway"
710
"github.com/aws/aws-sdk-go-v2/service/apigateway/types"
811
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
@@ -25,6 +28,11 @@ func fetchApigatewayClientCertificates(ctx context.Context, meta schema.ClientMe
2528
func resolveApigatewayClientCertificateArn(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
2629
cl := meta.(*client.Client)
2730
cert := resource.Item.(types.ClientCertificate)
28-
arn := cl.RegionGlobalARN(client.ApigatewayService, "/clientcertificates", *cert.ClientCertificateId)
29-
return resource.Set(c.Name, arn)
31+
return resource.Set(c.Name, arn.ARN{
32+
Partition: cl.Partition,
33+
Service: string(client.ApigatewayService),
34+
Region: cl.Region,
35+
AccountID: "",
36+
Resource: fmt.Sprintf("/clientcertificates/%s", aws.ToString(cert.ClientCertificateId)),
37+
}.String())
3038
}

plugins/source/aws/resources/services/apigateway/domain_names_fetch.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package apigateway
22

33
import (
44
"context"
5+
"fmt"
56

7+
"github.com/aws/aws-sdk-go-v2/aws"
8+
"github.com/aws/aws-sdk-go-v2/aws/arn"
69
"github.com/aws/aws-sdk-go-v2/service/apigateway"
710
"github.com/aws/aws-sdk-go-v2/service/apigateway/types"
811
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
@@ -25,8 +28,13 @@ func fetchApigatewayDomainNames(ctx context.Context, meta schema.ClientMeta, par
2528
func resolveApigatewayDomainNameArn(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
2629
cl := meta.(*client.Client)
2730
domain := resource.Item.(types.DomainName)
28-
arn := cl.RegionGlobalARN(client.ApigatewayService, domainNameIDPart, *domain.DomainName)
29-
return resource.Set(c.Name, arn)
31+
return resource.Set(c.Name, arn.ARN{
32+
Partition: cl.Partition,
33+
Service: string(client.ApigatewayService),
34+
Region: cl.Region,
35+
AccountID: "",
36+
Resource: fmt.Sprintf("/domainnames/%s", aws.ToString(domain.DomainName)),
37+
}.String())
3038
}
3139
func fetchApigatewayDomainNameBasePathMappings(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
3240
r := parent.Item.(types.DomainName)
@@ -46,6 +54,11 @@ func resolveApigatewayDomainNameBasePathMappingArn(ctx context.Context, meta sch
4654
cl := meta.(*client.Client)
4755
domain := resource.Parent.Item.(types.DomainName)
4856
mapping := resource.Item.(types.BasePathMapping)
49-
arn := cl.RegionGlobalARN(client.ApigatewayService, domainNameIDPart, *domain.DomainName, "basepathmappings", *mapping.BasePath)
50-
return resource.Set(c.Name, arn)
57+
return resource.Set(c.Name, arn.ARN{
58+
Partition: cl.Partition,
59+
Service: string(client.ApigatewayService),
60+
Region: cl.Region,
61+
AccountID: "",
62+
Resource: fmt.Sprintf("/domainnames/%s/basepathmappings/%s", aws.ToString(domain.DomainName), aws.ToString(mapping.BasePath)),
63+
}.String())
5164
}

plugins/source/aws/resources/services/apigateway/rest_apis_fetch.go

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package apigateway
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/aws/aws-sdk-go-v2/aws"
8+
"github.com/aws/aws-sdk-go-v2/aws/arn"
79
"github.com/aws/aws-sdk-go-v2/service/apigateway"
810
"github.com/aws/aws-sdk-go-v2/service/apigateway/types"
911
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
@@ -26,8 +28,13 @@ func fetchApigatewayRestApis(ctx context.Context, meta schema.ClientMeta, parent
2628
func resolveApigatewayRestAPIArn(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
2729
cl := meta.(*client.Client)
2830
rapi := resource.Item.(types.RestApi)
29-
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id)
30-
return resource.Set(c.Name, arn)
31+
return resource.Set(c.Name, arn.ARN{
32+
Partition: cl.Partition,
33+
Service: string(client.ApigatewayService),
34+
Region: cl.Region,
35+
AccountID: "",
36+
Resource: fmt.Sprintf("/restapis/%s", aws.ToString(rapi.Id)),
37+
}.String())
3138
}
3239
func fetchApigatewayRestApiAuthorizers(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
3340
r := parent.Item.(types.RestApi)
@@ -54,8 +61,13 @@ func resolveApigatewayRestAPIAuthorizerArn(ctx context.Context, meta schema.Clie
5461
cl := meta.(*client.Client)
5562
auth := resource.Item.(types.Authorizer)
5663
rapi := resource.Parent.Item.(types.RestApi)
57-
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "authorizers", *auth.Id)
58-
return resource.Set(c.Name, arn)
64+
return resource.Set(c.Name, arn.ARN{
65+
Partition: cl.Partition,
66+
Service: string(client.ApigatewayService),
67+
Region: cl.Region,
68+
AccountID: "",
69+
Resource: fmt.Sprintf("/restapis/%s/authorizers/%s", aws.ToString(rapi.Id), aws.ToString(auth.Id)),
70+
}.String())
5971
}
6072
func fetchApigatewayRestApiDeployments(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
6173
r := parent.Item.(types.RestApi)
@@ -78,8 +90,13 @@ func resolveApigatewayRestAPIDeploymentArn(ctx context.Context, meta schema.Clie
7890
cl := meta.(*client.Client)
7991
d := resource.Item.(types.Deployment)
8092
rapi := resource.Parent.Item.(types.RestApi)
81-
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "deployments", *d.Id)
82-
return resource.Set(c.Name, arn)
93+
return resource.Set(c.Name, arn.ARN{
94+
Partition: cl.Partition,
95+
Service: string(client.ApigatewayService),
96+
Region: cl.Region,
97+
AccountID: "",
98+
Resource: fmt.Sprintf("/restapis/%s/deployments/%s", aws.ToString(rapi.Id), aws.ToString(d.Id)),
99+
}.String())
83100
}
84101
func fetchApigatewayRestApiDocumentationParts(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
85102
r := parent.Item.(types.RestApi)
@@ -106,8 +123,13 @@ func resolveApigatewayRestAPIDocumentationPartArn(ctx context.Context, meta sche
106123
cl := meta.(*client.Client)
107124
d := resource.Item.(types.DocumentationPart)
108125
rapi := resource.Parent.Item.(types.RestApi)
109-
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "documentation/parts", *d.Id)
110-
return resource.Set(c.Name, arn)
126+
return resource.Set(c.Name, arn.ARN{
127+
Partition: cl.Partition,
128+
Service: string(client.ApigatewayService),
129+
Region: cl.Region,
130+
AccountID: "",
131+
Resource: fmt.Sprintf("/restapis/%s/documentation/parts/%s", aws.ToString(rapi.Id), aws.ToString(d.Id)),
132+
}.String())
111133
}
112134
func fetchApigatewayRestApiDocumentationVersions(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
113135
r := parent.Item.(types.RestApi)
@@ -134,8 +156,13 @@ func resolveApigatewayRestAPIDocumentationVersionArn(ctx context.Context, meta s
134156
cl := meta.(*client.Client)
135157
v := resource.Item.(types.DocumentationVersion)
136158
rapi := resource.Parent.Item.(types.RestApi)
137-
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "documentation/versions", *v.Version)
138-
return resource.Set(c.Name, arn)
159+
return resource.Set(c.Name, arn.ARN{
160+
Partition: cl.Partition,
161+
Service: string(client.ApigatewayService),
162+
Region: cl.Region,
163+
AccountID: "",
164+
Resource: fmt.Sprintf("/restapis/%s/documentation/versions/%s", aws.ToString(rapi.Id), aws.ToString(v.Version)),
165+
}.String())
139166
}
140167
func fetchApigatewayRestApiGatewayResponses(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
141168
r := parent.Item.(types.RestApi)
@@ -162,8 +189,13 @@ func resolveApigatewayRestAPIGatewayResponseArn(ctx context.Context, meta schema
162189
cl := meta.(*client.Client)
163190
r := resource.Item.(types.GatewayResponse)
164191
rapi := resource.Parent.Item.(types.RestApi)
165-
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "gatewayresponses", string(r.ResponseType))
166-
return resource.Set(c.Name, arn)
192+
return resource.Set(c.Name, arn.ARN{
193+
Partition: cl.Partition,
194+
Service: string(client.ApigatewayService),
195+
Region: cl.Region,
196+
AccountID: "",
197+
Resource: fmt.Sprintf("/restapis/%s/gatewayresponses/%s", aws.ToString(rapi.Id), string(r.ResponseType)),
198+
}.String())
167199
}
168200
func fetchApigatewayRestApiModels(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
169201
r := parent.Item.(types.RestApi)
@@ -186,8 +218,13 @@ func resolveApigatewayRestAPIModelArn(ctx context.Context, meta schema.ClientMet
186218
cl := meta.(*client.Client)
187219
m := resource.Item.(types.Model)
188220
rapi := resource.Parent.Item.(types.RestApi)
189-
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "models", *m.Name)
190-
return resource.Set(c.Name, arn)
221+
return resource.Set(c.Name, arn.ARN{
222+
Partition: cl.Partition,
223+
Service: string(client.ApigatewayService),
224+
Region: cl.Region,
225+
AccountID: "",
226+
Resource: fmt.Sprintf("/restapis/%s/models/%s", aws.ToString(rapi.Id), aws.ToString(m.Name)),
227+
}.String())
191228
}
192229
func resolveApigatewayRestAPIModelModelTemplate(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
193230
r := resource.Item.(types.Model)
@@ -245,8 +282,13 @@ func resolveApigatewayRestAPIRequestValidatorArn(ctx context.Context, meta schem
245282
cl := meta.(*client.Client)
246283
r := resource.Item.(types.RequestValidator)
247284
rapi := resource.Parent.Item.(types.RestApi)
248-
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "requestvalidators", *r.Id)
249-
return resource.Set(c.Name, arn)
285+
return resource.Set(c.Name, arn.ARN{
286+
Partition: cl.Partition,
287+
Service: string(client.ApigatewayService),
288+
Region: cl.Region,
289+
AccountID: "",
290+
Resource: fmt.Sprintf("/restapis/%s/requestvalidators/%s", aws.ToString(rapi.Id), aws.ToString(r.Id)),
291+
}.String())
250292
}
251293
func fetchApigatewayRestApiResources(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
252294
r := parent.Item.(types.RestApi)
@@ -269,8 +311,13 @@ func resolveApigatewayRestAPIResourceArn(ctx context.Context, meta schema.Client
269311
cl := meta.(*client.Client)
270312
r := resource.Item.(types.Resource)
271313
rapi := resource.Parent.Item.(types.RestApi)
272-
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "resources", *r.Id)
273-
return resource.Set(c.Name, arn)
314+
return resource.Set(c.Name, arn.ARN{
315+
Partition: cl.Partition,
316+
Service: string(client.ApigatewayService),
317+
Region: cl.Region,
318+
AccountID: "",
319+
Resource: fmt.Sprintf("/restapis/%s/resources/%s", aws.ToString(rapi.Id), aws.ToString(r.Id)),
320+
}.String())
274321
}
275322
func fetchApigatewayRestApiStages(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
276323
r := parent.Item.(types.RestApi)
@@ -293,6 +340,11 @@ func resolveApigatewayRestAPIStageArn(ctx context.Context, meta schema.ClientMet
293340
cl := meta.(*client.Client)
294341
s := resource.Item.(types.Stage)
295342
rapi := resource.Parent.Item.(types.RestApi)
296-
arn := cl.RegionGlobalARN(client.ApigatewayService, restApiIDPart, *rapi.Id, "stages", *s.StageName)
297-
return resource.Set(c.Name, arn)
343+
return resource.Set(c.Name, arn.ARN{
344+
Partition: cl.Partition,
345+
Service: string(client.ApigatewayService),
346+
Region: cl.Region,
347+
AccountID: "",
348+
Resource: fmt.Sprintf("/restapis/%s/stages/%s", aws.ToString(rapi.Id), aws.ToString(s.StageName)),
349+
}.String())
298350
}

plugins/source/aws/resources/services/apigateway/types.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)