auth.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 constructs import Construct
  7. from aws_cdk import (
  8. CfnOutput,
  9. Stack,
  10. aws_iam as iam
  11. )
  12. class AuthPolicy:
  13. """
  14. Creator of auth policies related for the example stack
  15. """
  16. def __init__(self, context: Construct):
  17. self._context = context
  18. self._policy_output = None
  19. def generate_user_policy(self, stack: Stack) -> None:
  20. """
  21. Generate require role policy for calling resources created in the stack.
  22. Currently, all resources use grant_access to groups so no direct policy
  23. is generated.
  24. :param stack: The stack to use to generate the policy for
  25. :return: The created Admin IAM managed policy.
  26. """
  27. return None
  28. def generate_admin_policy(self, stack: Stack) -> iam.ManagedPolicy:
  29. """
  30. Generate required role policy for calling service / using resources.
  31. :param stack: The stack to use to generate the policy for
  32. :return: The created Admin IAM managed policy.
  33. """
  34. policy_id = f'CoreExampleAdminPolicy'
  35. policy_statements = []
  36. # Add permissions to describe stacks and resources
  37. stack_statement = iam.PolicyStatement(
  38. actions=[
  39. "cloudformation:DescribeStackResources",
  40. "cloudformation:DescribeStackResource",
  41. "cloudformation:ListStackResources"
  42. ],
  43. effect=iam.Effect.ALLOW,
  44. resources=[
  45. f"arn:{stack.partition}:cloudformation:{stack.region}:{stack.account}:stack/{stack.stack_name}"
  46. ],
  47. sid="ReadDeploymentStacks",
  48. )
  49. policy_statements.append(stack_statement)
  50. policy = iam.ManagedPolicy(
  51. self._context,
  52. policy_id,
  53. managed_policy_name=f'{stack.stack_name}-AdminPolicy',
  54. statements=policy_statements)
  55. self._policy_output = CfnOutput(
  56. self._context,
  57. id=f'{policy_id}AdminOutput',
  58. description='Admin user policy arn to work with resources',
  59. export_name=f"{stack.stack_name}:{policy_id}",
  60. value=policy.managed_policy_arn)
  61. return policy