Browse Source

Creates ALSI Module for Aggregated Log Source Ingestion

Fred Damstra 4 years ago
parent
commit
32fcffbaea

+ 4 - 0
base/splunk_servers/alsi/README.md

@@ -0,0 +1,4 @@
+"Aggregated Log Source Ingestion"
+
+Cribl endpoints to which clients with centralized logging (e.g. elasticache, splunk) can forward logs.
+

+ 1 - 0
base/splunk_servers/alsi/amis.tf

@@ -0,0 +1 @@
+../../amis.tf

+ 31 - 0
base/splunk_servers/alsi/certificate.tf

@@ -0,0 +1,31 @@
+#Certificate 
+resource "aws_acm_certificate" "cert" {
+  domain_name       = "${var.prefix}-alsi.${var.dns_info["public"]["zone"]}"
+  validation_method = "DNS"
+
+  tags = merge(var.standard_tags, var.tags)
+}
+
+resource "aws_acm_certificate_validation" "cert" {
+  certificate_arn         = aws_acm_certificate.cert.arn
+  validation_record_fqdns = [for record in aws_route53_record.cert_validation: record.fqdn]
+}
+
+resource "aws_route53_record" "cert_validation" {
+  provider = aws.mdr-common-services-commercial
+
+  for_each = {
+    for dvo in aws_acm_certificate.cert.domain_validation_options : dvo.domain_name => {
+      name   = dvo.resource_record_name
+      record = dvo.resource_record_value
+      type   = dvo.resource_record_type
+    }
+  }
+
+  allow_overwrite = true
+  name            = each.value.name
+  records         = [each.value.record]
+  ttl             = 60
+  type            = each.value.type
+  zone_id         = var.dns_info["public"]["zone_id"]
+}

+ 78 - 0
base/splunk_servers/alsi/cloud-init/cloud-init.tpl

@@ -0,0 +1,78 @@
+#cloud-config
+preserve_hostname: false
+hostname: ${hostname}
+salt-master: ${salt_master}
+fqdn: ${fqdn}
+
+# Write files happens early
+write_files:
+- content: |
+    proxy=http://${proxy}:80
+  path: /etc/yum.conf
+  append: true
+- content: |
+    proxy_host: ${proxy}
+    proxy_port: 80
+  path: /etc/salt/minion.d/proxy.conf
+- content: |
+    [global]
+    proxy=${proxy}
+  path: /etc/pip.conf
+- content: |
+    export HTTPS_PROXY=http://${proxy}:80
+    export HTTP_PROXY=http://${proxy}:80
+    export NO_PROXY=localhost,127.0.0.1,169.254.169.254,pvt.xdrtest.accenturefederalcyber.com,pvt.xdr.accenturefederalcyber.com,reposerver.msoc.defpoint.local,jenkins.msoc.defpoint.local,pod1search-splunk-sh.msoc.defpoint.local,s3.amazonaws.com,ssm.${ aws_region }.amazonaws.com,ec2messages.${ aws_region }.amazonaws.com,ec2.${ aws_region }.amazonaws.com,ssmmessages.${ aws_region }.amazonaws.com,iratemoses.mdr.defpoint.com,jira.mdr.defpoint.com,reposerver.pvt.xdr.accenturefederalcyber.com,jenkins.pvt.xdr.accenturefederalcyber.com,pod1search-splunk-sh.pvt.xdr.accenturefederalcyber.com,reposerver.pvt.xdrtest.accenturefederalcyber.com,jenkins.pvt.xdrtest.accenturefederalcyber.com,pod1search-splunk-sh.pvt.xdrtest.accenturefederalcyber.com,iratemoses.xdr.accenturefederalcyber.com,jira.xdr.accenturefederalcyber.com,iratemoses.xdrtest.accenturefederalcyber.com,jira.xdrtest.accenturefederalcyber.com
+    export https_proxy=$HTTPS_PROXY
+    export http_proxy=$HTTP_PROXY
+    export no_proxy=$NO_PROXY
+  path: /etc/profile.d/proxy.sh
+- content: |
+    ${fqdn}
+  path: /etc/salt/minion_id
+- content: |
+    master: ${salt_master}
+  path: /etc/salt/minion
+- content: |
+    grains:
+      environment: ${ environment }
+      aws_partition: ${ aws_partition }
+      aws_partition_alias: ${ aws_partition_alias }
+      splunk_prefix: ${ splunk_prefix }
+      aws_region: ${ aws_region }
+  path: /etc/salt/minion.d/cloud_init_grains.conf
+
+#yum_repos:
+#  epel-release:
+#    baseurl: http://download.fedoraproject.org/pub/epel/7/$basearch
+#    enabled: false
+#    failovermethod: priority
+#    gpgcheck: true
+#    gpgkey: http://download.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7
+#    name: Extra Packages for Enterprise Linux 7 - Release
+
+packages:
+ - vim
+
+package_update: true # Always patch
+
+growpart:
+  mode: auto
+  devices: [ '/', '/var', '/var/log', '/var/log/audit', '/var/tmp', '/tmp', '/home' ]
+  ignore_growroot_disabled: false
+
+runcmd:
+ - /bin/systemctl restart salt-minion
+ - /bin/systemctl enable salt-minion
+ - /bin/systemctl start amazon-ssm-agent
+ - /bin/systemctl enable amazon-ssm-agent
+ - /usr/sbin/aide --update --verbose=0
+ - /bin/cp /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
+
+# Either final message or power state, but probably not both
+final_message: "The system is up after $UPTIME seconds"
+#power_state:
+#  delay: "+30"
+#  mode: reboot
+#  message: "System configured after $UPTIME seconds"
+#  timeout: 300
+#  condition: true

+ 130 - 0
base/splunk_servers/alsi/elb.tf

@@ -0,0 +1,130 @@
+resource "aws_lb" "alsi-alb" {
+  name               = "${var.prefix}-alsi-alb"
+  internal           = false
+  load_balancer_type = "application"
+  # Not supported for NLB
+  security_groups    = [aws_security_group.alsi-alb-sg.id]
+  # Note, changing subnets results in recreation of the resource
+  subnets            = var.subnets
+  enable_cross_zone_load_balancing = true
+
+  access_logs {
+    bucket  = "xdr-elb-${ var.environment }"
+    enabled = true
+  }
+
+  tags = merge(var.standard_tags, var.tags)
+}
+
+#########################
+# Listeners
+resource "aws_lb_listener" "alsi-alb-listener-https" {
+  load_balancer_arn = aws_lb.alsi-alb.arn
+  port              = "443"
+  protocol          = "HTTPS"
+  ssl_policy        = "ELBSecurityPolicy-FS-1-2-Res-2019-08" # PFS, TLS1.2, most "restrictive" policy (took awhile to find that)
+  certificate_arn   = aws_acm_certificate.cert.arn
+
+  default_action {
+    type             = "forward"
+    target_group_arn = aws_lb_target_group.alsi-alb-target-443.arn
+  }
+}
+
+# Only alb's can redirect
+resource "aws_lb_listener" "alsi-alb-listener-http" {
+  load_balancer_arn = aws_lb.alsi-alb.arn
+  port              = "80"
+  protocol          = "HTTP"
+
+  default_action {
+    type             = "redirect"
+
+    redirect {
+      port        = "443"
+      protocol    = "HTTPS"
+      status_code = "HTTP_301"
+    }
+  }
+}
+
+#########################
+# Targets
+resource "aws_lb_target_group" "alsi-alb-target-443" {
+  name     = "${var.prefix}-alsi-alb-target-443"
+  port     = 443
+  protocol = "HTTPS"
+  target_type = "instance"
+  vpc_id   = var.vpc_id
+  tags = merge(var.standard_tags, var.tags)
+
+  health_check {
+    enabled = true
+    path = "/api/v1/health"
+    port = 443
+    protocol = "HTTPS"
+  }
+
+  # Does cribl need stickiness?
+  #stickiness {
+  #  type = "lb_cookie"
+  #  cookie_duration = 86400 # 1 day
+  #  enabled = true
+  #}
+}
+
+resource "aws_lb_target_group_attachment" "alsi-alb-target-443-instance" {
+  count            = var.alsi_count
+  target_group_arn = aws_lb_target_group.alsi-alb-target-443.arn
+  target_id        = aws_instance.instance[count.index].id
+  port             = 443
+}
+
+#########################
+# Security Group for ALB
+resource "aws_security_group" "alsi-alb-sg" {
+  name_prefix = "${var.prefix}-alsi-alb-sg"
+  lifecycle { create_before_destroy = true } # handle updates gracefully
+  description = "Security Group for the Cribl ALB"
+  vpc_id = var.vpc_id
+  tags = merge(var.standard_tags, var.tags)
+}
+
+resource "aws_security_group_rule" "alsi-alb-https-in" {
+  type              = "ingress"
+  from_port         = 443
+  to_port           = 443
+  protocol          = "tcp"
+  cidr_blocks       = toset(concat(var.cidr_map["vpc-access"], var.trusted_ips, var.splunk_data_sources))
+  security_group_id = aws_security_group.alsi-alb-sg.id
+}
+
+resource "aws_security_group_rule" "alsi-http-in" {
+  # Port 80 is open as a redirect to 443
+  type              = "ingress"
+  from_port         = 80
+  to_port           = 80
+  protocol          = "tcp"
+  cidr_blocks       = toset(concat(var.cidr_map["vpc-access"], var.trusted_ips, var.splunk_data_sources))
+  security_group_id = aws_security_group.alsi-alb-sg.id
+}
+
+resource "aws_security_group_rule" "alsi-alb-443-out" {
+  type              = "egress"
+  from_port         = 443
+  to_port           = 443
+  protocol          = "tcp"
+  cidr_blocks       = [ var.vpc_cidr ]
+  security_group_id = aws_security_group.alsi-alb-sg.id
+}
+
+#########################
+# DNS Entry
+resource "aws_route53_record" "alsi" {
+  zone_id = var.dns_info["public"]["zone_id"]
+  name    = "${ var.prefix }-alsi"
+  type    = "CNAME"
+  records = [aws_lb.alsi-alb.dns_name]
+  ttl = "60"
+  provider = aws.mdr-common-services-commercial
+}

+ 191 - 0
base/splunk_servers/alsi/main.tf

@@ -0,0 +1,191 @@
+# Some instance variables
+locals {
+  ami_selection = "minion" # master, minion, ...
+  instance_name = "${ var.prefix }-alsi"
+  is_moose = length(regexall("moose", var.prefix)) > 0 ? true : false
+}
+
+# Rather than pass in the aws security group, we just look it up.
+data "aws_security_group" "typical-host" {
+  name   = "typical-host"
+  vpc_id = var.vpc_id
+}
+
+# Use the default EBS key
+data "aws_kms_key" "ebs-key" {
+  key_id = "alias/ebs_root_encrypt_decrypt"
+}
+
+resource "aws_network_interface" "instance" {
+  count = var.alsi_count
+  subnet_id = var.subnets[ count.index % length(var.subnets) ] # evenly distributed across subnets
+  security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.alsi_security_group.id ]
+  description = "${local.instance_name}-${count.index}"
+  tags = merge( var.standard_tags, 
+                var.tags, 
+                { 
+                  Name = "${local.instance_name}-${count.index}", 
+                  instance_num = count.index, 
+                  instance_count = var.alsi_count 
+                }
+              )
+}
+
+resource "aws_instance" "instance" {
+  count = var.alsi_count
+  #availability_zone = var.azs[count.index % 2] # automatically determined by the network interface
+  tenancy = "default"
+  ebs_optimized = true
+  disable_api_termination = var.instance_termination_protection
+  instance_initiated_shutdown_behavior = "stop"
+  instance_type = var.instance_type
+  key_name = "msoc-build"
+  monitoring = false
+  iam_instance_profile = "msoc-default-instance-profile"
+
+  ami = local.ami_map[local.ami_selection]
+  # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
+  # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
+  # that could be removed.
+  lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
+
+  # These device definitions are optional, but added for clarity.
+  root_block_device {
+    volume_type = "gp2"
+    #volume_size = Override via var?
+    delete_on_termination = true
+    encrypted = true
+    kms_key_id = data.aws_kms_key.ebs-key.arn
+  }
+
+  network_interface {
+    device_index = 0
+    network_interface_id = aws_network_interface.instance[count.index].id
+  }
+
+  user_data = data.template_cloudinit_config.cloud-init[count.index].rendered
+  tags = merge( var.standard_tags, 
+                var.tags, 
+                { 
+                  Name = "${local.instance_name}-${count.index}", 
+                  instance_num = count.index, 
+                  instance_count = var.alsi_count 
+                }
+              )
+  volume_tags = merge( var.standard_tags, 
+                var.tags, 
+                { 
+                  Name = "${local.instance_name}-${count.index}", 
+                  instance_num = count.index, 
+                  instance_count = var.alsi_count 
+                }
+              )
+}
+
+module "private_dns_record" {
+  count = var.alsi_count
+  source = "../../../submodules/dns/private_A_record"
+
+  name = "${local.instance_name}-${count.index}"
+  ip_addresses = [ aws_instance.instance[count.index].private_ip ]
+  dns_info = var.dns_info
+  reverse_enabled = var.reverse_enabled
+
+  providers = {
+    aws.c2 = aws.c2
+  }
+}
+
+data "template_file" "cloud-init" {
+  count = var.alsi_count
+
+  # Should these be in a common directory? I suspect they'd be reusable
+  template = file("${path.module}/cloud-init/cloud-init.tpl")
+
+  vars = {
+    hostname = "${local.instance_name}-${count.index}"
+    fqdn = "${local.instance_name}-${count.index}.${var.dns_info["private"]["zone"]}"
+    splunk_prefix = var.prefix
+    environment = var.environment
+    salt_master  = var.salt_master
+    proxy = var.proxy
+    aws_partition = var.aws_partition
+    aws_partition_alias = var.aws_partition_alias
+    aws_region = var.aws_region
+  }
+}
+
+# Render a multi-part cloud-init config making use of the part
+# above, and other source files
+data "template_cloudinit_config" "cloud-init" {
+  count         = var.alsi_count
+  gzip          = true
+  base64_encode = true
+
+  # Main cloud-config configuration file.
+  part {
+    filename     = "init.cfg"
+    content_type = "text/cloud-config"
+    content      = data.template_file.cloud-init[count.index].rendered
+  }
+}
+
+## ALSI
+#
+# Summary:
+#   Ingress:
+#     443 - from ALB
+#     443 - From vpc-access
+#
+#   Egress:
+#     8089 - To Splunk
+#     9997 - To Splunk
+resource "aws_security_group" "alsi_security_group" {
+  name_prefix = "${ var.prefix }_alsi_security_group" # name prefix and livecycle allow for smooth updates
+  lifecycle { create_before_destroy = true } # handle updates gracefully
+  description = "Security Group for Aggregated Log Source Ingestion"
+  vpc_id = var.vpc_id
+  tags = merge(var.standard_tags, var.tags)
+}
+
+# Ingress
+resource "aws_security_group_rule" "alsi-alb-web-in" {
+  description       = "Web access"
+  type              = "ingress"
+  from_port         = 443
+  to_port           = 443
+  protocol          = "tcp"
+  source_security_group_id = aws_security_group.alsi-alb-sg.id
+  security_group_id = aws_security_group.alsi_security_group.id
+}
+
+resource "aws_security_group_rule" "alsi-access-web-in" {
+  description       = "Web access"
+  type              = "ingress"
+  from_port         = 443
+  to_port           = 443
+  protocol          = "tcp"
+  cidr_blocks       = var.cidr_map["vpc-access"]
+  security_group_id = aws_security_group.alsi_security_group.id
+}
+
+# Egress
+resource "aws_security_group_rule" "splunk-mgmt" {
+  description       = "Management Access"
+  type              = "egress"
+  from_port         = 8089
+  to_port           = 8089
+  protocol          = "tcp"
+  cidr_blocks       = [ var.vpc_cidr ]
+  security_group_id = aws_security_group.alsi_security_group.id
+} 
+
+resource "aws_security_group_rule" "splunk-data" {
+  description       = "Management Access"
+  type              = "egress"
+  from_port         = 9997
+  to_port           = 9998
+  protocol          = "tcp"
+  cidr_blocks       = [ var.vpc_cidr ]
+  security_group_id = aws_security_group.alsi_security_group.id
+}

+ 15 - 0
base/splunk_servers/alsi/outputs.tf

@@ -0,0 +1,15 @@
+output elb_fqdn {
+  value = aws_route53_record.alsi.fqdn
+}
+
+output instance_fqdn {
+  value = module.private_dns_record[*].forward
+}
+
+output instance_arn {
+  value = aws_instance.instance[*].arn
+}
+
+output instance_private_ip {
+  value = aws_instance.instance[*].private_ip
+}

+ 66 - 0
base/splunk_servers/alsi/vars.tf

@@ -0,0 +1,66 @@
+variable "alsi_count" {
+  type = number
+}
+
+variable "prefix" {
+  description = "Prefix for Instance Names"
+  type = string
+}
+
+variable "splunk_legacy_cidr" {
+  description = "The legacy CIDR block(s)"
+  default = []
+  type = list(string)
+}
+
+variable "azs" {
+  type = list(string)
+}
+
+variable "splunk_volume_sizes" {
+  type = map(map(number))
+}
+
+variable "subnets" {
+  type = list(string)
+}
+
+variable "vpc_id" {
+  type = string
+}
+
+variable "vpc_cidr" {
+  type = string
+}
+
+variable "tags" {
+  description = "Tags to add to the resource (in addition to global standard tags)"
+  type        = map
+  default     = { }
+}
+
+variable "instance_type" { 
+  type = string
+  default = "t3a.micro"
+}
+
+variable "reverse_enabled" { 
+  description = "Whether to create the reverse DNS entry."
+  type = bool
+  default = true
+}
+
+variable "trusted_ips" { type = list(string) }
+variable "splunk_data_sources" { type = list(string) }
+variable "proxy" { type = string }
+variable "salt_master" { type = string }
+
+variable "cidr_map" { type = map }
+variable "dns_info" { type = map }
+variable "standard_tags" { type = map }
+variable "environment" { type = string }
+variable "aws_region" { type = string }
+variable "aws_partition" { type = string }
+variable "aws_partition_alias" { type = string }
+variable "common_services_account" { type = string }
+variable "instance_termination_protection" { type = bool }

+ 3 - 0
base/splunk_servers/alsi/version.tf

@@ -0,0 +1,3 @@
+terraform {
+  required_version = "~> 0.13"
+}