Skip to content

Commit

Permalink
Redirecting http to https when ssl is present
Browse files Browse the repository at this point in the history
Following this solution: kubernetes/ingress-nginx#2724 (comment)

Tested it with both no https and with https.
ALL SERVICES WILL REDIRECT PUBLIC TRAFFIC HTTP TO HTTPS IF SSL IS PRESENT, NO EXCEPTION
  • Loading branch information
juandiegopalomino committed Feb 25, 2021
1 parent a79bd8a commit 632d5d3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
7 changes: 4 additions & 3 deletions config/tf_modules/k8s-base/ingress_nginx.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// NOTE: following this solution for http -> https redirect: https://github.com/kubernetes/ingress-nginx/issues/2724#issuecomment-593769295
resource "helm_release" "ingress-nginx" {
chart = "ingress-nginx"
name = "ingress-nginx"
Expand All @@ -9,9 +10,7 @@ resource "helm_release" "ingress-nginx" {
values = [
yamlencode({
controller: {
config: {
"use-proxy-protocol": false # TODO: set this to true and figure out how to play nicely w/ NLB
}
config: local.config
podAnnotations: {
"linkerd.io/inject": "enabled"
}
Expand Down Expand Up @@ -56,12 +55,14 @@ resource "helm_release" "ingress-nginx" {
]
}
}
containerPort: local.container_ports
service: {
loadBalancerSourceRanges: ["0.0.0.0/0"]
externalTrafficPolicy: "Local"
enableHttps: var.cert_arn == "" ? false : true
targetPorts: local.target_ports
annotations: {
"service.beta.kubernetes.io/aws-load-balancer-backend-protocol": "tcp"
"service.beta.kubernetes.io/aws-load-balancer-type": "nlb"
"service.beta.kubernetes.io/aws-load-balancer-ssl-ports": var.cert_arn == "" ? "" : "https"
"service.beta.kubernetes.io/aws-load-balancer-ssl-cert": var.cert_arn
Expand Down
12 changes: 11 additions & 1 deletion config/tf_modules/k8s-base/variables.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
locals {
target_ports = var.cert_arn == "" ? { http: "http" } : { http: "http", https: "http" }
target_ports = var.cert_arn == "" ? { http: "http" } : { http: "http", https: "special" }
container_ports = var.cert_arn == "" ? { http: 80, https: 443 } : { http: 80, https: 443, special: 8000 }
config = var.cert_arn == "" ? { ssl-redirect: false } : {
ssl-redirect: false
server-snippet: <<EOF
listen 8000;
if ( $server_port = 80 ) {
return 308 https://$host$request_uri;
}
EOF
}
}

data "aws_eks_cluster" "main" {
Expand Down
2 changes: 1 addition & 1 deletion opta/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def _cleanup() -> None:
logger.error(e.stderr.decode("utf-8"))
sys.exit(1)
except UserErrors as e:
logger.debug(e)
logger.error(e)
sys.exit(1)
except Exception as e:
logger.exception(e)
Expand Down

0 comments on commit 632d5d3

Please sign in to comment.