Skip to content

Commit 675a26f

Browse files
authored
Merge pull request #1 from meese-enterprises/fix/regions-issue
Improved error handling
2 parents ec8fbf2 + 260789a commit 675a26f

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

commands/collect.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pyjq
1212
import urllib.parse
1313
from botocore.exceptions import ClientError, EndpointConnectionError, NoCredentialsError
14-
from shared.common import get_account, custom_serializer
14+
from shared.common import custom_serializer, get_account, get_default_region
1515
from botocore.config import Config
1616

1717
__description__ = "Run AWS API calls to collect data from the account"
@@ -223,13 +223,7 @@ def collect(arguments):
223223
make_directory("account-data/{}".format(account_dir))
224224

225225
# Identify the default region used by global services such as IAM
226-
default_region = os.environ.get("AWS_REGION", "us-east-1")
227-
if "gov-" in default_region:
228-
default_region = "us-gov-west-1"
229-
elif "cn-" in default_region:
230-
default_region = "cn-north-1"
231-
else:
232-
default_region = "us-east-1"
226+
default_region = get_default_region()
233227

234228
regions_filter = None
235229
if len(arguments.regions_filter) > 0:

commands/weboftrust.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def get_iam_trusts(account, nodes, connections, connections_to_get):
198198
# Validate that the federated principal and the SAML provider is coming from known accounts.
199199
# WoT will show us the direction of that trust for further inspection.
200200
# this enables cross_account_admin_sts (STS between accounts)
201+
saml_provider_arn = ""
201202
for saml in saml_providers:
202203
if saml["Arn"] == federated_principal:
203204
saml_provider_arn = saml["Arn"]
@@ -296,7 +297,7 @@ def get_iam_trusts(account, nodes, connections, connections_to_get):
296297
}
297298
)
298299
continue
299-
else:
300+
elif saml_provider_arn != "":
300301
raise Exception(
301302
"Unknown federation provider: {}".format(
302303
saml_provider_arn.lower()

shared/common.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from __future__ import print_function
2+
from netaddr import IPNetwork
23
import argparse
3-
import json
44
import datetime
5+
import json
6+
import os
57
import pyjq
6-
import yaml
78
import sys
8-
from netaddr import IPNetwork
9+
import yaml
910

1011
from shared.nodes import Account, Region
1112
from shared.query import query_aws, get_parameter_file
@@ -152,11 +153,25 @@ def is_unblockable_cidr(cidr):
152153
return False
153154

154155

156+
def get_default_region():
157+
default_region = os.environ.get("AWS_REGION", "us-east-1")
158+
if "gov-" in default_region:
159+
return "us-gov-west-1"
160+
elif "cn-" in default_region:
161+
return "cn-north-1"
162+
else:
163+
return "us-east-1"
164+
165+
155166
def get_regions(account, outputfilter={}):
156167
# aws ec2 describe-regions
157168
region_data = query_aws(account, "describe-regions")
169+
170+
# Fallback to default region if no region data is found
158171
if not region_data:
159-
raise InvalidAccountData("region data not found for {}".format(account.name))
172+
default_region = get_default_region()
173+
log_warning(f"No region data found for account {account.name}. Falling back to default region: {default_region}")
174+
return [{"RegionName": default_region}]
160175

161176
region_filter = ""
162177
if "regions" in outputfilter:

0 commit comments

Comments
 (0)