Refacto and add modules

This commit is contained in:
2024-02-06 11:03:20 +01:00
parent 159b576b24
commit bcdf666cc0
47 changed files with 661 additions and 338 deletions

View File

@@ -1,12 +1,10 @@
locals {
forward_outpost_providers = jsondecode(data.http.get_forward_outpost.response_body).results[0].providers
forward_outpost_pk = jsondecode(data.http.get_forward_outpost.response_body).results[0].pk
app_slug = "${var.instance}${var.component==""?"":"-"}${var.component}"
app_slug = "${var.instance}${var.component == "" ? "" : "-"}${var.component}"
forward_labels = merge(var.labels, {
"app.kubernetes.io/component" = "authentik-forward"
})
main_group = format("app-%s", var.app_name)
external_url = format("https://%s", var.dns_names[0])
main_group = format("app-%s", var.app_name)
external_url = format("https://%s", var.dns_names[0])
rules_icons = [for v in var.dns_names : {
"host" = "${v}"
"http" = {
@@ -19,9 +17,11 @@ locals {
}]
}
}]
forward_outpost_providers = jsondecode(data.http.get_forward_outpost.response_body).results[0].providers
forward_outpost_pk = jsondecode(data.http.get_forward_outpost.response_body).results[0].pk
}
resource "kubectl_manifest" "prj_ingress_icon" {
resource "kubectl_manifest" "ingress_icon" {
force_conflicts = true
yaml_body = <<-EOF
apiVersion: "networking.k8s.io/v1"
@@ -39,20 +39,20 @@ resource "kubectl_manifest" "prj_ingress_icon" {
EOF
}
data "authentik_flow" "default-authorization-flow" {
data "authentik_flow" "default_authorization_flow" {
slug = "default-provider-authorization-implicit-consent"
}
resource "authentik_provider_proxy" "prj_forward" {
resource "authentik_provider_proxy" "forward" {
name = local.app_slug
external_host = local.external_url
authorization_flow = data.authentik_flow.default-authorization-flow.id
authorization_flow = data.authentik_flow.default_authorization_flow.id
mode = "forward_single"
access_token_validity = var.access_token_validity
}
data "http" "get_forward_outpost" {
depends_on = [authentik_provider_proxy.prj_forward]
depends_on = [authentik_provider_proxy.forward]
url = "http://authentik.${var.domain}-auth.svc/api/v3/outposts/instances/?name__iexact=forward"
method = "GET"
request_headers = var.request_headers
@@ -68,11 +68,11 @@ resource "restapi_object" "forward_outpost_binding" {
path = "/outposts/instances/${local.forward_outpost_pk}/"
data = jsonencode({
name = "forward"
providers = contains(local.forward_outpost_providers, authentik_provider_proxy.prj_forward.id) ? local.forward_outpost_providers : concat(local.forward_outpost_providers, [authentik_provider_proxy.prj_forward.id])
providers = contains(local.forward_outpost_providers, authentik_provider_proxy.forward.id) ? local.forward_outpost_providers : concat(local.forward_outpost_providers, [authentik_provider_proxy.forward.id])
})
}
resource "kubectl_manifest" "prj_middleware" {
resource "kubectl_manifest" "middleware" {
yaml_body = <<-EOF
apiVersion: traefik.io/v1alpha1
kind: Middleware

View File

@@ -1,5 +1,5 @@
output "provider-id" {
value = authentik_provider_proxy.prj_forward.id
output "provider_id" {
value = authentik_provider_proxy.forward.id
}
output "sso_logout" {
@@ -8,4 +8,4 @@ output "sso_logout" {
output "middleware" {
value = "${local.app_slug}-forward"
}
}

View File

@@ -1,20 +1,20 @@
terraform {
required_providers {
kubectl = {
source = "gavinbunney/kubectl"
version = "~> 1.14.0"
source = "gavinbunney/kubectl"
version = "~> 1.14.0"
}
authentik = {
source = "goauthentik/authentik"
version = "~> 2023.5.0"
source = "goauthentik/authentik"
version = "~> 2023.5.0"
}
http = {
source = "hashicorp/http"
version = "~> 3.3.0"
source = "hashicorp/http"
version = "~> 3.3.0"
}
restapi = {
source = "Mastercard/restapi"
version = "~> 1.18.0"
source = "Mastercard/restapi"
version = "~> 1.18.0"
}
}
}

View File

@@ -6,10 +6,6 @@ variable "instance" {
type = string
}
variable "icon" {
type = string
}
variable "domain" {
type = string
}
@@ -18,12 +14,16 @@ variable "namespace" {
type = string
}
variable "labels" {
type = map(string)
}
variable "ingress_class" {
type = string
}
variable "labels" {
type = map(string)
variable "icon" {
type = string
}
variable "dns_names" {
@@ -36,11 +36,17 @@ variable "access_token_validity" {
}
variable "app_name" {
type = string
type = string
default = ""
}
variable "service" {
type = object({
name = string
port = object({
number = number
})
})
}
variable "request_headers" {