auth.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. from aws_cdk import (
  7. core,
  8. aws_iam as iam
  9. )
  10. class AuthPolicy:
  11. """
  12. Creator of auth policies related for the example stack
  13. """
  14. def __init__(self, context: core.Construct):
  15. self._context = context
  16. self._policy_output = None
  17. def generate_user_policy(self, stack: core.Stack) -> None:
  18. """
  19. Generate require role policy for calling resources created in the stack.
  20. Currently all resources use grant_access to groups so no direct policy
  21. is generated.
  22. :param stack: The stack to use to generate the policy for
  23. :return: The created Admin IAM managed policy.
  24. """
  25. return None
  26. def generate_admin_policy(self, stack: core.Stack) -> iam.ManagedPolicy:
  27. """
  28. Generate required role policy for calling service / using resources.
  29. :param stack: The stack to use to generate the policy for
  30. :return: The created Admin IAM managed policy.
  31. """
  32. policy_id = f'CoreExampleAdminPolicy'
  33. policy_statements = []
  34. # Add permissions to describe stacks and resources
  35. stack_statement = iam.PolicyStatement(
  36. actions=[
  37. "cloudformation:DescribeStackResources",
  38. "cloudformation:DescribeStackResource",
  39. "cloudformation:ListStackResources"
  40. ],
  41. effect=iam.Effect.ALLOW,
  42. resources=[
  43. f"arn:{stack.partition}:cloudformation:{stack.region}:{stack.account}:stack/{stack.stack_name}"
  44. ],
  45. sid="ReadDeploymentStacks",
  46. )
  47. policy_statements.append(stack_statement)
  48. policy = iam.ManagedPolicy(
  49. self._context,
  50. policy_id,
  51. managed_policy_name=f'{stack.stack_name}-AdminPolicy',
  52. statements=policy_statements)
  53. self._policy_output = core.CfnOutput(
  54. self._context,
  55. id=f'{policy_id}AdminOutput',
  56. description='Admin user policy arn to work with resources',
  57. export_name=f"{stack.stack_name}:{policy_id}",
  58. value=policy.managed_policy_arn)
  59. return policy