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

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

provider "aws" {
  region  = var.aws_region
  alias   = "networking"
  profile = "networking"

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/royaltyshare-sftp/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_route53_zone" "route53_zone" {
  name = "theorchard.io."
}

data "aws_route53_zone" "route53_zone_networking" {
  provider = aws.networking

  name = "theorchard.io."
}

module "aws_transfer_family" {
  # checkov:skip=CKV_AWS_380: Ensure AWS Transfer Server uses latest Security Policy, some old clients may not support latest security policies

  source                                            = "git@github.com:theorchard/terraform-aws-transfer-family//?ref=1.0.1"
  service_name                                      = var.service_name
  environment                                       = var.environment
  aws_transfer_server_api_gateway_lambda_arn        = module.lambda_aws_ftp.lambda_arn
  aws_transfer_server_api_gateway_lambda_invoke_arn = module.lambda_aws_ftp.lambda_invoke_arn
  vpc_id                                            = data.aws_vpc.vpc.id
  block_waf_enabled                                 = true
}

module "lambda_aws_ftp" {
  source                   = "git@github.com:theorchard/terraform-lambda.git//?ref=5.2.3"
  application_family       = var.application_family
  environment              = var.environment
  lambda_name              = var.service_name
  lambda_description       = "A function to lookup and return user data from AWS Secrets Manager."
  lambda_function_handler  = "index.lambda_handler"
  lambda_runtime           = "python3.10"
  vpc_enabled              = true
  vpc_id                   = data.aws_vpc.vpc.id
  vpc_subnet_ids           = data.aws_subnets.private_subnets.ids
  datadog_advanced_enabled = false

  lambda_function_environment_variables = {
    ENVIRONMENT            = var.environment
    SERVICE_NAME           = var.service_name
    SECRETS_MANAGER_REGION = var.aws_region
  }
}

# Assume role policy document
data "aws_iam_policy_document" "royaltyshare_sftp_assume_role_policy_document" {
  statement {
    actions = [
      "sts:AssumeRole",
    ]

    principals {
      type = "Service"
      identifiers = [
        "transfer.amazonaws.com",
      ]
    }
  }
}

# Role policy document
data "aws_iam_policy_document" "royaltyshare_sftp_write_role_policy_document" {
  statement {
    sid    = "AllowListingOfUserFolder"
    effect = "Allow"
    resources = [
      "arn:aws:s3:::orcd-rs-sftp"
    ]

    actions = [
      "s3:ListBucket",
      "s3:GetBucketLocation",
    ]
  }

  statement {
    sid    = "HomeDirObjectAccess"
    effect = "Allow"
    resources = [
      "arn:aws:s3:::orcd-rs-sftp/*"
    ]

    actions = [
      "s3:PutObject",
      "s3:GetObject",
      "s3:DeleteObjectVersion",
      "s3:DeleteObject",
      "s3:GetObjectVersion",
    ]
  }
}

# Role read/write policy to access the bucket
resource "aws_iam_policy" "royaltyshare_sftp_write_role_policy" {
  name        = "${var.environment}-${var.service_name}-RW-policy"
  description = "royaltyshare sftp read, write policy"
  policy      = data.aws_iam_policy_document.royaltyshare_sftp_write_role_policy_document.json
}

# Read/Write role
resource "aws_iam_role" "royaltyshare_sftp_write_role" {
  name               = "${var.environment}-${var.service_name}-RW-role"
  assume_role_policy = data.aws_iam_policy_document.royaltyshare_sftp_assume_role_policy_document.json
}

# Attach iam policy to the read/write role
resource "aws_iam_role_policy_attachment" "royaltyshare_sftp_write_role_attachment" {
  policy_arn = aws_iam_policy.royaltyshare_sftp_write_role_policy.arn
  role       = aws_iam_role.royaltyshare_sftp_write_role.name
}

# Create a vanity DNS name
resource "aws_route53_record" "royaltyshare_sftp_dns_record" {
  zone_id = data.aws_route53_zone.route53_zone.zone_id
  name    = "${var.environment}-${var.service_name}"
  type    = "CNAME"
  ttl     = "300"
  records = [
    "${module.aws_transfer_family.aws_transfer_server_endpoint_output}"
  ]
}

resource "aws_route53_record" "royaltyshare_sftp_dns_record_networking" {
  provider = aws.networking

  zone_id = data.aws_route53_zone.route53_zone_networking.zone_id
  name    = "${var.environment}-${var.service_name}"
  type    = "CNAME"
  ttl     = "300"
  records = [
    "${module.aws_transfer_family.aws_transfer_server_endpoint_output}"
  ]
}

data "aws_vpc" "vpc" {
  tags = {
    Name = var.environment
  }
}


data "aws_subnets" "private_subnets" {
  filter {
    name   = "vpc-id"
    values = [data.aws_vpc.vpc.id]
  }

  tags = {
    Name = "prod_private_subnet_*"
    tier = "private"
  }
}
