module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=2.0.0"
  environment        = var.environment
  application_family = var.application_family
  team_name          = var.team_name
  service_name       = var.service_name
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "shared-orcd-terraform-state"
    key     = "shared/gsirt/security-lake/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_kms_key" "guardduty_log_bucket_key" {
  key_id = "alias/${var.environment}-orcd-guardduty-logs-key"
}

data "aws_iam_policy_document" "assume_role_policy_document" {
  statement {
    effect  = "Allow"
    actions = ["sts:AssumeRole"]
    principals {
      type        = "Service"
      identifiers = ["events.amazonaws.com"]
    }
  }

  statement {
    effect  = "Allow"
    actions = ["sts:AssumeRole"]
    principals {
      type = "AWS"
      identifiers = [
        "arn:aws:iam::${var.security_lake_account_id}:role/Sony-Security-Lake-ocsf-transformer-us-east-1",
        "arn:aws:iam::${var.security_lake_account_id}:role/Sony-Security-Lake-ocsf-transformer-eu-central-1",
      ]
    }
  }
}

resource "aws_iam_role" "security_lake_role" {
  name               = var.security_lake_iam_role_name
  assume_role_policy = data.aws_iam_policy_document.assume_role_policy_document.json
}

data "aws_iam_policy_document" "eventbridge_forward_policy_document" {
  statement {
    effect  = "Allow"
    actions = ["events:PutEvents"]
    resources = [
      "arn:aws:events:*:${var.security_lake_account_id}:event-bus/default",
    ]
  }
}

resource "aws_iam_policy" "eventbridge_policy" {
  name   = "Eventbridge-${var.security_lake_iam_role_name}-put-events"
  policy = data.aws_iam_policy_document.eventbridge_forward_policy_document.json
}

resource "aws_iam_role_policy_attachment" "eventbridge_forward" {
  role       = aws_iam_role.security_lake_role.name
  policy_arn = aws_iam_policy.eventbridge_policy.arn
}

module "s3_read_policy" {
  source = "git@github.com:theorchard/terraform-iam-policy-templates.git//documents/s3/buckets_with_prefixes?ref=0.4.0"

  buckets = [
    for bucket_name_prefix in var.s3_bucket_name_prefixes :
    {
      bucket = bucket_name_prefix
    }
  ]
  action_types = ["read"]
}

resource "aws_iam_policy" "s3_read_policy" {
  name   = "S3-${var.environment}-${var.service_name}-RO"
  policy = module.s3_read_policy.policy.json
}

resource "aws_iam_role_policy_attachment" "s3_read_policy" {
  role       = aws_iam_role.security_lake_role.name
  policy_arn = aws_iam_policy.s3_read_policy.arn
}

data "aws_iam_policy_document" "kms_decrypt_policy_document" {
  statement {
    effect  = "Allow"
    actions = [
      "kms:Decrypt",
      "kms:DescribeKey",
    ]
    resources = [
      data.aws_kms_key.guardduty_log_bucket_key.arn,
    ]
  }
}

resource "aws_iam_policy" "kms_decrypt_policy" {
  name   = "KMS-${var.environment}-${var.service_name}-Decrypt"
  policy = data.aws_iam_policy_document.kms_decrypt_policy_document.json
}

resource "aws_iam_role_policy_attachment" "kms_decrypt_policy" {
  role       = aws_iam_role.security_lake_role.name
  policy_arn = aws_iam_policy.kms_decrypt_policy.arn
}
