{
  "Version": "2012-10-17",
  "Id": "cmk-key-policy-1",
  "Statement": [
    {
      "Sid": "EnableRoleDelegation",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::${AWS::AccountId}:root"
      },
      "Action": "kms:*",
      "Resource": "*"
    },
    {
      "Sid": "AtlassianGrantCreation",
      "Effect": "Allow",
      "Principal": "*",
      "Action": [
        "kms:CreateGrant"
      ],
      "Resource": "*",
      "Condition": {
        "StringEquals": {
          "kms:GrantConstraintType": "EncryptionContextSubset"
        },
        "ForAnyValue:StringLike": {
          "aws:PrincipalOrgPaths": ["o-rab3nm4fez/*/ou-6ypf-7xjy6ark/*"]
        },
        "ArnLike": {
          "aws:PrincipalArn": "arn:aws:iam::*:role/security-resources-byok-key-management"
        }
      }
    },
    {
      "Sid": "AtlassianGrantManagement",
      "Effect": "Allow",
      "Principal": "*",
      "Action": [
        "kms:RevokeGrant",
        "kms:ListGrants",
        "kms:RetireGrant"
      ],
      "Resource": "*",
      "Condition": {
        "ForAnyValue:StringLike": {
          "aws:PrincipalOrgPaths": ["o-rab3nm4fez/*/ou-6ypf-7xjy6ark/*"]
        },
        "ArnLike": {
          "aws:PrincipalArn": "arn:aws:iam::*:role/security-resources-byok-key-management"
        }
      }
    },
    {
      "Sid": "AwsManagedService",
      "Effect": "Allow",
      "Principal": "*",
      "Action": [
        "kms:CreateGrant"
      ],
      "Resource": "*",
      "Condition": {
        "StringEquals": {
          "kms:ViaService": [
                "rds.${AWS::Region}.amazonaws.com",
                "aoss.${AWS::Region}.amazonaws.com",
                "es.${AWS::Region}.amazonaws.com"
          ]
        },
        "ForAnyValue:StringLike": {
          "aws:PrincipalOrgPaths": ${OrganisationalUnits}
        }
      }
    },
    {
      "Sid": "AtlassianDescribeKey",
      "Effect": "Allow",
      "Principal": "*",
      "Action": [
        "kms:DescribeKey"
      ],
      "Resource": "*",
      "Condition": {
        "ForAnyValue:StringLike": {
          "aws:PrincipalOrgPaths": ["o-rab3nm4fez/*"]
        }
      }
    },
    {
      "Sid": "AtlassianRdsPerformanceInsightsUsage",
      "Effect": "Allow",
      "Principal": "*",
      "Action": [
        "kms:GenerateDataKey",
        "kms:Decrypt"
      ],
      "Resource": "*",
      "Condition": {
        "ForAnyValue:StringLike": {
          "aws:PrincipalOrgPaths": ${OrganisationalUnits}
        },
        "StringEquals": {
          "kms:ViaService": [
                "rds.${AWS::Region}.amazonaws.com"
          ]
        }
      }
    }
  ]
}
