{
  "Description": "Creates the IAM role that Qovery assumes to deploy and manage your clusters: it is needed to create a cluster, to delete it and for any change to its IAM roles and policies. Once the cluster exists, you can optionally use the restricted permissions role for day-to-day operations. The Metadata of the Role resource explains why each permission is needed.",
  "Parameters": {
    "RoleName": {
      "Description": "The name of the role that is going to be created to allow extended access to Qovery, to create, update and delete clusters",
      "Type": "String",
      "Default": "qovery-extended-user-role",
      "MaxLength": 64
    },
    "OrganizationId": {
      "Description": "The id of your Qovery organization. Only Qovery acting on behalf of this organization will be able to assume the role",
      "Type": "String",
      "AllowedPattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
      "ConstraintDescription": "must be a Qovery organization id, a lowercase UUID like 3f2b7c1e-8d4a-4b6e-9f0a-1c2d3e4f5a6b"
    },
    "RestrictedQoveryUserRole": {
      "Description": "Optional. The name of the Qovery restricted permissions role of your account (the RoleName of the qovery-restricted-user-role stack, qovery-restricted-user-role by default). When set, this role is denied any change to that role, so it can't be used to give the restricted role more access. Leave empty if you don't use a restricted role, e.g. when this role is the only one used by your clusters",
      "Type": "String",
      "Default": "",
      "MaxLength": 64,
      "AllowedPattern": "^([\\w+=,.@-]+)?$",
      "ConstraintDescription": "must be empty or an IAM role name: letters, digits and the characters _+=,.@-, without any path or wildcard"
    },
    "AwsEksEncryptSecretsKmsKeyArn": {
      "Description": "ARN of your own KMS key, if you set one in the cluster's aws_eks_encrypt_secrets_kms_key_arn advanced setting. Leave empty if you don't: the role then gets no permission on KMS keys that Qovery doesn't create",
      "Type": "String",
      "Default": "",
      "AllowedPattern": "^(arn:aws[a-z-]*:kms:[a-z0-9-]+:[0-9]{12}:key/[0-9a-f-]+)?$",
      "ConstraintDescription": "must be empty or a KMS key ARN (arn:aws:kms:<region>:<account id>:key/<key id>), not an alias"
    },
    "AwsEksEnableEfsAddon": {
      "Description": "Whether the cluster's aws_eks_enable_efs_addon advanced setting is on. When false (default), the role gets no permission on Amazon EFS. Set it to true to let Qovery manage the EFS file system of the cluster and its mount targets",
      "Type": "String",
      "Default": "false",
      "AllowedValues": [
        "true",
        "false"
      ]
    },
    "AllowQoveryDeployerAssumeRole": {
      "Description": "Whether the Qovery deployer is allowed to assume the role. When false (default), the trust policy denies sts:AssumeRole to everyone, so nobody can assume the role. Set it to true to let Qovery deploy and manage your clusters with it",
      "Type": "String",
      "Default": "false",
      "AllowedValues": [
        "true",
        "false"
      ]
    }
  },
  "Conditions": {
    "HasRestrictedQoveryUserRole": {
      "Fn::Not": [
        {
          "Fn::Equals": [
            {
              "Ref": "RestrictedQoveryUserRole"
            },
            ""
          ]
        }
      ]
    },
    "HasEksEncryptSecretsKmsKey": {
      "Fn::Not": [
        {
          "Fn::Equals": [
            {
              "Ref": "AwsEksEncryptSecretsKmsKeyArn"
            },
            ""
          ]
        }
      ]
    },
    "EksEfsAddonEnabled": {
      "Fn::Equals": [
        {
          "Ref": "AwsEksEnableEfsAddon"
        },
        "true"
      ]
    },
    "QoveryDeployerCanAssumeRole": {
      "Fn::Equals": [
        {
          "Ref": "AllowQoveryDeployerAssumeRole"
        },
        "true"
      ]
    }
  },
  "Resources": {
    "Role": {
      "Type": "AWS::IAM::Role",
      "Metadata": {
        "Comments": {
          "AwsServicesUnscoped": {
            "Why": [
              "The AWS services Qovery uses to create, update and delete the cluster's infrastructure and the managed databases, plus read-only lookups on the account (Organizations, Service Quotas, CloudTrail events, resource tags)."
            ]
          },
          "EfsUnscoped": {
            "Why": [
              "elasticfilesystem:*: the EFS file system of the cluster and its mount targets, created by Qovery when the aws_eks_enable_efs_addon advanced setting is on.",
              "Only written when the AwsEksEnableEfsAddon parameter is true.",
              "Unscoped: a file system is identified by a generated id, not by a name Qovery can rely on."
            ]
          },
          "S3SqsQoveryScoped": {
            "Why": "Full access to the S3 buckets and SQS queues created by Qovery. Scoped to the names starting with qovery: the other buckets and queues of the account are out of reach."
          },
          "SimulatePolicyFromQovery": {
            "Why": [
              "Pre-flight check: lets Qovery dry-run the permissions of this role before a deployment, so a missing permission is reported up front instead of making the deployment fail halfway through.",
              "Scoped to this role: it can't be used to inspect the permissions of the other users and roles of the account."
            ],
            "Actions": {
              "iam:SimulatePrincipalPolicy": "simulate this role's policies against the actions Qovery needs"
            }
          },
          "IamManageQoveryIdentities": {
            "Why": [
              "The IAM roles, policies and instance profiles of the cluster, created, updated and deleted by Qovery:",
              "  - EKS control plane and nodes: qovery-eks-<cluster id>, qovery-eks-workers-<cluster id>, KarpenterNodeRole-<cluster>",
              "  - Karpenter controller: KarpenterControllerRole-<cluster>",
              "  - EBS and EFS CSI drivers: eks-ebs-csi-plugin-<cluster id>, eks-efs-csi-plugin-<cluster id>, AmazonEKS_EBS_CSI_Policy-<cluster id>, AmazonEKS_EFS_CSI_Policy-<cluster id>",
              "  - Qovery's cluster components, each with its own role and policy named qovery-*: Loki, Thanos, cluster-autoscaler, Grafana, CloudWatch exporter, AWS Load Balancer controller, KEDA, External Secrets, IAM user mapper, RDS enhanced monitoring",
              "  - Instance profiles: KarpenterNodeInstanceProfile-<cluster>, qovery-eks-alb-controller-<cluster id>",
              "Scoped to the names of resources created & managed by Qovery"
            ],
            "Actions": {
              "iam:CreateRole": "create the roles listed above",
              "iam:CreatePolicyVersion": "update a policy when a new Qovery version changes its content",
              "iam:GetRole": "read a role on every deployment to detect changes",
              "iam:TagRole": "tag the roles with the cluster tags (QoveryProduct, ClusterId, your custom tags)",
              "iam:UntagRole": "remove a tag that is no longer set on the cluster",
              "iam:ListRoleTags": "re-read a role's tags after an update, when their values were only known at apply time",
              "iam:ListRolePolicies": "list a role's inline policies, to read them and to remove them before deleting the role",
              "iam:ListInstanceProfilesForRole": "find the instance profiles of a role, to detach it from them before deleting it",
              "iam:UpdateAssumeRolePolicy": "update who can assume a role, e.g. when a component uses a new Kubernetes service account",
              "iam:UpdateRoleDescription": "update a role's description when a new Qovery version changes it",
              "iam:CreatePolicy": "create the policies listed above",
              "iam:GetPolicy": "read a policy on every deployment to detect changes",
              "iam:GetPolicyVersion": "read the current content of a policy",
              "iam:ListPolicyVersions": "find the old versions of a policy, to delete them",
              "iam:DeletePolicyVersion": "IAM keeps 5 versions per policy at most, and old versions must be deleted before the policy",
              "iam:PutRolePolicy": "write a role's inline policies (Karpenter controller, ECR pull-through cache, load balancers)",
              "iam:GetRolePolicy": "read the content of an inline policy",
              "iam:AttachRolePolicy": "attach to a role its Qovery policy, the AWS managed ones it needs (AmazonEKSClusterPolicy, AmazonEKSWorkerNodePolicy, AmazonEKS_CNI_Policy, ...) and the node policies set on the cluster",
              "iam:ListAttachedRolePolicies": "list the policies attached to a role, to read them and to detach them before deleting the role",
              "iam:CreateInstanceProfile": "create the Karpenter nodes and AWS Load Balancer controller instance profiles",
              "iam:GetInstanceProfile": "read an instance profile on every deployment to detect changes",
              "iam:AddRoleToInstanceProfile": "put its role in an instance profile",
              "iam:TagInstanceProfile": "tag the instance profiles with the cluster tags",
              "iam:UntagInstanceProfile": "remove a tag that is no longer set on the cluster",
              "iam:PassRole": "hand a role to AWS: the control plane role to EKS, the node roles to the EKS node groups, the CSI driver roles to the EKS add-ons, and a role to its instance profile",
              "iam:DeleteRole": "delete the roles when the cluster is deleted",
              "iam:DeleteRolePolicy": "delete an inline policy that is removed, or before deleting its role",
              "iam:DeletePolicy": "delete the policies when the cluster is deleted",
              "iam:DetachRolePolicy": "detach a policy that is removed, or before deleting its role",
              "iam:RemoveRoleFromInstanceProfile": "take a role out of its instance profile before deleting one or the other",
              "iam:DeleteInstanceProfile": "delete the instance profiles when the cluster is deleted"
            }
          },
          "IamPassRdsMonitoringRole": {
            "Why": [
              "The RDS databases deployed with a Qovery Blueprint: the blueprint's enhanced monitoring role isn't named qovery-*, so IamManageQoveryIdentities doesn't cover it.",
              "Condition: any role of the account, but it can only be handed to RDS enhanced monitoring (monitoring.rds.amazonaws.com), not to any other AWS service."
            ],
            "Actions": {
              "iam:PassRole": "hand the enhanced monitoring role to RDS when a Blueprint database is created or updated"
            }
          },
          "IamServiceLinkedRoleLookup": {
            "Why": [
              "AWS service-linked roles live under the aws-service-role/ path. IamManageQoveryIdentities only covers two of them (AWSServiceRoleForEC2Spot and AWSServiceRoleForAmazonEKSNodegroup): this statement lets Qovery read any of them.",
              "Read-only: lets AWS and Qovery check whether one already exists before creating it (see IamServiceLinkedRolesScoped).",
              "While a role doesn't exist yet, IAM can't know its path: it checks the permission against role/<name>, without the aws-service-role/ path, hence the second ARN."
            ],
            "Actions": {
              "iam:GetRole": "check that a service-linked role exists: EKS does it for AWSServiceRoleForAmazonEKSNodegroup before creating a managed node group, Qovery for AWSServiceRoleForEC2Spot before running spot instances"
            }
          },
          "IamOidcProviderScoped": {
            "Why": [
              "The cluster's OpenID Connect provider: it lets Kubernetes service accounts assume IAM roles (IRSA), which is how Karpenter, the CSI drivers, the AWS Load Balancer controller, Loki, Thanos, ... get their AWS permissions.",
              "Scoped to the EKS providers (oidc.eks.<region>.amazonaws.com/id/<id>): AWS names a provider after its cluster's issuer URL, so it can't be narrowed down to Qovery's clusters, but the account's other identity providers (GitHub Actions, GitLab, ...) are out of reach."
            ],
            "Actions": {
              "iam:CreateOpenIDConnectProvider": "register the cluster's OIDC issuer in IAM when the cluster is created",
              "iam:GetOpenIDConnectProvider": "read the provider on every deployment to detect changes",
              "iam:TagOpenIDConnectProvider": "tag the provider with the cluster tags",
              "iam:UntagOpenIDConnectProvider": "remove a tag that is no longer set on the cluster",
              "iam:AddClientIDToOpenIDConnectProvider": "add an allowed audience to the provider (today: sts.amazonaws.com only)",
              "iam:UpdateOpenIDConnectProviderThumbprint": "update the fingerprint of the issuer's certificate when AWS changes it",
              "iam:DeleteOpenIDConnectProvider": "delete the provider when the cluster is deleted"
            }
          },
          "IamServiceLinkedRolesScoped": {
            "Why": "Some AWS services need a service-linked role in the account, created by AWS on behalf of the caller the first time the service is used. Limited to the services Qovery uses.",
            "Actions": {
              "iam:CreateServiceLinkedRole": "let AWS create the service-linked role of one of the services below"
            },
            "AWSServiceNames": {
              "eks.amazonaws.com": "AWSServiceRoleForAmazonEKS, created with the first EKS cluster of the account",
              "eks-nodegroup.amazonaws.com": "AWSServiceRoleForAmazonEKSNodegroup, created with the first EKS managed node group",
              "autoscaling.amazonaws.com": "AWSServiceRoleForAutoScaling, used by the Auto Scaling groups behind the EKS node groups",
              "elasticache.amazonaws.com": "AWSServiceRoleForElastiCache, required by ElastiCache to create the cache subnet group of the cluster's VPC, used by the Redis databases managed by Qovery and the ElastiCache clusters deployed with a Qovery Blueprint",
              "elasticfilesystem.amazonaws.com": "AWSServiceRoleForAmazonElasticFileSystem, created by EFS with the first file system of the account, needed when the aws_eks_enable_efs_addon advanced setting is on",
              "kafka.amazonaws.com": "AWSServiceRoleForKafka, created with the first Amazon MSK cluster of the account, deployed with a Qovery Blueprint",
              "mq.amazonaws.com": "AWSServiceRoleForAmazonMQ, created with the first Amazon MQ broker of the account, deployed with a Qovery Blueprint",
              "rds.amazonaws.com": "AWSServiceRoleForRDS, created with the first RDS instance of the account, used by the PostgreSQL, MySQL and MongoDB (DocumentDB) databases managed by Qovery",
              "spot.amazonaws.com": "AWSServiceRoleForEC2Spot, needed by Karpenter to run spot instances: Qovery creates it when spot instances are enabled on the cluster and the account doesn't have it yet"
            }
          },
          "KmsCreateKeyScoped": {
            "Why": [
              "Qovery creates 2 KMS keys per cluster, to encrypt its S3 buckets: Loki logs and Thanos metrics.",
              "Condition: a key must be created with the QoveryProduct=EKS tag, which Qovery sets on every key it creates."
            ],
            "Actions": {
              "kms:CreateKey": "create the logs and metrics encryption keys"
            }
          },
          "KmsManageKeysScoped": {
            "Why": "Manage the keys of KmsCreateKeyScoped. Condition: only keys tagged QoveryProduct=EKS, so the other keys of the account are out of reach.",
            "Actions": {
              "kms:DescribeKey": "read a key on every deployment to detect changes",
              "kms:GetKeyPolicy": "read a key's policy",
              "kms:GetKeyRotationStatus": "check that automatic key rotation is on",
              "kms:ListResourceTags": "read a key's tags",
              "kms:EnableKeyRotation": "turn on automatic key rotation (yearly)",
              "kms:UpdateKeyDescription": "update a key's description",
              "kms:TagResource": "add or update the cluster tags on a key",
              "kms:UntagResource": "remove a tag that is no longer set on the cluster",
              "kms:ScheduleKeyDeletion": "delete the keys when the cluster is deleted (AWS waits 30 days before removing them)"
            }
          },
          "KmsCreateGrantOwnKey": {
            "Why": [
              "Your own KMS key, if you set one in the cluster's aws_eks_encrypt_secrets_kms_key_arn advanced setting to encrypt its Kubernetes secrets.",
              "Only written when the AwsEksEncryptSecretsKmsKeyArn parameter is set, and scoped to that key: your key isn't created by Qovery, so it has no name or tag Qovery can rely on, hence its ARN."
            ],
            "Actions": {
              "kms:DescribeKey": "read your key to check it before EKS uses it",
              "kms:CreateGrant": "let EKS use your key to encrypt the cluster's Kubernetes secrets"
            }
          },
          "DenySelfModification": {
            "Why": [
              "This role's name starts with qovery-, so IamManageQoveryIdentities would let it change its own trust policy and permissions, e.g. to give itself more access.",
              "Explicit deny on this role only: it overrides the allow above. Qovery can still read the role (iam:GetRole, iam:SimulatePrincipalPolicy, ...), and updating this role is done through your CloudFormation stack, not by the role itself."
            ],
            "Actions": {
              "iam:UpdateAssumeRolePolicy": "change who can assume the role",
              "iam:UpdateRole": "change the role's settings",
              "iam:UpdateRoleDescription": "change the role's description",
              "iam:PutRolePolicy": "add or replace an inline policy",
              "iam:DeleteRolePolicy": "remove an inline policy",
              "iam:AttachRolePolicy": "attach a managed policy",
              "iam:DetachRolePolicy": "detach a managed policy",
              "iam:PutRolePermissionsBoundary": "set a permissions boundary",
              "iam:DeleteRolePermissionsBoundary": "remove the permissions boundary",
              "iam:TagRole": "add or update a tag",
              "iam:UntagRole": "remove a tag",
              "iam:DeleteRole": "delete the role"
            }
          },
          "DenyRestrictedRoleModification": {
            "Why": [
              "The restricted permissions role is named qovery-restricted-user-role by default, so IamManageQoveryIdentities would let this role change its trust policy and permissions, e.g. to give it more access than you granted.",
              "Explicit deny on the role named by the RestrictedQoveryUserRole parameter, same actions as DenySelfModification. Qovery can still read it. Updating the restricted role is done through its own CloudFormation stack.",
              "Only written when the RestrictedQoveryUserRole parameter is set: it is optional, as this role can be the only one used by your clusters, without a restricted role."
            ],
            "Actions": {
              "iam:UpdateAssumeRolePolicy": "change who can assume the restricted role",
              "iam:UpdateRole": "change the restricted role's settings",
              "iam:UpdateRoleDescription": "change the restricted role's description",
              "iam:PutRolePolicy": "add or replace an inline policy",
              "iam:DeleteRolePolicy": "remove an inline policy",
              "iam:AttachRolePolicy": "attach a managed policy",
              "iam:DetachRolePolicy": "detach a managed policy",
              "iam:PutRolePermissionsBoundary": "set a permissions boundary",
              "iam:DeleteRolePermissionsBoundary": "remove the permissions boundary",
              "iam:TagRole": "add or update a tag",
              "iam:UntagRole": "remove a tag",
              "iam:DeleteRole": "delete the restricted role"
            }
          }
        }
      },
      "Properties": {
        "RoleName": {
          "Ref": "RoleName"
        },
        "MaxSessionDuration": 28800,
        "Policies": [
          {
            "PolicyName": "qovery-permissions",
            "PolicyDocument": {
              "Version": "2012-10-17",
              "Statement": [
                {
                  "Sid": "AwsServicesUnscoped",
                  "Effect": "Allow",
                  "Action": [
                    "application-autoscaling:*",
                    "autoscaling:*",
                    "cloudtrail:LookupEvents",
                    "cloudwatch:*",
                    "ec2:*",
                    "ecr:*",
                    "eks:*",
                    "elasticache:*",
                    "elasticloadbalancing:*",
                    "es:AddTags",
                    "es:CreateElasticsearchDomain",
                    "es:DeleteElasticsearchDomain",
                    "es:DescribeElasticsearchDomain",
                    "es:ListTags",
                    "es:RemoveTags",
                    "events:DeleteRule",
                    "events:DescribeRule",
                    "events:ListRuleNamesByTarget",
                    "events:ListTagsForResource",
                    "events:ListTargetsByRule",
                    "events:PutRule",
                    "events:PutTargets",
                    "events:RemoveTargets",
                    "events:TagResource",
                    "events:UntagResource",
                    "logs:*",
                    "organizations:DescribeAccount",
                    "organizations:DescribeOrganization",
                    "organizations:DescribeOrganizationalUnit",
                    "organizations:DescribePolicy",
                    "organizations:ListChildren",
                    "organizations:ListParents",
                    "organizations:ListPolicies",
                    "organizations:ListPoliciesForTarget",
                    "organizations:ListRoots",
                    "organizations:ListTargetsForPolicy",
                    "rds:*",
                    "s3:ListAllMyBuckets",
                    "servicequotas:GetServiceQuota",
                    "tag:GetResources"
                  ],
                  "Resource": "*"
                },
                {
                  "Fn::If": [
                    "EksEfsAddonEnabled",
                    {
                      "Sid": "EfsUnscoped",
                      "Effect": "Allow",
                      "Action": "elasticfilesystem:*",
                      "Resource": "*"
                    },
                    {
                      "Ref": "AWS::NoValue"
                    }
                  ]
                },
                {
                  "Sid": "S3SqsQoveryScoped",
                  "Effect": "Allow",
                  "Action": [
                    "s3:*",
                    "sqs:*"
                  ],
                  "Resource": [
                    "arn:aws:s3:::qovery*",
                    "arn:aws:s3:::qovery*/*",
                    "arn:aws:sqs:*:*:qovery*",
                    "arn:aws:sqs:*:*:qovery*/*"
                  ]
                },
                {
                  "Sid": "SimulatePolicyFromQovery",
                  "Effect": "Allow",
                  "Action": [
                    "iam:SimulatePrincipalPolicy"
                  ],
                  "Resource": {
                    "Fn::Sub": "arn:aws:iam::${AWS::AccountId}:role/${RoleName}"
                  }
                },
                {
                  "Sid": "IamManageQoveryIdentities",
                  "Effect": "Allow",
                  "Action": [
                    "iam:CreateRole",
                    "iam:CreatePolicyVersion",
                    "iam:GetRole",
                    "iam:TagRole",
                    "iam:UntagRole",
                    "iam:ListRoleTags",
                    "iam:ListRolePolicies",
                    "iam:ListInstanceProfilesForRole",
                    "iam:UpdateAssumeRolePolicy",
                    "iam:UpdateRoleDescription",
                    "iam:CreatePolicy",
                    "iam:GetPolicy",
                    "iam:GetPolicyVersion",
                    "iam:ListPolicyVersions",
                    "iam:DeletePolicyVersion",
                    "iam:PutRolePolicy",
                    "iam:GetRolePolicy",
                    "iam:AttachRolePolicy",
                    "iam:ListAttachedRolePolicies",
                    "iam:CreateInstanceProfile",
                    "iam:GetInstanceProfile",
                    "iam:AddRoleToInstanceProfile",
                    "iam:TagInstanceProfile",
                    "iam:UntagInstanceProfile",
                    "iam:PassRole",
                    "iam:DeleteRole",
                    "iam:DeleteRolePolicy",
                    "iam:DeletePolicy",
                    "iam:DetachRolePolicy",
                    "iam:RemoveRoleFromInstanceProfile",
                    "iam:DeleteInstanceProfile"
                  ],
                  "Resource": [
                    "arn:aws:iam::*:role/qovery-*",
                    "arn:aws:iam::*:policy/qovery-*",
                    "arn:aws:iam::*:instance-profile/qovery-*",
                    "arn:aws:iam::*:role/Karpenter*",
                    "arn:aws:iam::*:instance-profile/KarpenterNodeInstanceProfile-*",
                    "arn:aws:iam::*:role/eks-ebs-csi-plugin-*",
                    "arn:aws:iam::*:role/eks-efs-csi-plugin-*",
                    "arn:aws:iam::*:policy/AmazonEKS_EBS_CSI_Policy-*",
                    "arn:aws:iam::*:policy/AmazonEKS_EFS_CSI_Policy-*",
                    "arn:aws:iam::*:role/aws-service-role/spot.amazonaws.com/AWSServiceRoleForEC2Spot",
                    "arn:aws:iam::*:role/aws-service-role/eks-nodegroup.amazonaws.com/AWSServiceRoleForAmazonEKSNodegroup"
                  ]
                },
                {
                  "Sid": "IamPassRdsMonitoringRole",
                  "Effect": "Allow",
                  "Action": "iam:PassRole",
                  "Resource": "arn:aws:iam::*:role/*",
                  "Condition": {
                    "StringEquals": {
                      "iam:PassedToService": "monitoring.rds.amazonaws.com"
                    }
                  }
                },
                {
                  "Sid": "IamServiceLinkedRoleLookup",
                  "Effect": "Allow",
                  "Action": "iam:GetRole",
                  "Resource": [
                    "arn:aws:iam::*:role/aws-service-role/*",
                    "arn:aws:iam::*:role/AWSServiceRoleFor*"
                  ]
                },
                {
                  "Sid": "IamOidcProviderScoped",
                  "Effect": "Allow",
                  "Action": [
                    "iam:CreateOpenIDConnectProvider",
                    "iam:GetOpenIDConnectProvider",
                    "iam:TagOpenIDConnectProvider",
                    "iam:UntagOpenIDConnectProvider",
                    "iam:AddClientIDToOpenIDConnectProvider",
                    "iam:UpdateOpenIDConnectProviderThumbprint",
                    "iam:DeleteOpenIDConnectProvider"
                  ],
                  "Resource": "arn:aws:iam::*:oidc-provider/oidc.eks.*"
                },
                {
                  "Sid": "IamServiceLinkedRolesScoped",
                  "Effect": "Allow",
                  "Action": "iam:CreateServiceLinkedRole",
                  "Resource": "*",
                  "Condition": {
                    "StringEquals": {
                      "iam:AWSServiceName": [
                        "eks.amazonaws.com",
                        "eks-nodegroup.amazonaws.com",
                        "autoscaling.amazonaws.com",
                        "elasticache.amazonaws.com",
                        "elasticfilesystem.amazonaws.com",
                        "kafka.amazonaws.com",
                        "mq.amazonaws.com",
                        "rds.amazonaws.com",
                        "spot.amazonaws.com"
                      ]
                    }
                  }
                },
                {
                  "Sid": "KmsCreateKeyScoped",
                  "Effect": "Allow",
                  "Action": "kms:CreateKey",
                  "Resource": "*",
                  "Condition": {
                    "StringEquals": {
                      "aws:RequestTag/QoveryProduct": "EKS"
                    }
                  }
                },
                {
                  "Sid": "KmsManageKeysScoped",
                  "Effect": "Allow",
                  "Action": [
                    "kms:DescribeKey",
                    "kms:GetKeyPolicy",
                    "kms:GetKeyRotationStatus",
                    "kms:ListResourceTags",
                    "kms:EnableKeyRotation",
                    "kms:UpdateKeyDescription",
                    "kms:TagResource",
                    "kms:UntagResource",
                    "kms:ScheduleKeyDeletion"
                  ],
                  "Resource": "*",
                  "Condition": {
                    "StringEquals": {
                      "aws:ResourceTag/QoveryProduct": "EKS"
                    }
                  }
                },
                {
                  "Fn::If": [
                    "HasEksEncryptSecretsKmsKey",
                    {
                      "Sid": "KmsCreateGrantOwnKey",
                      "Effect": "Allow",
                      "Action": [
                        "kms:DescribeKey",
                        "kms:CreateGrant"
                      ],
                      "Resource": {
                        "Ref": "AwsEksEncryptSecretsKmsKeyArn"
                      }
                    },
                    {
                      "Ref": "AWS::NoValue"
                    }
                  ]
                },
                {
                  "Sid": "DenySelfModification",
                  "Effect": "Deny",
                  "Action": [
                    "iam:UpdateAssumeRolePolicy",
                    "iam:UpdateRole",
                    "iam:UpdateRoleDescription",
                    "iam:PutRolePolicy",
                    "iam:DeleteRolePolicy",
                    "iam:AttachRolePolicy",
                    "iam:DetachRolePolicy",
                    "iam:PutRolePermissionsBoundary",
                    "iam:DeleteRolePermissionsBoundary",
                    "iam:TagRole",
                    "iam:UntagRole",
                    "iam:DeleteRole"
                  ],
                  "Resource": {
                    "Fn::Sub": "arn:aws:iam::${AWS::AccountId}:role/${RoleName}"
                  }
                },
                {
                  "Fn::If": [
                    "HasRestrictedQoveryUserRole",
                    {
                      "Sid": "DenyRestrictedRoleModification",
                      "Effect": "Deny",
                      "Action": [
                        "iam:UpdateAssumeRolePolicy",
                        "iam:UpdateRole",
                        "iam:UpdateRoleDescription",
                        "iam:PutRolePolicy",
                        "iam:DeleteRolePolicy",
                        "iam:AttachRolePolicy",
                        "iam:DetachRolePolicy",
                        "iam:PutRolePermissionsBoundary",
                        "iam:DeleteRolePermissionsBoundary",
                        "iam:TagRole",
                        "iam:UntagRole",
                        "iam:DeleteRole"
                      ],
                      "Resource": {
                        "Fn::Sub": "arn:aws:iam::${AWS::AccountId}:role/${RestrictedQoveryUserRole}"
                      }
                    },
                    {
                      "Ref": "AWS::NoValue"
                    }
                  ]
                }
              ]
            }
          }
        ],
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Fn::If": [
                "QoveryDeployerCanAssumeRole",
                {
                  "Effect": "Allow",
                  "Principal": {
                    "AWS": [
                      "arn:aws:iam::283389881690:role/qovery-deployer",
                      "arn:aws:iam::283389881690:user/qovery-deployer"
                    ]
                  },
                  "Action": "sts:AssumeRole",
                  "Condition": {
                    "StringLike": {
                      "sts:ExternalId": {
                        "Fn::Sub": "${OrganizationId}:*"
                      }
                    }
                  }
                },
                {
                  "Effect": "Deny",
                  "Principal": {
                    "AWS": "*"
                  },
                  "Action": "sts:AssumeRole"
                }
              ]
            }
          ]
        }
      }
    }
  },
  "Outputs": {
    "RoleARN": {
      "Description": "Arn of the created role. This is the information to give in Qovery console",
      "Value": {
        "Fn::GetAtt": [
          "Role",
          "Arn"
        ]
      },
      "Export": {
        "Name": {
          "Fn::Sub": "${AWS::StackName}-RoleARN"
        }
      }
    }
  }
}
