Refacto OAuth2
This commit is contained in:
@@ -6,14 +6,16 @@ locals {
|
||||
app_name = var.app_name != "" ? var.app_name : var.component == var.instance ? var.instance : format("%s-%s", var.instance, var.component)
|
||||
main_group = format("app-%s", local.app_slug)
|
||||
secret_name = var.cert_name != "" ? var.cert_name : "${local.app_slug}-cert"
|
||||
url_icon = startswith(var.icon, "fa-") ? "fa://${var.icon}" : format("https://%s/%s", var.dns_name, var.icon)
|
||||
dns_name = var.dns_name != "" ? var.dns_name : var.rule_mapper.host
|
||||
url_icon = startswith(var.icon, "fa-") ? "fa://${var.icon}" : format("https://%s/%s", local.dns_name, var.icon)
|
||||
backend = var.rule_mapper.paths[0].backend
|
||||
rules_icons = [{
|
||||
"host" = var.rule_mapper.host
|
||||
"host" = local.dns_name
|
||||
"http" = {
|
||||
"paths" = [for mapper in var.rule_mapper.paths : {
|
||||
"paths" = [{
|
||||
"path" = "/${var.icon}"
|
||||
"pathType" = "Prefix"
|
||||
"backend" = mapper.backend
|
||||
"backend" = local.backend
|
||||
}]
|
||||
}
|
||||
}]
|
||||
@@ -33,8 +35,8 @@ resource "kubectl_manifest" "ingress_icon" {
|
||||
ingressClassName: "${var.ingress_class}"
|
||||
rules: ${jsonencode(local.rules_icons)}
|
||||
tls:
|
||||
- hosts: ${jsonencode([var.dns_name])}
|
||||
secretName: "${local.secret_name}"
|
||||
- secretName: "${local.secret_name}"
|
||||
hosts: ${jsonencode([local.dns_name])}
|
||||
EOF
|
||||
}
|
||||
|
||||
@@ -58,7 +60,7 @@ resource "authentik_application" "app" {
|
||||
group = var.app_group
|
||||
protocol_provider = var.protocol_provider
|
||||
backchannel_providers = var.backchannel_providers
|
||||
meta_launch_url = format("https://%s", var.dns_name)
|
||||
meta_launch_url = format("https://%s", local.dns_name)
|
||||
meta_icon = local.url_icon
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,11 @@ output "application_id" {
|
||||
output "policy_id" {
|
||||
value = authentik_policy_expression.policy.id
|
||||
}
|
||||
|
||||
output "main_group" {
|
||||
value = local.main_group
|
||||
}
|
||||
|
||||
output "main_group_id" {
|
||||
value = authentik_group.groups.id
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@ variable "protocol_provider" {
|
||||
}
|
||||
variable "dns_name" {
|
||||
type = string
|
||||
description = "Deprecated, use rule_mapper"
|
||||
default = ""
|
||||
}
|
||||
variable "app_name" {
|
||||
type = string
|
||||
@@ -33,7 +35,7 @@ variable "backchannel_providers" {
|
||||
|
||||
variable "ingress_class" {
|
||||
type = string
|
||||
default = ""
|
||||
default = "traefik"
|
||||
}
|
||||
|
||||
variable "rule_mapper" {
|
||||
|
||||
@@ -4,6 +4,7 @@ locals {
|
||||
"app.kubernetes.io/component" = "authentik-oauth2"
|
||||
})
|
||||
}
|
||||
|
||||
resource "random_password" "client_id" {
|
||||
length = 32
|
||||
special = false
|
||||
@@ -30,8 +31,8 @@ data "authentik_flow" "default_authentication_flow" {
|
||||
resource "authentik_provider_oauth2" "oauth2" {
|
||||
name = local.app_slug
|
||||
client_id = random_password.client_id.result
|
||||
authentication_flow = data.authentik_flow.default-authentication-flow.id
|
||||
authorization_flow = data.authentik_flow.default-authorization-flow.id
|
||||
authentication_flow = data.authentik_flow.default_authentication_flow.id
|
||||
authorization_flow = data.authentik_flow.default_authorization_flow.id
|
||||
client_type = "confidential"
|
||||
sub_mode = "user_username"
|
||||
signing_key = data.authentik_certificate_key_pair.ca.id
|
||||
@@ -41,40 +42,22 @@ resource "authentik_provider_oauth2" "oauth2" {
|
||||
]
|
||||
}
|
||||
|
||||
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" {
|
||||
metadata {
|
||||
name = "${local.app_slug}-secret"
|
||||
namespace = var.namespace
|
||||
labels = local.oauth2_labels
|
||||
}
|
||||
data = {
|
||||
client-id = random_password.client_id.result
|
||||
client-secret = authentik_provider_oauth2.oauth2.client_secret
|
||||
}
|
||||
}
|
||||
|
||||
data "kubernetes_secret_v1" "oauth2_client_secret" {
|
||||
depends_on = [kubernetes_secret_v1.oauth2_client_secret]
|
||||
metadata {
|
||||
name = kubernetes_secret_v1.oauth2_client_secret.metadata[0].name
|
||||
namespace = var.namespace
|
||||
}
|
||||
}
|
||||
|
||||
data "kubernetes_ingress_v1" "authentik" {
|
||||
metadata {
|
||||
name = "authentik"
|
||||
namespace = "${var.domain}-auth"
|
||||
}
|
||||
}
|
||||
|
||||
resource "kubernetes_secret" "oauth2_client_secret" {
|
||||
metadata {
|
||||
name = "${local.app_slug}-oauth2"
|
||||
namespace = var.namespace
|
||||
labels = local.oauth2_labels
|
||||
}
|
||||
data = {
|
||||
oidc_endpoint = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/application/o/${local.app_slug}/"
|
||||
client_id = random_password.client_id.result
|
||||
client_secret = authentik_provider_oauth2.oauth2.client_secret
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,34 +5,32 @@ output "provider_id" {
|
||||
output "sso_signout_url" {
|
||||
value = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/application/o/${var.component}-${var.instance}/end-session/"
|
||||
}
|
||||
|
||||
output "sso_configuration_url" {
|
||||
value = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/application/o/${local.app_slug}/"
|
||||
}
|
||||
|
||||
output "sso_userinfo_url" {
|
||||
value = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/application/o/userinfo/"
|
||||
}
|
||||
|
||||
output "sso_authorize_url" {
|
||||
value = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/application/o/authorize/"
|
||||
}
|
||||
|
||||
output "sso_token_url" {
|
||||
value = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/application/o/token/"
|
||||
}
|
||||
|
||||
output "client_id" {
|
||||
value = random_password.client_id.result
|
||||
}
|
||||
|
||||
output "client_secret" {
|
||||
value = authentik_provider_oauth2.oauth2.client_secret
|
||||
}
|
||||
|
||||
output "secret_client_id_name" {
|
||||
value = kubernetes_secret_v1.oauth2-client-secret.metadata[0].name
|
||||
}
|
||||
output "secret_client_secret_name" {
|
||||
value = kubernetes_secret_v1.oauth2_client_secret.metadata[0].name
|
||||
}
|
||||
output "secret_client_id_key" {
|
||||
value = "client-id"
|
||||
}
|
||||
output "secret_client_secret_key" {
|
||||
value = "client-secret"
|
||||
output "oauth2_secret_name" {
|
||||
value = kubernetes_secret.oauth2_client_secret.metadata[0].name
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
locals {
|
||||
selector = var.selector==null?var.labels:var.selector
|
||||
app_slug = "${var.instance}${var.component == "" ? "" : "-"}${var.component}"
|
||||
cluster_ports = var.svc_type == "ClusterIP" ? [for idx, target in var.targets : {
|
||||
"name" = target
|
||||
"port" = var.ports[idx]
|
||||
"protocol" = var.protocols[idx]
|
||||
"targetPort" = target
|
||||
default_ports = var.svc_type != "NodePort" ? [for port_map in var.port_mapper : {
|
||||
"name" = port_map.name != null && port_map.name != "" ? port_map.name : "${port_map.port}-${port_map.protocol}"
|
||||
"port" = port_map.port
|
||||
"protocol" = port_map.protocol
|
||||
"targetPort" = port_map.target
|
||||
}] : []
|
||||
node_ports = var.svc_type == "NodePort" ? [for port_map in var.port_mapper : {
|
||||
"port" = port_map.port
|
||||
@@ -25,8 +24,9 @@ locals {
|
||||
spec = {
|
||||
"ClusterIP" = {
|
||||
type = "ClusterIP"
|
||||
ports = local.cluster_ports
|
||||
selector = local.selector
|
||||
ports = local.default_ports
|
||||
selector = var.labels
|
||||
ipFamilyPolicy = var.ip_family
|
||||
},
|
||||
"ExternalName" = {
|
||||
type = "ExternalName"
|
||||
@@ -35,13 +35,14 @@ locals {
|
||||
},
|
||||
"NodePort" = {
|
||||
type = "NodePort"
|
||||
selector = local.selector
|
||||
selector = var.labels
|
||||
ports = local.node_ports
|
||||
ipFamilyPolicy = var.ip_family
|
||||
},
|
||||
"LoadBalancer" = {
|
||||
type = "LoadBalancer"
|
||||
selector = local.selector
|
||||
ports = local.lb_ports
|
||||
selector = var.labels
|
||||
ports = local.default_ports
|
||||
externalTrafficPolicy = var.lb_policy
|
||||
ipFamilyPolicy = var.ip_family
|
||||
}
|
||||
|
||||
@@ -10,10 +10,6 @@ variable "namespace" {
|
||||
variable "labels" {
|
||||
type = map(string)
|
||||
}
|
||||
variable "selector" {
|
||||
type = map(string)
|
||||
default = null
|
||||
}
|
||||
variable "annotations" {
|
||||
type = map(string)
|
||||
default = {}
|
||||
|
||||
Reference in New Issue
Block a user