locals {
  ingress-alb = [
    { from_port = 443, to_port = 443, source_sg_id = module.sg-alb.sg_id, description = "Allow inbound https from ALBs" },
    { from_port = 80, to_port = 80, source_sg_id = module.sg-jenkins-master.sg_id, description = "Allow inbound http from jenkins master" },
    { from_port = 443, to_port = 443, source_sg_id = module.sg-jenkins-master.sg_id, description = "Allow inbound https from jenkins master" },
  ]
  ingress-jenkins-master = [
    { from_port = 8080, to_port = 8080, source_sg_id = module.sg-alb.sg_id, description = "Allow inbound http from ALBs" },
    { from_port = 5000, to_port = 5000, source_sg_id = module.sg-jenkins-master.sg_id, description = "Allow inbound connections master itself" },
    { from_port = 5000, to_port = 5000, source_sg_id = module.sg-jenkins-ec2-agent.sg_id, description = "Allow inbound connections from EC2 slaves" }
  ]
  ingress-jenkins-ec2-agent = [
    { from_port = 22, to_port = 22, source_sg_id = module.sg-jenkins-master.sg_id, description = "Allow inbound connections from jenkins master" }
  ]
  ingress-endpoint = [
    { from_port = 443, to_port = 443, source_sg_id = module.sg-jenkins-master.sg_id, description = "Allow inbound connections master itself" },
    { from_port = 443, to_port = 443, source_sg_id = module.sg-jenkins-ec2-agent.sg_id, description = "Allow inbound connections from EC2 slaves" },
    { from_port = 443, to_port = 443, source_sg_id = "sg-0ef48b284c0545df5", description = "Allow inbound connections from Atlantis" }, // sg created outside of the module
  ]
  whitelisted_cidrs = concat(module.ip_whitelist.whitelisted_cidrs, var.projectgroup_whitelist)
  vpn_cidrs         = [for cidr in var.vpn_masq_cidrs : { cidr = cidr, description = "Access for VPN users" }]
  access_list = var.external_access_mode == "whitelist" ? local.whitelisted_cidrs : (
    var.external_access_mode == "vpn" ? local.vpn_cidrs : concat(local.vpn_cidrs, local.whitelisted_cidrs)
  )
}

module "ip_whitelist" {
  source = "../../../modules/ip_whitelist"
}

# Create security group for load balancers
module "sg-alb" {
  source           = "../../../modules/vpc/sg"
  sg_description   = "Allows HTTP connections to ALB"
  vpc_id           = var.vpc_id
  protocol         = "tcp"
  ingress_template = local.ingress-alb
  tags = merge(
    var.common_tags,
    {
      "Name" = format("%v-sg-alb", var.name_prefix)
    },
  )
}

module "sg-jenkins-master" {
  source           = "../../../modules/vpc/sg"
  sg_description   = "Allows connections to jenkins master servers"
  vpc_id           = var.vpc_id
  protocol         = "tcp"
  ingress_template = local.ingress-jenkins-master
  tags = merge(
    var.common_tags,
    {
      "Name" = format("%v-sg-jenkins-master", var.name_prefix)
    },
  )
}

module "sg-jenkins-ec2-agent" {
  source           = "../../../modules/vpc/sg"
  sg_description   = "Allows connections to jenkins EC2 agents"
  vpc_id           = var.vpc_id
  protocol         = "tcp"
  ingress_template = local.ingress-jenkins-ec2-agent
  tags = merge(
    var.common_tags,
    {
      "Name" = format("%v-sg-ec2-agent", var.name_prefix)
    },
  )
}

module "sg-endpoint" {
  source = "../../../modules/vpc/sg"

  name             = format("%v-sg-endpoint", var.name_prefix)
  sg_description   = "Allows access to vpc endpoints"
  vpc_id           = var.vpc_id
  protocol         = "tcp"
  ingress_template = local.ingress-endpoint

  tags = merge(
    var.common_tags,
    {
      "Name" = format("%v-sg-endpoint", var.name_prefix)
    },
  )
}

resource "aws_security_group" "remote_management_ssh_access" {
  description = "Allow SSH connections from specified addresses"
  vpc_id      = var.vpc_id

  # SSH
  dynamic "ingress" {
    for_each = local.access_list
    content {
      description = ingress.value["description"]
      from_port   = 22
      to_port     = 22
      protocol    = "tcp"
      cidr_blocks = [ingress.value["cidr"]]
    }
  }

  ingress {
    description     = "Access from transit gateway"
    from_port       = 22
    to_port         = 22
    protocol        = "tcp"
    prefix_list_ids = var.managed_prefix_list_ids
  }

  egress {
    description = "Allow all outbound traffic."
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = merge(
    var.common_tags,
    {
      "Name"           = format("%v-sg-remote-access", var.name_prefix),
      "eiso-exception" = "aws.08.30",
    },
  )
}

resource "aws_security_group" "ext_web_sg" {
  description = "HTTPS access for list of external ip addresses"
  vpc_id      = var.vpc_id

  # https 443
  dynamic "ingress" {
    # In case VPN is enabled, add whitelisted IP addresses to the SG
    for_each = concat(var.external_https_access, local.access_list)
    content {
      description = ingress.value["description"]
      from_port   = 443
      to_port     = 443
      protocol    = "tcp"
      cidr_blocks = [ingress.value["cidr"]]
    }
  }

  ingress {
    description     = "Access from transit gateway"
    from_port       = 443
    to_port         = 443
    protocol        = "tcp"
    prefix_list_ids = var.managed_prefix_list_ids
  }

  tags = merge(
    var.common_tags,
    {
      Name             = "${var.name_prefix}-sg-external-web",
      "eiso-exception" = "aws.08.30",
    },
  )
}

resource "aws_security_group" "ext_web_http_sg" {
  description = "HTTP access for list of external ip addresses"
  vpc_id      = var.vpc_id

  # http 80
  dynamic "ingress" {
    # In case VPN is enabled, add whitelisted IP addresses to the SG
    for_each = concat(var.external_https_access, local.access_list)
    content {
      description = ingress.value["description"]
      from_port   = 80
      to_port     = 80
      protocol    = "tcp"
      cidr_blocks = [ingress.value["cidr"]]
    }
  }

  ingress {
    description     = "Access from transit gateway"
    from_port       = 80
    to_port         = 80
    protocol        = "tcp"
    prefix_list_ids = var.managed_prefix_list_ids
  }

  tags = merge(
    var.common_tags,
    {
      Name             = "${var.name_prefix}-sg-external-web-http",
      "eiso-exception" = "aws.08.30",
    },
  )
}

resource "aws_security_group" "github_access" {
  //Creates dynamic list of security groups based on how many addresses has been provided in the list.
  //Due to the limitation of max rules per security group
  count       = ceil((length(var.github_ip_addr)) / var.max_sg_rules)
  description = "Connections from github ${count.index}"
  vpc_id      = var.vpc_id
  # https 443
  dynamic "ingress" {
    for_each = slice(
      var.github_ip_addr,
      var.max_sg_rules * count.index,
      length(var.github_ip_addr) > var.max_sg_rules * (count.index + 1) ? var.max_sg_rules * (count.index + 1) : length(var.github_ip_addr)
    )
    content {
      description = ingress.value["description"]
      from_port   = 443
      to_port     = 443
      protocol    = "tcp"
      cidr_blocks = [ingress.value["cidr"]]
    }
  }

  tags = merge(
    var.common_tags,
    {
      "Name"           = "${var.name_prefix}-sg-github",
      "eiso-exception" = "aws.08.30",
    },
  )
}
