Skip to content

Commit e3538db

Browse files
committed
refactor(meta): throw error if we region or partition list is not loaded
Signed-off-by: Fred Myerscough <oniice@gmail.com>
1 parent 0ddc6ad commit e3538db

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

rules/awsmeta/patterns.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import (
1313
func GetRegionPattern() *regexp.Regexp {
1414
regionList, err := regions.ListAllRegions()
1515
if err != nil {
16-
// Fallback to empty pattern if we can't load regions
17-
return regexp.MustCompile(`^$`)
16+
panic(fmt.Sprintf("failed to load AWS regions: %v", err))
17+
}
18+
19+
if len(regionList) == 0 {
20+
panic("AWS region list is empty")
1821
}
1922

2023
regionNames := make([]string, 0, len(regionList))
@@ -30,8 +33,11 @@ func GetRegionPattern() *regexp.Regexp {
3033
func GetRegionInStringPattern() *regexp.Regexp {
3134
regionList, err := regions.ListAllRegions()
3235
if err != nil {
33-
// Fallback to empty pattern if we can't load regions
34-
return regexp.MustCompile(`^$`)
36+
panic(fmt.Sprintf("failed to load AWS regions: %v", err))
37+
}
38+
39+
if len(regionList) == 0 {
40+
panic("AWS region list is empty")
3541
}
3642

3743
regionNames := make([]string, 0, len(regionList))
@@ -47,16 +53,18 @@ func GetRegionInStringPattern() *regexp.Regexp {
4753
func GetAvailabilityZonePattern() *regexp.Regexp {
4854
regionList, err := regions.ListAllRegions()
4955
if err != nil {
50-
// Fallback to empty pattern if we can't load regions
51-
return regexp.MustCompile(`^$`)
56+
panic(fmt.Sprintf("failed to load AWS regions: %v", err))
57+
}
58+
59+
if len(regionList) == 0 {
60+
panic("AWS region list is empty")
5261
}
5362

5463
regionNames := make([]string, 0, len(regionList))
5564
for _, region := range regionList {
5665
regionNames = append(regionNames, regexp.QuoteMeta(region.RegionId))
5766
}
5867

59-
// AZ pattern: region code + letter (a-z)
6068
pattern := fmt.Sprintf("^(%s)[a-z]$", strings.Join(regionNames, "|"))
6169
return regexp.MustCompile(pattern)
6270
}
@@ -65,8 +73,11 @@ func GetAvailabilityZonePattern() *regexp.Regexp {
6573
func GetARNRegionPattern() *regexp.Regexp {
6674
regionList, err := regions.ListAllRegions()
6775
if err != nil {
68-
// Fallback to empty pattern if we can't load regions
69-
return regexp.MustCompile(`^$`)
76+
panic(fmt.Sprintf("failed to load AWS regions: %v", err))
77+
}
78+
79+
if len(regionList) == 0 {
80+
panic("AWS region list is empty")
7081
}
7182

7283
regionNames := make([]string, 0, len(regionList))
@@ -82,8 +93,11 @@ func GetARNRegionPattern() *regexp.Regexp {
8293
func GetPartitionPattern() *regexp.Regexp {
8394
partitionList, err := partitions.List()
8495
if err != nil {
85-
// Fallback to empty pattern if we can't load partitions
86-
return regexp.MustCompile(`^$`)
96+
panic(fmt.Sprintf("failed to load AWS partitions: %v", err))
97+
}
98+
99+
if len(partitionList) == 0 {
100+
panic("AWS partition list is empty")
87101
}
88102

89103
partitionNames := make([]string, 0, len(partitionList))

rules/awsmeta/patterns_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,4 @@ func TestGetRegionInStringPattern(t *testing.T) {
116116
}
117117
}
118118
}
119+

0 commit comments

Comments
 (0)