diff --git a/infrastructure/istio-config/uralkal/istio-config.yaml b/infrastructure/istio-config/uralkal/istio-config.yaml index 5122620..55da5ed 100644 --- a/infrastructure/istio-config/uralkal/istio-config.yaml +++ b/infrastructure/istio-config/uralkal/istio-config.yaml @@ -22,11 +22,47 @@ spec: environments: uralkal: istio: - # RequestAuthentication audit-jwt ВЫКЛЮЧЕНА: консоль Zitadel ходит с opaque-токенами, - # а jwt_authn валидирует только JWT — opaque ломал консоль (401 "Jwt is not in the form"). - # Актор (x-sarex-audit-sub/org в журнале, user.id в трейсах) пуст, пока не вернём: - # либо перевести клиентов на JWT-токены (Access Token Type: JWT), либо ext_authz - # с introspection opaque-токенов в Zitadel (отдельная задача). Прежний блок — в git history. + requestAuthentications: + api-jwt: + namespace: istio-system + selector: + matchLabels: + istio: ingressgateway + jwtRules: + - issuer: default_issuer + jwks: | + { + "keys": [ + { + "kty": "RSA", + "kid": "s57i--vgmVINy1H4Ud7ZxMhudCPY7GqT6dqCIWyG7io", + "use": "sig", + "alg": "RS512", + "n": "tJdh3c-S2vXMGqPI9X0nYo-J006LxYOExkmRN_7UrALoUGyCN0yOZi6jpcUAWbG-0i-tNfjlzk8rA0z3nYuWaq0dXBIUvl3awJwMXPcjr0IKzAJNfNisg6clRoeoxUBEfu5nNjOkzKnvSZft_X-Jchtc_qIm4itfbBYbQBHeb5ROnjV6tjUgumagsTeA8ApMlYS0fP0W34xkDdtxd4SipRGvrRaDR0aqPzC9CmXXZ0-hQXNsGb-9wlQQmDVXvUlL2fQ7FcUK49Oz2_cr4PUC5ucxWz9VLYRgZSVuNau1CX5jBDlOJXLECVsEKWbYpjwmRh3slA_-yQfhPVAoEJ095uDDoVZ2Wf_KE6iVq9gI5hylDpJicAeDa11e8nNztF9Jt6SqRjiETFXbGEaJzizfAmC7A0b2YaoxsymusuPLyo1q2rIlGiX5_GiModx3tcDa2wA7Z_1UsJgRQ6g3CHo3gnOc6u8tt3etduTMxbVoBvM4bHtLHxsSGoY7sM-e-Y7h0THSEAtAo44h0OFef_wIvpsr5spGDdWwb3Ff8rbegkiFt1wQ8J2-oTXvcvrgaUrxedw5Eb3acFT7biYGDFHJOPcQVvdBfANIMZu2pcGi5aHvBxZrMEFIIOTlFgBZI4D5_ZpIa_L_eeD2-UgBLrIBTHvJa8hj1-btQUmr4Ub35I0", + "e": "AQAB" + } + ] + } + forwardOriginalToken: true + outputClaimToHeaders: + - claim: user_id + header: x-user-id + - claim: company_ids + header: x-company-ids + zitadel-jwt: + namespace: istio-system + selector: + matchLabels: + istio: ingressgateway + jwtRules: + - issuer: https://login.sarex.local.uralkali.com + jwksUri: http://login.sarex.local.uralkali.com:8080/oauth/v2/keys + forwardOriginalToken: true + outputClaimToHeaders: + - claim: urn:zitadel:iam:user:metadata.user_id + header: x-user-id + - claim: urn:zitadel:iam:user:metadata.company_ids + header: x-company-ids # traceparent из b3-заголовков sidecar'а для приложений (W3C), см. чарт istio-config-contour. # traceparent из b3-заголовков sidecar'а для приложений (W3C), см. чарт istio-config-contour. add-traceparent-filter: @@ -180,6 +216,332 @@ spec: decode_header_if_needed(handle, "x-user-id") decode_header_if_needed(handle, "x-company-ids") end + strip-jwt-on-public-hosts: + 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.jwt_authn + patch: + operation: INSERT_BEFORE + value: + name: envoy.filters.http.lua + typed_config: + '@type': type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua + inlineCode: | + local public_hosts = { + ["login.sarex.local.uralkali.com"] = true, + ["bi.sarex.local.uralkali.com"] = true, + ["document-link.sarex.local.uralkali.com"] = true, + ["camunda-keycloak.sarex.local.uralkali.com"] = true, + ["camunda-identity.sarex.local.uralkali.com"] = true, + ["camunda-operate.sarex.local.uralkali.com"] = true, + ["camunda-optimize.sarex.local.uralkali.com"] = true, + ["camunda-tasklist.sarex.local.uralkali.com"] = true, + } + + local public_paths = { + "^/documentations/api/v1/public/qr", + "^/documentations/api/v1/public/documents/public_link", + } + + local function is_public(host, path) + if not host then return false end + host = string.gsub(host, ":%d+$", "") + if public_hosts[host] then + return true + end + if path then + for _,pref in ipairs(public_paths) do + if string.find(path, pref) == 1 then + return true + end + end + end + return false + end + + function envoy_on_request(handle) + local h = handle:headers() + local host = h:get(":authority") + local path = h:get(":path") + if is_public(host, path) then + local auth = h:get("authorization") + if auth then + h:replace("x-orig-authorization", auth) + h:remove("authorization") + end + end + end + - applyTo: HTTP_FILTER + match: + context: GATEWAY + listener: + filterChain: + filter: + name: envoy.filters.network.http_connection_manager + subFilter: + name: envoy.filters.http.jwt_authn + patch: + operation: INSERT_AFTER + value: + name: envoy.filters.http.lua + typed_config: + '@type': type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua + inlineCode: | + function envoy_on_request(handle) + local h = handle:headers() + local xauth = h:get("x-orig-authorization") + if xauth and not h:get("authorization") then + h:replace("authorization", xauth) + h:remove("x-orig-authorization") + end + end + strip-jwt-on-public-paths: + 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.jwt_authn + patch: + operation: INSERT_BEFORE + value: + name: envoy.filters.http.lua + typed_config: + '@type': type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua + inlineCode: | + local public_prefixes = { + "^/documentations/api/v1/public/qr", + "^/documentations/api/v1/public/documents/public_link" + } + + local download_prefixes = { + "^/files/api/v1/documents/folders", + "^/files/api/v1/documents", + "^/files/api/v1/bundles" + } + + local function starts_with_any(path, prefixes) + for _, pref in ipairs(prefixes) do + if string.find(path, pref) == 1 then + return true + end + end + return false + end + + local function has_download_type(path) + if not path or path == "" then + return false + end + return string.match(path, "[%?&]download_type=") ~= nil + end + + local function should_strip(path) + if not path then + return false + end + + if starts_with_any(path, public_prefixes) then + return true + end + + if starts_with_any(path, download_prefixes) and has_download_type(path) then + return true + end + + return false + end + + function envoy_on_request(handle) + local h = handle:headers() + local p = h:get(":path") or "" + + if should_strip(p) then + local auth = h:get("authorization") + if auth then + h:replace("x-orig-authorization-public-paths", auth) + h:remove("authorization") + end + + local identity = h:get("identity") + if identity then + h:replace("x-orig-identity-public-paths", identity) + h:remove("identity") + end + + h:replace("x-jwt-stripped-public-paths", "1") + end + end + - applyTo: HTTP_FILTER + match: + context: GATEWAY + listener: + filterChain: + filter: + name: envoy.filters.network.http_connection_manager + subFilter: + name: envoy.filters.http.jwt_authn + patch: + operation: INSERT_AFTER + value: + name: envoy.filters.http.lua + typed_config: + '@type': type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua + inlineCode: | + function envoy_on_request(handle) + local h = handle:headers() + local stripped = h:get("x-jwt-stripped-public-paths") + + if stripped == "1" then + local xauth = h:get("x-orig-authorization-public-paths") + if xauth and not h:get("authorization") then + h:replace("authorization", xauth) + end + h:remove("x-orig-authorization-public-paths") + + local xidentity = h:get("x-orig-identity-public-paths") + if xidentity and not h:get("identity") then + h:replace("identity", xidentity) + end + h:remove("x-orig-identity-public-paths") + + h:remove("x-jwt-stripped-public-paths") + end + end + cors-json-401: + namespace: istio-system + workloadSelector: + labels: + istio: ingressgateway + configPatches: + - applyTo: HTTP_FILTER + match: + context: GATEWAY + listener: + filterChain: + filter: + name: envoy.filters.http.jwt_authn + patch: + operation: INSERT_BEFORE + value: + name: envoy.filters.http.cors + typed_config: + '@type': type.googleapis.com/envoy.extensions.filters.http.cors.v3.Cors + cors: + allow_credentials: true + allow_headers: Authorization,Content-Type + allow_methods: GET,POST,PUT,PATCH,DELETE,OPTIONS + allow_origin_string_match: + - safe_regex: + regex: https://.*\.sarex\.local\.uralkali\.com + - safe_regex: + regex: https://localhost:.* + max_age: '86400' + - applyTo: NETWORK_FILTER + match: + context: GATEWAY + listener: + filterChain: + filter: + name: envoy.filters.network.http_connection_manager + patch: + operation: MERGE + value: + typed_config: + '@type': type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager + local_reply_config: + mappers: + - body: + inline_string: | + {"error":"unauthorized","detail":"Invalid or expired token"} + filter: + status_code_filter: + comparison: + op: EQ + value: + default_value: 401 + runtime_key: default + headers_to_add: + - append_action: OVERWRITE_IF_EXISTS_OR_ADD + header: + key: Content-Type + value: application/json + - append_action: OVERWRITE_IF_EXISTS_OR_ADD + header: + key: Access-Control-Allow-Origin + value: '%REQ(Origin)%' + - append_action: OVERWRITE_IF_EXISTS_OR_ADD + header: + key: Access-Control-Allow-Credentials + value: 'true' + set-auth-type-header: + 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: | + local function detect_auth_type(authz) + if not authz or authz == "" then + return "no" + end + if authz:match("^Basic%s+") then + return "basic" + end + if authz:match("^Bearer%s+") then + return "jwt" + end + return "no" + end + + function envoy_on_request(handle) + local auth_type = detect_auth_type(handle:headers():get("authorization")) + handle:headers():replace("x-auth-type", auth_type) + handle:streamInfo():dynamicMetadata():set("sarex.auth_type", "value", auth_type) + end + + function envoy_on_response(handle) + local auth_type = "no" + local md = handle:streamInfo():dynamicMetadata():get("sarex.auth_type") + if md and md["value"] and md["value"] ~= "" then + auth_type = md["value"] + end + handle:headers():replace("x-auth-type", auth_type) + end gateway-hsts: namespace: istio-system # там же, где поды ingressgateway workloadSelector: