From 6f1eae6f021fe10de4b87a8a635a147fdbc93fe1 Mon Sep 17 00:00:00 2001 From: Brian Pandola Date: Wed, 17 Feb 2021 01:06:22 -0800 Subject: [PATCH] Refactor ARNs to remove hardcoded account id (#3701) --- moto/elasticbeanstalk/models.py | 18 ++++++++---------- moto/elasticbeanstalk/responses.py | 4 ++-- moto/elasticbeanstalk/utils.py | 11 +++++++++++ tests/test_elasticbeanstalk/test_eb.py | 2 ++ 4 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 moto/elasticbeanstalk/utils.py diff --git a/moto/elasticbeanstalk/models.py b/moto/elasticbeanstalk/models.py index 3767846c1..13650bb24 100644 --- a/moto/elasticbeanstalk/models.py +++ b/moto/elasticbeanstalk/models.py @@ -2,8 +2,9 @@ import weakref from boto3 import Session -from moto.core import BaseBackend, BaseModel +from moto.core import BaseBackend, BaseModel, ACCOUNT_ID from .exceptions import InvalidParameterValueError, ResourceNotFoundException +from .utils import make_arn class FakeEnvironment(BaseModel): @@ -23,15 +24,8 @@ class FakeEnvironment(BaseModel): @property def environment_arn(self): - return ( - "arn:aws:elasticbeanstalk:{region}:{account_id}:" - "environment/{application_name}/{environment_name}".format( - region=self.region, - account_id="123456789012", - application_name=self.application_name, - environment_name=self.environment_name, - ) - ) + resource_path = "%s/%s" % (self.application_name, self.environment_name) + return make_arn(self.region, ACCOUNT_ID, "environment", resource_path) @property def platform_arn(self): @@ -68,6 +62,10 @@ class FakeApplication(BaseModel): def region(self): return self.backend.region + @property + def arn(self): + return make_arn(self.region, ACCOUNT_ID, "application", self.application_name) + class EBBackend(BaseBackend): def __init__(self, region): diff --git a/moto/elasticbeanstalk/responses.py b/moto/elasticbeanstalk/responses.py index 9b6d10994..9759f71c9 100644 --- a/moto/elasticbeanstalk/responses.py +++ b/moto/elasticbeanstalk/responses.py @@ -91,7 +91,7 @@ EB_CREATE_APPLICATION = """ - arn:aws:elasticbeanstalk:{{ region_name }}:111122223333:application/{{ application.application_name }} + {{ application.arn }} {{ application.application_name }} 2019-09-03T13:08:29.049Z @@ -125,7 +125,7 @@ EB_DESCRIBE_APPLICATIONS = """ - arn:aws:elasticbeanstalk:{{ region_name }}:111122223333:application/{{ application.application_name }} + {{ application.arn }} {{ application.application_name }} 2019-09-03T13:08:29.049Z diff --git a/moto/elasticbeanstalk/utils.py b/moto/elasticbeanstalk/utils.py new file mode 100644 index 000000000..e3e22a11d --- /dev/null +++ b/moto/elasticbeanstalk/utils.py @@ -0,0 +1,11 @@ +def make_arn(region, account_id, resource_type, resource_path): + arn_template = ( + "arn:aws:elasticbeanstalk:{region}:{account_id}:{resource_type}/{resource_path}" + ) + arn = arn_template.format( + region=region, + account_id=account_id, + resource_type=resource_type, + resource_path=resource_path, + ) + return arn diff --git a/tests/test_elasticbeanstalk/test_eb.py b/tests/test_elasticbeanstalk/test_eb.py index 15b8f86e5..410f1862b 100644 --- a/tests/test_elasticbeanstalk/test_eb.py +++ b/tests/test_elasticbeanstalk/test_eb.py @@ -42,6 +42,7 @@ def test_create_environment(): app = conn.create_application(ApplicationName="myapp",) env = conn.create_environment(ApplicationName="myapp", EnvironmentName="myenv",) env["EnvironmentName"].should.equal("myenv") + env["EnvironmentArn"].should.contain("myapp/myenv") @mock_elasticbeanstalk @@ -58,6 +59,7 @@ def test_describe_environments(): len(envs).should.equal(1) envs[0]["ApplicationName"].should.equal("myapp") envs[0]["EnvironmentName"].should.equal("myenv") + envs[0]["EnvironmentArn"].should.contain("myapp/myenv") def tags_dict_to_list(tag_dict):