Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
fix: Fix panic on fetching wafv2 without us-east-1 (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
irmatov authored May 5, 2022
1 parent 1f3aac7 commit f6d3f5d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 20 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const (
defaultRegion = "us-east-1"
awsFailedToConfigureErrMsg = "failed to retrieve credentials for account %s. AWS Error: %w, detected aws env variables: %s"
defaultVar = "default"
cloudfrontScopeRegion = defaultRegion
)

var errInvalidRegion = fmt.Errorf("region wildcard \"*\" is only supported as first argument")
Expand Down Expand Up @@ -162,7 +163,8 @@ type ServicesAccountRegionMap map[string]map[string]*Services

// ServicesManager will hold the entire map of (account X region) services
type ServicesManager struct {
services ServicesAccountRegionMap
services ServicesAccountRegionMap
wafScopeServices map[string]*Services
}

func (s *ServicesManager) ServicesByAccountAndRegion(accountId string, region string) *Services {
Expand All @@ -172,13 +174,24 @@ func (s *ServicesManager) ServicesByAccountAndRegion(accountId string, region st
return s.services[accountId][region]
}

func (s *ServicesManager) ServicesByAccountForWAFScope(accountId string) *Services {
return s.wafScopeServices[accountId]
}

func (s *ServicesManager) InitServicesForAccountAndRegion(accountId string, region string, services Services) {
if s.services[accountId] == nil {
s.services[accountId] = make(map[string]*Services)
}
s.services[accountId][region] = &services
}

func (s *ServicesManager) InitServicesForAccountAndScope(accountId string, services Services) {
if s.wafScopeServices == nil {
s.wafScopeServices = make(map[string]*Services)
}
s.wafScopeServices[accountId] = &services
}

type Client struct {
// Those are already normalized values after configure and this is why we don't want to hold
// config directly.
Expand Down Expand Up @@ -242,7 +255,11 @@ func (c *Client) Identify() string {
}

func (c *Client) Services() *Services {
return c.ServicesManager.ServicesByAccountAndRegion(c.AccountID, c.Region)
s := c.ServicesManager.ServicesByAccountAndRegion(c.AccountID, c.Region)
if s == nil && c.WAFScope == wafv2types.ScopeCloudfront {
return c.ServicesManager.ServicesByAccountForWAFScope(c.AccountID)
}
return s
}

func (c *Client) withAccountID(accountID string) *Client {
Expand Down Expand Up @@ -498,6 +515,7 @@ func Configure(logger hclog.Logger, providerConfig interface{}) (schema.ClientMe
for _, region := range account.Regions {
client.ServicesManager.InitServicesForAccountAndRegion(*output.Account, region, initServices(region, awsCfg))
}
client.ServicesManager.InitServicesForAccountAndScope(*output.Account, initServices(cloudfrontScopeRegion, awsCfg))
}
if len(client.Accounts) == 0 {
return nil, diags.Add(diag.FromError(errors.New("no accounts instantiated"), diag.INTERNAL))
Expand Down
2 changes: 1 addition & 1 deletion client/multiplexers.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func ServiceAccountRegionScopeMultiplexer(service string) func(meta schema.Clien
client := meta.(*Client)
for accountID := range client.ServicesManager.services {
// always fetch cloudfront related resources
l = append(l, client.withAccountIDRegionAndScope(accountID, "us-east-1", wafv2types.ScopeCloudfront))
l = append(l, client.withAccountIDRegionAndScope(accountID, cloudfrontScopeRegion, wafv2types.ScopeCloudfront))
for region := range client.ServicesManager.services[accountID] {
if !isSupportedServiceForRegion(service, region) {
meta.Logger().Trace("region is not supported for service", "service", service, "region", region)
Expand Down

0 comments on commit f6d3f5d

Please sign in to comment.