3 Commits

Author SHA1 Message Date
159b576b24 No more sercretString dans oauth2 2024-05-17 16:10:54 +02:00
1c42b356c1 Add selector(optional) to service 2024-05-17 16:05:20 +02:00
6ea3cfc0bf fix redirect url 2024-05-17 12:11:06 +02:00
4 changed files with 28 additions and 31 deletions

View File

@@ -4,29 +4,9 @@ locals {
"app.kubernetes.io/component" = "authentik-oauth2" "app.kubernetes.io/component" = "authentik-oauth2"
}) })
} }
resource "kubectl_manifest" "oauth2-secret" { resource "random_password" "client_id" {
ignore_fields = ["metadata.annotations"] length = 32
yaml_body = <<-EOF special = false
apiVersion: "secretgenerator.mittwald.de/v1alpha1"
kind: "StringSecret"
metadata:
name: "${local.app_slug}-id"
namespace: "${var.namespace}"
labels: ${jsonencode(local.oauth2_labels)}
spec:
forceRegenerate: false
fields:
- fieldName: "client-id"
length: "32"
EOF
}
data "kubernetes_secret_v1" "oauth2-client-id" {
depends_on = [kubectl_manifest.oauth2-secret]
metadata {
name = kubectl_manifest.oauth2-secret.name
namespace = var.namespace
labels = local.oauth2_labels
}
} }
data "authentik_certificate_key_pair" "ca" { data "authentik_certificate_key_pair" "ca" {
@@ -49,7 +29,7 @@ data "authentik_flow" "default-authentication-flow" {
resource "authentik_provider_oauth2" "oauth2" { resource "authentik_provider_oauth2" "oauth2" {
name = "${local.app_slug}" name = "${local.app_slug}"
client_id = data.kubernetes_secret_v1.oauth2-client-id.data["client-id"] client_id = random_password.client_id.result
authentication_flow = data.authentik_flow.default-authentication-flow.id authentication_flow = data.authentik_flow.default-authentication-flow.id
authorization_flow = data.authentik_flow.default-authorization-flow.id authorization_flow = data.authentik_flow.default-authorization-flow.id
client_type = "confidential" client_type = "confidential"
@@ -57,10 +37,21 @@ resource "authentik_provider_oauth2" "oauth2" {
signing_key = data.authentik_certificate_key_pair.ca.id signing_key = data.authentik_certificate_key_pair.ca.id
property_mappings = data.authentik_scope_mapping.oauth2.ids property_mappings = data.authentik_scope_mapping.oauth2.ids
redirect_uris = [ redirect_uris = [
"https://${var.dns_name}/${var.redirect_path}" "https://${var.redirect_path!=""?"${var.dns_name}/${var.redirect_path}":"${var.dns_name}"}"
] ]
} }
resource "kubernetes_secret_v1" "oauth2-client-id" {
metadata {
name = "${local.app_slug}-id"
namespace = var.namespace
labels = local.oauth2_labels
}
data = {
client-id = random_password.client_id.result
}
}
resource "kubernetes_secret_v1" "oauth2-client-secret" { resource "kubernetes_secret_v1" "oauth2-client-secret" {
metadata { metadata {
name = "${local.app_slug}-secret" name = "${local.app_slug}-secret"
@@ -68,6 +59,7 @@ resource "kubernetes_secret_v1" "oauth2-client-secret" {
labels = local.oauth2_labels labels = local.oauth2_labels
} }
data = { data = {
client-id = random_password.client_id.result
client-secret = authentik_provider_oauth2.oauth2.client_secret client-secret = authentik_provider_oauth2.oauth2.client_secret
} }
} }

View File

@@ -18,14 +18,14 @@ output "sso_token_url" {
value = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/application/o/token/" value = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/application/o/token/"
} }
output "client_id" { output "client_id" {
value = data.kubernetes_secret_v1.oauth2-client-id.data["client-id"] value = random_password.client_id.result
} }
output "client_secret" { output "client_secret" {
value = data.kubernetes_secret_v1.oauth2-client-secret.data["client-secret"] value = authentik_provider_oauth2.oauth2.client_secret
} }
output "secret_client_id_name" { output "secret_client_id_name" {
value = kubectl_manifest.oauth2-secret.name value = kubernetes_secret_v1.oauth2-client-secret.metadata[0].name
} }
output "secret_client_secret_name" { output "secret_client_secret_name" {
value = kubernetes_secret_v1.oauth2-client-secret.metadata[0].name value = kubernetes_secret_v1.oauth2-client-secret.metadata[0].name

View File

@@ -1,4 +1,5 @@
locals { locals {
selector = var.selector==null?var.labels:var.selector
app_slug = "${var.instance}${var.component==""?"":"-"}${var.component}" app_slug = "${var.instance}${var.component==""?"":"-"}${var.component}"
cluster_ports = var.svc_type == "ClusterIP" ? [for idx, target in var.targets : { cluster_ports = var.svc_type == "ClusterIP" ? [for idx, target in var.targets : {
"name" = target "name" = target
@@ -35,7 +36,7 @@ locals {
"ClusterIP" = { "ClusterIP" = {
type = "ClusterIP" type = "ClusterIP"
ports = local.cluster_ports ports = local.cluster_ports
selector = var.labels selector = local.selector
}, },
"ExternalName" = { "ExternalName" = {
type = "ExternalName" type = "ExternalName"
@@ -44,12 +45,12 @@ locals {
}, },
"NodePort" = { "NodePort" = {
type = "NodePort" type = "NodePort"
selector = var.labels selector = local.selector
ports = local.node_ports ports = local.node_ports
}, },
"LoadBalancer" = { "LoadBalancer" = {
type = "LoadBalancer" type = "LoadBalancer"
selector = var.labels selector = local.selector
ports = local.lb_ports ports = local.lb_ports
externalTrafficPolicy = var.lb_policy externalTrafficPolicy = var.lb_policy
} }

View File

@@ -10,6 +10,10 @@ variable "namespace" {
variable "labels" { variable "labels" {
type = map(string) type = map(string)
} }
variable "selector" {
type = map(string)
default = null
}
variable "annotations" { variable "annotations" {
type = map(string) type = map(string)
default = {} default = {}