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
}

provider "aws" {
  region = "us-east-1"

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "prod-accounting-terraform-state"
    key     = "prod/us-east-1/vpc-endpoints/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

data "aws_vpc" "vpc" {
  tags = {
    Name = "${var.environment}-terraform-aws-vpc"
  }
}

data "aws_route_tables" "all_vpc_route_tables" {
  vpc_id = data.aws_vpc.vpc.id
}

locals {
  subnet_ids_string = join(",", data.aws_subnets.private_subnets.ids)
  subnet_ids_list   = split(",", local.subnet_ids_string)
}

data "aws_vpc_endpoint_service" "s3" {
  service      = "s3"
  service_type = "Gateway"
}

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

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

data "aws_subnet" "private_subnets" {
  count = length(data.aws_subnets.private_subnets.ids)
  id    = element(local.subnet_ids_list, count.index)
}

resource "aws_vpc_endpoint" "gsirt" {
  vpc_id            = data.aws_vpc.vpc.id
  service_name      = "com.amazonaws.vpce.us-east-1.vpce-svc-0172c4b1d11bfe606"
  vpc_endpoint_type = "Interface"

  security_group_ids = [
    aws_security_group.gsirt_vpc_endpoint_security_group.id,
  ]

  subnet_ids = data.aws_subnet.private_subnets.*.id

  tags = {
    Name         = "${var.environment}-gsirt-vpc-endpoint"
    service_name = "vpc_endpoint"
    terraformed  = true
  }
}

# Security group for VPC endpoint
resource "aws_security_group" "gsirt_vpc_endpoint_security_group" {
  name        = "${var.environment}-gsirt-vpc-endpoint-security-group"
  description = "Allow GSIRT VPC endpoint traffic"
  vpc_id      = data.aws_vpc.vpc.id

  ingress {
    description = "syslog"
    from_port   = 514
    to_port     = 514
    protocol    = "tcp"
    cidr_blocks = tolist([data.aws_vpc.vpc.cidr_block])
  }

  ingress {
    description = "splunk"
    from_port   = 9998
    to_port     = 9998
    protocol    = "tcp"
    cidr_blocks = tolist([data.aws_vpc.vpc.cidr_block])
  }

  ingress {
    description = "splunk"
    from_port   = 8089
    to_port     = 8089
    protocol    = "tcp"
    cidr_blocks = tolist([data.aws_vpc.vpc.cidr_block])
  }

  ingress {
    description = "splunk-switchboard"
    from_port   = 9998
    to_port     = 9998
    protocol    = "tcp"
    cidr_blocks = ["10.110.0.0/16"]
  }

  ingress {
    description = "splunk-switchboard"
    from_port   = 8089
    to_port     = 8089
    protocol    = "tcp"
    cidr_blocks = ["10.110.0.0/16"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name           = "${var.environment}-gsirt-vpc-endpoint-security-group"
    service_name   = "vpc_endpoint"
    terraformed    = true
    eiso-exception = "aws.08.30"
  }
}
