{
  "schemaVersion": 1,
  "contractVersion": "solar-scanner-policy.v2",
  "source": {
    "repository": "https://github.com/plexlabs-tech/solar-ai",
    "path": "infra/solar-onboarding.yml",
    "commit": "e0bfc381eb486f54a340f83adf8250b79bb2a298",
    "sha256": "afbe0474f64028a3d48d65d6f8dd3281979ac4a41cb802c3ff75237cdc33ade9"
  },
  "note": "Exact policy documents extracted from the CloudFormation template. Ref and Fn:: expressions are unresolved CloudFormation references, not tenant values. Use the template through SolarCloud onboarding; this bundle cannot be attached directly as an IAM policy.",
  "parameters": {
    "TrustedPrincipalArn": {
      "Type": "String",
      "Description": "The Solar principal that can assume the scanner role"
    },
    "ExternalId": {
      "Type": "String",
      "Description": "External ID used by Solar when assuming roles"
    },
    "ScannerRoleName": {
      "Type": "String",
      "Default": "SolarCloudScanner",
      "Description": "Name of the read-only scanner role"
    },
    "BillingExportPrefix": {
      "Type": "String",
      "Default": "solar/billing/cur2",
      "Description": "Prefix inside the billing export bucket for CUR 2.0 files"
    },
    "BillingExportBucketName": {
      "Type": "String",
      "Default": "",
      "Description": "Optional S3 bucket name for billing exports. Leave blank to let CloudFormation name it."
    },
    "EnableDiagnostics": {
      "Type": "String",
      "Default": "false",
      "AllowedValues": [
        "true",
        "false"
      ],
      "Description": "Optionally allow bounded metadata and metrics investigations. Log and secret contents remain inaccessible."
    }
  },
  "conditions": {
    "UseCustomBillingBucketName": {
      "Fn::Not": [
        {
          "Fn::Equals": [
            {
              "Ref": "BillingExportBucketName"
            },
            ""
          ]
        }
      ]
    },
    "DiagnosticsEnabled": {
      "Fn::Equals": [
        {
          "Ref": "EnableDiagnostics"
        },
        "true"
      ]
    }
  },
  "trustPolicy": {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Effect": "Allow",
        "Principal": {
          "AWS": {
            "Ref": "TrustedPrincipalArn"
          }
        },
        "Action": "sts:AssumeRole",
        "Condition": {
          "StringEquals": {
            "sts:ExternalId": {
              "Ref": "ExternalId"
            }
          }
        }
      }
    ]
  },
  "policies": [
    {
      "condition": null,
      "enabledByDefault": true,
      "PolicyName": "SolarInventoryReadOnlyV1",
      "PolicyDocument": {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Sid": "SupportedInventory",
            "Effect": "Allow",
            "Action": [
              "apigateway:GET",
              "cloudformation:DescribeStacks",
              "cloudformation:ListStacks",
              "cloudwatch:GetMetricData",
              "config:DescribeConfigurationRecorders",
              "config:DescribeConfigurationRecorderStatus",
              "config:DescribeDeliveryChannels",
              "cloudfront:ListDistributions",
              "dynamodb:DescribeTable",
              "dynamodb:ListTables",
              "ec2:DescribeInstances",
              "ec2:DescribeRegions",
              "ec2:DescribeSecurityGroups",
              "ec2:DescribeSubnets",
              "ec2:DescribeVolumes",
              "ec2:DescribeVpcs",
              "ecr:DescribeImages",
              "ecr:DescribeRepositories",
              "ecs:DescribeClusters",
              "ecs:DescribeServices",
              "ecs:ListClusters",
              "ecs:ListServices",
              "elasticloadbalancing:DescribeLoadBalancers",
              "elasticloadbalancing:DescribeTargetGroups",
              "elasticloadbalancing:DescribeTargetHealth",
              "iam:ListRoles",
              "iot:ListThings",
              "kafka:ListClustersV2",
              "lambda:ListFunctions",
              "lambda:ListEventSourceMappings",
              "logs:DescribeLogGroups",
              "rds:DescribeDBInstances",
              "route53:ListHostedZones",
              "route53:ListResourceRecordSets",
              "s3:GetBucketLocation",
              "s3:ListAllMyBuckets",
              "sns:ListTopics",
              "sqs:ListQueues",
              "tag:GetResources"
            ],
            "Resource": "*"
          },
          {
            "Sid": "VerifyScannerRolePolicy",
            "Effect": "Allow",
            "Action": [
              "iam:GetRole",
              "iam:GetRolePolicy",
              "iam:ListAttachedRolePolicies",
              "iam:ListRolePolicies"
            ],
            "Resource": {
              "Fn::Sub": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${ScannerRoleName}"
            }
          }
        ]
      }
    },
    {
      "condition": null,
      "enabledByDefault": true,
      "PolicyName": "SolarBillingReadOnlyV1",
      "PolicyDocument": {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Sid": "CostExplorerRead",
            "Effect": "Allow",
            "Action": [
              "ce:GetCostAndUsage",
              "ce:GetCostForecast"
            ],
            "Resource": "*"
          },
          {
            "Sid": "BillingExportList",
            "Effect": "Allow",
            "Action": [
              "s3:ListBucket"
            ],
            "Resource": {
              "Fn::GetAtt": [
                "BillingExportBucket",
                "Arn"
              ]
            },
            "Condition": {
              "StringLike": {
                "s3:prefix": [
                  {
                    "Ref": "BillingExportPrefix"
                  },
                  {
                    "Fn::Sub": "${BillingExportPrefix}/*"
                  }
                ]
              }
            }
          },
          {
            "Sid": "BillingExportObjects",
            "Effect": "Allow",
            "Action": [
              "s3:GetObject"
            ],
            "Resource": {
              "Fn::Sub": "${BillingExportBucket.Arn}/${BillingExportPrefix}/*"
            }
          }
        ]
      }
    },
    {
      "condition": null,
      "enabledByDefault": true,
      "PolicyName": "SolarSelfCleanupV1",
      "PolicyDocument": {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Sid": "VerifyAndEmptySolarBillingBucket",
            "Effect": "Allow",
            "Action": [
              "s3:DeleteBucket",
              "s3:DeleteBucketPolicy",
              "s3:GetBucketTagging",
              "s3:GetBucketPolicy",
              "s3:ListBucket",
              "s3:ListBucketVersions"
            ],
            "Resource": {
              "Fn::GetAtt": [
                "BillingExportBucket",
                "Arn"
              ]
            }
          },
          {
            "Sid": "DeleteSolarBillingObjects",
            "Effect": "Allow",
            "Action": [
              "s3:DeleteObject",
              "s3:DeleteObjectVersion"
            ],
            "Resource": {
              "Fn::Sub": "${BillingExportBucket.Arn}/*"
            }
          },
          {
            "Sid": "DeleteSolarBillingExport",
            "Effect": "Allow",
            "Action": [
              "bcm-data-exports:DeleteExport"
            ],
            "Resource": {
              "Fn::GetAtt": [
                "BillingCur2Export",
                "ExportArn"
              ]
            }
          },
          {
            "Sid": "DeleteSolarScannerRole",
            "Effect": "Allow",
            "Action": [
              "iam:DeleteRole",
              "iam:DeleteRolePolicy",
              "iam:DetachRolePolicy",
              "iam:TagRole",
              "iam:UntagRole"
            ],
            "Resource": {
              "Fn::Sub": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${ScannerRoleName}"
            }
          },
          {
            "Sid": "DeleteOnlyThisOnboardingStack",
            "Effect": "Allow",
            "Action": [
              "cloudformation:DeleteStack",
              "cloudformation:DescribeStackResources"
            ],
            "Resource": {
              "Fn::Sub": "arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${AWS::StackName}/*"
            }
          }
        ]
      }
    },
    {
      "condition": "DiagnosticsEnabled",
      "enabledByDefault": false,
      "PolicyName": "SolarDiagnosticsReadOnlyV1",
      "PolicyDocument": {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Sid": "BoundedDiagnostics",
            "Effect": "Allow",
            "Action": [
              "cloudformation:ListStacks",
              "cloudfront:GetDistribution",
              "cloudtrail:LookupEvents",
              "cloudwatch:DescribeAlarms",
              "cloudwatch:GetMetricData",
              "cloudwatch:GetMetricStatistics",
              "ec2:DescribeInstanceStatus",
              "ec2:DescribeInternetGateways",
              "ec2:DescribeNatGateways",
              "ec2:DescribeNetworkInterfaces",
              "ec2:DescribeRouteTables",
              "ecr:ListImages",
              "eks:DescribeCluster",
              "eks:ListClusters",
              "elasticache:DescribeCacheClusters",
              "elasticache:DescribeReplicationGroups",
              "elasticloadbalancing:DescribeTargetGroups",
              "elasticloadbalancing:DescribeTargetHealth",
              "events:DescribeRule",
              "events:ListRules",
              "events:ListTargetsByRule",
              "kafka:DescribeClusterV2",
              "rds:DescribeDBClusters",
              "sns:GetTopicAttributes",
              "sqs:GetQueueAttributes"
            ],
            "Resource": "*"
          }
        ]
      }
    }
  ],
  "billingBucketPolicy": {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Sid": "AllowBillingDataExportsWrite",
        "Effect": "Allow",
        "Principal": {
          "Service": "bcm-data-exports.amazonaws.com"
        },
        "Action": [
          "s3:PutObject",
          "s3:PutObjectAcl"
        ],
        "Resource": {
          "Fn::Sub": "${BillingExportBucket.Arn}/*"
        },
        "Condition": {
          "StringEquals": {
            "aws:SourceAccount": {
              "Ref": "AWS::AccountId"
            }
          },
          "StringLike": {
            "aws:SourceArn": {
              "Fn::Sub": "arn:${AWS::Partition}:bcm-data-exports:${AWS::Region}:${AWS::AccountId}:export/*"
            }
          }
        }
      },
      {
        "Sid": "AllowBillingDataExportsBucketChecks",
        "Effect": "Allow",
        "Principal": {
          "Service": "bcm-data-exports.amazonaws.com"
        },
        "Action": [
          "s3:GetBucketAcl",
          "s3:GetBucketPolicy",
          "s3:GetBucketLocation",
          "s3:ListBucket"
        ],
        "Resource": {
          "Fn::GetAtt": [
            "BillingExportBucket",
            "Arn"
          ]
        },
        "Condition": {
          "StringEquals": {
            "aws:SourceAccount": {
              "Ref": "AWS::AccountId"
            }
          },
          "StringLike": {
            "aws:SourceArn": {
              "Fn::Sub": "arn:${AWS::Partition}:bcm-data-exports:${AWS::Region}:${AWS::AccountId}:export/*"
            }
          }
        }
      }
    ]
  },
  "createdResources": [
    {
      "logicalId": "ScannerRole",
      "type": "AWS::IAM::Role"
    },
    {
      "logicalId": "BillingExportBucket",
      "type": "AWS::S3::Bucket"
    },
    {
      "logicalId": "BillingExportBucketPolicy",
      "type": "AWS::S3::BucketPolicy"
    },
    {
      "logicalId": "BillingCur2Export",
      "type": "AWS::BCMDataExports::Export"
    }
  ]
}
