Skip to content

Commit 6b646bd

Browse files
CLOUDS-2607:Properly parse partitions for gov and china service arns (#598)
* CLOUDS-2607:Properly parse partitions for gov and china service arns * Handle redshift gov events * Fix gov partition * Formatting
1 parent b24e501 commit 6b646bd

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

aws/logs_monitoring/parsing.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
DD_USE_VPC,
3737
)
3838

39+
GOV, CN = "gov", "cn"
40+
3941

4042
logger = logging.getLogger()
4143

@@ -378,6 +380,16 @@ def find_s3_source(key):
378380
return "s3"
379381

380382

383+
def get_partition_from_region(region):
384+
partition = "aws"
385+
if region:
386+
if GOV in region:
387+
partition = "aws-us-gov"
388+
elif CN in region:
389+
partition = "aws-cn"
390+
return partition
391+
392+
381393
def parse_service_arn(source, key, bucket, context):
382394
if source == "elb":
383395
# For ELB logs we parse the filename to extract parameters in order to rebuild the ARN
@@ -406,8 +418,9 @@ def parse_service_arn(source, key, bucket, context):
406418
elbname = name.replace(".", "/")
407419
if len(idsplit) > 1:
408420
idvalue = idsplit[1]
409-
return "arn:aws:elasticloadbalancing:{}:{}:loadbalancer/{}".format(
410-
region, idvalue, elbname
421+
partition = get_partition_from_region(region)
422+
return "arn:{}:elasticloadbalancing:{}:{}:loadbalancer/{}".format(
423+
partition, region, idvalue, elbname
411424
)
412425
if source == "s3":
413426
# For S3 access logs we use the bucket name to rebuild the arn
@@ -446,8 +459,8 @@ def parse_service_arn(source, key, bucket, context):
446459
filesplit = filename.split("_")
447460
if len(filesplit) == 6:
448461
clustername = filesplit[3]
449-
return "arn:aws:redshift:{}:{}:cluster:{}:".format(
450-
region, accountID, clustername
462+
return "arn:{}:redshift:{}:{}:cluster:{}:".format(
463+
get_partition_from_region(region), region, accountID, clustername
451464
)
452465
return
453466

aws/logs_monitoring/tests/test_parsing.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ def test_redshift_event(self):
213213
"redshift",
214214
)
215215

216+
def test_redshift_gov_event(self):
217+
self.assertEqual(
218+
parse_event_source(
219+
{"Records": ["logs-from-s3"]},
220+
"AWSLogs/123456779121/redshift/us-gov-east-1/2020/10/21/123456779121_redshift_us-gov-east"
221+
"-1_mycluster_userlog_2020-10-21T18:01.gz",
222+
),
223+
"redshift",
224+
)
225+
216226
def test_route53_event(self):
217227
self.assertEqual(
218228
parse_event_source(
@@ -358,6 +368,20 @@ def test_elb_s3_key_multi_prefix(self):
358368
"arn:aws:elasticloadbalancing:us-east-1:123456789123:loadbalancer/app/my-alb-name/123456789aabcdef",
359369
)
360370

371+
def test_elb_s3_key_multi_prefix_gov(self):
372+
self.assertEqual(
373+
parse_service_arn(
374+
"elb",
375+
"elasticloadbalancing/my-alb-name/AWSLogs/123456789123/elasticloadbalancing/us-gov-east-1/2022/02/08"
376+
"/123456789123_elasticloadbalancing_us-gov-east-1_app.my-alb-name.123456789aabcdef_20220208T1127Z_10"
377+
".0.0.2_1abcdef2.log.gz",
378+
None,
379+
None,
380+
),
381+
"arn:aws-us-gov:elasticloadbalancing:us-gov-east-1:123456789123:loadbalancer/app/my-alb-name"
382+
"/123456789aabcdef",
383+
)
384+
361385

362386
class TestParseAwsWafLogs(unittest.TestCase):
363387
def test_waf_string_invalid_json(self):

0 commit comments

Comments
 (0)