From 4ca206722b0c3490a48355bac7c26b9a0c85a420 Mon Sep 17 00:00:00 2001 From: emelinda Date: Sun, 8 Nov 2026 22:39:02 +0300 Subject: [PATCH] istio-config/uralkal: map zitadel metadata user_id/company_ids to headers (as prod), add decode-zitadel-headers Lua filter, switch audit-jwt to outputClaims (chart 0.1.1 drops outputClaimToHeaders silently) --- .../istio-config/uralkal/istio-config.yaml | 87 +++++++++++++++++-- 1 file changed, 82 insertions(+), 5 deletions(-) diff --git a/infrastructure/istio-config/uralkal/istio-config.yaml b/infrastructure/istio-config/uralkal/istio-config.yaml index 29f3ca1..ecbde5f 100644 --- a/infrastructure/istio-config/uralkal/istio-config.yaml +++ b/infrastructure/istio-config/uralkal/istio-config.yaml @@ -45,11 +45,15 @@ spec: fromHeaders: - {name: Authorization, prefix: "Bearer "} - {name: Identity, prefix: "Bearer "} - outputClaimToHeaders: - - {header: x-sarex-audit-sub, claim: sub} - # TODO(verify): есть ли claim в access token (README security-audit) - - {header: x-sarex-audit-org, claim: "urn:zitadel:iam:user:resourceowner:id"} - - {header: x-user-id, claim: sub} + # Чарт 0.1.1 (ветка contour) понимает только outputClaims (map claim→header). + # Как на prod: числовой user_id/company_ids из metadata; значения приходят + # base64url — декодирует EnvoyFilter decode-zitadel-headers (ниже). + # TODO(verify): x-sarex-audit-org — есть ли claim в access token (README security-audit) + outputClaims: + sub: x-sarex-audit-sub + "urn:zitadel:iam:user:resourceowner:id": x-sarex-audit-org + "urn:zitadel:iam:user:metadata.user_id": x-user-id + "urn:zitadel:iam:user:metadata.company_ids": x-company-ids # TODO(verify): issuer и jwksUri camunda keycloak (черновик README security-audit) - issuer: https://camunda-keycloak.sarex.local.uralkali.com/auth/realms/camunda-platform jwksUri: http://camunda-keycloak.camunda.svc.cluster.local/auth/realms/camunda-platform/protocol/openid-connect/certs @@ -128,6 +132,79 @@ spec: tls: credentialName: uralkal-camunda-tls envoyFilters: + # Декодирование base64url-значений metadata-claims Zitadel (x-user-id, x-company-ids), + # дословно с prod (istio-config values). INSERT_BEFORE router — после jwt_authn, + # который проставляет заголовки из claims. Числовые значения не трогает. + decode-zitadel-headers: + namespace: istio-system + workloadSelector: + labels: + istio: ingressgateway + configPatches: + - applyTo: HTTP_FILTER + match: + context: GATEWAY + listener: + filterChain: + filter: + name: envoy.filters.network.http_connection_manager + subFilter: + name: envoy.filters.http.router + patch: + operation: INSERT_BEFORE + value: + name: envoy.filters.http.lua + typed_config: + '@type': type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua + inlineCode: | + function base64_decode(input) + local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' + input = input:gsub('[^'..b..'=]', '') + local result = {} + local bits = 0 + local acc = 0 + for i = 1, #input do + local c = input:sub(i, i) + if c == '=' then break end + local idx = b:find(c, 1, true) + if idx then + acc = acc * 64 + (idx - 1) + bits = bits + 6 + while bits >= 8 do + bits = bits - 8 + local byte = math.floor(acc / (2 ^ bits)) % 256 + result[#result + 1] = string.char(byte) + end + end + end + return table.concat(result) + end + + function is_numeric(value) + return value ~= nil and value:match("^%d+$") ~= nil + end + + function decode_header_if_needed(handle, header_name) + local value = handle:headers():get(header_name) + if not value or value == "" then + return + end + + -- Если уже число (например "1234"), не декодируем + if is_numeric(value) then + return + end + + local decoded = base64_decode(value) + if decoded and decoded ~= "" then + handle:headers():replace(header_name, decoded) + end + end + + function envoy_on_request(handle) + decode_header_if_needed(handle, "x-user-id") + decode_header_if_needed(handle, "x-company-ids") + end gateway-hsts: namespace: istio-system # там же, где поды ingressgateway workloadSelector: