Copilot commented on code in PR #896: URL: https://github.com/apache/ranger/pull/896#discussion_r3005631543
########## pdp/src/main/java/org/apache/ranger/pdp/RangerPdpStatusServlet.java: ########## @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.ranger.pdp; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.lang3.StringUtils; +import org.apache.ranger.pdp.config.RangerPdpConfig; +import org.apache.ranger.pdp.config.RangerPdpConstants; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import java.io.File; +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.Map; + +public class RangerPdpStatusServlet extends HttpServlet { + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private final RangerPdpStats runtimeState; + private final RangerPdpConfig config; + private final Mode mode; + + public enum Mode { LIVE, READY, METRICS } + + public RangerPdpStatusServlet(RangerPdpStats runtimeState, RangerPdpConfig config, Mode mode) { + this.runtimeState = runtimeState; + this.config = config; + this.mode = mode; + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + switch (mode) { + case LIVE: + writeLive(resp); + break; + case READY: + writeReady(resp); + break; + case METRICS: + writeMetrics(resp); + break; + default: + resp.setStatus(HttpServletResponse.SC_NOT_FOUND); + } + } + + private void writeLive(HttpServletResponse resp) throws IOException { + Map<String, Object> payload = new LinkedHashMap<>(); + + payload.put("status", runtimeState.isServerStarted() ? "UP" : "DOWN"); + payload.put("service", "ranger-pdp"); + payload.put("live", runtimeState.isServerStarted()); + + resp.setStatus(runtimeState.isServerStarted() ? HttpServletResponse.SC_OK : HttpServletResponse.SC_SERVICE_UNAVAILABLE); + resp.setContentType("application/json"); + + MAPPER.writeValue(resp.getOutputStream(), payload); + } + + private void writeReady(HttpServletResponse resp) throws IOException { + boolean ready = runtimeState.isServerStarted() && runtimeState.isAuthorizerInitialized() && runtimeState.isAcceptingRequests(); + + Map<String, Object> payload = new LinkedHashMap<>(); + + payload.put("status", ready ? "READY" : "NOT_READY"); + payload.put("service", "ranger-pdp"); + payload.put("ready", ready); + payload.put("authorizerInitialized", runtimeState.isAuthorizerInitialized()); + payload.put("acceptingRequests", runtimeState.isAcceptingRequests()); + payload.put("policyCacheAgeMs", getPolicyCacheAgeMs()); + + resp.setStatus(ready ? HttpServletResponse.SC_OK : HttpServletResponse.SC_SERVICE_UNAVAILABLE); + resp.setContentType("application/json"); + + MAPPER.writeValue(resp.getOutputStream(), payload); + } + + private void writeMetrics(HttpServletResponse resp) throws IOException { + StringBuilder sb = new StringBuilder(512); + + sb.append("# TYPE ranger_pdp_requests_total counter\n"); + sb.append("ranger_pdp_requests_total ").append(runtimeState.getTotalRequests()).append('\n'); Review Comment: `ranger_pdp_requests_total` is sourced from `runtimeState.getTotalRequests()`, but `totalRequests` is only incremented for 2xx/400/5xx authz outcomes and not for 401/403 auth failures. This makes the metric name misleading (it’s not actually total HTTP requests). Consider incrementing `totalRequests` in `recordAuthFailure()` (or in `recordRequestMetrics()` for 401/403) or renaming the metric to reflect that it excludes auth failures. ```suggestion sb.append("# TYPE ranger_pdp_authz_requests_total counter\n"); sb.append("ranger_pdp_authz_requests_total ").append(runtimeState.getTotalRequests()).append('\n'); ``` ########## pdp/src/main/resources/ranger-pdp-default.xml: ########## @@ -0,0 +1,364 @@ +<?xml version="1.0" encoding="UTF-8"?> +<?xml-stylesheet type="text/xsl" href="configuration.xsl"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<configuration> + <property> + <name>ranger.pdp.port</name> + <value>6500</value> + <description>Port the PDP server listens on.</description> + </property> + + <property> + <name>ranger.pdp.log.dir</name> + <value>/var/log/ranger/pdp</value> + <description>Directory for PDP server log files.</description> + </property> + + <property> + <name>ranger.pdp.service.*.delegation.users</name> + <value/> + <description>Comma-separated users, allowed to call on behalf of other users in all services</description> + </property> + + <!-- SSL/TLS --> + <property> + <name>ranger.pdp.ssl.enabled</name> + <value>false</value> + <description>Set to true to enable HTTPS.</description> + </property> + + <property> + <name>ranger.pdp.ssl.keystore.file</name> + <value/> + <description>Path to the keystore file (required when SSL is enabled).</description> + </property> + + <property> + <name>ranger.pdp.ssl.keystore.password</name> + <value/> + <description>Keystore password.</description> + </property> + + <property> + <name>ranger.pdp.ssl.keystore.type</name> + <value>JKS</value> + <description>Keystore type (JKS, PKCS12).</description> + </property> + + <property> + <name>ranger.pdp.ssl.truststore.enabled</name> + <value>false</value> + <description>Set to true to require client certificate authentication.</description> + </property> + + <property> + <name>ranger.pdp.ssl.truststore.file</name> + <value/> + <description>Path to the truststore file.</description> + </property> + + <property> + <name>ranger.pdp.ssl.truststore.password</name> + <value/> + <description>Truststore password.</description> + </property> + + <property> + <name>ranger.pdp.ssl.truststore.type</name> + <value>JKS</value> + <description>Truststore type (JKS, PKCS12).</description> + </property> + + <!-- HTTP/2 --> + <property> + <name>ranger.pdp.http2.enabled</name> + <value>true</value> + <description> + Enable HTTP/2 via upgrade on the connector. + Supports both h2 (over TLS) and h2c (cleartext upgrade) alongside HTTP/1.1. + </description> + </property> + + <!-- Connector concurrency / queue limits --> + <property> + <name>ranger.pdp.http.connector.maxThreads</name> + <value>200</value> + <description>Maximum number of worker threads handling simultaneous requests.</description> + </property> + + <property> + <name>ranger.pdp.http.connector.minSpareThreads</name> + <value>20</value> + <description>Minimum number of spare worker threads kept ready.</description> + </property> + + <property> + <name>ranger.pdp.http.connector.acceptCount</name> + <value>100</value> + <description>Queued connection backlog when all worker threads are busy.</description> + </property> + + <property> + <name>ranger.pdp.http.connector.maxConnections</name> + <value>10000</value> + <description>Maximum concurrent TCP connections accepted by the connector.</description> + </property> + + <!-- Authentication for incoming REST requests --> + <property> + <name>ranger.pdp.authn.types</name> + <value>header,jwt,kerberos</value> + <description> + Comma-separated list of authentication methods for incoming REST requests, + tried in listed order. Supported values: header, jwt, kerberos. + </description> + </property> + + <!-- HTTP Header authentication for incoming REST requests --> + <property> + <name>ranger.pdp.authn.header.enabled</name> + <value>false</value> + <description> Review Comment: The default config enables `ranger.pdp.authn.types=header,jwt,kerberos` but has all corresponding `*.enabled` flags set to `false`. With this combination, `RangerPdpAuthNFilter` registers no handlers and throws `ServletException("No valid authentication handlers configured")`, preventing the server from starting with the default config. Consider either enabling at least one authn method by default, or making `ranger.pdp.authn.types` empty by default (with a clearer error), so the out-of-the-box behavior is consistent. ########## ranger-examples/sample-client/src/main/python/sample_pdp_client.py: ########## @@ -0,0 +1,101 @@ +#!/usr/bin/env python + +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from apache_ranger.client.ranger_pdp_client import RangerPDPClient +from apache_ranger.model.ranger_authz import RangerAccessContext, RangerAccessInfo +from apache_ranger.model.ranger_authz import RangerAuthzRequest, RangerMultiAuthzRequest +from apache_ranger.model.ranger_authz import RangerResourceInfo, RangerResourcePermissionsRequest, RangerUserInfo + + +## +## Step 1: create a client to connect to Ranger PDP +## +pdp_url = "http://localhost:6500" + +# For Kerberos authentication +# +# from requests_kerberos import HTTPKerberosAuth +# +# pdp = RangerPDPClient(pdp_url, HTTPKerberosAuth()) + +# For trusted-header authN with PDP (example only): +# +pdp = RangerPDPClient(pdp_url, auth=None, headers={"X-Forwarded-User": "hive"}) + Review Comment: This sample client is configured to use trusted-header auth (`X-Forwarded-User`) by default, but the provided Docker PDP config disables header auth and enables Kerberos (`ranger.pdp.authn.header.enabled=false`, `ranger.pdp.authn.kerberos.enabled=true`). Running this sample against the Docker setup will therefore get `401` unless the user switches to the Kerberos snippet (or enables header auth server-side). Consider making the Kerberos path the default for the Docker-oriented sample, or adding an explicit note right here about the server-side setting required for header auth. ########## pdp/conf.dist/ranger-pdp-site.xml: ########## @@ -0,0 +1,364 @@ +<?xml version="1.0" encoding="UTF-8"?> +<?xml-stylesheet type="text/xsl" href="configuration.xsl"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<configuration> + <property> + <name>ranger.pdp.port</name> + <value>6500</value> + <description>Port the PDP server listens on.</description> + </property> + + <property> + <name>ranger.pdp.log.dir</name> + <value>/var/log/ranger/pdp</value> + <description>Directory for PDP server log files.</description> + </property> + + <property> + <name>ranger.pdp.service.*.delegation.users</name> + <value/> + <description>Comma-separated users, allowed to call on behalf of other users in all services</description> + </property> + + <!-- SSL/TLS --> + <property> + <name>ranger.pdp.ssl.enabled</name> + <value>false</value> + <description>Set to true to enable HTTPS.</description> + </property> + + <property> + <name>ranger.pdp.ssl.keystore.file</name> + <value/> + <description>Path to the keystore file (required when SSL is enabled).</description> + </property> + + <property> + <name>ranger.pdp.ssl.keystore.password</name> + <value/> + <description>Keystore password.</description> + </property> + + <property> + <name>ranger.pdp.ssl.keystore.type</name> + <value>JKS</value> + <description>Keystore type (JKS, PKCS12).</description> + </property> + + <property> + <name>ranger.pdp.ssl.truststore.enabled</name> + <value>false</value> + <description>Set to true to require client certificate authentication.</description> + </property> + + <property> + <name>ranger.pdp.ssl.truststore.file</name> + <value/> + <description>Path to the truststore file.</description> + </property> + + <property> + <name>ranger.pdp.ssl.truststore.password</name> + <value/> + <description>Truststore password.</description> + </property> + + <property> + <name>ranger.pdp.ssl.truststore.type</name> + <value>JKS</value> + <description>Truststore type (JKS, PKCS12).</description> + </property> + + <!-- HTTP/2 --> + <property> + <name>ranger.pdp.http2.enabled</name> + <value>true</value> + <description> + Enable HTTP/2 via upgrade on the connector. + Supports both h2 (over TLS) and h2c (cleartext upgrade) alongside HTTP/1.1. + </description> + </property> + + <!-- Connector concurrency / queue limits --> + <property> + <name>ranger.pdp.http.connector.maxThreads</name> + <value>200</value> + <description>Maximum number of worker threads handling simultaneous requests.</description> + </property> + + <property> + <name>ranger.pdp.http.connector.minSpareThreads</name> + <value>20</value> + <description>Minimum number of spare worker threads kept ready.</description> + </property> + + <property> + <name>ranger.pdp.http.connector.acceptCount</name> + <value>100</value> + <description>Queued connection backlog when all worker threads are busy.</description> + </property> + + <property> + <name>ranger.pdp.http.connector.maxConnections</name> + <value>10000</value> + <description>Maximum concurrent TCP connections accepted by the connector.</description> + </property> + + <!-- Authentication for incoming REST requests --> + <property> + <name>ranger.pdp.authn.types</name> + <value>header,jwt,kerberos</value> + <description> + Comma-separated list of authentication methods for incoming REST requests, + tried in listed order. Supported values: header, jwt, kerberos. + </description> + </property> + + <!-- HTTP Header authentication for incoming REST requests --> + <property> + <name>ranger.pdp.authn.header.enabled</name> + <value>false</value> Review Comment: This shipped `conf.dist/ranger-pdp-site.xml` also sets `ranger.pdp.authn.types=header,jwt,kerberos` while all `ranger.pdp.authn.<type>.enabled` flags are `false`. As a result, `RangerPdpAuthNFilter` will register no handlers and fail startup with `No valid authentication handlers configured` unless the operator edits the file. Consider adjusting the template so it’s runnable after install (e.g., enable one authn method or leave `ranger.pdp.authn.types` empty with explicit guidance). ```suggestion <value>true</value> ``` ########## pdp/src/test/java/org/apache/ranger/pdp/security/RangerPdpAuthNFilterTest.java: ########## @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.ranger.pdp.security; + +import org.apache.ranger.pdp.config.RangerPdpConstants; +import org.junit.jupiter.api.Test; + +import javax.servlet.FilterConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; + +import java.lang.reflect.Field; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class RangerPdpAuthNFilterTest { + @Test + public void testInit_skipsHeaderHandlerWhenDisabled() { + RangerPdpAuthNFilter filter = new RangerPdpAuthNFilter(); + Map<String, String> params = new HashMap<>(); + + params.put(RangerPdpConstants.PROP_AUTHN_TYPES, "header"); + params.put(RangerPdpConstants.PROP_AUTHN_HEADER_ENABLED, "false"); + + assertThrows(ServletException.class, () -> filter.init(new TestFilterConfig(params))); + } Review Comment: Test name is misleading: `testInit_skipsHeaderHandlerWhenDisabled` expects `filter.init()` to throw `ServletException` (i.e., init fails), not that it “skips” the handler and continues. Renaming the test to reflect the expected failure would make intent clearer. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
