Manage OpenArtifact Keycloak SSO and publish its client credential to Vaultwarden
Update Kubernetes Services Wiki / Generate and Update K8s Wiki (push) Successful in 7s
Keycloak Terraform / Terraform (push) Failing after 21s

This commit is contained in:
Aleksandr Bogomiakov committed 2026-11-24 20:15:33 +02:00
1 parent cabc9b167a
commit dd591ecd1d
11 files changed
+208 -1

No files matched your search

+6
View File
@@ -47,9 +47,15 @@ jobs:
working-directory: ./terraform/keycloak
- name: Terraform Apply
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
env:
TF_VAR_keycloak_client_secret: ${{ secrets.KEYCLOAK_CLIENT_SECRET }}
TF_VAR_google_client_id: ${{ secrets.GOOGLE_CLIENT_ID }}
TF_VAR_google_client_secret: ${{ secrets.GOOGLE_CLIENT_SECRET }}
TF_VAR_vaultwarden_organization_id: ${{ vars.VAULTWARDEN_ORGANIZATION_ID }}
TF_VAR_vaultwarden_collection_id: ${{ vars.VAULTWARDEN_COLLECTION_ID }}
BW_CLIENTID: ${{ secrets.BW_CLIENTID }}
BW_CLIENTSECRET: ${{ secrets.BW_CLIENTSECRET }}
BW_PASSWORD: ${{ secrets.BW_PASSWORD }}
run: terraform apply -input=false -auto-approve -parallelism=100
working-directory: ./terraform/keycloak
+35
View File
@@ -0,0 +1,35 @@
import json
from http.cookiejar import CookieJar
from pathlib import Path
from urllib.request import HTTPCookieProcessor, Request, build_opener
server = "https://repo.hexor.cy"
cookies = CookieJar()
session = build_opener(HTTPCookieProcessor(cookies))
headers = {"Content-Type": "application/json", "Origin": server}
def post(path, body):
request = Request(server + "/api/v1/" + path, json.dumps(body).encode(), headers)
with session.open(request, timeout=30) as response:
return response.read()
manifest = Path("/config/sso.yaml").read_text()
credentials = Path("/credentials")
client_secret = (credentials / "client-secret").read_text()
post("auth/login", {"username": "admin", "password": (credentials / "admin-password").read_text()})
try:
headers["X-CSRF-Token"] = next(cookie.value for cookie in cookies if cookie.name == "oa_csrf")
result = json.loads(post("admin/declarative/apply", {
"manifest": manifest,
"force_secrets": True,
"secrets": {"OIDC_CLIENT_SECRET": client_secret},
}))
[provider] = result["items"]
if provider["name"] != "keycloak" or provider["result"] != "applied":
raise RuntimeError("OpenArtifact did not apply the Keycloak provider")
print("Keycloak SSO provider applied")
finally:
post("auth/logout", {})
@@ -5,6 +5,14 @@ namespace: openartifact
resources:
- db-secret.yaml
- secrets.yaml
- oidc-client.yaml
- sso-job.yaml
configMapGenerator:
- name: openartifact-sso
files:
- sso.yaml
- apply-sso.py
helmCharts:
- name: openartifact
+18
View File
@@ -0,0 +1,18 @@
apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata:
name: openartifact-oidc-client
spec:
refreshInterval: 5m
secretStoreRef:
kind: ClusterSecretStore
name: vaultwarden-login
target:
name: openartifact-oidc-client
creationPolicy: Owner
deletionPolicy: Retain
data:
- secretKey: client-secret
remoteRef:
key: keycloak/openartifact
property: login.password
+50
View File
@@ -0,0 +1,50 @@
apiVersion: batch/v1
kind: Job
metadata:
name: openartifact-apply-sso
annotations:
argocd.argoproj.io/hook: PostSync
argocd.argoproj.io/hook-delete-policy: BeforeHookCreation,HookSucceeded
spec:
backoffLimit: 2
activeDeadlineSeconds: 300
template:
spec:
automountServiceAccountToken: false
restartPolicy: Never
securityContext:
runAsNonRoot: true
runAsUser: 10001
runAsGroup: 10001
fsGroup: 10001
seccompProfile: {type: RuntimeDefault}
containers:
- name: apply
image: python:3.13-alpine3.22@sha256:e81548ac35b07a3bd4805f275107592ef458b1e893c0e04d45aedaa19416cca5
command: [python, -B, /config/apply-sso.py]
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities: {drop: [ALL]}
resources:
requests: {cpu: 10m, memory: 24Mi}
limits: {memory: 64Mi}
volumeMounts:
- {name: config, mountPath: /config, readOnly: true}
- {name: credentials, mountPath: /credentials, readOnly: true}
volumes:
- name: config
configMap:
name: openartifact-sso
- name: credentials
projected:
defaultMode: 0440
sources:
- secret:
name: openartifact-admin
items:
- {key: admin-password, path: admin-password}
- secret:
name: openartifact-oidc-client
items:
- {key: client-secret, path: client-secret}
+21
View File
@@ -0,0 +1,21 @@
apiVersion: openartifact.io/v1
kind: SSOProvider
metadata:
name: keycloak
spec:
kind: oidc
display_name: Keycloak
enabled: true
auto_provision_users: true
link_by_email: false
oidc_config:
issuer: https://auth.hexor.cy/auth/realms/hexor
client_id: openartifact
client_secret:
secretRef: {env: OIDC_CLIENT_SECRET}
scopes: [openid, email, profile]
groups_claim: groups
group_mapping:
/hexor-admin: keycloak-hexor-admin
permission_mapping:
keycloak-hexor-admin: admin-all
+23
View File
@@ -49,3 +49,26 @@ provider "registry.terraform.io/keycloak/keycloak" {
"zh:f57319d9a7ac387d5070c909fd0084ccfc296f1f3193efde995ae24aa1729a39",
]
}
provider "registry.terraform.io/maxlaverse/bitwarden" {
version = "0.23.0"
constraints = "0.23.0"
hashes = [
"h1:rGw+VwEbPnaUlT61GdMDFFLOPonrZ75/Y1fvlykkLyQ=",
"h1:vX8Ov0YGlR7vlJNFMZkqRcKuS/+SKDkNianb1ogIzCU=",
"zh:14a4b8900a58c2b3e829d7f12ac7e180c3885fa6be7ec7741beb18bf7ff4287f",
"zh:27b8830f6071f23081ee8afb6b3724ff6d146a871d8d5d97ec8697fedb3accd8",
"zh:3fbeada2810a4b069984b1d54d5c0b6b98b025a90690c3bcabe691aff8111323",
"zh:415fe80a7d80d1830812dcc1a42ceedfee2998ea3cf91c3d2d089f7e7f6cc5cc",
"zh:41e0e4964ab5906868e9976c75ef3169881290f253d19e494bdd7f7c012052b2",
"zh:5e217a3a46ea4a58be3fc8cb4b5fef1b0bebf3499ceda41ff094f3c1a5417aa0",
"zh:89f7ef1ec3b56873fc24ebcd88ddf7ef82195e5ac8172b5828c58336102ce3ee",
"zh:8df088984a2aee5466321c4ae75efd9250b6c06b6f66a6db5b6bd170c1cf2fd6",
"zh:98944e414519c7e9a7d7fcf0793ca1f50af35e685bec26ea323b09e3668fa5be",
"zh:a63e6c56918011bf1340ac4ddb89dc6a5fad29202beb235f0c9c546f3dc7427b",
"zh:d70d868ed09999fde1f11ac78f60b674a5d787dbaaa3e539a5dfb3192a3cedc5",
"zh:df794e4eb1bfc7c7e752ff59f61e95784526f84e0341694b38d54b2ea7c83b16",
"zh:f809ab383cca0a5f83072981c64208cbd7fa67e986a86ee02dd2c82333221e32",
"zh:ff919bc4dda39d8879bf9d659d61ec02ed65908781cf220cfb9a7265d360aea4",
]
}
+11 -1
View File
@@ -274,6 +274,16 @@ resource "keycloak_openid_client" "oauth2_app" {
access_token_lifespan = each.value.access_token_lifespan
}
resource "bitwarden_item_login" "oauth2_app" {
for_each = { for key, app in var.oauth2_applications : key => app if app.publish_to_vaultwarden }
name = "keycloak/${each.key}"
username = keycloak_openid_client.oauth2_app[each.key].client_id
password = keycloak_openid_client.oauth2_app[each.key].client_secret
organization_id = var.vaultwarden_organization_id
collection_ids = [var.vaultwarden_collection_id]
}
resource "keycloak_openid_group_membership_protocol_mapper" "oauth2_app_groups" {
for_each = var.oauth2_applications
@@ -281,7 +291,7 @@ resource "keycloak_openid_group_membership_protocol_mapper" "oauth2_app_groups"
client_id = keycloak_openid_client.oauth2_app[each.key].id
name = "groups"
claim_name = "groups"
full_path = false
full_path = each.value.group_full_path
}
resource "keycloak_openid_client_default_scopes" "oauth2_app" {
+9
View File
@@ -4,6 +4,10 @@ terraform {
source = "keycloak/keycloak"
version = ">= 5.0.0"
}
bitwarden = {
source = "maxlaverse/bitwarden"
version = "= 0.23.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = ">= 2.0.0"
@@ -22,3 +26,8 @@ provider "kubernetes" {
# CI supplies the current cluster context through its KUBECONFIG secret.
config_path = "~/.kube/config"
}
provider "bitwarden" {
server = "https://vw.hexor.cy"
client_implementation = "embedded"
}
+7
View File
@@ -34,6 +34,13 @@ proxy_applications = {
}
oauth2_applications = {
openartifact = {
redirect_uris = ["https://repo.hexor.cy/api/v1/auth/sso/oidc/keycloak/callback"]
web_origins = ["https://repo.hexor.cy"]
post_logout_redirect_uris = ["https://repo.hexor.cy/login"]
group_full_path = true
publish_to_vaultwarden = true
}
gitea = {
redirect_uris = ["https://gt.hexor.cy/user/oauth2/Keycloak/callback"]
web_origins = ["https://gt.hexor.cy"]
+20
View File
@@ -51,7 +51,27 @@ variable "oauth2_applications" {
extra_default_scopes = optional(list(string), [])
extra_optional_scopes = optional(list(string), [])
allowed_groups = optional(list(string), [])
group_full_path = optional(bool, false)
publish_to_vaultwarden = optional(bool, false)
access_token_lifespan = optional(string) # seconds; null keeps the realm default
}))
default = {}
}
variable "vaultwarden_organization_id" {
description = "Vaultwarden organization receiving published Keycloak client credentials"
type = string
validation {
condition = can(regex("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", var.vaultwarden_organization_id))
error_message = "Set the UUID of the target Vaultwarden organization."
}
}
variable "vaultwarden_collection_id" {
description = "Vaultwarden collection shared by the Terraform writer and application secret reader"
type = string
validation {
condition = can(regex("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", var.vaultwarden_collection_id))
error_message = "Set the UUID of the target Vaultwarden collection."
}
}