diff --git a/addons/okta-conditional-access/.header.md b/addons/okta-conditional-access/.header.md new file mode 100644 index 0000000..c3d2669 --- /dev/null +++ b/addons/okta-conditional-access/.header.md @@ -0,0 +1,121 @@ +# Okta conditoinal access + +Since mTLS is used for conditional access with Okta, we need to move the connection request to a separate subdomain that requires mTLS and uses the CA stored in Fleet and the certificate issued to the device seeking access. + +This module helps automate that for a terraform deployment. By default the module assumes that okta is added as a subdomain to the Fleet primary domain (e.g. fleet.example.com leverages okta.fleet.example.com), but this can be customized. + +A separate load balancer is created to handle the mTLS authorization. The module only handles basic certificate validation and passes the certificate serial on to Fleet via a header in order to handle the final authorization or revocation step. + +## Requirements + +- A separate subdomain for conditional access +- A valid TLS certificate issued for the domain name +- HTTPS listener rules applied to the main Fleet load balancer to handle redirects (generated as an output of this module) +- The load balancer configured by this module applied as an extra load balancer to the fleet_config on the primary Fleet in terraform +- The CA certificate in PEM format stored at "resources/conditional-ca.pem" + +## Obtaining the CA certificate + +Run these commands while in your Fleet terraform directory/checkout. + +```sh +mkdir -p resources +# Replace with your domain name. This is the public cert and doesn't need special protection. +curl 'https://fleet.example.com/api/fleet/conditional_access/scep?operation=GetCACert' --output cacert.tmp +openssl x509 -inform der -in cacert.tmp -out resources/conditional-ca.pem +rm cacert.tmp +``` + +## Configuration Example + +```hcl +locals { + domain_name = "fleet.example.com" + okta_subdomain = "okta.${local.domain_name}" +} + +module "okta_acm" { + source = "terraform-aws-modules/acm/aws" + version = "4.3.1" + + domain_name = local.okta_subdomain + # Assumes you are managing your domain in route53 inside of this config. + zone_id = aws_route53_zone.main.id + + wait_for_validation = true +} + +resource "aws_route53_record" "okta" { + # If you change the route53_zone to a data source this also needs to become "data.aws_route53_zone.main.id" + zone_id = aws_route53_zone.main.id + name = local.okta_subdomain + type = "A" + + alias { + name = module.okta-conditional-access.alb.lb_dns_name + zone_id = module.okta-conditional-access.alb.lb_zone_id + evaluate_target_health = true + } +} + +module "fleet" { + source = "github.com/fleetdm/fleet-terraform?depth=1&ref=tf-mod-root-v1.19.0" + ... + fleet_config = { + ... + # Required to allow the ALB to talk to the ECS containers. + networking = { + ingress_sources = { + security_groups = [module.okta-conditional-access.alb.security_group_id] + } + } + extra_load_balancers = [{ + target_group_arn = module.okta-conditional-access.alb.target_groups["tg-0"].arn + container_name = "fleet" + container_port = 8080 + }] + } + ... + alb_config = { + ... + # If you have existing rules, use concat() to combine these with them. + # Note: by default the rules use the highest priority indexes starting at 1, + # but that can be configured inside the module. + https_listener_rules = module.okta-conditional-access.redirect_rules + } +} + +module "okta-conditional-access" { + source = "github.com/fleetdm/fleet-terraform/addons/okta-conditional-access?depth=1&ref=tf-mod-addon-okta-conditional-access-v0.5.0" + customer_prefix = "fleet" + vpc_id = module.fleet.vpc.vpc_id + trust_store_s3_config = { + bucket_prefix = "fleet-okta-trust-store" + } + alb_config = { + name = "fleet-okta" + # If using the ALB logging module: + access_logs = { + bucket = module.logging_alb.log_s3_bucket_id + prefix = "fleet" + enabled = true + } + subnets = module.fleet.vpc.public_subnets + certificate_arn = module.okta_acm.acm_certificate_arn + idle_timeout = 60 + trust_store = { + ca_certificates_bundle_s3_key = "ca.pem" + ca_certificates_bundle_s3_object_version = null + ca_certificates_bundle_file = "${path.module}/resources/conditional-ca.pem" + create_trust_store_revocation = false + trust_store_revocation_lists = {} + } + } +} +``` + +## Final comments + +Once this is configured, when you attempt to leverage the conditional access, you will be prompted to use the certificate that is installed on your system if it already there during the authoriation redriect. If the certificate is not installed, the load balancer will immediately terminate the connection during the handshake and you will get a connection resset error message. + + diff --git a/addons/okta-conditional-access/.terraform-docs.yml b/addons/okta-conditional-access/.terraform-docs.yml new file mode 100644 index 0000000..1d139dd --- /dev/null +++ b/addons/okta-conditional-access/.terraform-docs.yml @@ -0,0 +1 @@ +header-from: .header.md diff --git a/addons/okta-conditional-access/README.md b/addons/okta-conditional-access/README.md new file mode 100644 index 0000000..b607345 --- /dev/null +++ b/addons/okta-conditional-access/README.md @@ -0,0 +1,168 @@ +# Okta conditoinal access + +Since mTLS is used for conditional access with Okta, we need to move the connection request to a separate subdomain that requires mTLS and uses the CA stored in Fleet and the certificate issued to the device seeking access. + +This module helps automate that for a terraform deployment. By default the module assumes that okta is added as a subdomain to the Fleet primary domain (e.g. fleet.example.com leverages okta.fleet.example.com), but this can be customized. + +A separate load balancer is created to handle the mTLS authorization. The module only handles basic certificate validation and passes the certificate serial on to Fleet via a header in order to handle the final authorization or revocation step. + +## Requirements + +- A separate subdomain for conditional access +- A valid TLS certificate issued for the domain name +- HTTPS listener rules applied to the main Fleet load balancer to handle redirects (generated as an output of this module) +- The load balancer configured by this module applied as an extra load balancer to the fleet\_config on the primary Fleet in terraform +- The CA certificate in PEM format stored at "resources/conditional-ca.pem" + +## Obtaining the CA certificate + +Run these commands while in your Fleet terraform directory/checkout. + +```sh +mkdir -p resources +# Replace with your domain name. This is the public cert and doesn't need special protection. +curl 'https://fleet.example.com/api/fleet/conditional_access/scep?operation=GetCACert' --output cacert.tmp +openssl x509 -inform der -in cacert.tmp -out resources/conditional-ca.pem +rm cacert.tmp +``` + +## Configuration Example + +```hcl +locals { + domain_name = "fleet.example.com" + okta_subdomain = "okta.${local.domain_name}" +} + +module "okta_acm" { + source = "terraform-aws-modules/acm/aws" + version = "4.3.1" + + domain_name = local.okta_subdomain + # Assumes you are managing your domain in route53 inside of this config. + zone_id = aws_route53_zone.main.id + + wait_for_validation = true +} + +resource "aws_route53_record" "okta" { + # If you change the route53_zone to a data source this also needs to become "data.aws_route53_zone.main.id" + zone_id = aws_route53_zone.main.id + name = local.okta_subdomain + type = "A" + + alias { + name = module.okta-conditional-access.alb.lb_dns_name + zone_id = module.okta-conditional-access.alb.lb_zone_id + evaluate_target_health = true + } +} + +module "fleet" { + source = "github.com/fleetdm/fleet-terraform?depth=1&ref=tf-mod-root-v1.19.0" + ... + fleet_config = { + ... + # Required to allow the ALB to talk to the ECS containers. + networking = { + ingress_sources = { + security_groups = [module.okta-conditional-access.alb.security_group_id] + } + } + extra_load_balancers = [{ + target_group_arn = module.okta-conditional-access.alb.target_groups["tg-0"].arn + container_name = "fleet" + container_port = 8080 + }] + } + ... + alb_config = { + ... + # If you have existing rules, use concat() to combine these with them. + # Note: by default the rules use the highest priority indexes starting at 1, + # but that can be configured inside the module. + https_listener_rules = module.okta-conditional-access.redirect_rules + } +} + +module "okta-conditional-access" { + source = "github.com/fleetdm/fleet-terraform/addons/okta-conditional-access?depth=1&ref=tf-mod-addon-okta-conditional-access-v0.5.0" + customer_prefix = "fleet" + vpc_id = module.fleet.vpc.vpc_id + trust_store_s3_config = { + bucket_prefix = "fleet-okta-trust-store" + } + alb_config = { + name = "fleet-okta" + # If using the ALB logging module: + access_logs = { + bucket = module.logging_alb.log_s3_bucket_id + prefix = "fleet" + enabled = true + } + subnets = module.fleet.vpc.public_subnets + certificate_arn = module.okta_acm.acm_certificate_arn + idle_timeout = 60 + trust_store = { + ca_certificates_bundle_s3_key = "ca.pem" + ca_certificates_bundle_s3_object_version = null + ca_certificates_bundle_file = "${path.module}/resources/conditional-ca.pem" + create_trust_store_revocation = false + trust_store_revocation_lists = {} + } + } +} +``` + +## Final comments + +Once this is configured, when you attempt to leverage the conditional access, you will be prompted to use the certificate that is installed on your system if it already there during the authoriation redriect. If the certificate is not installed, the load balancer will immediately terminate the connection during the handshake and you will get a connection resset error message. + +## Requirements + +No requirements. + +## Providers + +| Name | Version | +|------|---------| +| [aws](#provider\_aws) | 6.28.0 | + +## Modules + +| Name | Source | Version | +|------|--------|---------| +| [lb\_trust\_store\_bucket](#module\_lb\_trust\_store\_bucket) | terraform-aws-modules/s3-bucket/aws | 5.9.1 | +| [okta\_mtls\_alb](#module\_okta\_mtls\_alb) | terraform-aws-modules/alb/aws | 10.4.0 | + +## Resources + +| Name | Type | +|------|------| +| [aws_lb_trust_store.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_trust_store) | resource | +| [aws_lb_trust_store_revocation.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_trust_store_revocation) | resource | +| [aws_s3_object.trust_store_ca](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_object) | resource | +| [aws_security_group.alb](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource | +| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source | +| [aws_iam_policy_document.alb_trust_store_restricted](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | +| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source | +| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [alb\_config](#input\_alb\_config) | n/a |
object({
name = optional(string, "fleet-okta")
subnets = list(string)
security_groups = optional(list(string), [])
access_logs = optional(map(string), {})
certificate_arn = string
allowed_cidrs = optional(list(string), ["0.0.0.0/0"])
allowed_ipv6_cidrs = optional(list(string), ["::/0"])
egress_cidrs = optional(list(string), ["0.0.0.0/0"])
egress_ipv6_cidrs = optional(list(string), ["::/0"])
extra_target_groups = optional(any, [])
https_listener_rules = optional(any, [])
https_overrides = optional(any, {})
xff_header_processing_mode = optional(string, null)
tls_policy = optional(string, "ELBSecurityPolicy-TLS13-1-2-2021-06")
idle_timeout = optional(number, 60)
internal = optional(bool, false)
enable_deletion_protection = optional(bool, false)
subdomain_prefix = optional(string, "okta")
trust_store = optional(any, {
ca_certificates_bundle_s3_key = "ca.pem"
ca_certificates_bundle_s3_object_version = null
ca_certificates_bundle_file = null
create_trust_store_revocation = false
trust_store_revocation_lists = {}
})
})
| n/a | yes | +| [customer\_prefix](#input\_customer\_prefix) | n/a | `string` | `"fleet"` | no | +| [redirect\_priority](#input\_redirect\_priority) | The priority of the redirect https\_listener\_rule generated in the output | `number` | `1` | no | +| [trust\_store\_s3\_config](#input\_trust\_store\_s3\_config) | n/a |
object({
bucket_prefix = optional(string, "fleet-okta-trust-store")
newer_noncurrent_versions = optional(number, 5)
noncurrent_version_expiration_days = optional(number, 30)
})
|
{
"bucket_prefix": "fleet-okta-trust-store",
"newer_noncurrent_versions": 5,
"noncurrent_version_expiration_days": 30
}
| no | +| [vpc\_id](#input\_vpc\_id) | n/a | `string` | n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| [alb](#output\_alb) | n/a | +| [alb\_security\_group](#output\_alb\_security\_group) | n/a | +| [lb\_trust\_store\_\_bucket](#output\_lb\_trust\_store\_\_bucket) | n/a | +| [redirect\_rules](#output\_redirect\_rules) | n/a | diff --git a/addons/okta-conditional-access/main.tf b/addons/okta-conditional-access/main.tf new file mode 100644 index 0000000..deebe67 --- /dev/null +++ b/addons/okta-conditional-access/main.tf @@ -0,0 +1,224 @@ +data "aws_partition" "current" {} +data "aws_caller_identity" "current" {} +data "aws_region" "current" {} + +### Trust store and dependencies + +resource "aws_lb_trust_store" "this" { + + ca_certificates_bundle_s3_bucket = module.lb_trust_store_bucket.s3_bucket_id + ca_certificates_bundle_s3_key = var.alb_config.trust_store.ca_certificates_bundle_s3_key + ca_certificates_bundle_s3_object_version = try(var.alb_config.trust_store.ca_certificates_bundle_s3_object_version, null) + name = "${var.customer_prefix}-trust-store" + +} + +resource "aws_lb_trust_store_revocation" "this" { + for_each = var.alb_config.trust_store.create_trust_store_revocation && var.alb_config.trust_store.revocation_lists != null ? var.alb_config.trust_store.revocation_lists : {} + + trust_store_arn = aws_lb_trust_store.this.arn + revocations_s3_bucket = module.lb_trust_store_bucket.s3_bucket_id + revocations_s3_key = each.value.revocations_s3_key + revocations_s3_object_version = try(each.value.revocations_s3_object_version, null) +} + +data "aws_iam_policy_document" "alb_trust_store_restricted" { + statement { + sid = "AllowSpecificALBReadTrustStore" + effect = "Allow" + + principals { + type = "Service" + identifiers = ["elasticloadbalancing.amazonaws.com"] + } + + actions = ["s3:GetObject"] + + # Specify the exact certificate object ARN + resources = [ + "${module.lb_trust_store_bucket.s3_bucket_arn}/*" + ] + + # Restrict to the specific ALB and Account + condition { + test = "ArnEquals" + variable = "aws:SourceArn" + values = [module.okta_mtls_alb.arn] + } + + condition { + test = "StringEquals" + variable = "aws:SourceAccount" + values = [data.aws_caller_identity.current.account_id] + } + } +} + +module "lb_trust_store_bucket" { + source = "terraform-aws-modules/s3-bucket/aws" + version = "5.9.1" + + bucket_prefix = var.trust_store_s3_config.bucket_prefix + + # Allow deletion of non-empty bucket + force_destroy = true + + attach_deny_insecure_transport_policy = true + attach_require_latest_tls_policy = true + policy = data.aws_iam_policy_document.alb_trust_store_restricted.json + block_public_acls = true + block_public_policy = true + ignore_public_acls = true + restrict_public_buckets = true + acl = "private" + control_object_ownership = true + object_ownership = "ObjectWriter" + + server_side_encryption_configuration = { + rule = { + bucket_key_enabled = true + apply_server_side_encryption_by_default = { + sse_algorithm = "aws:kms" + } + } + } + lifecycle_rule = [ + { + id = "trust_store" + enabled = true + + noncurrent_version_expiration = { + newer_noncurrent_versions = var.trust_store_s3_config.newer_noncurrent_versions + days = var.trust_store_s3_config.noncurrent_version_expiration_days + } + filter = [] + } + ] +} + +resource "aws_s3_object" "trust_store_ca" { + count = var.alb_config.trust_store.ca_certificates_bundle_file != null && var.alb_config.trust_store.ca_certificates_bundle_file != "" ? 1 : 0 + bucket = module.lb_trust_store_bucket.s3_bucket_id + key = var.alb_config.trust_store.ca_certificates_bundle_s3_key + source = var.alb_config.trust_store.ca_certificates_bundle_file + etag = filemd5(var.alb_config.trust_store.ca_certificates_bundle_file) +} + +### ALB +module "okta_mtls_alb" { + source = "terraform-aws-modules/alb/aws" + version = "10.4.0" + + name = var.alb_config.name + + load_balancer_type = "application" + + vpc_id = var.vpc_id + subnets = var.alb_config.subnets + security_groups = concat(var.alb_config.security_groups, [aws_security_group.alb.id]) + access_logs = var.alb_config.access_logs + idle_timeout = var.alb_config.idle_timeout + internal = var.alb_config.internal + enable_deletion_protection = var.alb_config.enable_deletion_protection + + target_groups = { + "tg-0" = { + name = var.alb_config.name + backend_protocol = "HTTP" + backend_port = 80 + target_type = "ip" + create_attachment = false + health_check = { + path = "/healthz" + matcher = "200" + timeout = 10 + interval = 15 + healthy_threshold = 5 + unhealthy_threshold = 5 + } + } + } + + xff_header_processing_mode = var.alb_config.xff_header_processing_mode + + listeners = { + http = { + port = 80 + protocol = "HTTP" + redirect = { + port = "443" + protocol = "HTTPS" + status_code = "HTTP_301" + } + } + https = { + ssl_policy = var.alb_config.tls_policy + port = 443 + protocol = "HTTPS" + certificate_arn = var.alb_config.certificate_arn + mutual_authentication = { + mode = "verify" + advertise_trust_store_ca_names = "on" + trust_store_arn = aws_lb_trust_store.this.arn + } + fixed_response = { + content_type = "text/plain" + message_body = "Not Found" + status_code = "404" + } + routing_http_request_x_amzn_mtls_clientcert_serial_number_header_name = "X-Client-Cert-Serial" + rules = { + "https/okta-forward" = { + actions = [{ + forward = { + target_group_key = "tg-0" + } + order = 1 + type = "forward" + }] + conditions = [{ + path_pattern = { + values = ["/api/fleet/conditional_access/idp/sso"] + } + }] + } + } + } + } + + tags = { + Name = var.alb_config.name + } +} + +resource "aws_security_group" "alb" { + #checkov:skip=CKV2_AWS_5:False positive + vpc_id = var.vpc_id + description = "Fleet ALB Security Group" + ingress { + description = "Ingress from all, its a public load balancer" + from_port = 443 + to_port = 443 + protocol = "tcp" + cidr_blocks = var.alb_config.allowed_cidrs + ipv6_cidr_blocks = var.alb_config.allowed_ipv6_cidrs + } + + ingress { + description = "For http to https redirect" + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = var.alb_config.allowed_cidrs + ipv6_cidr_blocks = var.alb_config.allowed_ipv6_cidrs + } + + egress { + description = "Egress to all" + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = var.alb_config.egress_cidrs + ipv6_cidr_blocks = var.alb_config.egress_ipv6_cidrs + } +} diff --git a/addons/okta-conditional-access/outputs.tf b/addons/okta-conditional-access/outputs.tf new file mode 100644 index 0000000..caab0e9 --- /dev/null +++ b/addons/okta-conditional-access/outputs.tf @@ -0,0 +1,34 @@ +output "redirect_rules" { + value = [{ + actions = [{ + type = "redirect" + host = "${var.alb_config.subdomain_prefix}.#{host}" + path = "/api/fleet/conditional_access/idp/sso" + protocol = "HTTPS" + status_code = "HTTP_302" + }] + conditions = [{ + path_patterns = ["/api/fleet/conditional_access/idp/sso"] + }] + priority = var.redirect_priority + }] +} + +output "lb_trust_store__bucket" { + value = module.lb_trust_store_bucket +} + +output "alb" { + value = merge(module.okta_mtls_alb, { + lb_dns_name = module.okta_mtls_alb.dns_name + lb_zone_id = module.okta_mtls_alb.zone_id + target_group_names = [for k, tg in module.okta_mtls_alb.target_groups : tg.name] + target_group_arn_suffixes = [for k, tg in module.okta_mtls_alb.target_groups : tg.arn_suffix] + target_group_arns = [for k, tg in module.okta_mtls_alb.target_groups : tg.arn] + lb_arn_suffix = module.okta_mtls_alb.arn_suffix + }) +} + +output "alb_security_group" { + value = aws_security_group.alb +} diff --git a/addons/okta-conditional-access/variables.tf b/addons/okta-conditional-access/variables.tf new file mode 100644 index 0000000..62f600f --- /dev/null +++ b/addons/okta-conditional-access/variables.tf @@ -0,0 +1,57 @@ +variable "vpc_id" { + type = string +} + +variable "customer_prefix" { + type = string + default = "fleet" +} + +variable "redirect_priority" { + description = "The priority of the redirect https_listener_rule generated in the output" + type = number + default = 1 +} + +variable "trust_store_s3_config" { + type = object({ + bucket_prefix = optional(string, "fleet-okta-trust-store") + newer_noncurrent_versions = optional(number, 5) + noncurrent_version_expiration_days = optional(number, 30) + }) + default = { + bucket_prefix = "fleet-okta-trust-store" + newer_noncurrent_versions = 5 + noncurrent_version_expiration_days = 30 + } +} + +variable "alb_config" { + type = object({ + name = optional(string, "fleet-okta") + subnets = list(string) + security_groups = optional(list(string), []) + access_logs = optional(map(string), {}) + certificate_arn = string + allowed_cidrs = optional(list(string), ["0.0.0.0/0"]) + allowed_ipv6_cidrs = optional(list(string), ["::/0"]) + egress_cidrs = optional(list(string), ["0.0.0.0/0"]) + egress_ipv6_cidrs = optional(list(string), ["::/0"]) + extra_target_groups = optional(any, []) + https_listener_rules = optional(any, []) + https_overrides = optional(any, {}) + xff_header_processing_mode = optional(string, null) + tls_policy = optional(string, "ELBSecurityPolicy-TLS13-1-2-2021-06") + idle_timeout = optional(number, 60) + internal = optional(bool, false) + enable_deletion_protection = optional(bool, false) + subdomain_prefix = optional(string, "okta") + trust_store = optional(any, { + ca_certificates_bundle_s3_key = "ca.pem" + ca_certificates_bundle_s3_object_version = null + ca_certificates_bundle_file = null + create_trust_store_revocation = false + trust_store_revocation_lists = {} + }) + }) +}