Skip to content

Commit 9e570ee

Browse files
authored
fix: global region and missing string methods (#857)
* fix(resources): add string method to missing resources * fix(awsutil): support pseudo global region for specific services
1 parent aff7b89 commit 9e570ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+256
-24
lines changed

pkg/awsutil/config.go

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ import (
1919
log "github.com/sirupsen/logrus"
2020
)
2121

22+
// GlobalServices is the set of AWS services that operate globally
23+
// and should only be processed in the "global" pseudo-region.
24+
// The keys are the service IDs as returned by middleware.GetServiceID(ctx).
25+
var GlobalServices = map[string]struct{}{
26+
"CloudFront": {},
27+
"IAM": {},
28+
"Route 53": {},
29+
"WAF": {}, // WAF Classic (global)
30+
"CloudFront KeyValueStore": {},
31+
}
32+
33+
// IsGlobalService returns true if the service should only run in global region
34+
func IsGlobalService(service string) bool {
35+
_, ok := GlobalServices[service]
36+
return ok
37+
}
38+
2239
func (c *Credentials) NewConfig(ctx context.Context, region, serviceType string) (*aws.Config, error) {
2340
log.Debugf("creating new config in %s for %s", region, serviceType)
2441

@@ -73,6 +90,10 @@ func (c *Credentials) NewConfig(ctx context.Context, region, serviceType string)
7390
cfgCopy.APIOptions = append(cfgCopy.APIOptions, func(stack *middleware.Stack) error {
7491
return stack.Initialize.Add(SkipGlobal{}, middleware.After)
7592
})
93+
} else {
94+
cfgCopy.APIOptions = append(cfgCopy.APIOptions, func(stack *middleware.Stack) error {
95+
return stack.Initialize.Add(SkipRegionalForGlobalService{}, middleware.After)
96+
})
7697
}
7798
cfg = &cfgCopy
7899
}
@@ -153,29 +174,9 @@ func (c *Credentials) rootConfig(ctx context.Context) (*aws.Config, error) {
153174
return c.cfg, nil
154175
}
155176

156-
// SkipGlobal returns ErrSkipRequest when operating in the global
157-
// pseudo-region.
158-
//
159-
// FUTURE: define mechanism for allowing specific resources, such as those in
160-
// IAM, to override this skip.
161-
//
162-
// The simplest way to do this would be to remove this middleware through
163-
// functional options on relevant operations. e.g.:
164-
//
165-
// func isGlobalResource(o *iam.Options) {
166-
// o.APIOptions = append(o.APIOptions, func(stack *middleware.Stack) error {
167-
// stack.Initialize.Remove(config.SkipGlobal{}.ID())
168-
// })
169-
// }
170-
//
171-
// // per-operation
172-
// out, err := svc.ListRoles(context.Background(), nil, isGlobalResource)
173-
// // on a client, if you know you're only operating in the context of global resources
174-
// svc := iam.NewFromConfig(cfg, isGlobalResource)
175-
//
176-
// You could also define some kind of "is global resource" Context flag, which
177-
// SkipGlobal could react to. That may be preferrable to having SkipGlobal be
178-
// exported from this package.
177+
// SkipGlobal skips requests for non-global services when operating in the
178+
// global pseudo-region. Global services (CloudFront, IAM, Route 53, etc.)
179+
// are allowed through, while regional services are skipped.
179180
type SkipGlobal struct{}
180181

181182
func (SkipGlobal) ID() string {
@@ -187,7 +188,41 @@ func (SkipGlobal) HandleInitialize(
187188
) (
188189
out middleware.InitializeOutput, md middleware.Metadata, err error,
189190
) {
190-
return out, md, liberrors.ErrSkipRequest(fmt.Sprintf("skip global: '%s'", middleware.GetServiceID(ctx)))
191+
service := middleware.GetServiceID(ctx)
192+
193+
if IsGlobalService(service) {
194+
// Global service in global region - allow
195+
return next.HandleInitialize(ctx, in)
196+
}
197+
198+
// Non-global service in global region - skip
199+
return out, md, liberrors.ErrSkipRequest(
200+
fmt.Sprintf("service '%s' is not global, but the session is", service))
201+
}
202+
203+
// SkipRegionalForGlobalService skips requests for global-only services
204+
// when operating in a non-global region context. This ensures global
205+
// services like CloudFront and IAM are only processed once in the
206+
// "global" pseudo-region rather than in every regional scan.
207+
type SkipRegionalForGlobalService struct{}
208+
209+
func (SkipRegionalForGlobalService) ID() string {
210+
return "aws-nuke::skipRegionalForGlobalService"
211+
}
212+
213+
func (SkipRegionalForGlobalService) HandleInitialize(
214+
ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler,
215+
) (
216+
out middleware.InitializeOutput, md middleware.Metadata, err error,
217+
) {
218+
service := middleware.GetServiceID(ctx)
219+
220+
if IsGlobalService(service) {
221+
return out, md, liberrors.ErrSkipRequest(
222+
fmt.Sprintf("service '%s' is global, but the session is not", service))
223+
}
224+
225+
return next.HandleInitialize(ctx, in)
191226
}
192227

193228
type traceRequest struct{}

resources/amp-scraper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ func (f *AMPScraper) Remove(ctx context.Context) error {
6969
func (f *AMPScraper) Properties() types.Properties {
7070
return types.NewPropertiesFromStruct(f)
7171
}
72+
73+
func (f *AMPScraper) String() string {
74+
return *f.ScraperID
75+
}

resources/amp-workspace.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,7 @@ func (f *AMPWorkspace) Remove(ctx context.Context) error {
7171
func (f *AMPWorkspace) Properties() types.Properties {
7272
return types.NewPropertiesFromStruct(f)
7373
}
74+
75+
func (f *AMPWorkspace) String() string {
76+
return *f.WorkspaceId
77+
}

resources/appconfig-applications.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,7 @@ func (f *AppConfigApplication) Properties() types.Properties {
7272
Set("ID", f.id).
7373
Set("Name", f.name)
7474
}
75+
76+
func (f *AppConfigApplication) String() string {
77+
return *f.name
78+
}

resources/appconfig-configurationprofiles.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,7 @@ func (f *AppConfigConfigurationProfile) Properties() types.Properties {
9292
Set("ID", f.id).
9393
Set("Name", f.name)
9494
}
95+
96+
func (f *AppConfigConfigurationProfile) String() string {
97+
return *f.name
98+
}

resources/appconfig-deploymentstrategies.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,7 @@ func (f *AppConfigDeploymentStrategy) Properties() types.Properties {
7777
Set("ID", f.id).
7878
Set("Name", f.name)
7979
}
80+
81+
func (f *AppConfigDeploymentStrategy) String() string {
82+
return *f.name
83+
}

resources/appconfig-environments.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,7 @@ func (f *AppConfigEnvironment) Properties() types.Properties {
8888
Set("ID", f.id).
8989
Set("Name", f.name)
9090
}
91+
92+
func (f *AppConfigEnvironment) String() string {
93+
return *f.name
94+
}

resources/appconfig-hostedconfigurationversions.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package resources
22

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

67
"github.com/sirupsen/logrus"
78

@@ -90,3 +91,7 @@ func (f *AppConfigHostedConfigurationVersion) Properties() types.Properties {
9091
Set("ConfigurationProfileID", f.configurationProfileID).
9192
Set("VersionNumber", f.versionNumber)
9293
}
94+
95+
func (f *AppConfigHostedConfigurationVersion) String() string {
96+
return fmt.Sprintf("%d", *f.versionNumber)
97+
}

resources/appmesh-gatewayroute.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,7 @@ func (f *AppMeshGatewayRoute) Properties() types.Properties {
119119

120120
return properties
121121
}
122+
123+
func (f *AppMeshGatewayRoute) String() string {
124+
return *f.routeName
125+
}

resources/appmesh-mesh.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,7 @@ func (f *AppMeshMesh) Properties() types.Properties {
7878

7979
return properties
8080
}
81+
82+
func (f *AppMeshMesh) String() string {
83+
return *f.meshName
84+
}

0 commit comments

Comments
 (0)