Skip to content

Commit

Permalink
Merge pull request #211 from unity-sds/429-fix-alb-lockdown
Browse files Browse the repository at this point in the history
Fix ALB lockdown configurations
  • Loading branch information
LucaCinquini authored Sep 25, 2024
2 parents 9df0efc + 9676edc commit 95dad09
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 10 deletions.
3 changes: 3 additions & 0 deletions terraform-unity/modules/terraform-unity-sps-airflow/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ No modules.
| [aws_s3_bucket.airflow_logs](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/resources/s3_bucket) | resource |
| [aws_security_group.airflow_efs](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/resources/security_group) | resource |
| [aws_security_group.airflow_ingress_sg](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/resources/security_group) | resource |
| [aws_security_group.airflow_ingress_sg_internal](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/resources/security_group) | resource |
| [aws_security_group_rule.airflow_efs](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/resources/security_group_rule) | resource |
| [aws_ssm_parameter.airflow_api_health_check_endpoint](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/resources/ssm_parameter) | resource |
| [aws_ssm_parameter.airflow_api_url](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/resources/ssm_parameter) | resource |
Expand All @@ -54,6 +55,7 @@ No modules.
| [helm_release.airflow](https://registry.terraform.io/providers/hashicorp/helm/2.13.1/docs/resources/release) | resource |
| [helm_release.keda](https://registry.terraform.io/providers/hashicorp/helm/2.13.1/docs/resources/release) | resource |
| [kubernetes_ingress_v1.airflow_ingress](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/resources/ingress_v1) | resource |
| [kubernetes_ingress_v1.airflow_ingress_internal](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/resources/ingress_v1) | resource |
| [kubernetes_namespace.keda](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/resources/namespace) | resource |
| [kubernetes_persistent_volume.airflow_deployed_dags](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/resources/persistent_volume) | resource |
| [kubernetes_persistent_volume.airflow_kpo](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/resources/persistent_volume) | resource |
Expand All @@ -77,6 +79,7 @@ No modules.
| [aws_ssm_parameter.subnet_ids](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/data-sources/ssm_parameter) | data source |
| [aws_vpc.cluster_vpc](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/data-sources/vpc) | data source |
| [kubernetes_ingress_v1.airflow_ingress](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/data-sources/ingress_v1) | data source |
| [kubernetes_ingress_v1.airflow_ingress_internal](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/data-sources/ingress_v1) | data source |
| [kubernetes_namespace.service_area](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/data-sources/namespace) | data source |

## Inputs
Expand Down
7 changes: 7 additions & 0 deletions terraform-unity/modules/terraform-unity-sps-airflow/data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ data "kubernetes_ingress_v1" "airflow_ingress" {
}
}

data "kubernetes_ingress_v1" "airflow_ingress_internal" {
metadata {
name = kubernetes_ingress_v1.airflow_ingress_internal.metadata[0].name
namespace = data.kubernetes_namespace.service_area.metadata[0].name
}
}

data "aws_db_instance" "db" {
db_instance_identifier = var.db_instance_identifier
}
Expand Down
60 changes: 54 additions & 6 deletions terraform-unity/modules/terraform-unity-sps-airflow/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,17 @@ resource "aws_security_group" "airflow_ingress_sg" {
})
}

resource "aws_security_group" "airflow_ingress_sg_internal" {
name = "${var.project}-${var.venue}-airflow-internal-ingress-sg"
description = "SecurityGroup for Airflow LoadBalancer internal ingress"
vpc_id = data.aws_vpc.cluster_vpc.id
tags = merge(local.common_tags, {
Name = format(local.resource_name_prefix, "AirflowLBSg")
Component = "airflow"
Stack = "airflow"
})
}

#tfsec:ignore:AVD-AWS-0107
resource "aws_vpc_security_group_ingress_rule" "airflow_ingress_sg_jpl_rule" {
for_each = toset(["128.149.0.0/16", "137.78.0.0/16", "137.79.0.0/16"])
Expand All @@ -437,7 +448,7 @@ data "aws_security_groups" "venue_proxy_sg" {

resource "aws_vpc_security_group_ingress_rule" "airflow_ingress_sg_proxy_rule" {
count = length(data.aws_security_groups.venue_proxy_sg.ids) > 0 ? 1 : 0
security_group_id = aws_security_group.airflow_ingress_sg.id
security_group_id = aws_security_group.airflow_ingress_sg_internal.id
description = "SecurityGroup ingress rule for venue-services proxy"
ip_protocol = "tcp"
from_port = local.load_balancer_port
Expand Down Expand Up @@ -482,6 +493,43 @@ resource "kubernetes_ingress_v1" "airflow_ingress" {
depends_on = [helm_release.airflow]
}

resource "kubernetes_ingress_v1" "airflow_ingress_internal" {
metadata {
name = "airflow-ingress-internal"
namespace = data.kubernetes_namespace.service_area.metadata[0].name
annotations = {
"alb.ingress.kubernetes.io/scheme" = "internal"
"alb.ingress.kubernetes.io/target-type" = "ip"
"alb.ingress.kubernetes.io/subnets" = join(",", jsondecode(data.aws_ssm_parameter.subnet_ids.value)["private"])
"alb.ingress.kubernetes.io/listen-ports" = "[{\"HTTP\": ${local.load_balancer_port}}]"
"alb.ingress.kubernetes.io/security-groups" = aws_security_group.airflow_ingress_sg_internal.id
"alb.ingress.kubernetes.io/manage-backend-security-group-rules" = "true"
"alb.ingress.kubernetes.io/healthcheck-path" = "/health"
}
}
spec {
ingress_class_name = "alb"
rule {
http {
path {
path = "/"
path_type = "Prefix"
backend {
service {
name = "airflow-webserver"
port {
number = 8080
}
}
}
}
}
}
}
wait_for_load_balancer = true
depends_on = [helm_release.airflow]
}

resource "aws_ssm_parameter" "airflow_ui_url" {
name = format("/%s", join("/", compact(["", var.project, var.venue, var.service_area, "processing", "airflow", "ui_url"])))
description = "The URL of the Airflow UI."
Expand All @@ -500,8 +548,8 @@ resource "aws_ssm_parameter" "airflow_ui_health_check_endpoint" {
type = "String"
value = jsonencode({
"componentName" : "Airflow UI"
"healthCheckUrl" : "http://${data.kubernetes_ingress_v1.airflow_ingress.status[0].load_balancer[0].ingress[0].hostname}:5000/health"
"landingPageUrl" : "http://${data.kubernetes_ingress_v1.airflow_ingress.status[0].load_balancer[0].ingress[0].hostname}:5000"
"healthCheckUrl" : "http://${data.kubernetes_ingress_v1.airflow_ingress_internal.status[0].load_balancer[0].ingress[0].hostname}:5000/health"
"landingPageUrl" : "http://${data.kubernetes_ingress_v1.airflow_ingress_internal.status[0].load_balancer[0].ingress[0].hostname}:5000"
})
tags = merge(local.common_tags, {
Name = format(local.resource_name_prefix, "health-check-endpoints-airflow_ui")
Expand Down Expand Up @@ -531,8 +579,8 @@ resource "aws_ssm_parameter" "airflow_api_health_check_endpoint" {
type = "String"
value = jsonencode({
"componentName" : "Airflow API"
"healthCheckUrl" : "http://${data.kubernetes_ingress_v1.airflow_ingress.status[0].load_balancer[0].ingress[0].hostname}:5000/api/v1/health"
"landingPageUrl" : "http://${data.kubernetes_ingress_v1.airflow_ingress.status[0].load_balancer[0].ingress[0].hostname}:5000/api/v1"
"healthCheckUrl" : "http://${data.kubernetes_ingress_v1.airflow_ingress_internal.status[0].load_balancer[0].ingress[0].hostname}:5000/api/v1/health"
"landingPageUrl" : "http://${data.kubernetes_ingress_v1.airflow_ingress_internal.status[0].load_balancer[0].ingress[0].hostname}:5000/api/v1"
})
tags = merge(local.common_tags, {
Name = format(local.resource_name_prefix, "health-check-endpoints-airflow_api")
Expand All @@ -557,7 +605,7 @@ resource "aws_ssm_parameter" "unity_proxy_airflow_ui" {
Redirect "/${var.project}/${var.venue}/sps/home"
</Location>
<LocationMatch "^/${var.project}/${var.venue}/sps/(.*)$">
ProxyPassMatch "http://${data.kubernetes_ingress_v1.airflow_ingress.status[0].load_balancer[0].ingress[0].hostname}:5000/$1"
ProxyPassMatch "http://${data.kubernetes_ingress_v1.airflow_ingress_internal.status[0].load_balancer[0].ingress[0].hostname}:5000/$1"
ProxyPreserveHost On
FallbackResource /management/index.html
AddOutputFilterByType INFLATE;SUBSTITUTE;DEFLATE text/html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ No modules.
|------|------|
| [aws_lambda_invocation.unity_proxy_lambda_invocation](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/resources/lambda_invocation) | resource |
| [aws_security_group.ogc_ingress_sg](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/resources/security_group) | resource |
| [aws_security_group.ogc_ingress_sg_internal](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/resources/security_group) | resource |
| [aws_ssm_parameter.ogc_processes_api_health_check_endpoint](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/resources/ssm_parameter) | resource |
| [aws_ssm_parameter.ogc_processes_api_url](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/resources/ssm_parameter) | resource |
| [aws_ssm_parameter.ogc_processes_ui_url](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/resources/ssm_parameter) | resource |
Expand All @@ -33,6 +34,7 @@ No modules.
| [kubernetes_deployment.ogc_processes_api](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/resources/deployment) | resource |
| [kubernetes_deployment.redis](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/resources/deployment) | resource |
| [kubernetes_ingress_v1.ogc_processes_api_ingress](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/resources/ingress_v1) | resource |
| [kubernetes_ingress_v1.ogc_processes_api_ingress_internal](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/resources/ingress_v1) | resource |
| [kubernetes_service.ogc_processes_api](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/resources/service) | resource |
| [kubernetes_service.redis](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/resources/service) | resource |
| [aws_db_instance.db](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/data-sources/db_instance) | data source |
Expand All @@ -43,6 +45,7 @@ No modules.
| [aws_ssm_parameter.subnet_ids](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/data-sources/ssm_parameter) | data source |
| [aws_vpc.cluster_vpc](https://registry.terraform.io/providers/hashicorp/aws/5.50.0/docs/data-sources/vpc) | data source |
| [kubernetes_ingress_v1.ogc_processes_api_ingress](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/data-sources/ingress_v1) | data source |
| [kubernetes_ingress_v1.ogc_processes_api_ingress_internal](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/data-sources/ingress_v1) | data source |
| [kubernetes_namespace.service_area](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/data-sources/namespace) | data source |
| [kubernetes_persistent_volume_claim.airflow_deployed_dags](https://registry.terraform.io/providers/hashicorp/kubernetes/2.29.0/docs/data-sources/persistent_volume_claim) | data source |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,10 @@ data "kubernetes_ingress_v1" "ogc_processes_api_ingress" {
namespace = data.kubernetes_namespace.service_area.metadata[0].name
}
}

data "kubernetes_ingress_v1" "ogc_processes_api_ingress_internal" {
metadata {
name = kubernetes_ingress_v1.ogc_processes_api_ingress_internal.metadata[0].name
namespace = data.kubernetes_namespace.service_area.metadata[0].name
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,17 @@ resource "aws_security_group" "ogc_ingress_sg" {
})
}

resource "aws_security_group" "ogc_ingress_sg_internal" {
name = "${var.project}-${var.venue}-ogc-internal-ingress-sg"
description = "SecurityGroup for OGC API LoadBalancer internal ingress"
vpc_id = data.aws_vpc.cluster_vpc.id
tags = merge(local.common_tags, {
Name = format(local.resource_name_prefix, "OgcLBSg")
Component = "ogc"
Stack = "ogc"
})
}

#tfsec:ignore:AVD-AWS-0107
resource "aws_vpc_security_group_ingress_rule" "ogc_ingress_sg_jpl_rule" {
for_each = toset(["128.149.0.0/16", "137.78.0.0/16", "137.79.0.0/16"])
Expand All @@ -240,7 +251,7 @@ data "aws_security_groups" "venue_proxy_sg" {

resource "aws_vpc_security_group_ingress_rule" "ogc_ingress_sg_proxy_rule" {
count = length(data.aws_security_groups.venue_proxy_sg.ids) > 0 ? 1 : 0
security_group_id = aws_security_group.ogc_ingress_sg.id
security_group_id = aws_security_group.ogc_ingress_sg_internal.id
description = "SecurityGroup ingress rule for venue-services proxy"
ip_protocol = "tcp"
from_port = local.load_balancer_port
Expand Down Expand Up @@ -284,6 +295,41 @@ resource "kubernetes_ingress_v1" "ogc_processes_api_ingress" {
wait_for_load_balancer = true
}

resource "kubernetes_ingress_v1" "ogc_processes_api_ingress_internal" {
metadata {
name = "ogc-processes-api-ingress-internal"
namespace = data.kubernetes_namespace.service_area.metadata[0].name
annotations = {
"alb.ingress.kubernetes.io/scheme" = "internal"
"alb.ingress.kubernetes.io/target-type" = "ip"
"alb.ingress.kubernetes.io/subnets" = join(",", jsondecode(data.aws_ssm_parameter.subnet_ids.value)["private"])
"alb.ingress.kubernetes.io/listen-ports" = "[{\"HTTP\": ${local.load_balancer_port}}]"
"alb.ingress.kubernetes.io/security-groups" = aws_security_group.ogc_ingress_sg_internal.id
"alb.ingress.kubernetes.io/manage-backend-security-group-rules" = "true"
"alb.ingress.kubernetes.io/healthcheck-path" = "/health"
}
}
spec {
ingress_class_name = "alb"
rule {
http {
path {
path = "/"
path_type = "Prefix"
backend {
service {
name = kubernetes_service.ogc_processes_api.metadata[0].name
port {
number = 80
}
}
}
}
}
}
}
wait_for_load_balancer = true
}

resource "aws_ssm_parameter" "ogc_processes_ui_url" {
name = format("/%s", join("/", compact(["", var.project, var.venue, var.service_area, "processing", "ogc_processes", "ui_url"])))
Expand Down Expand Up @@ -315,8 +361,8 @@ resource "aws_ssm_parameter" "ogc_processes_api_health_check_endpoint" {
type = "String"
value = jsonencode({
"componentName" : "OGC API"
"healthCheckUrl" : "http://${data.kubernetes_ingress_v1.ogc_processes_api_ingress.status[0].load_balancer[0].ingress[0].hostname}:5001/health"
"landingPageUrl" : "http://${data.kubernetes_ingress_v1.ogc_processes_api_ingress.status[0].load_balancer[0].ingress[0].hostname}:5001"
"healthCheckUrl" : "http://${data.kubernetes_ingress_v1.ogc_processes_api_ingress_internal.status[0].load_balancer[0].ingress[0].hostname}:5001/health"
"landingPageUrl" : "http://${data.kubernetes_ingress_v1.ogc_processes_api_ingress_internal.status[0].load_balancer[0].ingress[0].hostname}:5001"
})
tags = merge(local.common_tags, {
Name = format(local.resource_name_prefix, "health-check-endpoints-ogc_processes_api")
Expand All @@ -338,7 +384,7 @@ resource "aws_ssm_parameter" "unity_proxy_ogc_api" {
ProxyPassReverse "/"
</Location>
<LocationMatch "^/${var.project}/${var.venue}/ogc/(.*)$">
ProxyPassMatch "http://${data.kubernetes_ingress_v1.ogc_processes_api_ingress.status[0].load_balancer[0].ingress[0].hostname}:5001/$1"
ProxyPassMatch "http://${data.kubernetes_ingress_v1.ogc_processes_api_ingress_internal.status[0].load_balancer[0].ingress[0].hostname}:5001/$1"
ProxyPreserveHost On
FallbackResource /management/index.html
AddOutputFilterByType INFLATE;SUBSTITUTE;DEFLATE text/html
Expand Down

0 comments on commit 95dad09

Please sign in to comment.