Skip to content

Commit

Permalink
Add dependency of SSM to cognito url trigger (#1395)
Browse files Browse the repository at this point in the history
### Feature or Bugfix
- Bugfix

### Detail
When deploying an internet facing deployment for the first time, It
fails to deploy in the frontend stage -in the cognito url custom
resource trigger:
```
[ERROR] ParameterNotFound: An error occurred (ParameterNotFound) when calling the GetParameter operation:
Traceback (most recent call last):
File "/var/task/cognito_urls.py", line 77, in handler
setup_cognito(
File "/var/task/cognito_urls.py", line 27, in setup_cognito
user_guide_link = ssm.get_parameter(
File "/var/runtime/botocore/client.py", line 553, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/var/runtime/botocore/client.py", line 1009, in _make_api_call
raise error_class(parsed_response, operation_name)
```

The issue is that the SSM parameters that is trying to access do not
exist yet in the first deployment because they are deployed as part of
the frontend stack.

In this PR we add a dependency of the SSM parameters needed to run the
trigger

### Relates
- #1366 

### Security
Please answer the questions below briefly where applicable, or write
`N/A`. Based on
[OWASP 10](https://owasp.org/Top10/en/).

- Does this PR introduce or modify any input fields or queries - this
includes
fetching data from storage outside the application (e.g. a database, an
S3 bucket)?
  - Is the input sanitized?
- What precautions are you taking before deserializing the data you
consume?
  - Is injection prevented by parametrizing queries?
  - Have you ensured no `eval` or similar functions are used?
- Does this PR introduce any functionality or component that requires
authorization?
- How have you ensured it respects the existing AuthN/AuthZ mechanisms?
  - Are you logging failed auth attempts?
- Are you using or adding any cryptographic features?
  - Do you use a standard proven implementations?
  - Are the used keys controlled by the customer? Where are they stored?
- Are you introducing any new policies/roles/users?
  - Have you used the least-privilege principle? How?


By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license.
  • Loading branch information
dlpzx authored Jul 10, 2024
1 parent 9c8bbb5 commit f4020bc
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions deploy/stacks/cloudfront.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,19 @@ def __init__(
]
self.user_docs_bucket = None
if custom_auth is None:
userguide_docs_distribution, user_docs_bucket = self.build_static_site(
'userguide',
acl,
auth_at_edge,
envname,
resource_prefix,
userguide_domain_names,
certificate,
ssl_support_method,
security_policy,
logging_bucket,
userguide_docs_distribution, user_docs_bucket, ssm_distribution_domain_name_userguide = (
self.build_static_site(
'userguide',
acl,
auth_at_edge,
envname,
resource_prefix,
userguide_domain_names,
certificate,
ssl_support_method,
security_policy,
logging_bucket,
)
)

self.userguide_docs_distribution = userguide_docs_distribution
Expand Down Expand Up @@ -274,7 +276,13 @@ def __init__(
)

if not custom_auth:
self.cognito_urls_config(resource_prefix, envname, backend_region, custom_domain, [cloudfront_distribution])
self.cognito_urls_config(
resource_prefix,
envname,
backend_region,
custom_domain,
[cloudfront_distribution, ssm_distribution_domain_name, ssm_distribution_domain_name_userguide],
)

CfnOutput(
self,
Expand Down Expand Up @@ -441,8 +449,10 @@ def build_static_site(

param_path = f'/dataall/{envname}/cloudfront/docs/user'

self.store_distribution_params(cloudfront_bucket, construct_id, cloudfront_distribution, param_path)
return cloudfront_distribution, cloudfront_bucket
domain_name_ssm_param = self.store_distribution_params(
cloudfront_bucket, construct_id, cloudfront_distribution, param_path
)
return cloudfront_distribution, cloudfront_bucket, domain_name_ssm_param

def store_distribution_params(self, cloudfront_bucket, construct_id, distribution, param_path):
ssm.StringParameter(
Expand All @@ -451,7 +461,7 @@ def store_distribution_params(self, cloudfront_bucket, construct_id, distributio
parameter_name=f'{param_path}/CloudfrontDistributionId',
string_value=distribution.distribution_id,
)
ssm.StringParameter(
domain_name = ssm.StringParameter(
self,
f'{construct_id}DistributionDomain',
parameter_name=f'{param_path}/CloudfrontDistributionDomainName',
Expand All @@ -463,6 +473,7 @@ def store_distribution_params(self, cloudfront_bucket, construct_id, distributio
parameter_name=f'{param_path}/CloudfrontDistributionBucket',
string_value=cloudfront_bucket.bucket_name,
)
return domain_name

@staticmethod
def additional_documentation_behavior(func) -> cloudfront.BehaviorOptions:
Expand Down

0 comments on commit f4020bc

Please sign in to comment.