You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ranger.apache.org by "mneethiraj (via GitHub)" <gi...@apache.org> on 2026/03/28 02:27:43 UTC
[PR] RANGER-5371: pdp server implementation [ranger]
mneethiraj opened a new pull request, #896:
URL: https://github.com/apache/ranger/pull/896
## What changes were proposed in this pull request?
- introduce Ranger PDP (policy decision point) server with REST APIs to evaluate authorization requests
- updated Ranger Python client to support Ranger PDP REST APIs
- updated docker setup to support ranger-pdp server
## How was this patch tested?
- verified that ranger-pdp server starts successfully in docker setup
- verified that Ranger Python client successfully calls PDP REST APIs, and PDP server audits authorizations
--
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: dev-unsubscribe@ranger.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] RANGER-5371: pdp server implementation [ranger]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #896:
URL: https://github.com/apache/ranger/pull/896#discussion_r3005830482
##########
pdp/src/main/java/org/apache/ranger/pdp/security/KerberosAuthNHandler.java:
##########
@@ -0,0 +1,275 @@
+/*
+ * 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.commons.codec.binary.Base64;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.security.authentication.util.KerberosName;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.ietf.jgss.GSSContext;
+import org.ietf.jgss.GSSCredential;
+import org.ietf.jgss.GSSException;
+import org.ietf.jgss.GSSManager;
+import org.ietf.jgss.GSSName;
+import org.ietf.jgss.Oid;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.auth.Subject;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.auth.login.AppConfigurationEntry;
+import javax.security.auth.login.Configuration;
+import javax.security.auth.login.LoginContext;
+import javax.security.auth.login.LoginException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.security.PrivilegedExceptionAction;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Authenticates requests using Kerberos SPNEGO (HTTP Negotiate).
+ *
+ * <p>Uses the JDK's built-in GSSAPI/JGSS support – no external Kerberos library is required.
+ * The service principal and keytab must be configured via:
+ * <ul>
+ * <li>{@code ranger.pdp.authn.kerberos.spnego.principal} – e.g. {@code HTTP/host.example.com@REALM}
+ * <li>{@code ranger.pdp.authn.kerberos.spnego.keytab} – absolute path to the keytab file
+ * <li>{@code ranger.pdp.authn.kerberos.name.rules} – Hadoop-style name rules (default: {@code DEFAULT})
+ * </ul>
+ *
+ * <p>Authentication flow:
+ * <ol>
+ * <li>If no {@code Authorization: Negotiate} header is present the handler returns {@code SKIP}.
+ * <li>The SPNEGO token is extracted, validated via GSSAPI, and – if a response token is
+ * produced (mutual authentication) – written to {@code WWW-Authenticate: Negotiate <token>}.
+ * <li>On success the short-form principal name (strip {@literal @REALM} and host components)
+ * is returned as the authenticated user.
+ * <li>On failure a {@code 401 Negotiate} challenge is sent and {@code CHALLENGE} is returned.
+ * </ol>
+ */
+public class KerberosAuthNHandler implements PdpAuthNHandler {
+ private static final Logger LOG = LoggerFactory.getLogger(KerberosAuthNHandler.class);
+
+ public static final String AUTH_TYPE = "KERBEROS";
+
+ private static final String NEGOTIATE_PREFIX = "Negotiate ";
+ private static final String WWW_AUTHENTICATE = "WWW-Authenticate";
+ private static final String AUTHORIZATION = "Authorization";
+ private static final Oid SPNEGO_OID;
+ private static final Oid KRB5_OID;
+
+ static {
+ try {
+ SPNEGO_OID = new Oid("1.3.6.1.5.5.2");
+ KRB5_OID = new Oid("1.2.840.113554.1.2.2");
+ } catch (GSSException e) {
+ throw new ExceptionInInitializerError(e);
+ }
+ }
+
+ private Subject serviceSubject;
+ private GSSManager gssManager;
+ private GSSCredential serverCred;
+
+ @Override
+ public void init(Properties config) throws Exception {
+ String principal = config.getProperty(RangerPdpConstants.PROP_AUTHN_KERBEROS_SPNEGO_PRINCIPAL);
+ String keytab = config.getProperty(RangerPdpConstants.PROP_AUTHN_KERBEROS_SPNEGO_KEYTAB);
+ String nameRules = config.getProperty(RangerPdpConstants.PROP_AUTHN_KERBEROS_NAME_RULES, "DEFAULT");
+ String tokenValidity = config.getProperty(RangerPdpConstants.PROP_AUTHN_KERBEROS_KRB_TOKEN_VALIDITY);
+
+ int tokenLifetime = StringUtils.isBlank(tokenValidity) ? GSSCredential.INDEFINITE_LIFETIME : Integer.parseInt(tokenValidity);
+
+ if (StringUtils.isBlank(principal) || StringUtils.isBlank(keytab)) {
+ throw new IllegalArgumentException("Kerberos auth requires configurations " + RangerPdpConstants.PROP_AUTHN_KERBEROS_SPNEGO_PRINCIPAL + " and " + RangerPdpConstants.PROP_AUTHN_KERBEROS_SPNEGO_KEYTAB);
+ }
+
+ serviceSubject = loginWithKeytab(principal, keytab);
+
+ initializeKerberosNameRules(nameRules);
+
+ gssManager = GSSManager.getInstance();
+
+ GSSName serverName = gssManager.createName(principal, GSSName.NT_USER_NAME);
+
+ // Create acceptor credentials in the logged-in Subject to avoid fallback to JVM default keytab.
+ serverCred = Subject.doAs(serviceSubject, (PrivilegedExceptionAction<GSSCredential>) () ->
+ gssManager.createCredential(serverName, tokenLifetime, new Oid[] {SPNEGO_OID, KRB5_OID}, GSSCredential.ACCEPT_ONLY));
+
+ LOG.info("KerberosAuthHandler initialized; principal={} (bound acceptor credential to configured principal)", principal);
+ }
Review Comment:
Log message refers to "KerberosAuthHandler" but the class is `KerberosAuthNHandler` (and similarly in other handlers). Using the exact handler name in logs will make debugging/grep’ing configuration issues easier.
##########
pdp/conf.dist/logback.xml:
##########
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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>
+ <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
+ <Target>System.out</Target>
Review Comment:
In Logback `ConsoleAppender`, the element is `<target>`, not `<Target>`. With the current casing the setting may be ignored (falling back to the default) and can be confusing for operators. Please change it to the correct lowercase element name for consistency with other logback configs in the repo.
```suggestion
<target>System.out</target>
```
##########
pdp/src/main/java/org/apache/ranger/pdp/RangerPdpServer.java:
##########
@@ -0,0 +1,293 @@
+/*
+ * 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 org.apache.catalina.Context;
+import org.apache.catalina.LifecycleException;
+import org.apache.catalina.connector.Connector;
+import org.apache.catalina.startup.Tomcat;
+import org.apache.catalina.valves.AccessLogValve;
+import org.apache.coyote.http2.Http2Protocol;
+import org.apache.ranger.authz.api.RangerAuthorizer;
+import org.apache.ranger.authz.api.RangerAuthzException;
+import org.apache.ranger.authz.embedded.RangerEmbeddedAuthorizer;
+import org.apache.ranger.pdp.config.RangerPdpConfig;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.apache.ranger.pdp.rest.RangerPdpApplication;
+import org.apache.ranger.pdp.security.RangerPdpAuthNFilter;
+import org.apache.ranger.pdp.security.RangerPdpRequestContextFilter;
+import org.apache.tomcat.util.descriptor.web.FilterDef;
+import org.apache.tomcat.util.descriptor.web.FilterMap;
+import org.glassfish.jersey.servlet.ServletContainer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.Servlet;
+import javax.servlet.http.HttpServlet;
+
+import java.io.File;
+
+/**
+ * Main entry point for the Ranger Policy Decision Point (PDP) server.
+ *
+ * <p>Starts an embedded Apache Tomcat instance that:
+ * <ul>
+ * <li>Creates and initialises a {@link RangerEmbeddedAuthorizer} singleton
+ * <li>Exposes the three authorizer methods as REST endpoints under {@code /authz/v1/}
+ * <li>Enforces authentication via {@link RangerPdpAuthNFilter} (Kerberos/JWT/HTTP-Header)
+ * <li>Optionally enables HTTP/2 ({@code Http2Protocol} upgrade on the connector)
+ * </ul>
+ *
+ * <p><b>Startup:</b> {@code java -jar ranger-pdp.jar}
+ * <br>Override config with: {@code -Dranger.pdp.conf.dir=/etc/ranger/pdp}
+ */
+public class RangerPdpServer {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpServer.class);
+
+ private final RangerPdpConfig config;
+ private final RangerPdpStats runtimeStats = new RangerPdpStats();
+ private Tomcat tomcat;
+ private RangerAuthorizer authorizer;
+
+ public RangerPdpServer() {
+ this.config = new RangerPdpConfig();
+ }
+
+ public static void main(String[] args) throws Exception {
+ new RangerPdpServer().start();
+ }
+
+ public void start() throws Exception {
+ LOG.info("Starting Ranger PDP server");
+
+ initAuthorizer();
+ startTomcat();
+ }
+
+ public void stop() {
+ LOG.info("Stopping Ranger PDP server");
+
+ runtimeStats.setAcceptingRequests(false);
+ runtimeStats.setServerStarted(false);
+
+ if (tomcat != null) {
+ try {
+ if (tomcat.getConnector() != null) {
+ tomcat.getConnector().pause();
+ }
+
+ tomcat.stop();
+ tomcat.destroy();
+ } catch (LifecycleException e) {
+ LOG.warn("Error stopping Tomcat", e);
+ }
+ }
+
+ if (authorizer != null) {
+ try {
+ authorizer.close();
+ } catch (RangerAuthzException e) {
+ LOG.warn("Error closing authorizer", e);
+ }
+ }
+ }
+
+ private void initAuthorizer() throws RangerAuthzException {
+ authorizer = new RangerEmbeddedAuthorizer(config.getAuthzProperties());
+
+ authorizer.init();
+
+ runtimeStats.setAuthorizerInitialized(true);
+
+ LOG.info("RangerEmbeddedAuthorizer initialised");
+ }
+
+ private void startTomcat() throws Exception {
+ tomcat = new Tomcat();
+
+ tomcat.setConnector(createConnector());
+
+ String docBase = new File(System.getProperty("java.io.tmpdir"), "ranger-pdp-webapps").getAbsolutePath();
+
+ new File(docBase).mkdirs();
+
+ Context ctx = tomcat.addContext("", docBase);
+
+ ctx.getServletContext().setAttribute(RangerPdpConstants.SERVLET_CTX_ATTR_AUTHORIZER, authorizer);
+ ctx.getServletContext().setAttribute(RangerPdpConstants.SERVLET_CTX_ATTR_CONFIG, config);
+ ctx.getServletContext().setAttribute(RangerPdpConstants.SERVLET_CTX_ATTR_RUNTIME_STATE, runtimeStats);
+
+ addAuthFilter(ctx);
+ addJerseyServlet(ctx);
+ addStatusEndpoints(ctx);
+ addAccessLogValve();
+
+ Runtime.getRuntime().addShutdownHook(new Thread(this::stop, "ranger-pdp-shutdown"));
+
+ tomcat.start();
+
+ runtimeStats.setServerStarted(true);
+ runtimeStats.setAcceptingRequests(true);
+
+ LOG.info("Ranger PDP server listening on port {} (SSL={}, HTTP/2={})", config.getPort(), config.isSslEnabled(), config.isHttp2Enabled());
+
+ tomcat.getServer().await();
+ }
+
+ /**
+ * Builds the Tomcat connector.
+ *
+ * <p>When SSL is enabled the connector is configured as an HTTPS endpoint.
+ * When HTTP/2 is enabled an {@link Http2Protocol} upgrade protocol is added,
+ * supporting both {@code h2} (over TLS) and {@code h2c} (cleartext upgrade) depending
+ * on whether SSL is enabled.
+ */
+ private Connector createConnector() {
+ Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
+
+ connector.setPort(config.getPort());
+ connector.setProperty("maxThreads", String.valueOf(config.getHttpConnectorMaxThreads()));
+ connector.setProperty("minSpareThreads", String.valueOf(config.getHttpConnectorMinSpareThreads()));
+ connector.setProperty("acceptCount", String.valueOf(config.getHttpConnectorAcceptCount()));
+ connector.setProperty("maxConnections", String.valueOf(config.getHttpConnectorMaxConnections()));
+
+ LOG.info("Configured HTTP connector limits: maxThreads={}, minSpareThreads={}, acceptCount={}, maxConnections={}",
+ config.getHttpConnectorMaxThreads(), config.getHttpConnectorMinSpareThreads(),
+ config.getHttpConnectorAcceptCount(), config.getHttpConnectorMaxConnections());
+
+ if (config.isSslEnabled()) {
+ connector.setSecure(true);
+ connector.setScheme("https");
+ connector.setProperty("SSLEnabled", "true");
+ connector.setProperty("protocol", "org.apache.coyote.http11.Http11NioProtocol");
+ connector.setProperty("keystoreFile", config.getKeystoreFile());
+ connector.setProperty("keystorePass", config.getKeystorePassword());
+ connector.setProperty("keystoreType", config.getKeystoreType());
+ connector.setProperty("sslProtocol", "TLS");
+
+ if (config.isTruststoreEnabled()) {
+ connector.setProperty("truststoreFile", config.getTruststoreFile());
+ connector.setProperty("truststorePass", config.getTruststorePassword());
+ connector.setProperty("truststoreType", config.getTruststoreType());
+ connector.setProperty("clientAuth", "want");
Review Comment:
When truststore is enabled, the code sets Tomcat `clientAuth` to "want" (optional mTLS), but the config/docs describe this setting as requiring client certificate authentication. This mismatch can lead to deployments believing mTLS is enforced when it isn’t. Consider using the Tomcat setting that requires client certs (or update the property name/description to reflect optional client auth).
```suggestion
connector.setProperty("clientAuth", "true");
```
##########
pdp/src/main/java/org/apache/ranger/pdp/security/RangerPdpAuthNFilter.java:
##########
@@ -0,0 +1,187 @@
+/*
+ * 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.commons.lang3.StringUtils;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_AUTHN_TYPES;
+
+/**
+ * Servlet filter that enforces authentication for all PDP REST endpoints.
+ *
+ * <p>Handlers are configured via the {@code ranger.pdp.authn.types} filter init parameter
+ * (comma-separated list of {@code header}, {@code jwt}, {@code kerberos}). Handlers are
+ * tried in the listed order; the first successful match wins.
+ *
+ * <p>On success the authenticated username is stored in the request attribute
+ * {@link RangerPdpConstants#ATTR_AUTHENTICATED_USER} so that REST resources can read it.
+ *
+ * <p>If all handlers return {@code SKIP} (no recognisable credentials found), the filter
+ * sends a {@code 401} response with {@code WWW-Authenticate} headers for every
+ * configured handler that provides a challenge.
+ */
+public class RangerPdpAuthNFilter implements Filter {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpAuthNFilter.class);
+
+ private final List<PdpAuthNHandler> handlers = new ArrayList<>();
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ Properties config = toProperties(filterConfig);
+ String authnTypes = filterConfig.getInitParameter(PROP_AUTHN_TYPES);
+
+ if (StringUtils.isNotBlank(authnTypes)) {
+ for (String authnType : authnTypes.split(",")) {
+ PdpAuthNHandler handler = createHandler(authnType.trim().toLowerCase(), filterConfig);
+
+ if (handler == null) {
+ continue;
+ }
+
+ try {
+ handler.init(config);
+
+ handlers.add(handler);
+
+ LOG.info("{}: successfully registered authentication handler", authnType);
+ } catch (Exception excp) {
+ LOG.error("{}: failed to initialize authentication handler. Handler disabled", authnType, excp);
+ }
+ }
+ }
+
+ if (handlers.isEmpty()) {
+ throw new ServletException("No valid authentication handlers configured");
+ }
+ }
+
+ @Override
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
+ HttpServletRequest httpReq = (HttpServletRequest) request;
+ HttpServletResponse httpResp = (HttpServletResponse) response;
+
+ for (PdpAuthNHandler handler : handlers) {
+ PdpAuthNHandler.Result result = handler.authenticate(httpReq, httpResp);
+
+ switch (result.getStatus()) {
+ case AUTHENTICATED:
+ httpReq.setAttribute(RangerPdpConstants.ATTR_AUTHENTICATED_USER, result.getUserName());
+ httpReq.setAttribute(RangerPdpConstants.ATTR_AUTHN_TYPE, result.getAuthType());
+
+ LOG.debug("doFilter(): authenticated user={}, type={}", result.getUserName(), result.getAuthType());
+
+ chain.doFilter(request, response);
+ return;
+
+ case CHALLENGE:
+ // handler has already written the 401; stop processing
+ return;
+
+ case SKIP:
+ default:
+ // try the next handler
+ break;
+ }
+ }
+
+ // No handler could authenticate the request; send a 401 with all challenge headers
+ LOG.debug("doFilter(): no handler authenticated request from {}", httpReq.getRemoteAddr());
+
+ sendUnauthenticated(httpResp);
+ }
+
+ @Override
+ public void destroy() {
+ handlers.clear();
+ }
+
+ private void sendUnauthenticated(HttpServletResponse response) throws IOException {
+ for (PdpAuthNHandler handler : handlers) {
+ String challenge = handler.getChallengeHeader();
+
+ if (StringUtils.isNotBlank(challenge)) {
+ response.addHeader("WWW-Authenticate", challenge);
+ }
+ }
+
+ response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
+ response.setContentType("application/json");
+ response.getWriter().write("{\"code\":\"UNAUTHENTICATED\",\"message\":\"Authentication required\"}");
+ }
Review Comment:
The unauthenticated response body here uses a custom schema/value (`{"code":"UNAUTHENTICATED",...}`) that differs from the REST resource error schema (`ErrorResponse` uses `status.name()`, e.g., `UNAUTHORIZED`). This inconsistency makes client-side error handling harder. Consider reusing the same error response shape/codes for 401s (or omit the body and rely on status + `WWW-Authenticate`).
##########
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>
+ Enable trusted HTTP header authentication. Use only behind a trusted proxy.
+ </description>
+ </property>
Review Comment:
With the default settings here, `ranger.pdp.authn.types` lists header/jwt/kerberos but all corresponding `*.enabled` flags are `false`, which will cause `RangerPdpAuthNFilter` to start with no handlers and fail server startup. Either enable at least one authn method by default, adjust the default `authn.types` to match enabled handlers, or explicitly document that users must enable at least one auth method before the PDP can start.
--
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: dev-unsubscribe@ranger.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] RANGER-5371: pdp server implementation [ranger]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #896:
URL: https://github.com/apache/ranger/pull/896#discussion_r3003994157
##########
pdp/src/test/java/org/apache/ranger/pdp/security/RangerPdpAuthFilterTest.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.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 RangerPdpAuthFilterTest {
+ @Test
+ public void testInit_skipsHeaderHandlerWhenDisabled() {
+ RangerPdpAuthFilter filter = new RangerPdpAuthFilter();
+ Map<String, String> params = new HashMap<>();
+
+ params.put(RangerPdpAuthFilter.PARAM_AUTH_TYPES, "header");
+ params.put(RangerPdpAuthFilter.PARAM_HEADER_AUTHN_ENABLED, "false");
+
+ assertThrows(ServletException.class, () -> filter.init(new TestFilterConfig(params)));
+ }
Review Comment:
Test name `testInit_skipsHeaderHandlerWhenDisabled` is a bit misleading: the assertion expects `filter.init(...)` to throw, which happens because skipping the only configured handler leaves zero handlers and causes init to fail. Consider renaming the test to reflect the expected behavior (e.g., init fails when only `header` is configured but `header.enabled=false`).
##########
dev-support/ranger-docker/README.md:
##########
@@ -111,7 +111,7 @@ Similarly, check the `depends` section of the `docker-compose.ranger-service.yam
#### Bring up all containers
~~~
./scripts/ozone/ozone-plugin-docker-setup.sh
-docker compose -f docker-compose.ranger.yml -f docker-compose.ranger-usersync.yml -f docker-compose.ranger-tagsync.yml -f docker-compose.ranger-kms.yml -f docker-compose.ranger-hadoop.yml -f docker-compose.ranger-hbase.yml -f docker-compose.ranger-kafka.yml -f docker-compose.ranger-hive.yml -f docker-compose.ranger-knox.yml -f docker-compose.ranger-ozone.yml up -d
+docker compose -f docker-compose.ranger.yml -f docker-compose.ranger-usersync.yml -f docker-compose.ranger-tagsync.yml -f docker-compose.ranger-pdp.xml -f docker-compose.ranger-kms.yml -f docker-compose.ranger-hadoop.yml -f docker-compose.ranger-hbase.yml -f docker-compose.ranger-kafka.yml -f docker-compose.ranger-hive.yml -f docker-compose.ranger-knox.yml -f docker-compose.ranger-ozone.yml up -d
~~~
Review Comment:
This second compose command also references `docker-compose.ranger-pdp.xml`, but the compose file committed in this PR is `docker-compose.ranger-pdp.yml`. Please update the README so copy/paste works.
##########
dev-support/ranger-docker/README.md:
##########
@@ -74,14 +74,14 @@ cd dev-support/ranger-docker
### Run Ranger Services in Containers
-#### Bring up ranger-core services: ranger, usersync, tagsync and ranger-kms in containers
+#### Bring up ranger-core services: ranger, usersync, tagsync, pdp and ranger-kms in containers
~~~
# To enable file based sync source for usersync do:
# export ENABLE_FILE_SYNC_SOURCE=true
# valid values for RANGER_DB_TYPE: mysql/postgres/oracle
-docker compose -f docker-compose.ranger.yml -f docker-compose.ranger-usersync.yml -f docker-compose.ranger-tagsync.yml -f docker-compose.ranger-kms.yml up -d
+docker compose -f docker-compose.ranger.yml -f docker-compose.ranger-usersync.yml -f docker-compose.ranger-tagsync.yml -f docker-compose.ranger-pdp.xml -f docker-compose.ranger-kms.yml up -d
Review Comment:
The docker compose command references `docker-compose.ranger-pdp.xml`, but the added file is `docker-compose.ranger-pdp.yml`. This typo will cause the documented command to fail. Update the filename/extension in the README to match the actual compose file.
```suggestion
docker compose -f docker-compose.ranger.yml -f docker-compose.ranger-usersync.yml -f docker-compose.ranger-tagsync.yml -f docker-compose.ranger-pdp.yml -f docker-compose.ranger-kms.yml up -d
```
##########
pdp/src/main/java/org/apache/ranger/pdp/config/RangerPdpConstants.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.config;
+
+public final class RangerPdpConstants {
+ private RangerPdpConstants() {
+ // no instances
+ }
+
+ // Servlet context attributes
+ public static final String SERVLET_CTX_ATTR_CONFIG = "ranger.pdp.config";
+ public static final String SERVLET_CTX_ATTR_AUTHORIZER = "ranger.pdp.authorizer";
+ public static final String SERVLET_CTX_ATTR_RUNTIME_STATE = "ranger.pdp.runtime.state";
+
+ // Request attributes set by auth filter
+ public static final String ATTR_AUTHENTICATED_USER = "ranger.pdp.authenticated.user";
+ public static final String ATTR_AUTH_TYPE = "ranger.pdp.auth.type";
+
+ // Server
+ public static final String PROP_CONF_DIR = "ranger.pdp.conf.dir";
+ public static final String PROP_PORT = "ranger.pdp.port";
+ public static final String PROP_LOG_DIR = "ranger.pdp.log.dir";
+
+ // SSL/TLS
+ public static final String PROP_SSL_ENABLED = "ranger.pdp.ssl.enabled";
+ public static final String PROP_SSL_KEYSTORE_FILE = "ranger.pdp.ssl.keystore.file";
+ public static final String PROP_SSL_KEYSTORE_PASSWORD = "ranger.pdp.ssl.keystore.password";
+ public static final String PROP_SSL_KEYSTORE_TYPE = "ranger.pdp.ssl.keystore.type";
+ public static final String PROP_SSL_TRUSTSTORE_ENABLED = "ranger.pdp.ssl.truststore.enabled";
+ public static final String PROP_SSL_TRUSTSTORE_FILE = "ranger.pdp.ssl.truststore.file";
+ public static final String PROP_SSL_TRUSTSTORE_PASSWORD = "ranger.pdp.ssl.truststore.password";
+ public static final String PROP_SSL_TRUSTSTORE_TYPE = "ranger.pdp.ssl.truststore.type";
+
+ // HTTP/2
+ public static final String PROP_HTTP2_ENABLED = "ranger.pdp.http2.enabled";
+
+ // HTTP connector limits
+ public static final String PROP_HTTP_CONNECTOR_MAX_THREADS = "ranger.pdp.http.connector.maxThreads";
+ public static final String PROP_HTTP_CONNECTOR_MIN_SPARE_THREADS = "ranger.pdp.http.connector.minSpareThreads";
+ public static final String PROP_HTTP_CONNECTOR_ACCEPT_COUNT = "ranger.pdp.http.connector.acceptCount";
+ public static final String PROP_HTTP_CONNECTOR_MAX_CONNECTIONS = "ranger.pdp.http.connector.maxConnections";
+
+ // Authentication types
+ public static final String PROP_AUTH_TYPES = "ranger.pdp.auth.types";
+
+ // HTTP header auth
+ public static final String PROP_HEADER_AUTHN_ENABLED = "ranger.pdp.authn.header.enabled";
+ public static final String PROP_HEADER_AUTHN_USERNAME = "ranger.pdp.authn.header.username";
+
+ // JWT auth
+ public static final String PROP_JWT_PROVIDER_URL = "ranger.pdp.jwt.provider.url";
+ public static final String PROP_JWT_PUBLIC_KEY = "ranger.pdp.jwt.public.key";
+ public static final String PROP_JWT_COOKIE_NAME = "ranger.pdp.jwt.cookie.name";
+ public static final String PROP_JWT_AUDIENCES = "ranger.pdp.jwt.audiences";
+
+ // Kerberos/SPNEGO auth
+ public static final String PROP_SPNEGO_PRINCIPAL = "ranger.pdp.kerberos.spnego.principal";
+ public static final String PROP_SPNEGO_KEYTAB = "ranger.pdp.kerberos.spnego.keytab";
+ public static final String PROP_KRB_NAME_RULES = "hadoop.security.auth_to_local";
+ public static final String PROP_KRB_TOKEN_VALIDITY = "ranger.pdp.kerberos.token.valid.seconds";
+ public static final String PROP_KRB_COOKIE_DOMAIN = "ranger.pdp.kerberos.cookie.domain";
+ public static final String PROP_KRB_COOKIE_PATH = "ranger.pdp.kerberos.cookie.path";
+
+ // Authorizer/audit properties referenced by PDP code
+ public static final String PROP_AUTHZ_POLICY_CACHE_DIR = "ranger.authz.default.policy.cache.dir";
+ public static final String PROP_AUTHZ_PREFIX = "ranger.authz.";
+ public static final String PROP_PDP_PREFIX = "ranger.pdp.";
+ public static final String PROP_PDP_SERVICE_PREFIX = PROP_PDP_PREFIX + "service.";
+ public static final String PROP_SPNEGO_PREFIX = "ranger.spnego.";
+ public static final String PROP_HADOOP_SECURITY_PREFIX = "hadoop.security.";
+
+ // delegation users
+ public static final String PROP_SUFFIX_DELEGATION_USERS = ".delegation.users";
+ public static final String WILDCARD_SERVICE_NAME = "*";
Review Comment:
`PROP_SUFFIX_DELEGATION_USERS` is defined as `.delegation.users`, but the default config templates and REST Javadoc refer to `.allowed.users`. This mismatch will prevent delegation/allowed-caller config from being loaded unless users guess the correct suffix. Consider renaming this constant (and related variable names) or switching the suffix to `.allowed.users` to match the documented config keys (and update docker configs/tests accordingly).
##########
pdp/src/main/resources/ranger-pdp-default.xml:
##########
@@ -0,0 +1,392 @@
+<?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>
+
+ <!-- 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
+ Comma-separated list of methods tried in the listed order.
+ Supported values: header, jwt, kerberos
+ ================================================================ -->
+ <property>
+ <name>ranger.pdp.auth.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>
Review Comment:
`ranger.pdp.auth.types` defaults to `header,jwt,kerberos`, but the default JWT/SPNEGO configuration values in this same file are empty. `JwtAuthHandler`/`KerberosAuthHandler` initialization will throw when required settings are missing, which prevents the server from starting with the out-of-the-box defaults. Consider changing the default auth types to only include handlers that can be initialized with the provided defaults (or provide non-empty defaults / skip handlers when unconfigured).
##########
pdp/src/main/java/org/apache/ranger/pdp/rest/RangerPdpREST.java:
##########
@@ -0,0 +1,503 @@
+/*
+ * 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.rest;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.MapUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.ranger.authz.api.RangerAuthorizer;
+import org.apache.ranger.authz.api.RangerAuthzException;
+import org.apache.ranger.authz.model.RangerAccessInfo;
+import org.apache.ranger.authz.model.RangerAuthzRequest;
+import org.apache.ranger.authz.model.RangerAuthzResult;
+import org.apache.ranger.authz.model.RangerMultiAuthzRequest;
+import org.apache.ranger.authz.model.RangerMultiAuthzResult;
+import org.apache.ranger.authz.model.RangerResourceInfo;
+import org.apache.ranger.authz.model.RangerResourcePermissions;
+import org.apache.ranger.authz.model.RangerResourcePermissionsRequest;
+import org.apache.ranger.authz.model.RangerUserInfo;
+import org.apache.ranger.pdp.RangerPdpStats;
+import org.apache.ranger.pdp.config.RangerPdpConfig;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_PDP_SERVICE_PREFIX;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_SUFFIX_DELEGATION_USERS;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.WILDCARD_SERVICE_NAME;
+
+/**
+ * REST resource that exposes the three core {@link RangerAuthorizer} methods over HTTP.
+ *
+ * <p>All endpoints are under {@code /authz/v1} and produce/consume {@code application/json}.
+ * Authentication is enforced upstream by {@link RangerPdpAuthFilter}; the authenticated
+ * caller's identity is read from the {@link RangerPdpConstants#ATTR_AUTHENTICATED_USER}
+ * request attribute.
+ *
+ * <table border="1">
+ * <tr><th>Method</th><th>Path</th><th>Request body</th><th>Response body</th></tr>
+ * <tr><td>POST</td><td>/authz/v1/authorize</td>
+ * <td>{@link RangerAuthzRequest}</td><td>{@link RangerAuthzResult}</td></tr>
+ * <tr><td>POST</td><td>/authz/v1/authorizeMulti</td>
+ * <td>{@link RangerMultiAuthzRequest}</td><td>{@link RangerMultiAuthzResult}</td></tr>
+ * <tr><td>POST</td><td>/authz/v1/permissions</td>
+ * <td>{@link RangerResourcePermissionsRequest}</td>
+ * <td>{@link RangerResourcePermissions}</td></tr>
+ * </table>
+ */
+@Path("/v1")
+@Produces(MediaType.APPLICATION_JSON)
+@Consumes(MediaType.APPLICATION_JSON)
+@Singleton
+public class RangerPdpREST {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpREST.class);
+
+ private static final Response RESPONSE_OK = Response.ok().build();
+
+ private final Map<String, Set<String>> delegationUsersByService = new HashMap<>();
+
+ @Inject
+ private RangerAuthorizer authorizer;
+
+ @Inject
+ private RangerPdpConfig config;
+
+ @Context
+ private ServletContext servletContext;
+
+ @PostConstruct
+ public void initialize() {
+ initializeDelegationUsers();
+ }
+
+ /**
+ * Evaluates a single access request.
+ *
+ * @param request the authorization request
+ * @return {@code 200 OK} with {@link RangerAuthzResult}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/authorize")
+ public Response authorize(RangerAuthzRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String requestId = request != null ? request.getRequestId() : null;
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+ RangerUserInfo user = request != null ? request.getUser() : null;
+ RangerAccessInfo access = request != null ? request.getAccess() : null;
+
+ LOG.debug("==> authorize(requestId={}, caller={}, serviceName={})", requestId, caller, serviceName);
+
+ ret = validateCaller(caller, user, access, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
+ RangerAuthzResult result = authorizer.authorize(request);
+
+ ret = Response.ok(result).build();
+ } catch (RangerAuthzException e) {
+ LOG.warn("authorize(requestId={}): authorization error; caller={}", requestId, caller, e);
+
+ ret = badRequest(e);
+ } catch (Exception e) {
+ LOG.error("authorize(requestId={}): internal error; caller={}", requestId, caller, e);
+
+ ret = serverError(e);
+ }
+ }
+
+ LOG.debug("<== authorize(requestId={}, caller={}, serviceName={}): ret={}", requestId, caller, serviceName, ret != null ? ret.getStatus() : null);
+ } finally {
+ recordRequestMetrics(ret, startNanos, httpRequest);
+ }
+
+ return ret;
+ }
+
+ /**
+ * Evaluates multiple access requests in a single call.
+ *
+ * @param request the multi-access authorization request
+ * @return {@code 200 OK} with {@link RangerMultiAuthzResult}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/authorizeMulti")
+ public Response authorizeMulti(RangerMultiAuthzRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String requestId = request != null ? request.getRequestId() : null;
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+ RangerUserInfo user = request != null ? request.getUser() : null;
+ List<RangerAccessInfo> accesses = request != null ? request.getAccesses() : null;
+
+ LOG.debug("==> authorizeMulti(requestId={}, caller={}, serviceName={})", requestId, caller, serviceName);
+
+ ret = validateCaller(caller, user, accesses, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
+ RangerMultiAuthzResult result = authorizer.authorize(request);
+
+ ret = Response.ok(result).build();
+ } catch (RangerAuthzException e) {
+ LOG.warn("authorizeMulti(requestId={}): authorization error; caller={}", requestId, caller, e);
+
+ ret = badRequest(e);
+ } catch (Exception e) {
+ LOG.error("authorizeMulti(requestId={}): internal error; caller={}", requestId, caller, e);
+
+ ret = serverError(e);
+ }
+ }
+
+ LOG.debug("<== authorizeMulti(requestId={}, caller={}, serviceName={}): ret={}", requestId, caller, serviceName, ret != null ? ret.getStatus() : null);
+ } finally {
+ recordRequestMetrics(ret, startNanos, httpRequest);
+ }
+
+ return ret;
+ }
+
+ /**
+ * Returns the effective permissions for a resource, broken down by user/group/role.
+ *
+ * @param request wrapper containing the resource info and access context
+ * @return {@code 200 OK} with {@link RangerResourcePermissions}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/permissions")
+ public Response getResourcePermissions(RangerResourcePermissionsRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+
+ LOG.debug("==> getResourcePermissions(caller={}, serviceName={})", caller, serviceName);
+
+ ret = validateCaller(caller, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
+ RangerResourcePermissions result = authorizer.getResourcePermissions(request);
+
+ ret = Response.ok(result).build();
+ } catch (RangerAuthzException e) {
+ LOG.warn("getResourcePermissions(): validation error; caller={}", caller, e);
+
+ ret = badRequest(e);
+ } catch (Exception e) {
+ LOG.error("getResourcePermissions(): unexpected error; caller={}", caller, e);
+
+ ret = serverError(e);
+ }
+ }
+
+ LOG.debug("==> getResourcePermissions(caller={}, serviceName={}): ret={}", caller, serviceName, ret != null ? ret.getStatus() : null);
+ } finally {
Review Comment:
The exit log line uses the same `==>` prefix as the entry log (`LOG.debug("==> getResourcePermissions...")`). For consistency with the other methods (and to make logs easier to scan), this looks like it should be `<== getResourcePermissions(...)`.
##########
pdp/conf.dist/ranger-pdp-site.xml:
##########
@@ -0,0 +1,299 @@
+<?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.
+-->
+<!-- Copy this file to conf/ranger-pdp-site.xml and edit to override defaults. -->
+
+<configuration>
+
+ <!-- ================================================================
+ PDP Server
+ ================================================================ -->
+
+ <property>
+ <name>ranger.pdp.port</name>
+ <value>6500</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.log.dir</name>
+ <value>/var/log/ranger/pdp</value>
+ </property>
+
+ <!-- ================================================================
+ SSL/TLS
+ ================================================================ -->
+
+ <property>
+ <name>ranger.pdp.ssl.enabled</name>
+ <value>false</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.ssl.keystore.file</name>
+ <value/>
+ </property>
+
+ <property>
+ <name>ranger.pdp.ssl.keystore.password</name>
+ <value/>
+ </property>
+
+ <property>
+ <name>ranger.pdp.ssl.keystore.type</name>
+ <value>JKS</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.ssl.truststore.enabled</name>
+ <value>false</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.ssl.truststore.file</name>
+ <value/>
+ </property>
+
+ <property>
+ <name>ranger.pdp.ssl.truststore.password</name>
+ <value/>
+ </property>
+
+ <property>
+ <name>ranger.pdp.ssl.truststore.type</name>
+ <value>JKS</value>
+ </property>
+
+ <!-- ================================================================
+ HTTP/2
+ ================================================================ -->
+
+ <property>
+ <name>ranger.pdp.http2.enabled</name>
+ <value>true</value>
+ </property>
+
+ <!-- Connector concurrency / queue limits -->
+ <property>
+ <name>ranger.pdp.http.connector.maxThreads</name>
+ <value>200</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.http.connector.minSpareThreads</name>
+ <value>20</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.http.connector.acceptCount</name>
+ <value>100</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.http.connector.maxConnections</name>
+ <value>10000</value>
+ </property>
+
+ <!-- ================================================================
+ Authentication for incoming REST requests
+ Comma-separated list of methods tried in listed order.
+ Supported values: header, jwt, kerberos
+ ================================================================ -->
+
+ <property>
+ <name>ranger.pdp.auth.types</name>
+ <value>header,jwt,kerberos</value>
+ </property>
+
+ <!-- HTTP Header authentication -->
+
+ <property>
+ <name>ranger.pdp.authn.header.enabled</name>
+ <value>false</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.authn.header.username</name>
+ <value>X-Forwarded-User</value>
+ </property>
+
+ <!-- JWT bearer token authentication -->
+
+ <property>
+ <name>ranger.pdp.jwt.provider.url</name>
+ <value/>
+ </property>
+
+ <property>
+ <name>ranger.pdp.jwt.public.key</name>
+ <value/>
+ </property>
+
+ <property>
+ <name>ranger.pdp.jwt.cookie.name</name>
+ <value>hadoop-jwt</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.jwt.audiences</name>
+ <value/>
+ </property>
+
+ <!-- Kerberos / SPNEGO authentication -->
+
+ <property>
+ <name>ranger.pdp.kerberos.spnego.principal</name>
+ <value/>
+ </property>
+
+ <property>
+ <name>ranger.pdp.kerberos.spnego.keytab</name>
+ <value/>
+ </property>
+
+ <property>
+ <name>hadoop.security.auth_to_local</name>
+ <value>DEFAULT</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.kerberos.token.valid.seconds</name>
+ <value>30</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.kerberos.cookie.domain</name>
+ <value/>
+ </property>
+
+ <property>
+ <name>ranger.pdp.kerberos.cookie.path</name>
+ <value>/</value>
+ </property>
+
+ <!--
+ Allowed callers for PDP REST APIs, per serviceName in request context.
+ Format:
+ ranger.pdp.service.<serviceName>.allowed.users=user1,user2
+ ranger.pdp.service.*.allowed.users=user3,user4
+ If no such entries are configured, this validation is disabled.
+ -->
+ <property>
+ <name>ranger.pdp.service.*.allowed.users</name>
Review Comment:
The conf.dist template describes allowed-caller keys as `ranger.pdp.service.*.allowed.users`, but the PDP implementation currently loads keys ending in `.delegation.users`. As-is, users copying this template into `conf/` will configure a property that PDP never reads. Align the template property name(s) with the code/constants (or vice versa).
```suggestion
ranger.pdp.service.<serviceName>.delegation.users=user1,user2
ranger.pdp.service.*.delegation.users=user3,user4
If no such entries are configured, this validation is disabled.
-->
<property>
<name>ranger.pdp.service.*.delegation.users</name>
```
##########
pdp/src/main/resources/ranger-pdp-default.xml:
##########
@@ -0,0 +1,392 @@
+<?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>
+
+ <!-- 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
+ Comma-separated list of methods tried in the listed order.
+ Supported values: header, jwt, kerberos
+ ================================================================ -->
+ <property>
+ <name>ranger.pdp.auth.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
+ Consistent with: ranger.admin.authn.header.* in security-admin -->
+ <property>
+ <name>ranger.pdp.authn.header.enabled</name>
+ <value>false</value>
+ <description>
+ Enable trusted HTTP header authentication. Use only behind a trusted proxy.
+ </description>
+ </property>
+
+ <property>
+ <name>ranger.pdp.authn.header.username</name>
+ <value>X-Forwarded-User</value>
+ <description>HTTP header name from which the authenticated username is read.</description>
+ </property>
+
+ <!-- JWT bearer token authentication
+ PDP validates tokens presented as "Authorization: Bearer token" or via cookie.
+ This is distinct from Knox SSO (browser redirect flow); no SSO redirect is performed. -->
+ <property>
+ <name>ranger.pdp.jwt.provider.url</name>
+ <value/>
+ <description>URL of the JWT provider (used to fetch the public key).</description>
+ </property>
+
+ <property>
+ <name>ranger.pdp.jwt.public.key</name>
+ <value/>
+ <description>Base64-encoded public key for verifying JWT signatures.</description>
+ </property>
+
+ <property>
+ <name>ranger.pdp.jwt.cookie.name</name>
+ <value>hadoop-jwt</value>
+ <description>Cookie name from which a JWT bearer token may be read.</description>
+ </property>
+
+ <property>
+ <name>ranger.pdp.jwt.audiences</name>
+ <value/>
+ <description>Comma-separated list of accepted JWT audiences. Empty means any audience is accepted.</description>
+ </property>
+
+ <!-- Kerberos / SPNEGO authentication for incoming REST requests -->
+ <property>
+ <name>ranger.pdp.kerberos.spnego.principal</name>
+ <value/>
+ <description>Kerberos service principal for SPNEGO authentication, e.g. HTTP/host@REALM.</description>
+ </property>
+
+ <property>
+ <name>ranger.pdp.kerberos.spnego.keytab</name>
+ <value/>
+ <description>Path to the keytab file for the SPNEGO service principal.</description>
+ </property>
+
+ <property>
+ <name>hadoop.security.auth_to_local</name>
+ <value>DEFAULT</value>
+ <description>Rules for mapping Kerberos principal names to short usernames.</description>
+ </property>
+
+ <property>
+ <name>ranger.pdp.kerberos.token.valid.seconds</name>
+ <value>30</value>
+ <description>Validity period (seconds) for Kerberos authentication tokens.</description>
+ </property>
+
+ <property>
+ <name>ranger.pdp.kerberos.cookie.domain</name>
+ <value/>
+ <description>Cookie domain used for the Kerberos authentication cookie.</description>
+ </property>
+
+ <property>
+ <name>ranger.pdp.kerberos.cookie.path</name>
+ <value>/</value>
+ <description>Cookie path used for the Kerberos authentication cookie.</description>
+ </property>
+
+ <!--
+ Allowed callers for PDP REST APIs, per serviceName in request context.
+ Format:
+ ranger.pdp.service.<serviceName>.allowed.users=user1,user2
+ ranger.pdp.service.*.allowed.users=user3,user4
+ If no such entries are configured, this validation is disabled.
+ -->
+ <property>
+ <name>ranger.pdp.service.*.allowed.users</name>
+ <value/>
+ <description>Comma-separated users allowed to invoke PDP APIs for all services.</description>
+ </property>
Review Comment:
The default config documents the allowed-caller keys as `ranger.pdp.service.*.allowed.users`, but the PDP code currently reads `ranger.pdp.service.<service>.delegation.users` (suffix `.delegation.users`). As written, these defaults won't be picked up by `RangerPdpREST.initializeDelegationUsers()`. Update the property name(s) here (or update the code/constants) so the shipped defaults and the implementation agree.
##########
pdp/src/main/java/org/apache/ranger/pdp/rest/RangerPdpREST.java:
##########
@@ -0,0 +1,503 @@
+/*
+ * 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.rest;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.MapUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.ranger.authz.api.RangerAuthorizer;
+import org.apache.ranger.authz.api.RangerAuthzException;
+import org.apache.ranger.authz.model.RangerAccessInfo;
+import org.apache.ranger.authz.model.RangerAuthzRequest;
+import org.apache.ranger.authz.model.RangerAuthzResult;
+import org.apache.ranger.authz.model.RangerMultiAuthzRequest;
+import org.apache.ranger.authz.model.RangerMultiAuthzResult;
+import org.apache.ranger.authz.model.RangerResourceInfo;
+import org.apache.ranger.authz.model.RangerResourcePermissions;
+import org.apache.ranger.authz.model.RangerResourcePermissionsRequest;
+import org.apache.ranger.authz.model.RangerUserInfo;
+import org.apache.ranger.pdp.RangerPdpStats;
+import org.apache.ranger.pdp.config.RangerPdpConfig;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_PDP_SERVICE_PREFIX;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_SUFFIX_DELEGATION_USERS;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.WILDCARD_SERVICE_NAME;
+
+/**
+ * REST resource that exposes the three core {@link RangerAuthorizer} methods over HTTP.
+ *
+ * <p>All endpoints are under {@code /authz/v1} and produce/consume {@code application/json}.
+ * Authentication is enforced upstream by {@link RangerPdpAuthFilter}; the authenticated
+ * caller's identity is read from the {@link RangerPdpConstants#ATTR_AUTHENTICATED_USER}
+ * request attribute.
+ *
+ * <table border="1">
+ * <tr><th>Method</th><th>Path</th><th>Request body</th><th>Response body</th></tr>
+ * <tr><td>POST</td><td>/authz/v1/authorize</td>
+ * <td>{@link RangerAuthzRequest}</td><td>{@link RangerAuthzResult}</td></tr>
+ * <tr><td>POST</td><td>/authz/v1/authorizeMulti</td>
+ * <td>{@link RangerMultiAuthzRequest}</td><td>{@link RangerMultiAuthzResult}</td></tr>
+ * <tr><td>POST</td><td>/authz/v1/permissions</td>
+ * <td>{@link RangerResourcePermissionsRequest}</td>
+ * <td>{@link RangerResourcePermissions}</td></tr>
+ * </table>
+ */
+@Path("/v1")
+@Produces(MediaType.APPLICATION_JSON)
+@Consumes(MediaType.APPLICATION_JSON)
+@Singleton
+public class RangerPdpREST {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpREST.class);
+
+ private static final Response RESPONSE_OK = Response.ok().build();
+
+ private final Map<String, Set<String>> delegationUsersByService = new HashMap<>();
+
+ @Inject
+ private RangerAuthorizer authorizer;
+
+ @Inject
+ private RangerPdpConfig config;
+
+ @Context
+ private ServletContext servletContext;
+
+ @PostConstruct
+ public void initialize() {
+ initializeDelegationUsers();
+ }
+
+ /**
+ * Evaluates a single access request.
+ *
+ * @param request the authorization request
+ * @return {@code 200 OK} with {@link RangerAuthzResult}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/authorize")
+ public Response authorize(RangerAuthzRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String requestId = request != null ? request.getRequestId() : null;
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+ RangerUserInfo user = request != null ? request.getUser() : null;
+ RangerAccessInfo access = request != null ? request.getAccess() : null;
+
+ LOG.debug("==> authorize(requestId={}, caller={}, serviceName={})", requestId, caller, serviceName);
+
+ ret = validateCaller(caller, user, access, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
+ RangerAuthzResult result = authorizer.authorize(request);
+
+ ret = Response.ok(result).build();
+ } catch (RangerAuthzException e) {
+ LOG.warn("authorize(requestId={}): authorization error; caller={}", requestId, caller, e);
+
+ ret = badRequest(e);
+ } catch (Exception e) {
+ LOG.error("authorize(requestId={}): internal error; caller={}", requestId, caller, e);
+
+ ret = serverError(e);
+ }
+ }
+
+ LOG.debug("<== authorize(requestId={}, caller={}, serviceName={}): ret={}", requestId, caller, serviceName, ret != null ? ret.getStatus() : null);
+ } finally {
+ recordRequestMetrics(ret, startNanos, httpRequest);
+ }
+
+ return ret;
+ }
+
+ /**
+ * Evaluates multiple access requests in a single call.
+ *
+ * @param request the multi-access authorization request
+ * @return {@code 200 OK} with {@link RangerMultiAuthzResult}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/authorizeMulti")
+ public Response authorizeMulti(RangerMultiAuthzRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String requestId = request != null ? request.getRequestId() : null;
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+ RangerUserInfo user = request != null ? request.getUser() : null;
+ List<RangerAccessInfo> accesses = request != null ? request.getAccesses() : null;
+
+ LOG.debug("==> authorizeMulti(requestId={}, caller={}, serviceName={})", requestId, caller, serviceName);
+
+ ret = validateCaller(caller, user, accesses, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
+ RangerMultiAuthzResult result = authorizer.authorize(request);
+
+ ret = Response.ok(result).build();
+ } catch (RangerAuthzException e) {
+ LOG.warn("authorizeMulti(requestId={}): authorization error; caller={}", requestId, caller, e);
+
+ ret = badRequest(e);
+ } catch (Exception e) {
+ LOG.error("authorizeMulti(requestId={}): internal error; caller={}", requestId, caller, e);
+
+ ret = serverError(e);
+ }
+ }
+
+ LOG.debug("<== authorizeMulti(requestId={}, caller={}, serviceName={}): ret={}", requestId, caller, serviceName, ret != null ? ret.getStatus() : null);
+ } finally {
+ recordRequestMetrics(ret, startNanos, httpRequest);
+ }
+
+ return ret;
+ }
+
+ /**
+ * Returns the effective permissions for a resource, broken down by user/group/role.
+ *
+ * @param request wrapper containing the resource info and access context
+ * @return {@code 200 OK} with {@link RangerResourcePermissions}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/permissions")
+ public Response getResourcePermissions(RangerResourcePermissionsRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+
+ LOG.debug("==> getResourcePermissions(caller={}, serviceName={})", caller, serviceName);
+
+ ret = validateCaller(caller, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
+ RangerResourcePermissions result = authorizer.getResourcePermissions(request);
+
+ ret = Response.ok(result).build();
+ } catch (RangerAuthzException e) {
+ LOG.warn("getResourcePermissions(): validation error; caller={}", caller, e);
+
+ ret = badRequest(e);
+ } catch (Exception e) {
+ LOG.error("getResourcePermissions(): unexpected error; caller={}", caller, e);
+
+ ret = serverError(e);
+ }
+ }
+
+ LOG.debug("==> getResourcePermissions(caller={}, serviceName={}): ret={}", caller, serviceName, ret != null ? ret.getStatus() : null);
+ } finally {
+ recordRequestMetrics(ret, startNanos, httpRequest);
+ }
+
+ return ret;
+ }
+
+ private String getAuthenticatedUser(HttpServletRequest httpRequest) {
+ Object user = httpRequest.getAttribute(RangerPdpConstants.ATTR_AUTHENTICATED_USER);
+
+ return user != null ? user.toString() : null;
+ }
+
+ private static String getServiceName(RangerAuthzRequest request) {
+ return request != null && request.getContext() != null ? request.getContext().getServiceName() : null;
+ }
+
+ private static String getServiceName(RangerMultiAuthzRequest request) {
+ return request != null && request.getContext() != null ? request.getContext().getServiceName() : null;
+ }
+
+ private static String getServiceName(RangerResourcePermissionsRequest request) {
+ return request != null && request.getContext() != null ? request.getContext().getServiceName() : null;
+ }
+
+ private Response validateCaller(String caller, RangerUserInfo user, RangerAccessInfo access, String serviceName) {
+ final Response ret;
+
+ if (StringUtils.isBlank(caller)) {
+ ret = Response.status(Response.Status.UNAUTHORIZED)
+ .entity(new ErrorResponse(Response.Status.UNAUTHORIZED, "Authentication required"))
+ .build();
+ } else {
+ boolean needsDelegation = isDelegationNeeded(caller, user) || isDelegationNeeded(access);
+
+ if (needsDelegation) {
+ if (!isDelegationUserForService(serviceName, caller)) {
+ LOG.info("{} is not a delegation user in service {}", caller, serviceName);
+
+ ret = Response.status(Response.Status.FORBIDDEN)
+ .entity(new ErrorResponse(Response.Status.FORBIDDEN, caller + " is not authorized"))
+ .build();
+ } else {
+ ret = RESPONSE_OK;
+ }
+ } else {
+ ret = RESPONSE_OK;
+ }
+ }
+
+ return ret;
+ }
+
+ private Response validateCaller(String caller, RangerUserInfo user, List<RangerAccessInfo> accesses, String serviceName) {
+ final Response ret;
+
+ if (StringUtils.isBlank(caller)) {
+ ret = Response.status(Response.Status.UNAUTHORIZED)
+ .entity(new ErrorResponse(Response.Status.UNAUTHORIZED, "Authentication required"))
+ .build();
+ } else {
+ boolean needsDelegation = isDelegationNeeded(caller, user) || isDelegationNeeded(accesses);
+
+ if (needsDelegation) {
+ if (!isDelegationUserForService(serviceName, caller)) {
+ LOG.info("{} is not a delegation user in service {}", caller, serviceName);
+
+ ret = Response.status(Response.Status.FORBIDDEN)
+ .entity(new ErrorResponse(Response.Status.FORBIDDEN, caller + " is not authorized"))
+ .build();
+ } else {
+ ret = RESPONSE_OK;
+ }
+ } else {
+ ret = RESPONSE_OK;
+ }
+ }
+
+ return ret;
+ }
+
+ private Response validateCaller(String caller, String serviceName) {
+ final Response ret;
+
+ if (StringUtils.isBlank(caller)) {
+ ret = Response.status(Response.Status.UNAUTHORIZED)
+ .entity(new ErrorResponse(Response.Status.UNAUTHORIZED, "Authentication required"))
+ .build();
+ } else if (!isDelegationUserForService(serviceName, caller)) {
+ LOG.info("{} is not a delegation user in service {}", caller, serviceName);
+
+ ret = Response.status(Response.Status.FORBIDDEN)
+ .entity(new ErrorResponse(Response.Status.FORBIDDEN, caller + " is not authorized"))
+ .build();
+ } else {
+ ret = RESPONSE_OK;
+ }
+
+ return ret;
+ }
+
+ private boolean isDelegationNeeded(String caller, RangerUserInfo user) {
+ String userName = user != null ? user.getName() : null;
+ boolean needsDelegation = !caller.equals(userName);
+
+ if (!needsDelegation) {
+ // don't trust user-attributes/groups/roles if caller doesn't have delegation permission
+ needsDelegation = MapUtils.isNotEmpty(user.getAttributes()) || CollectionUtils.isNotEmpty(user.getGroups()) || CollectionUtils.isNotEmpty(user.getRoles());
+ }
+
+ return needsDelegation;
+ }
+
+ private boolean isDelegationNeeded(RangerAccessInfo access) {
+ RangerResourceInfo resource = access != null ? access.getResource() : null;
+
+ // delegation permission is needed when resource attributes are specified
+ return (resource != null && MapUtils.isNotEmpty(resource.getAttributes()));
+ }
+
+ private boolean isDelegationNeeded(List<RangerAccessInfo> accesses) {
+ if (accesses != null) {
+ for (RangerAccessInfo access : accesses) {
+ if (isDelegationNeeded(access)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ private RangerPdpStats getRuntimeState(HttpServletRequest httpRequest) {
+ Object state = httpRequest.getServletContext().getAttribute(RangerPdpConstants.SERVLET_CTX_ATTR_RUNTIME_STATE);
+
+ if (state instanceof RangerPdpStats) {
+ return (RangerPdpStats) state;
+ } else {
+ Object fallback = servletContext != null ? servletContext.getAttribute(RangerPdpConstants.SERVLET_CTX_ATTR_RUNTIME_STATE) : null;
+
+ return (fallback instanceof RangerPdpStats) ? (RangerPdpStats) fallback : new RangerPdpStats();
+ }
+ }
+
+ private void recordRequestMetrics(Response ret, long startNanos, HttpServletRequest httpRequest) {
+ RangerPdpStats state = getRuntimeState(httpRequest);
+ int status = ret != null ? ret.getStatus() : 500;
+ long elapsed = System.nanoTime() - startNanos;
+
+ if (status >= 200 && status < 300) {
+ state.recordRequestSuccess(elapsed);
+ } else if (status == 401 || status == 403) { // UNAUTHORIZED or FORBIDDEN
+ state.recordAuthFailure();
+ } else if (status == 400) {
+ state.recordRequestBadRequest(elapsed);
+ } else {
+ state.recordRequestError(elapsed);
+ }
+ }
+
+ /**
+ * Allowed-users config format:
+ * ranger.pdp.service.<serviceName>.allowed.users=user1,user2
+ * ranger.pdp.service.*.allowed.users=user3,user4
+ *
+ * If no allowed-users entries are configured, this check is disabled
+ * (backward-compatible behavior).
Review Comment:
The Javadoc for the allowed/authorized callers uses the key suffix `.allowed.users`, but `initializeDelegationUsers()` actually loads keys ending with `RangerPdpConstants.PROP_SUFFIX_DELEGATION_USERS` (currently `.delegation.users`). With the current mismatch, users following the docs will configure `...allowed.users` and PDP will behave as if no users are configured. Align the documentation (and ideally naming) with the actual config keys used by the code, or change the constant/suffix parsing to match the documented property name.
##########
intg/src/main/python/apache_ranger/client/ranger_pdp_client.py:
##########
@@ -0,0 +1,90 @@
+#!/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.
+
+import logging
+from apache_ranger.client.ranger_client import RangerClientHttp
+from apache_ranger.model.ranger_authz import RangerAuthzRequest, RangerAuthzResult
+from apache_ranger.model.ranger_authz import RangerMultiAuthzRequest, RangerMultiAuthzResult
+from apache_ranger.model.ranger_authz import RangerResourcePermissions, RangerResourcePermissionsRequest
+from apache_ranger.utils import API, HttpMethod, HTTPStatus
+from apache_ranger.utils import type_coerce
+
+LOG = logging.getLogger(__name__)
+
+
+class RangerPDPClient:
+ """
+ Python client for Ranger PDP authorization APIs.
+
+ Base URL should point to PDP server endpoint, for example:
+ http://localhost:6500
+ """
+
+ def __init__(self, url, auth, query_params=None, headers=None):
+ self.client_http = RangerClientHttp(url, auth, query_params, headers)
+ self.session = self.client_http.session
+ logging.getLogger("requests").setLevel(logging.WARNING)
+
+ def authorize(self, authz_request):
+ """
+ Call POST /authz/v1/authorize
+ :param authz_request: dict-like or RangerAuthzRequest
+ :return: RangerAuthzResult
+ """
+ req = type_coerce(authz_request, RangerAuthzRequest)
+ resp = self.client_http.call_api(RangerPDPClient.AUTHORIZE, request_data=req)
+ return type_coerce(resp, RangerAuthzResult)
+
+ def authorize_multi(self, multi_authz_request):
+ """
+ Call POST /authz/v1/authorizeMulti
+ :param multi_authz_request: dict-like or RangerMultiAuthzRequest
+ :return: RangerMultiAuthzResult
+ """
+ req = type_coerce(multi_authz_request, RangerMultiAuthzRequest)
+ resp = self.client_http.call_api(RangerPDPClient.AUTHORIZE_MULTI, request_data=req)
+ return type_coerce(resp, RangerMultiAuthzResult)
+
+ def get_resource_permissions(self, resource, context=None):
+ """
+ Call POST /authz/v1/permissions
+ :param resource: dict-like resource payload OR RangerResourcePermissionsRequest
+ :param context: dict-like access context payload (ignored if 'resource' is already request object)
+ :return: RangerResourcePermissions
+ """
+ req = type_coerce(resource, RangerResourcePermissionsRequest)
+ if req is None:
+ req = RangerResourcePermissionsRequest({
+ 'resource': resource,
+ 'context': context
+ })
+
Review Comment:
`get_resource_permissions()` attempts to accept either a resource payload dict or a `RangerResourcePermissionsRequest`, but `type_coerce(resource, RangerResourcePermissionsRequest)` will coerce *any* dict into a `RangerResourcePermissionsRequest` (with `resource`/`context` likely `None`). This means passing a plain dict resource (as documented) results in an invalid request body. Consider detecting whether the dict already looks like a `RangerResourcePermissionsRequest` (e.g., contains `resource`/`context`) before coercing, or accept `RangerResourceInfo`/dict and always wrap it into a request object.
##########
pdp/src/main/java/org/apache/ranger/pdp/config/RangerPdpConfig.java:
##########
@@ -0,0 +1,328 @@
+/*
+ * 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.config;
+
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import javax.xml.XMLConstants;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+/**
+ * Reads Ranger PDP configuration from {@code ranger-pdp-default.xml} (classpath)
+ * overridden by {@code ranger-pdp-site.xml} (classpath or filesystem).
+ *
+ * <p>Both files use the Hadoop {@code <configuration>} XML format, consistent
+ * with other Ranger server modules (tagsync, kms, etc.).
+ * The format is parsed directly using the JDK DOM API to avoid an early
+ * class-load dependency on Hadoop's {@code Configuration} class.
+ *
+ * <p>Authentication property names:
+ * <ul>
+ * <li>Kerberos/SPNEGO: {@code ranger.pdp.kerberos.spnego.*}
+ * <li>JWT bearer token: {@code ranger.pdp.jwt.*}
+ * <li>HTTP header: {@code ranger.pdp.authn.header.*}
+ * </ul>
+ */
+public class RangerPdpConfig {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpConfig.class);
+
+ private static final String DEFAULT_CONFIG_FILE = "ranger-pdp-default.xml";
+ private static final String SITE_CONFIG_FILE = "ranger-pdp-site.xml";
+
+ private final Properties props;
+
+ public RangerPdpConfig() {
+ props = new Properties();
+
+ loadFromClasspath(DEFAULT_CONFIG_FILE);
+ loadFromClasspath(SITE_CONFIG_FILE);
+
+ String confDir = System.getProperty(RangerPdpConstants.PROP_CONF_DIR, "");
+
+ if (StringUtils.isNotBlank(confDir)) {
+ loadFromFile(new File(confDir, SITE_CONFIG_FILE));
+ }
+
+ applySystemPropertyOverrides();
+
+ LOG.info("RangerPdpConfig initialized (conf.dir={})", confDir);
+ }
+
+ public int getPort() {
+ return getInt(RangerPdpConstants.PROP_PORT, 6500);
+ }
+
+ public String getLogDir() {
+ return get(RangerPdpConstants.PROP_LOG_DIR, "/var/log/ranger/pdp");
+ }
+
+ public boolean isSslEnabled() {
+ return getBoolean(RangerPdpConstants.PROP_SSL_ENABLED, false);
+ }
+
+ public String getKeystoreFile() {
+ return get(RangerPdpConstants.PROP_SSL_KEYSTORE_FILE, "");
+ }
+
+ public String getKeystorePassword() {
+ return get(RangerPdpConstants.PROP_SSL_KEYSTORE_PASSWORD, "");
+ }
+
+ public String getKeystoreType() {
+ return get(RangerPdpConstants.PROP_SSL_KEYSTORE_TYPE, "JKS");
+ }
+
+ public boolean isTruststoreEnabled() {
+ return getBoolean(RangerPdpConstants.PROP_SSL_TRUSTSTORE_ENABLED, false);
+ }
+
+ public String getTruststoreFile() {
+ return get(RangerPdpConstants.PROP_SSL_TRUSTSTORE_FILE, "");
+ }
+
+ public String getTruststorePassword() {
+ return get(RangerPdpConstants.PROP_SSL_TRUSTSTORE_PASSWORD, "");
+ }
+
+ public String getTruststoreType() {
+ return get(RangerPdpConstants.PROP_SSL_TRUSTSTORE_TYPE, "JKS");
+ }
+
+ public boolean isHttp2Enabled() {
+ return getBoolean(RangerPdpConstants.PROP_HTTP2_ENABLED, true);
+ }
+
+ public int getHttpConnectorMaxThreads() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_MAX_THREADS, 200);
+ }
+
+ public int getHttpConnectorMinSpareThreads() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_MIN_SPARE_THREADS, 20);
+ }
+
+ public int getHttpConnectorAcceptCount() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_ACCEPT_COUNT, 100);
+ }
+
+ public int getHttpConnectorMaxConnections() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_MAX_CONNECTIONS, 10000);
+ }
+
+ public String getAuthTypes() {
+ return get(RangerPdpConstants.PROP_AUTH_TYPES, "header,jwt,kerberos");
+ }
Review Comment:
`getAuthTypes()` defaults to `header,jwt,kerberos`. Combined with the shipped defaults (empty JWT public key/JWKS URL and empty SPNEGO principal/keytab), this leads to `RangerPdpAuthFilter` failing to initialize and the server failing to start unless the user overrides these properties. Consider using a safer default here (e.g., require explicit config, or default to a single auth mechanism that can start with defaults) so the default distribution can boot predictably.
##########
pdp/src/main/java/org/apache/ranger/pdp/rest/RangerPdpREST.java:
##########
@@ -0,0 +1,503 @@
+/*
+ * 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.rest;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.MapUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.ranger.authz.api.RangerAuthorizer;
+import org.apache.ranger.authz.api.RangerAuthzException;
+import org.apache.ranger.authz.model.RangerAccessInfo;
+import org.apache.ranger.authz.model.RangerAuthzRequest;
+import org.apache.ranger.authz.model.RangerAuthzResult;
+import org.apache.ranger.authz.model.RangerMultiAuthzRequest;
+import org.apache.ranger.authz.model.RangerMultiAuthzResult;
+import org.apache.ranger.authz.model.RangerResourceInfo;
+import org.apache.ranger.authz.model.RangerResourcePermissions;
+import org.apache.ranger.authz.model.RangerResourcePermissionsRequest;
+import org.apache.ranger.authz.model.RangerUserInfo;
+import org.apache.ranger.pdp.RangerPdpStats;
+import org.apache.ranger.pdp.config.RangerPdpConfig;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_PDP_SERVICE_PREFIX;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_SUFFIX_DELEGATION_USERS;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.WILDCARD_SERVICE_NAME;
+
+/**
+ * REST resource that exposes the three core {@link RangerAuthorizer} methods over HTTP.
+ *
+ * <p>All endpoints are under {@code /authz/v1} and produce/consume {@code application/json}.
+ * Authentication is enforced upstream by {@link RangerPdpAuthFilter}; the authenticated
+ * caller's identity is read from the {@link RangerPdpConstants#ATTR_AUTHENTICATED_USER}
+ * request attribute.
+ *
+ * <table border="1">
+ * <tr><th>Method</th><th>Path</th><th>Request body</th><th>Response body</th></tr>
+ * <tr><td>POST</td><td>/authz/v1/authorize</td>
+ * <td>{@link RangerAuthzRequest}</td><td>{@link RangerAuthzResult}</td></tr>
+ * <tr><td>POST</td><td>/authz/v1/authorizeMulti</td>
+ * <td>{@link RangerMultiAuthzRequest}</td><td>{@link RangerMultiAuthzResult}</td></tr>
+ * <tr><td>POST</td><td>/authz/v1/permissions</td>
+ * <td>{@link RangerResourcePermissionsRequest}</td>
+ * <td>{@link RangerResourcePermissions}</td></tr>
+ * </table>
+ */
+@Path("/v1")
+@Produces(MediaType.APPLICATION_JSON)
+@Consumes(MediaType.APPLICATION_JSON)
+@Singleton
+public class RangerPdpREST {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpREST.class);
+
+ private static final Response RESPONSE_OK = Response.ok().build();
+
+ private final Map<String, Set<String>> delegationUsersByService = new HashMap<>();
+
+ @Inject
+ private RangerAuthorizer authorizer;
+
+ @Inject
+ private RangerPdpConfig config;
+
+ @Context
+ private ServletContext servletContext;
+
+ @PostConstruct
+ public void initialize() {
+ initializeDelegationUsers();
+ }
+
+ /**
+ * Evaluates a single access request.
+ *
+ * @param request the authorization request
+ * @return {@code 200 OK} with {@link RangerAuthzResult}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/authorize")
+ public Response authorize(RangerAuthzRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String requestId = request != null ? request.getRequestId() : null;
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+ RangerUserInfo user = request != null ? request.getUser() : null;
+ RangerAccessInfo access = request != null ? request.getAccess() : null;
+
+ LOG.debug("==> authorize(requestId={}, caller={}, serviceName={})", requestId, caller, serviceName);
+
+ ret = validateCaller(caller, user, access, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
+ RangerAuthzResult result = authorizer.authorize(request);
+
+ ret = Response.ok(result).build();
+ } catch (RangerAuthzException e) {
+ LOG.warn("authorize(requestId={}): authorization error; caller={}", requestId, caller, e);
+
+ ret = badRequest(e);
+ } catch (Exception e) {
+ LOG.error("authorize(requestId={}): internal error; caller={}", requestId, caller, e);
+
+ ret = serverError(e);
+ }
+ }
+
+ LOG.debug("<== authorize(requestId={}, caller={}, serviceName={}): ret={}", requestId, caller, serviceName, ret != null ? ret.getStatus() : null);
+ } finally {
+ recordRequestMetrics(ret, startNanos, httpRequest);
+ }
+
+ return ret;
+ }
+
+ /**
+ * Evaluates multiple access requests in a single call.
+ *
+ * @param request the multi-access authorization request
+ * @return {@code 200 OK} with {@link RangerMultiAuthzResult}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/authorizeMulti")
+ public Response authorizeMulti(RangerMultiAuthzRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String requestId = request != null ? request.getRequestId() : null;
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+ RangerUserInfo user = request != null ? request.getUser() : null;
+ List<RangerAccessInfo> accesses = request != null ? request.getAccesses() : null;
+
+ LOG.debug("==> authorizeMulti(requestId={}, caller={}, serviceName={})", requestId, caller, serviceName);
+
+ ret = validateCaller(caller, user, accesses, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
+ RangerMultiAuthzResult result = authorizer.authorize(request);
+
+ ret = Response.ok(result).build();
+ } catch (RangerAuthzException e) {
+ LOG.warn("authorizeMulti(requestId={}): authorization error; caller={}", requestId, caller, e);
+
+ ret = badRequest(e);
+ } catch (Exception e) {
+ LOG.error("authorizeMulti(requestId={}): internal error; caller={}", requestId, caller, e);
+
+ ret = serverError(e);
+ }
+ }
+
+ LOG.debug("<== authorizeMulti(requestId={}, caller={}, serviceName={}): ret={}", requestId, caller, serviceName, ret != null ? ret.getStatus() : null);
+ } finally {
+ recordRequestMetrics(ret, startNanos, httpRequest);
+ }
+
+ return ret;
+ }
+
+ /**
+ * Returns the effective permissions for a resource, broken down by user/group/role.
+ *
+ * @param request wrapper containing the resource info and access context
+ * @return {@code 200 OK} with {@link RangerResourcePermissions}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/permissions")
+ public Response getResourcePermissions(RangerResourcePermissionsRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+
+ LOG.debug("==> getResourcePermissions(caller={}, serviceName={})", caller, serviceName);
+
+ ret = validateCaller(caller, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
+ RangerResourcePermissions result = authorizer.getResourcePermissions(request);
+
+ ret = Response.ok(result).build();
+ } catch (RangerAuthzException e) {
+ LOG.warn("getResourcePermissions(): validation error; caller={}", caller, e);
+
+ ret = badRequest(e);
+ } catch (Exception e) {
+ LOG.error("getResourcePermissions(): unexpected error; caller={}", caller, e);
+
+ ret = serverError(e);
+ }
+ }
+
+ LOG.debug("==> getResourcePermissions(caller={}, serviceName={}): ret={}", caller, serviceName, ret != null ? ret.getStatus() : null);
+ } finally {
+ recordRequestMetrics(ret, startNanos, httpRequest);
+ }
+
+ return ret;
+ }
+
+ private String getAuthenticatedUser(HttpServletRequest httpRequest) {
+ Object user = httpRequest.getAttribute(RangerPdpConstants.ATTR_AUTHENTICATED_USER);
+
+ return user != null ? user.toString() : null;
+ }
+
+ private static String getServiceName(RangerAuthzRequest request) {
+ return request != null && request.getContext() != null ? request.getContext().getServiceName() : null;
+ }
+
+ private static String getServiceName(RangerMultiAuthzRequest request) {
+ return request != null && request.getContext() != null ? request.getContext().getServiceName() : null;
+ }
+
+ private static String getServiceName(RangerResourcePermissionsRequest request) {
+ return request != null && request.getContext() != null ? request.getContext().getServiceName() : null;
+ }
+
+ private Response validateCaller(String caller, RangerUserInfo user, RangerAccessInfo access, String serviceName) {
+ final Response ret;
+
+ if (StringUtils.isBlank(caller)) {
+ ret = Response.status(Response.Status.UNAUTHORIZED)
+ .entity(new ErrorResponse(Response.Status.UNAUTHORIZED, "Authentication required"))
+ .build();
+ } else {
+ boolean needsDelegation = isDelegationNeeded(caller, user) || isDelegationNeeded(access);
+
+ if (needsDelegation) {
+ if (!isDelegationUserForService(serviceName, caller)) {
+ LOG.info("{} is not a delegation user in service {}", caller, serviceName);
+
+ ret = Response.status(Response.Status.FORBIDDEN)
+ .entity(new ErrorResponse(Response.Status.FORBIDDEN, caller + " is not authorized"))
+ .build();
+ } else {
+ ret = RESPONSE_OK;
+ }
+ } else {
+ ret = RESPONSE_OK;
+ }
+ }
+
+ return ret;
+ }
+
+ private Response validateCaller(String caller, RangerUserInfo user, List<RangerAccessInfo> accesses, String serviceName) {
+ final Response ret;
+
+ if (StringUtils.isBlank(caller)) {
+ ret = Response.status(Response.Status.UNAUTHORIZED)
+ .entity(new ErrorResponse(Response.Status.UNAUTHORIZED, "Authentication required"))
+ .build();
+ } else {
+ boolean needsDelegation = isDelegationNeeded(caller, user) || isDelegationNeeded(accesses);
+
+ if (needsDelegation) {
+ if (!isDelegationUserForService(serviceName, caller)) {
+ LOG.info("{} is not a delegation user in service {}", caller, serviceName);
+
+ ret = Response.status(Response.Status.FORBIDDEN)
+ .entity(new ErrorResponse(Response.Status.FORBIDDEN, caller + " is not authorized"))
+ .build();
+ } else {
+ ret = RESPONSE_OK;
+ }
+ } else {
+ ret = RESPONSE_OK;
+ }
+ }
+
+ return ret;
+ }
+
+ private Response validateCaller(String caller, String serviceName) {
+ final Response ret;
+
+ if (StringUtils.isBlank(caller)) {
+ ret = Response.status(Response.Status.UNAUTHORIZED)
+ .entity(new ErrorResponse(Response.Status.UNAUTHORIZED, "Authentication required"))
+ .build();
+ } else if (!isDelegationUserForService(serviceName, caller)) {
+ LOG.info("{} is not a delegation user in service {}", caller, serviceName);
+
+ ret = Response.status(Response.Status.FORBIDDEN)
+ .entity(new ErrorResponse(Response.Status.FORBIDDEN, caller + " is not authorized"))
+ .build();
+ } else {
+ ret = RESPONSE_OK;
+ }
+
+ return ret;
+ }
+
+ private boolean isDelegationNeeded(String caller, RangerUserInfo user) {
+ String userName = user != null ? user.getName() : null;
+ boolean needsDelegation = !caller.equals(userName);
+
+ if (!needsDelegation) {
+ // don't trust user-attributes/groups/roles if caller doesn't have delegation permission
+ needsDelegation = MapUtils.isNotEmpty(user.getAttributes()) || CollectionUtils.isNotEmpty(user.getGroups()) || CollectionUtils.isNotEmpty(user.getRoles());
+ }
+
+ return needsDelegation;
+ }
+
+ private boolean isDelegationNeeded(RangerAccessInfo access) {
+ RangerResourceInfo resource = access != null ? access.getResource() : null;
+
+ // delegation permission is needed when resource attributes are specified
+ return (resource != null && MapUtils.isNotEmpty(resource.getAttributes()));
+ }
+
+ private boolean isDelegationNeeded(List<RangerAccessInfo> accesses) {
+ if (accesses != null) {
+ for (RangerAccessInfo access : accesses) {
+ if (isDelegationNeeded(access)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ private RangerPdpStats getRuntimeState(HttpServletRequest httpRequest) {
+ Object state = httpRequest.getServletContext().getAttribute(RangerPdpConstants.SERVLET_CTX_ATTR_RUNTIME_STATE);
+
+ if (state instanceof RangerPdpStats) {
+ return (RangerPdpStats) state;
+ } else {
+ Object fallback = servletContext != null ? servletContext.getAttribute(RangerPdpConstants.SERVLET_CTX_ATTR_RUNTIME_STATE) : null;
+
+ return (fallback instanceof RangerPdpStats) ? (RangerPdpStats) fallback : new RangerPdpStats();
+ }
+ }
+
+ private void recordRequestMetrics(Response ret, long startNanos, HttpServletRequest httpRequest) {
+ RangerPdpStats state = getRuntimeState(httpRequest);
+ int status = ret != null ? ret.getStatus() : 500;
+ long elapsed = System.nanoTime() - startNanos;
+
+ if (status >= 200 && status < 300) {
+ state.recordRequestSuccess(elapsed);
+ } else if (status == 401 || status == 403) { // UNAUTHORIZED or FORBIDDEN
+ state.recordAuthFailure();
+ } else if (status == 400) {
+ state.recordRequestBadRequest(elapsed);
+ } else {
+ state.recordRequestError(elapsed);
+ }
+ }
+
+ /**
+ * Allowed-users config format:
+ * ranger.pdp.service.<serviceName>.allowed.users=user1,user2
+ * ranger.pdp.service.*.allowed.users=user3,user4
+ *
+ * If no allowed-users entries are configured, this check is disabled
+ * (backward-compatible behavior).
+ */
+ private boolean isDelegationUserForService(String serviceName, String userName) {
+ boolean ret;
+ Map<String, Set<String>> delegationUsersByService = this.delegationUsersByService;
+
+ if (delegationUsersByService.isEmpty()) {
+ ret = false;
+ } else if (StringUtils.isBlank(serviceName) || StringUtils.isBlank(userName)) {
+ ret = false;
+ } else {
Review Comment:
`isDelegationUserForService()` returns `false` when `delegationUsersByService` is empty, but the preceding comment says the check should be disabled when no entries are configured. This causes `validateCaller(..., serviceName)` (used by `/permissions`) to always return 403 when no config is present, and also blocks delegation in other endpoints contrary to the documented behavior. Consider treating an empty map as "no restriction" (return `true`) or update the comment + validation logic to match the intended default behavior.
--
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: dev-unsubscribe@ranger.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] RANGER-5371: pdp server implementation [ranger]
Posted by "mneethiraj (via GitHub)" <gi...@apache.org>.
mneethiraj commented on PR #896:
URL: https://github.com/apache/ranger/pull/896#issuecomment-4151792172
Will create a new PR for this feature.
--
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: dev-unsubscribe@ranger.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] RANGER-5371: pdp server implementation [ranger]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #896:
URL: https://github.com/apache/ranger/pull/896#discussion_r3004611903
##########
pdp/src/main/java/org/apache/ranger/pdp/security/RangerPdpAuthFilter.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.commons.lang3.StringUtils;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * Servlet filter that enforces authentication for all PDP REST endpoints.
+ *
+ * <p>Handlers are configured via the {@code ranger.pdp.auth.types} filter init parameter
+ * (comma-separated list of {@code header}, {@code jwt}, {@code kerberos}). Handlers are
+ * tried in the listed order; the first successful match wins.
+ *
+ * <p>On success the authenticated username is stored in the request attribute
+ * {@link RangerPdpConstants#ATTR_AUTHENTICATED_USER} so that REST resources can read it.
+ *
+ * <p>If all handlers return {@code SKIP} (no recognisable credentials found), the filter
+ * sends a {@code 401} response with {@code WWW-Authenticate} headers for every
+ * configured handler that provides a challenge.
+ */
+public class RangerPdpAuthFilter implements Filter {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpAuthFilter.class);
+
+ // Auth type list — pdp-specific
+ public static final String PARAM_AUTH_TYPES = RangerPdpConstants.PROP_AUTH_TYPES;
+
+ // HTTP Header auth — consistent with ranger.admin.authn.header.* in security-admin
+ public static final String PARAM_HEADER_AUTHN_ENABLED = RangerPdpConstants.PROP_HEADER_AUTHN_ENABLED;
+ public static final String PARAM_HEADER_AUTHN_USERNAME = RangerPdpConstants.PROP_HEADER_AUTHN_USERNAME;
+
+ // JWT bearer token auth
+ public static final String PARAM_JWT_PROVIDER_URL = RangerPdpConstants.PROP_JWT_PROVIDER_URL;
+ public static final String PARAM_JWT_PUBLIC_KEY = RangerPdpConstants.PROP_JWT_PUBLIC_KEY;
+ public static final String PARAM_JWT_COOKIE_NAME = RangerPdpConstants.PROP_JWT_COOKIE_NAME;
+ public static final String PARAM_JWT_AUDIENCES = RangerPdpConstants.PROP_JWT_AUDIENCES;
+
+ // Kerberos / SPNEGO
+ public static final String PARAM_SPNEGO_PRINCIPAL = RangerPdpConstants.PROP_SPNEGO_PRINCIPAL;
+ public static final String PARAM_SPNEGO_KEYTAB = RangerPdpConstants.PROP_SPNEGO_KEYTAB;
+ public static final String PARAM_KRB_NAME_RULES = RangerPdpConstants.PROP_KRB_NAME_RULES;
+ public static final String PARAM_KRB_TOKEN_VALIDITY = RangerPdpConstants.PROP_KRB_TOKEN_VALIDITY;
+ public static final String PARAM_KRB_COOKIE_DOMAIN = RangerPdpConstants.PROP_KRB_COOKIE_DOMAIN;
+ public static final String PARAM_KRB_COOKIE_PATH = RangerPdpConstants.PROP_KRB_COOKIE_PATH;
+
+ private final List<PdpAuthHandler> handlers = new ArrayList<>();
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ Properties config = toProperties(filterConfig);
+ String types = filterConfig.getInitParameter(PARAM_AUTH_TYPES);
+ boolean headerAuthEnabled = Boolean.parseBoolean(StringUtils.defaultIfBlank(filterConfig.getInitParameter(PARAM_HEADER_AUTHN_ENABLED), "false"));
+
+ if (StringUtils.isBlank(types)) {
+ types = "header,jwt,kerberos";
+ }
+
+ for (String type : types.split(",")) {
+ String normalizedType = type.trim().toLowerCase();
+
+ if ("header".equals(normalizedType) && !headerAuthEnabled) {
+ LOG.info("Header auth type is configured but disabled via {}=false; skipping handler registration", PARAM_HEADER_AUTHN_ENABLED);
+ continue;
+ }
+
+ PdpAuthHandler handler = createHandler(normalizedType);
+
+ if (handler != null) {
+ try {
+ handler.init(config);
+ handlers.add(handler);
+
+ LOG.info("Registered auth handler: {}", normalizedType);
+ } catch (Exception e) {
+ throw new ServletException("Failed to initialize auth handler: " + type, e);
+ }
+ } else {
+ LOG.warn("Unknown auth type ignored: {}", type.trim());
+ }
+ }
Review Comment:
`init()` fails the entire filter if any configured handler throws during initialization. With the shipped defaults (`ranger.pdp.auth.types=jwt,kerberos` and empty Kerberos principal/keytab), `KerberosAuthHandler.init()` will throw and the PDP server won't start even though JWT could work. Consider treating missing/invalid per-handler config as “handler disabled” (log + continue) and only fail if *no* handlers can be registered, or adjust defaults so Kerberos isn't enabled unless configured.
##########
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.auth.types</name>
+ <value>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>
+ Enable trusted HTTP header authentication. Use only behind a trusted proxy.
+ </description>
+ </property>
+
+ <property>
+ <name>ranger.pdp.authn.header.username</name>
+ <value>X-Forwarded-User</value>
+ <description>HTTP header name from which the authenticated username is read.</description>
+ </property>
+
+ <!-- JWT bearer token authentication for incoming REST requests -->
+ <property>
+ <name>ranger.pdp.jwt.provider.url</name>
+ <value/>
+ <description>URL of the JWT provider (used to fetch the public key).</description>
+ </property>
+
+ <property>
+ <name>ranger.pdp.jwt.public.key</name>
+ <value/>
+ <description>Base64-encoded public key for verifying JWT signatures.</description>
+ </property>
Review Comment:
The description for `ranger.pdp.jwt.public.key` says "Base64-encoded public key", but the underlying Ranger JWT handler expects a PEM/X.509 formatted value (see `RangerJwtAuthHandler` using `X509CertUtils.parse(pemPublicKey)`). Please align this description with the actual expected format to avoid misconfiguration.
##########
pdp/src/main/java/org/apache/ranger/pdp/rest/RangerPdpREST.java:
##########
@@ -0,0 +1,494 @@
+/*
+ * 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.rest;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.MapUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.ranger.authz.api.RangerAuthorizer;
+import org.apache.ranger.authz.api.RangerAuthzException;
+import org.apache.ranger.authz.model.RangerAccessInfo;
+import org.apache.ranger.authz.model.RangerAuthzRequest;
+import org.apache.ranger.authz.model.RangerAuthzResult;
+import org.apache.ranger.authz.model.RangerMultiAuthzRequest;
+import org.apache.ranger.authz.model.RangerMultiAuthzResult;
+import org.apache.ranger.authz.model.RangerResourceInfo;
+import org.apache.ranger.authz.model.RangerResourcePermissions;
+import org.apache.ranger.authz.model.RangerResourcePermissionsRequest;
+import org.apache.ranger.authz.model.RangerUserInfo;
+import org.apache.ranger.pdp.RangerPdpStats;
+import org.apache.ranger.pdp.config.RangerPdpConfig;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_PDP_SERVICE_PREFIX;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_SUFFIX_DELEGATION_USERS;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.WILDCARD_SERVICE_NAME;
+
+/**
+ * REST resource that exposes the three core {@link RangerAuthorizer} methods over HTTP.
+ *
+ * <p>All endpoints are under {@code /authz/v1} and produce/consume {@code application/json}.
+ * Authentication is enforced upstream by {@link RangerPdpAuthFilter}; the authenticated
+ * caller's identity is read from the {@link RangerPdpConstants#ATTR_AUTHENTICATED_USER}
+ * request attribute.
+ *
+ * <table border="1">
+ * <tr><th>Method</th><th>Path</th><th>Request body</th><th>Response body</th></tr>
+ * <tr><td>POST</td><td>/authz/v1/authorize</td>
+ * <td>{@link RangerAuthzRequest}</td><td>{@link RangerAuthzResult}</td></tr>
+ * <tr><td>POST</td><td>/authz/v1/authorizeMulti</td>
+ * <td>{@link RangerMultiAuthzRequest}</td><td>{@link RangerMultiAuthzResult}</td></tr>
+ * <tr><td>POST</td><td>/authz/v1/permissions</td>
+ * <td>{@link RangerResourcePermissionsRequest}</td>
+ * <td>{@link RangerResourcePermissions}</td></tr>
+ * </table>
+ */
+@Path("/v1")
+@Produces(MediaType.APPLICATION_JSON)
+@Consumes(MediaType.APPLICATION_JSON)
+@Singleton
+public class RangerPdpREST {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpREST.class);
+
+ private static final Response RESPONSE_OK = Response.ok().build();
+
+ private final Map<String, Set<String>> delegationUsersByService = new HashMap<>();
+
+ @Inject
+ private RangerAuthorizer authorizer;
+
+ @Inject
+ private RangerPdpConfig config;
+
+ @Context
+ private ServletContext servletContext;
+
+ @PostConstruct
+ public void initialize() {
+ initializeDelegationUsers();
+ }
+
+ /**
+ * Evaluates a single access request.
+ *
+ * @param request the authorization request
+ * @return {@code 200 OK} with {@link RangerAuthzResult}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/authorize")
+ public Response authorize(RangerAuthzRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String requestId = request != null ? request.getRequestId() : null;
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+ RangerUserInfo user = request != null ? request.getUser() : null;
+ RangerAccessInfo access = request != null ? request.getAccess() : null;
+
+ LOG.debug("==> authorize(requestId={}, caller={}, serviceName={})", requestId, caller, serviceName);
+
+ ret = validateCaller(caller, user, access, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
+ RangerAuthzResult result = authorizer.authorize(request);
+
+ ret = Response.ok(result).build();
+ } catch (RangerAuthzException e) {
+ LOG.warn("authorize(requestId={}): authorization error; caller={}", requestId, caller, e);
+
+ ret = badRequest(e);
+ } catch (Exception e) {
+ LOG.error("authorize(requestId={}): internal error; caller={}", requestId, caller, e);
+
+ ret = serverError(e);
+ }
+ }
+
+ LOG.debug("<== authorize(requestId={}, caller={}, serviceName={}): ret={}", requestId, caller, serviceName, ret != null ? ret.getStatus() : null);
+ } finally {
+ recordRequestMetrics(ret, startNanos, httpRequest);
+ }
+
+ return ret;
+ }
+
+ /**
+ * Evaluates multiple access requests in a single call.
+ *
+ * @param request the multi-access authorization request
+ * @return {@code 200 OK} with {@link RangerMultiAuthzResult}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/authorizeMulti")
+ public Response authorizeMulti(RangerMultiAuthzRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String requestId = request != null ? request.getRequestId() : null;
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+ RangerUserInfo user = request != null ? request.getUser() : null;
+ List<RangerAccessInfo> accesses = request != null ? request.getAccesses() : null;
+
+ LOG.debug("==> authorizeMulti(requestId={}, caller={}, serviceName={})", requestId, caller, serviceName);
+
+ ret = validateCaller(caller, user, accesses, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
+ RangerMultiAuthzResult result = authorizer.authorize(request);
+
+ ret = Response.ok(result).build();
+ } catch (RangerAuthzException e) {
+ LOG.warn("authorizeMulti(requestId={}): authorization error; caller={}", requestId, caller, e);
+
+ ret = badRequest(e);
+ } catch (Exception e) {
+ LOG.error("authorizeMulti(requestId={}): internal error; caller={}", requestId, caller, e);
+
+ ret = serverError(e);
+ }
+ }
+
+ LOG.debug("<== authorizeMulti(requestId={}, caller={}, serviceName={}): ret={}", requestId, caller, serviceName, ret != null ? ret.getStatus() : null);
+ } finally {
+ recordRequestMetrics(ret, startNanos, httpRequest);
+ }
+
+ return ret;
+ }
+
+ /**
+ * Returns the effective permissions for a resource, broken down by user/group/role.
+ *
+ * @param request wrapper containing the resource info and access context
+ * @return {@code 200 OK} with {@link RangerResourcePermissions}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/permissions")
+ public Response getResourcePermissions(RangerResourcePermissionsRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+
+ LOG.debug("==> getResourcePermissions(caller={}, serviceName={})", caller, serviceName);
+
+ ret = validateCaller(caller, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
+ RangerResourcePermissions result = authorizer.getResourcePermissions(request);
+
+ ret = Response.ok(result).build();
+ } catch (RangerAuthzException e) {
+ LOG.warn("getResourcePermissions(): validation error; caller={}", caller, e);
+
+ ret = badRequest(e);
+ } catch (Exception e) {
+ LOG.error("getResourcePermissions(): unexpected error; caller={}", caller, e);
+
+ ret = serverError(e);
+ }
+ }
+
+ LOG.debug("<== getResourcePermissions(caller={}, serviceName={}): ret={}", caller, serviceName, ret != null ? ret.getStatus() : null);
+ } finally {
+ recordRequestMetrics(ret, startNanos, httpRequest);
+ }
+
+ return ret;
+ }
+
+ private String getAuthenticatedUser(HttpServletRequest httpRequest) {
+ Object user = httpRequest.getAttribute(RangerPdpConstants.ATTR_AUTHENTICATED_USER);
+
+ return user != null ? user.toString() : null;
+ }
+
+ private static String getServiceName(RangerAuthzRequest request) {
+ return request != null && request.getContext() != null ? request.getContext().getServiceName() : null;
+ }
+
+ private static String getServiceName(RangerMultiAuthzRequest request) {
+ return request != null && request.getContext() != null ? request.getContext().getServiceName() : null;
+ }
+
+ private static String getServiceName(RangerResourcePermissionsRequest request) {
+ return request != null && request.getContext() != null ? request.getContext().getServiceName() : null;
+ }
+
+ private Response validateCaller(String caller, RangerUserInfo user, RangerAccessInfo access, String serviceName) {
+ final Response ret;
+
+ if (StringUtils.isBlank(caller)) {
+ ret = Response.status(Response.Status.UNAUTHORIZED)
+ .entity(new ErrorResponse(Response.Status.UNAUTHORIZED, "Authentication required"))
+ .build();
+ } else {
+ boolean needsDelegation = isDelegationNeeded(caller, user) || isDelegationNeeded(access);
+
+ if (needsDelegation) {
+ if (!isDelegationUserForService(serviceName, caller)) {
+ LOG.info("{} is not a delegation user in service {}", caller, serviceName);
+
+ ret = Response.status(Response.Status.FORBIDDEN)
+ .entity(new ErrorResponse(Response.Status.FORBIDDEN, caller + " is not authorized"))
+ .build();
+ } else {
+ ret = RESPONSE_OK;
+ }
+ } else {
+ ret = RESPONSE_OK;
+ }
+ }
+
+ return ret;
+ }
+
+ private Response validateCaller(String caller, RangerUserInfo user, List<RangerAccessInfo> accesses, String serviceName) {
+ final Response ret;
+
+ if (StringUtils.isBlank(caller)) {
+ ret = Response.status(Response.Status.UNAUTHORIZED)
+ .entity(new ErrorResponse(Response.Status.UNAUTHORIZED, "Authentication required"))
+ .build();
+ } else {
+ boolean needsDelegation = isDelegationNeeded(caller, user) || isDelegationNeeded(accesses);
+
+ if (needsDelegation) {
+ if (!isDelegationUserForService(serviceName, caller)) {
+ LOG.info("{} is not a delegation user in service {}", caller, serviceName);
+
+ ret = Response.status(Response.Status.FORBIDDEN)
+ .entity(new ErrorResponse(Response.Status.FORBIDDEN, caller + " is not authorized"))
+ .build();
+ } else {
+ ret = RESPONSE_OK;
+ }
+ } else {
+ ret = RESPONSE_OK;
+ }
+ }
+
+ return ret;
+ }
+
+ private Response validateCaller(String caller, String serviceName) {
+ final Response ret;
+
+ if (StringUtils.isBlank(caller)) {
+ ret = Response.status(Response.Status.UNAUTHORIZED)
+ .entity(new ErrorResponse(Response.Status.UNAUTHORIZED, "Authentication required"))
+ .build();
+ } else if (!isDelegationUserForService(serviceName, caller)) {
+ LOG.info("{} is not a delegation user in service {}", caller, serviceName);
+
+ ret = Response.status(Response.Status.FORBIDDEN)
+ .entity(new ErrorResponse(Response.Status.FORBIDDEN, caller + " is not authorized"))
+ .build();
+ } else {
+ ret = RESPONSE_OK;
+ }
+
+ return ret;
+ }
+
+ private boolean isDelegationNeeded(String caller, RangerUserInfo user) {
+ String userName = user != null ? user.getName() : null;
+ boolean needsDelegation = !caller.equals(userName);
+
+ if (!needsDelegation) {
+ // don't trust user-attributes/groups/roles if caller doesn't have delegation permission
+ needsDelegation = MapUtils.isNotEmpty(user.getAttributes()) || CollectionUtils.isNotEmpty(user.getGroups()) || CollectionUtils.isNotEmpty(user.getRoles());
+ }
+
+ return needsDelegation;
+ }
+
+ private boolean isDelegationNeeded(RangerAccessInfo access) {
+ RangerResourceInfo resource = access != null ? access.getResource() : null;
+
+ // delegation permission is needed when resource attributes are specified
+ return (resource != null && MapUtils.isNotEmpty(resource.getAttributes()));
+ }
+
+ private boolean isDelegationNeeded(List<RangerAccessInfo> accesses) {
+ if (accesses != null) {
+ for (RangerAccessInfo access : accesses) {
+ if (isDelegationNeeded(access)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ private RangerPdpStats getRuntimeState(HttpServletRequest httpRequest) {
+ Object state = httpRequest.getServletContext().getAttribute(RangerPdpConstants.SERVLET_CTX_ATTR_RUNTIME_STATE);
+
+ if (state instanceof RangerPdpStats) {
+ return (RangerPdpStats) state;
+ } else {
+ Object fallback = servletContext != null ? servletContext.getAttribute(RangerPdpConstants.SERVLET_CTX_ATTR_RUNTIME_STATE) : null;
+
+ return (fallback instanceof RangerPdpStats) ? (RangerPdpStats) fallback : new RangerPdpStats();
+ }
+ }
+
+ private void recordRequestMetrics(Response ret, long startNanos, HttpServletRequest httpRequest) {
+ RangerPdpStats state = getRuntimeState(httpRequest);
+ int status = ret != null ? ret.getStatus() : 500;
+ long elapsed = System.nanoTime() - startNanos;
+
+ if (status >= 200 && status < 300) {
+ state.recordRequestSuccess(elapsed);
+ } else if (status == 401 || status == 403) { // UNAUTHORIZED or FORBIDDEN
+ state.recordAuthFailure();
+ } else if (status == 400) {
+ state.recordRequestBadRequest(elapsed);
+ } else {
+ state.recordRequestError(elapsed);
+ }
+ }
+
+ private boolean isDelegationUserForService(String serviceName, String userName) {
+ final boolean ret;
+ Map<String, Set<String>> delegationUsersByService = this.delegationUsersByService;
+
+ if (delegationUsersByService.isEmpty()) {
+ ret = false;
+ } else if (StringUtils.isBlank(serviceName) || StringUtils.isBlank(userName)) {
+ ret = false;
+ } else {
+ Set<String> delegationUsers = delegationUsersByService.get(serviceName);
+
+ if (delegationUsers == null) {
+ delegationUsers = delegationUsersByService.get(RangerPdpConstants.WILDCARD_SERVICE_NAME);
+ }
+
+ ret = delegationUsers != null && delegationUsers.contains(userName);
+ }
+
+ LOG.debug("isDelegationUserForService(serviceName={}, userName={}): ret={}", serviceName, userName, ret);
+
+ return ret;
+ }
+
+ private void initializeDelegationUsers() {
+ Properties properties = config != null ? config.getAuthzProperties() : new Properties();
+
+ for (String key : properties.stringPropertyNames()) {
+ if (key.startsWith(PROP_PDP_SERVICE_PREFIX) && key.endsWith(PROP_SUFFIX_DELEGATION_USERS)) {
+ String serviceName = key.substring(PROP_PDP_SERVICE_PREFIX.length(), key.length() - PROP_SUFFIX_DELEGATION_USERS.length());
+
+ if (StringUtils.isBlank(serviceName)) {
+ continue;
+ }
+
+ Set<String> delegationUsers = Arrays.stream(StringUtils.defaultString(properties.getProperty(key)).split(","))
+ .map(String::trim)
+ .filter(StringUtils::isNotBlank)
+ .collect(Collectors.toSet());
+
+ if (!delegationUsers.isEmpty()) {
+ delegationUsersByService.put(serviceName, delegationUsers);
+
+ LOG.info("Delegation users for service '{}': {}", serviceName, delegationUsers);
+ }
+ }
+ }
+
+ if (delegationUsersByService.isEmpty()) {
+ LOG.warn("No delegation users configured");
+ } else {
+ Set<String> wildcardDelegationUsers = delegationUsersByService.get(WILDCARD_SERVICE_NAME);
+
+ // delegation users for WILDCARD_SERVICE_NAME are delegates for all services
+ if (wildcardDelegationUsers != null && !wildcardDelegationUsers.isEmpty()) {
+ delegationUsersByService.forEach((k, v) -> v.addAll(wildcardDelegationUsers));
+ }
+ }
+ }
+
+ private Response badRequest(RangerAuthzException e) {
+ return Response.status(Response.Status.BAD_REQUEST)
+ .entity(new ErrorResponse(Response.Status.BAD_REQUEST, e.getMessage()))
+ .build();
+ }
+
+ private Response serverError(Exception e) {
+ return Response.serverError()
+ .entity(new ErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, e.getMessage()))
+ .build();
Review Comment:
`serverError()` returns `e.getMessage()` to the client. For unexpected 500s this can leak internal details (class names, paths, config, etc.) and is also unstable as an API surface. Consider returning a generic message (optionally including a requestId) and keep the full exception details only in server logs.
##########
pdp/conf.dist/ranger-pdp-site.xml:
##########
@@ -0,0 +1,365 @@
+<?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.
+-->
+<!-- Copy this file to conf/ranger-pdp-site.xml and edit to override defaults. -->
+<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.auth.types</name>
+ <value>jwt,kerberos</value>
+ <description>
+ Comma-separated list of authentication methods for incoming REST requests,
+ tried in listed order. Supported values: header, jwt, kerberos.
Review Comment:
Same as `ranger-pdp-default.xml`: `ranger.pdp.auth.types` defaults to `jwt,kerberos` while Kerberos principal/keytab are empty in this template. Unless Kerberos is always configured, this default will cause startup failures. Consider changing the template default to a working combination (e.g., `jwt`) or adding a prominent note next to the property.
```suggestion
<value>jwt</value>
<description>
Comma-separated list of authentication methods for incoming REST requests,
tried in listed order. Supported values: header, jwt, kerberos.
Add 'kerberos' here only after configuring the required Kerberos principal and keytab.
```
##########
pdp/scripts/ranger-pdp-services.sh:
##########
@@ -0,0 +1,223 @@
+#!/bin/bash
+
+# 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.
+
+if [[ -z $1 ]]; then
+ echo "No argument provided."
+ echo "Usage: $0 {start | run | stop | restart | version}"
+ exit 1
+fi
+
+action=$1
+action=$(echo "$action" | tr '[:lower:]' '[:upper:]')
+
+realScriptPath=$(readlink -f "$0")
+realScriptDir=$(dirname "$realScriptPath")
+cd "$realScriptDir" || exit 1
+cdir=$(pwd)
+
+ranger_pdp_max_heap_size=${RANGER_PDP_MAX_HEAP_SIZE:-1g}
+
+for custom_env_script in $(find "${cdir}/conf/" -name "ranger-pdp-env*" 2>/dev/null); do
+ if [ -f "$custom_env_script" ]; then
+ . "$custom_env_script"
+ fi
+done
+
+if [ -z "${RANGER_PDP_PID_DIR_PATH}" ]; then
+ RANGER_PDP_PID_DIR_PATH=/var/run/ranger
+fi
+
+if [ -z "${RANGER_PDP_PID_NAME}" ]; then
+ RANGER_PDP_PID_NAME=pdp.pid
+fi
+
+pidf="${RANGER_PDP_PID_DIR_PATH}/${RANGER_PDP_PID_NAME}"
+
+if [ -z "${UNIX_PDP_USER}" ]; then
+ UNIX_PDP_USER=ranger
+fi
+
+JAVA_OPTS=" ${JAVA_OPTS} -XX:MetaspaceSize=100m -XX:MaxMetaspaceSize=200m -Xmx${ranger_pdp_max_heap_size} -Xms256m"
+
+if [ "${action}" == "START" ]; then
+
+ if [ -f "${cdir}/conf/java_home.sh" ]; then
+ . "${cdir}/conf/java_home.sh"
+ fi
+
+ for custom_env_script in $(find "${cdir}/conf.dist/" -name "ranger-pdp-env*" 2>/dev/null); do
+ if [ -f "$custom_env_script" ]; then
+ . "$custom_env_script"
+ fi
+ done
+
+ if [ "$JAVA_HOME" != "" ]; then
+ export PATH=$JAVA_HOME/bin:$PATH
+ fi
+
+ if [ -z "${RANGER_PDP_LOG_DIR}" ]; then
+ RANGER_PDP_LOG_DIR=/var/log/ranger/pdp
+ fi
+
+ if [ ! -d "$RANGER_PDP_LOG_DIR" ]; then
+ mkdir -p "$RANGER_PDP_LOG_DIR"
+ chmod 777 "$RANGER_PDP_LOG_DIR"
Review Comment:
Creating the log directory with `chmod 777` makes it world-writable, which is risky on multi-tenant hosts. Consider using a more restrictive permission (e.g., 750/755) and ensuring ownership is set appropriately instead of granting write access to all users.
```suggestion
if [ -n "${UNIX_PDP_USER}" ]; then
chown "${UNIX_PDP_USER}":"${UNIX_PDP_USER}" "$RANGER_PDP_LOG_DIR" 2>/dev/null || true
chmod 750 "$RANGER_PDP_LOG_DIR"
else
chmod 755 "$RANGER_PDP_LOG_DIR"
fi
```
##########
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.auth.types</name>
+ <value>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>
Review Comment:
The default `ranger.pdp.auth.types` includes `kerberos`, but the default Kerberos principal/keytab values are empty. With the current auth filter behavior this will make the server fail to start unless Kerberos is configured. Consider defaulting this to `jwt` (or `jwt,header`), or document clearly that Kerberos must be removed unless configured.
##########
pdp/src/main/java/org/apache/ranger/pdp/config/RangerPdpConfig.java:
##########
@@ -0,0 +1,328 @@
+/*
+ * 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.config;
+
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import javax.xml.XMLConstants;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+/**
+ * Reads Ranger PDP configuration from {@code ranger-pdp-default.xml} (classpath)
+ * overridden by {@code ranger-pdp-site.xml} (classpath or filesystem).
+ *
+ * <p>Both files use the Hadoop {@code <configuration>} XML format, consistent
+ * with other Ranger server modules (tagsync, kms, etc.).
+ * The format is parsed directly using the JDK DOM API to avoid an early
+ * class-load dependency on Hadoop's {@code Configuration} class.
+ *
+ * <p>Authentication property names:
+ * <ul>
+ * <li>Kerberos/SPNEGO: {@code ranger.pdp.kerberos.spnego.*}
+ * <li>JWT bearer token: {@code ranger.pdp.jwt.*}
+ * <li>HTTP header: {@code ranger.pdp.authn.header.*}
+ * </ul>
+ */
+public class RangerPdpConfig {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpConfig.class);
+
+ private static final String DEFAULT_CONFIG_FILE = "ranger-pdp-default.xml";
+ private static final String SITE_CONFIG_FILE = "ranger-pdp-site.xml";
+
+ private final Properties props;
+
+ public RangerPdpConfig() {
+ props = new Properties();
+
+ loadFromClasspath(DEFAULT_CONFIG_FILE);
+ loadFromClasspath(SITE_CONFIG_FILE);
+
+ String confDir = System.getProperty(RangerPdpConstants.PROP_CONF_DIR, "");
+
+ if (StringUtils.isNotBlank(confDir)) {
+ loadFromFile(new File(confDir, SITE_CONFIG_FILE));
+ }
+
+ applySystemPropertyOverrides();
+
+ LOG.info("RangerPdpConfig initialized (conf.dir={})", confDir);
+ }
+
+ public int getPort() {
+ return getInt(RangerPdpConstants.PROP_PORT, 6500);
+ }
+
+ public String getLogDir() {
+ return get(RangerPdpConstants.PROP_LOG_DIR, "/var/log/ranger/pdp");
+ }
+
+ public boolean isSslEnabled() {
+ return getBoolean(RangerPdpConstants.PROP_SSL_ENABLED, false);
+ }
+
+ public String getKeystoreFile() {
+ return get(RangerPdpConstants.PROP_SSL_KEYSTORE_FILE, "");
+ }
+
+ public String getKeystorePassword() {
+ return get(RangerPdpConstants.PROP_SSL_KEYSTORE_PASSWORD, "");
+ }
+
+ public String getKeystoreType() {
+ return get(RangerPdpConstants.PROP_SSL_KEYSTORE_TYPE, "JKS");
+ }
+
+ public boolean isTruststoreEnabled() {
+ return getBoolean(RangerPdpConstants.PROP_SSL_TRUSTSTORE_ENABLED, false);
+ }
+
+ public String getTruststoreFile() {
+ return get(RangerPdpConstants.PROP_SSL_TRUSTSTORE_FILE, "");
+ }
+
+ public String getTruststorePassword() {
+ return get(RangerPdpConstants.PROP_SSL_TRUSTSTORE_PASSWORD, "");
+ }
+
+ public String getTruststoreType() {
+ return get(RangerPdpConstants.PROP_SSL_TRUSTSTORE_TYPE, "JKS");
+ }
+
+ public boolean isHttp2Enabled() {
+ return getBoolean(RangerPdpConstants.PROP_HTTP2_ENABLED, true);
+ }
+
+ public int getHttpConnectorMaxThreads() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_MAX_THREADS, 200);
+ }
+
+ public int getHttpConnectorMinSpareThreads() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_MIN_SPARE_THREADS, 20);
+ }
+
+ public int getHttpConnectorAcceptCount() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_ACCEPT_COUNT, 100);
+ }
+
+ public int getHttpConnectorMaxConnections() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_MAX_CONNECTIONS, 10000);
+ }
+
+ public String getAuthTypes() {
+ return get(RangerPdpConstants.PROP_AUTH_TYPES, "jwt,kerberos");
+ }
+
+ // --- HTTP Header auth ---
+
+ public boolean isHeaderAuthnEnabled() {
+ return getBoolean(RangerPdpConstants.PROP_HEADER_AUTHN_ENABLED, false);
+ }
+
+ public String getHeaderAuthnUsername() {
+ return get(RangerPdpConstants.PROP_HEADER_AUTHN_USERNAME, "X-Forwarded-User");
+ }
+
+ // --- JWT bearer token auth ---
+
+ public String getJwtProviderUrl() {
+ return get(RangerPdpConstants.PROP_JWT_PROVIDER_URL, "");
+ }
+
+ public String getJwtPublicKey() {
+ return get(RangerPdpConstants.PROP_JWT_PUBLIC_KEY, "");
+ }
+
+ public String getJwtCookieName() {
+ return get(RangerPdpConstants.PROP_JWT_COOKIE_NAME, "hadoop-jwt");
+ }
+
+ public String getJwtAudiences() {
+ return get(RangerPdpConstants.PROP_JWT_AUDIENCES, "");
+ }
+
+ // --- Kerberos / SPNEGO ---
+
+ public String getSpnegoPrincipal() {
+ return get(RangerPdpConstants.PROP_SPNEGO_PRINCIPAL, "");
+ }
+
+ public String getSpnegoKeytab() {
+ return get(RangerPdpConstants.PROP_SPNEGO_KEYTAB, "");
+ }
+
+ public String getKerberosNameRules() {
+ return get(RangerPdpConstants.PROP_KRB_NAME_RULES, "DEFAULT");
+ }
+
+ public int getKerberosTokenValiditySeconds() {
+ return getInt(RangerPdpConstants.PROP_KRB_TOKEN_VALIDITY, 30);
+ }
+
+ public String getKerberosCookieDomain() {
+ return get(RangerPdpConstants.PROP_KRB_COOKIE_DOMAIN, "");
+ }
+
+ public String getKerberosCookiePath() {
+ return get(RangerPdpConstants.PROP_KRB_COOKIE_PATH, "/");
+ }
+
+ /**
+ * Returns all properties for forwarding to {@code RangerEmbeddedAuthorizer}.
+ */
+ public Properties getAuthzProperties() {
+ return new Properties(props);
+ }
+
+ public String get(String key, String defaultValue) {
+ String val = props.getProperty(key);
+
+ return StringUtils.isNotBlank(val) ? val.trim() : defaultValue;
+ }
+
+ public int getInt(String key, int defaultValue) {
+ String val = props.getProperty(key);
+
+ if (StringUtils.isNotBlank(val)) {
+ try {
+ return Integer.parseInt(val.trim());
+ } catch (NumberFormatException e) {
+ LOG.warn("Invalid integer for {}: '{}'; using default {}", key, val, defaultValue);
+ }
+ }
+
+ return defaultValue;
+ }
+
+ public boolean getBoolean(String key, boolean defaultValue) {
+ String val = props.getProperty(key);
+
+ return StringUtils.isNotBlank(val) ? Boolean.parseBoolean(val.trim()) : defaultValue;
+ }
+
+ private void loadFromClasspath(String resourceName) {
+ try (InputStream in = getClass().getClassLoader().getResourceAsStream(resourceName)) {
+ if (in != null) {
+ parseHadoopXml(in, resourceName);
+ } else {
+ LOG.debug("Config resource not found on classpath: {}", resourceName);
+ }
+ } catch (IOException e) {
+ LOG.warn("Failed to close stream for classpath resource: {}", resourceName, e);
+ }
+ }
+
+ private void loadFromFile(File file) {
+ if (!file.exists() || !file.isFile()) {
+ LOG.debug("Config file not found: {}", file);
+ return;
+ }
+
+ try (InputStream in = new FileInputStream(file)) {
+ parseHadoopXml(in, file.getAbsolutePath());
+ } catch (IOException e) {
+ LOG.warn("Failed to read config file: {}", file, e);
+ }
+ }
+
+ /**
+ * Parses a Hadoop-style {@code <configuration>} XML document and merges all
+ * {@code <property>} entries into {@link #props}. Later entries override earlier
+ * ones, matching Hadoop's own override semantics.
+ *
+ * <pre>
+ * {@code
+ * <configuration>
+ * <property>
+ * <name>some.key</name>
+ * <value>some-value</value>
+ * </property>
+ * </configuration>
+ * }
+ * </pre>
+ */
+ private void parseHadoopXml(InputStream in, String source) {
+ try {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+
+ factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
+ factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
+
+ DocumentBuilder builder = factory.newDocumentBuilder();
Review Comment:
The XML parsing hardening here only disallows DOCTYPE; unlike other XML parsers in this repo it doesn't explicitly disable external general/parameter entities. To reduce XXE risk if a config file is ever attacker-controlled, set the standard JAXP features (external-general-entities/external-parameter-entities to false) and disable XInclude/expandEntityReferences, consistent with `common-utils/.../XMLUtils`.
##########
pdp/src/main/java/org/apache/ranger/pdp/security/JwtAuthHandler.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.authz.handler.RangerAuth;
+import org.apache.ranger.authz.handler.jwt.RangerDefaultJwtAuthHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.io.IOException;
+import java.util.Properties;
+
+/**
+ * Authenticates requests using a JWT bearer token.
+ *
+ * <p>Checks for the token in the {@code Authorization: Bearer <token>} header first,
+ * then in the configured JWT cookie. Delegates signature verification and expiry/audience
+ * checks to {@link RangerDefaultJwtAuthHandler} from the {@code ranger-authn} module.
+ *
+ * <p>Configuration keys (all prefixed with {@code ranger.pdp.auth.jwt.}):
+ * <ul>
+ * <li>{@code provider.url} – JWKS endpoint URL (optional if public key is set)
+ * <li>{@code public.key} – PEM-encoded public key (optional if provider URL is set)
+ * <li>{@code cookie.name} – JWT cookie name (default: {@code hadoop-jwt})
+ * <li>{@code audiences} – comma-separated list of accepted audiences (optional)
+ * </ul>
Review Comment:
The Javadoc claims JWT config keys are prefixed with `ranger.pdp.auth.jwt.` but the actual config keys used/forwarded are `ranger.pdp.jwt.*` (see `RangerPdpConstants.PROP_JWT_*`). Please correct the prefix in the documentation to match the real configuration keys.
##########
dev-support/ranger-docker/Dockerfile.ranger-pdp:
##########
@@ -0,0 +1,41 @@
+# 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.
+
+ARG RANGER_BASE_IMAGE=apache/ranger-base
+ARG RANGER_BASE_VERSION=20260123-2-8
+
+FROM ${RANGER_BASE_IMAGE}:${RANGER_BASE_VERSION}
+
+ARG PDP_VERSION
+
+COPY ./dist/ranger-${PDP_VERSION}-pdp.tar.gz /home/ranger/dist/
+COPY ./scripts/pdp/ranger-pdp.sh ${RANGER_SCRIPTS}/
+
+RUN tar xvfz /home/ranger/dist/ranger-${PDP_VERSION}-pdp.tar.gz --directory=${RANGER_HOME} \
+ && ln -s ${RANGER_HOME}/ranger-${PDP_VERSION}-pdp ${RANGER_HOME}/pdp \
+ && rm -f /home/ranger/dist/ranger-${PDP_VERSION}-pdp.tar.gz \
+ && mkdir -p /var/log/ranger/pdp /etc/ranger/cache /etc/init.d /etc/rc2.d /etc/rc3.d \
+ && touch /etc/init.d/ranger-pdp \
+ && ln -s /etc/init.d/ranger-pdp /etc/rc2.d/S99ranger-pdp \
+ && ln -s /etc/init.d/ranger-pdp /etc/rc2.d/K00ranger-pdp \
+ && ln -s /etc/init.d/ranger-pdp /etc/rc3.d/S99ranger-pdp \
+ && ln -s /etc/init.d/ranger-pdp /etc/rc3.d/K00ranger-pdp \
Review Comment:
The Dockerfile creates an empty `/etc/init.d/ranger-pdp` and then adds runlevel symlinks to it, but doesn't populate it with the actual init wrapper script (`ranger-pdp`). This can be confusing and may break environments that rely on init scripts. Consider copying the real init script into `/etc/init.d/ranger-pdp` (or dropping the runlevel links entirely if not needed in the container).
```suggestion
```
--
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: dev-unsubscribe@ranger.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] RANGER-5371: pdp server implementation [ranger]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #896:
URL: https://github.com/apache/ranger/pull/896#discussion_r3006586074
##########
pdp/src/main/java/org/apache/ranger/pdp/RangerPdpStatusServlet.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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 javax.ws.rs.core.MediaType;
+
+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(MediaType.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(MediaType.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');
+ sb.append("# TYPE ranger_pdp_requests_success_total counter\n");
+ sb.append("ranger_pdp_requests_success_total ").append(runtimeState.getTotalAuthzSuccess()).append('\n');
+ sb.append("# TYPE ranger_pdp_requests_bad_request_total counter\n");
+ sb.append("ranger_pdp_requests_bad_request_total ").append(runtimeState.getTotalAuthzBadRequest()).append('\n');
+ sb.append("# TYPE ranger_pdp_requests_error_total counter\n");
+ sb.append("ranger_pdp_requests_error_total ").append(runtimeState.getTotalAuthzErrors()).append('\n');
+ sb.append("# TYPE ranger_pdp_auth_failures_total counter\n");
+ sb.append("ranger_pdp_auth_failures_total ").append(runtimeState.getTotalAuthFailures()).append('\n');
+ sb.append("# TYPE ranger_pdp_request_latency_avg_ms gauge\n");
+ sb.append("ranger_pdp_request_latency_avg_ms ").append(runtimeState.getAverageLatencyMs()).append('\n');
+ sb.append("# TYPE ranger_pdp_policy_cache_age_ms gauge\n");
+ sb.append("ranger_pdp_policy_cache_age_ms ").append(getPolicyCacheAgeMs()).append('\n');
+
+ resp.setStatus(HttpServletResponse.SC_OK);
+ resp.setContentType("text/plain; version=0.0.4");
+
+ resp.getWriter().write(sb.toString());
+ }
+
+ private long getPolicyCacheAgeMs() {
+ String cacheDir = config.get(RangerPdpConstants.PROP_AUTHZ_POLICY_CACHE_DIR, null);
+
+ if (StringUtils.isNotBlank(cacheDir)) {
+ File dir = new File(cacheDir);
+
+ if (dir.exists() && dir.isDirectory()) {
+ File[] files = dir.listFiles((d, name) -> name.endsWith(".json"));
+
+ if (files != null && files.length > 0) {
+ long latestMtime = 0L;
+
+ for (File f : files) {
+ latestMtime = Math.max(latestMtime, f.lastModified());
+ }
+
+ if (latestMtime > 0L) {
+ return Math.max(0L, System.currentTimeMillis() - latestMtime);
+ }
+ }
+ }
+ }
+
+ return -1;
Review Comment:
`getPolicyCacheAgeMs()` scans the policy-cache directory and stats every `*.json` file on each `/health/ready` and `/metrics` request. With frequent Prometheus scraping (and/or large cache dirs) this can become unnecessary filesystem overhead. Consider caching the computed value for a short TTL, tracking latest mtime incrementally, or moving this expensive check behind a lower-frequency refresh.
##########
pdp/src/main/java/org/apache/ranger/pdp/config/RangerPdpConfig.java:
##########
@@ -0,0 +1,281 @@
+/*
+ * 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.config;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.ranger.plugin.util.XMLUtils;
+import org.ietf.jgss.GSSCredential;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.util.Properties;
+
+/**
+ * Reads Ranger PDP configuration from {@code ranger-pdp-default.xml} (classpath)
+ * overridden by {@code ranger-pdp-site.xml} (classpath or filesystem).
+ *
+ * <p>Both files use the Hadoop {@code <configuration>} XML format, consistent
+ * with other Ranger server modules (tagsync, kms, etc.).
+ * The format is parsed directly using the JDK DOM API to avoid an early
+ * class-load dependency on Hadoop's {@code Configuration} class.
+ *
+ * <p>Authentication property names:
+ * <ul>
+ * <li>Kerberos/SPNEGO: {@code ranger.pdp.authn.kerberos.*}
+ * <li>JWT bearer token: {@code ranger.pdp.authn.jwt.*}
+ * <li>HTTP header: {@code ranger.pdp.authn.header.*}
+ * </ul>
+ */
+public class RangerPdpConfig {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpConfig.class);
+
+ private static final String DEFAULT_CONFIG_FILE = "ranger-pdp-default.xml";
+ private static final String SITE_CONFIG_FILE = "ranger-pdp-site.xml";
+
+ private final Properties props = new Properties();
+
+ public RangerPdpConfig() {
+ loadFromClasspath(DEFAULT_CONFIG_FILE);
+ loadFromClasspath(SITE_CONFIG_FILE);
+
+ String confDir = System.getProperty(RangerPdpConstants.PROP_CONF_DIR, "");
+
+ if (StringUtils.isNotBlank(confDir)) {
+ loadFromFile(new File(confDir, SITE_CONFIG_FILE));
+ }
+
+ applySystemPropertyOverrides();
+
+ LOG.info("RangerPdpConfig initialized (conf.dir={})", confDir);
+ }
+
+ public int getPort() {
+ return getInt(RangerPdpConstants.PROP_PORT, 6500);
+ }
+
+ public String getLogDir() {
+ return get(RangerPdpConstants.PROP_LOG_DIR, "/var/log/ranger/pdp");
+ }
+
+ public boolean isSslEnabled() {
+ return getBoolean(RangerPdpConstants.PROP_SSL_ENABLED, false);
+ }
+
+ public String getKeystoreFile() {
+ return get(RangerPdpConstants.PROP_SSL_KEYSTORE_FILE, "");
+ }
+
+ public String getKeystorePassword() {
+ return get(RangerPdpConstants.PROP_SSL_KEYSTORE_PASSWORD, "");
+ }
+
+ public String getKeystoreType() {
+ return get(RangerPdpConstants.PROP_SSL_KEYSTORE_TYPE, "JKS");
+ }
+
+ public boolean isTruststoreEnabled() {
+ return getBoolean(RangerPdpConstants.PROP_SSL_TRUSTSTORE_ENABLED, false);
+ }
+
+ public String getTruststoreFile() {
+ return get(RangerPdpConstants.PROP_SSL_TRUSTSTORE_FILE, "");
+ }
+
+ public String getTruststorePassword() {
+ return get(RangerPdpConstants.PROP_SSL_TRUSTSTORE_PASSWORD, "");
+ }
+
+ public String getTruststoreType() {
+ return get(RangerPdpConstants.PROP_SSL_TRUSTSTORE_TYPE, "JKS");
+ }
+
+ public boolean isHttp2Enabled() {
+ return getBoolean(RangerPdpConstants.PROP_HTTP2_ENABLED, true);
+ }
+
+ public int getHttpConnectorMaxThreads() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_MAX_THREADS, 200);
+ }
+
+ public int getHttpConnectorMinSpareThreads() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_MIN_SPARE_THREADS, 20);
+ }
+
+ public int getHttpConnectorAcceptCount() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_ACCEPT_COUNT, 100);
+ }
+
+ public int getHttpConnectorMaxConnections() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_MAX_CONNECTIONS, 10000);
+ }
+
+ public String getAuthnTypes() {
+ return get(RangerPdpConstants.PROP_AUTHN_TYPES, "header,jwt,kerberos");
+ }
+
+ // --- HTTP Header auth ---
+ public boolean isHeaderAuthnEnabled() {
+ return getBoolean(RangerPdpConstants.PROP_AUTHN_HEADER_ENABLED, false);
+ }
+
+ public String getHeaderAuthnUsername() {
+ return get(RangerPdpConstants.PROP_AUTHN_HEADER_USERNAME, "X-Forwarded-User");
+ }
+
+ // --- JWT bearer token auth ---
+ public boolean isJwtAuthnEnabled() {
+ return getBoolean(RangerPdpConstants.PROP_AUTHN_JWT_ENABLED, false);
+ }
+
+ public String getJwtProviderUrl() {
+ return get(RangerPdpConstants.PROP_AUTHN_JWT_PROVIDER_URL, "");
+ }
+
+ public String getJwtPublicKey() {
+ return get(RangerPdpConstants.PROP_AUTHN_JWT_PUBLIC_KEY, "");
+ }
+
+ public String getJwtCookieName() {
+ return get(RangerPdpConstants.PROP_AUTHN_JWT_COOKIE_NAME, "hadoop-jwt");
+ }
+
+ public String getJwtAudiences() {
+ return get(RangerPdpConstants.PROP_AUTHN_JWT_AUDIENCES, "");
+ }
+
+ // --- Kerberos / SPNEGO ---
+ public boolean isKerberosAuthnEnabled() {
+ return getBoolean(RangerPdpConstants.PROP_AUTHN_KERBEROS_ENABLED, false);
+ }
+
+ public String getSpnegoPrincipal() {
+ return get(RangerPdpConstants.PROP_AUTHN_KERBEROS_SPNEGO_PRINCIPAL, "");
+ }
+
+ public String getSpnegoKeytab() {
+ return get(RangerPdpConstants.PROP_AUTHN_KERBEROS_SPNEGO_KEYTAB, "");
+ }
+
+ public int getKerberosTokenValiditySeconds() {
+ return getInt(RangerPdpConstants.PROP_AUTHN_KERBEROS_KRB_TOKEN_VALIDITY, GSSCredential.INDEFINITE_LIFETIME);
+ }
+
+ public String getKerberosNameRules() {
+ return get(RangerPdpConstants.PROP_AUTHN_KERBEROS_NAME_RULES, "DEFAULT");
+ }
+
+ /**
+ * Returns all properties for forwarding to {@code RangerEmbeddedAuthorizer}.
+ */
+ public Properties getAuthzProperties() {
+ return new Properties(props);
+ }
+
+ public String get(String key, String defaultValue) {
+ String val = props.getProperty(key);
+
+ return StringUtils.isNotBlank(val) ? val.trim() : defaultValue;
+ }
+
+ public int getInt(String key, int defaultValue) {
+ String val = props.getProperty(key);
+
+ if (StringUtils.isNotBlank(val)) {
+ try {
+ return Integer.parseInt(val.trim());
+ } catch (NumberFormatException e) {
+ LOG.warn("Invalid integer for {}: '{}'; using default {}", key, val, defaultValue);
+ }
+ }
+
+ return defaultValue;
+ }
+
+ public boolean getBoolean(String key, boolean defaultValue) {
+ String val = props.getProperty(key);
+
+ return StringUtils.isNotBlank(val) ? Boolean.parseBoolean(val.trim()) : defaultValue;
+ }
+
+ private void loadFromClasspath(String resourceName) {
+ try (InputStream in = getClass().getClassLoader().getResourceAsStream(resourceName)) {
+ if (in != null) {
+ parseHadoopXml(in, resourceName);
+ } else {
+ LOG.debug("Config resource not found on classpath: {}", resourceName);
+ }
+ } catch (IOException e) {
+ LOG.warn("Failed to close stream for classpath resource: {}", resourceName, e);
+ }
+ }
+
+ private void loadFromFile(File file) {
+ if (!file.exists() || !file.isFile()) {
+ LOG.debug("Config file not found: {}", file);
+ return;
+ }
+
+ try (InputStream in = Files.newInputStream(file.toPath())) {
+ parseHadoopXml(in, file.getAbsolutePath());
+ } catch (IOException e) {
+ LOG.warn("Failed to read config file: {}", file, e);
+ }
+ }
+
+ /**
+ * Parses a Hadoop-style {@code <configuration>} XML document and merges all
+ * {@code <property>} entries into {@link #props}. Later entries override earlier
+ * ones, matching Hadoop's own override semantics.
+ *
+ * <pre>
+ * {@code
+ * <configuration>
+ * <property>
+ * <name>some.key</name>
+ * <value>some-value</value>
+ * </property>
+ * </configuration>
+ * }
+ * </pre>
+ */
+ private void parseHadoopXml(InputStream in, String source) {
+ LOG.info("Loading from {}. Properties count {}", source, props.size());
+
+ XMLUtils.loadConfig(in, props);
+
+ LOG.info("Loaded from {}. Properties count {}", source, props.size());
+ }
Review Comment:
`parseHadoopXml()` logs config-load progress at INFO for every config source, which will add noisy startup/runtime logs (and is inconsistent with other Ranger config loaders that don’t log each load at INFO). Consider downgrading these messages to DEBUG (or logging only on failures) to keep production logs clean.
##########
pdp/src/main/java/org/apache/ranger/pdp/rest/RangerPdpREST.java:
##########
@@ -0,0 +1,486 @@
+/*
+ * 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.rest;
+
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.MapUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.ranger.authz.api.RangerAuthorizer;
+import org.apache.ranger.authz.api.RangerAuthzException;
+import org.apache.ranger.authz.model.RangerAccessInfo;
+import org.apache.ranger.authz.model.RangerAuthzRequest;
+import org.apache.ranger.authz.model.RangerAuthzResult;
+import org.apache.ranger.authz.model.RangerMultiAuthzRequest;
+import org.apache.ranger.authz.model.RangerMultiAuthzResult;
+import org.apache.ranger.authz.model.RangerResourceInfo;
+import org.apache.ranger.authz.model.RangerResourcePermissions;
+import org.apache.ranger.authz.model.RangerResourcePermissionsRequest;
+import org.apache.ranger.authz.model.RangerUserInfo;
+import org.apache.ranger.pdp.RangerPdpStats;
+import org.apache.ranger.pdp.config.RangerPdpConfig;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.apache.ranger.pdp.model.ErrorResponse;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+import static javax.ws.rs.core.Response.Status.FORBIDDEN;
+import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
+import static javax.ws.rs.core.Response.Status.UNAUTHORIZED;
+import static org.apache.ranger.authz.api.RangerAuthzApiErrorCode.INVALID_REQUEST_USER_INFO_MISSING;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_PDP_SERVICE_PREFIX;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_SUFFIX_DELEGATION_USERS;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.WILDCARD_SERVICE_NAME;
+
+/**
+ * REST resource that exposes the three core {@link RangerAuthorizer} methods over HTTP.
+ *
+ * <p>All endpoints are under {@code /authz/v1} and produce/consume {@code application/json}.
+ * Authentication is enforced upstream by {@link org.apache.ranger.pdp.security.RangerPdpAuthNFilter}; the authenticated
+ * caller's identity is read from the {@link RangerPdpConstants#ATTR_AUTHENTICATED_USER}
+ * request attribute.
+ *
+ * <table border="1">
+ * <tr><th>Method</th><th>Path</th><th>Request body</th><th>Response body</th></tr>
+ * <tr><td>POST</td><td>/authz/v1/authorize</td>
+ * <td>{@link RangerAuthzRequest}</td><td>{@link RangerAuthzResult}</td></tr>
+ * <tr><td>POST</td><td>/authz/v1/authorizeMulti</td>
+ * <td>{@link RangerMultiAuthzRequest}</td><td>{@link RangerMultiAuthzResult}</td></tr>
+ * <tr><td>POST</td><td>/authz/v1/permissions</td>
+ * <td>{@link RangerResourcePermissionsRequest}</td>
+ * <td>{@link RangerResourcePermissions}</td></tr>
+ * </table>
+ */
+@Path("/v1")
+@Produces(MediaType.APPLICATION_JSON)
+@Consumes(MediaType.APPLICATION_JSON)
+@Singleton
+public class RangerPdpREST {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpREST.class);
+
+ private static final Response RESPONSE_OK = Response.ok().build();
+
+ private final Map<String, Set<String>> delegationUsersByService = new HashMap<>();
+
+ @Inject
+ private RangerAuthorizer authorizer;
+
+ @Inject
+ private RangerPdpConfig config;
+
+ @Context
+ private ServletContext servletContext;
+
+ @PostConstruct
+ public void initialize() {
+ initializeDelegationUsers();
+ }
+
+ /**
+ * Evaluates a single access request.
+ *
+ * @param request the authorization request
+ * @return {@code 200 OK} with {@link RangerAuthzResult}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/authorize")
+ public Response authorize(RangerAuthzRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String requestId = request != null ? request.getRequestId() : null;
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+ RangerUserInfo user = request != null ? request.getUser() : null;
+ RangerAccessInfo access = request != null ? request.getAccess() : null;
+
+ LOG.debug("==> authorize(requestId={}, caller={}, serviceName={})", requestId, caller, serviceName);
+
+ ret = validateCaller(caller, user, access, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
Review Comment:
Using a concrete `Response` instance (`RESPONSE_OK`) as a sentinel and checking `RESPONSE_OK.equals(ret)` is brittle because `Response` equality is effectively object identity here. If `validateCaller()` is ever refactored to return a newly-built 200 response, this check will silently fail. Consider returning a boolean/enum from `validateCaller()` (or checking `ret.getStatus()`), and building the actual response in the caller.
##########
intg/src/main/python/apache_ranger/exceptions.py:
##########
@@ -33,17 +33,21 @@ def __init__(self, api, response):
self.msgDesc = None
self.messageList = None
- print(response)
-
if api is not None and response is not None:
if response.content:
- try:
- respJson = response.json()
- self.msgDesc = respJson['msgDesc'] if respJson is not None and 'msgDesc' in respJson else None
- self.messageList = respJson['messageList'] if respJson is not None and 'messageList' in respJson else None
- except Exception:
- self.msgDesc = response.content
- self.messageList = [ response.content ]
+ try:
+ respJson = response.json()
+
+ if respJson:
+ self.msgDesc = respJson['msgDesc'] if 'msgDesc' in respJson else None
+ self.messageList = respJson['messageList'] if 'messageList' in respJson else None
+
+ if self.msgDesc is None:
+ self.msgDesc = respJson['message'] if 'message' in respJson else None
+
+ except Exception:
+ self.msgDesc = response.content
+ self.messageList = [ response.content ]
Review Comment:
In the JSON parse failure path, `msgDesc`/`messageList` are populated from `response.content`, which is `bytes` in `requests`. This produces exception messages like `b'...'` and can leak raw bytes; it’s better to use `response.text` (or decode `response.content` with a safe charset/replace strategy).
```suggestion
self.msgDesc = response.text
self.messageList = [ response.text ]
```
##########
pdp/src/main/java/org/apache/ranger/pdp/security/KerberosAuthNHandler.java:
##########
@@ -0,0 +1,275 @@
+/*
+ * 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.commons.codec.binary.Base64;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.security.authentication.util.KerberosName;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.ietf.jgss.GSSContext;
+import org.ietf.jgss.GSSCredential;
+import org.ietf.jgss.GSSException;
+import org.ietf.jgss.GSSManager;
+import org.ietf.jgss.GSSName;
+import org.ietf.jgss.Oid;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.auth.Subject;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.auth.login.AppConfigurationEntry;
+import javax.security.auth.login.Configuration;
+import javax.security.auth.login.LoginContext;
+import javax.security.auth.login.LoginException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.security.PrivilegedExceptionAction;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Authenticates requests using Kerberos SPNEGO (HTTP Negotiate).
+ *
+ * <p>Uses the JDK's built-in GSSAPI/JGSS support – no external Kerberos library is required.
+ * The service principal and keytab must be configured via:
+ * <ul>
+ * <li>{@code ranger.pdp.authn.kerberos.spnego.principal} – e.g. {@code HTTP/host.example.com@REALM}
+ * <li>{@code ranger.pdp.authn.kerberos.spnego.keytab} – absolute path to the keytab file
+ * <li>{@code ranger.pdp.authn.kerberos.name.rules} – Hadoop-style name rules (default: {@code DEFAULT})
+ * </ul>
+ *
+ * <p>Authentication flow:
+ * <ol>
+ * <li>If no {@code Authorization: Negotiate} header is present the handler returns {@code SKIP}.
+ * <li>The SPNEGO token is extracted, validated via GSSAPI, and – if a response token is
+ * produced (mutual authentication) – written to {@code WWW-Authenticate: Negotiate <token>}.
+ * <li>On success the short-form principal name (strip {@literal @REALM} and host components)
+ * is returned as the authenticated user.
+ * <li>On failure a {@code 401 Negotiate} challenge is sent and {@code CHALLENGE} is returned.
+ * </ol>
+ */
+public class KerberosAuthNHandler implements PdpAuthNHandler {
+ private static final Logger LOG = LoggerFactory.getLogger(KerberosAuthNHandler.class);
+
+ public static final String AUTH_TYPE = "KERBEROS";
+
+ private static final String NEGOTIATE_PREFIX = "Negotiate ";
+ private static final String WWW_AUTHENTICATE = "WWW-Authenticate";
+ private static final String AUTHORIZATION = "Authorization";
+ private static final Oid SPNEGO_OID;
+ private static final Oid KRB5_OID;
+
+ static {
+ try {
+ SPNEGO_OID = new Oid("1.3.6.1.5.5.2");
+ KRB5_OID = new Oid("1.2.840.113554.1.2.2");
+ } catch (GSSException e) {
+ throw new ExceptionInInitializerError(e);
+ }
+ }
+
+ private Subject serviceSubject;
+ private GSSManager gssManager;
+ private GSSCredential serverCred;
+
+ @Override
+ public void init(Properties config) throws Exception {
+ String principal = config.getProperty(RangerPdpConstants.PROP_AUTHN_KERBEROS_SPNEGO_PRINCIPAL);
+ String keytab = config.getProperty(RangerPdpConstants.PROP_AUTHN_KERBEROS_SPNEGO_KEYTAB);
+ String nameRules = config.getProperty(RangerPdpConstants.PROP_AUTHN_KERBEROS_NAME_RULES, "DEFAULT");
+ String tokenValidity = config.getProperty(RangerPdpConstants.PROP_AUTHN_KERBEROS_KRB_TOKEN_VALIDITY);
+
+ int tokenLifetime = StringUtils.isBlank(tokenValidity) ? GSSCredential.INDEFINITE_LIFETIME : Integer.parseInt(tokenValidity);
+
Review Comment:
`token.valid.seconds` is parsed with `Integer.parseInt()` without validation. A malformed value will throw `NumberFormatException` during filter init and effectively disable Kerberos auth (and can prevent the server from starting if no other handlers are enabled). Consider using a safe parse with a default/fallback (and an explicit log message) similar to `RangerPdpConfig#getInt()`.
--
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: dev-unsubscribe@ranger.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] RANGER-5371: pdp server implementation [ranger]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
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: dev-unsubscribe@ranger.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] RANGER-5371: pdp server implementation [ranger]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #896:
URL: https://github.com/apache/ranger/pull/896#discussion_r3005522777
##########
pdp/src/main/java/org/apache/ranger/pdp/config/RangerPdpConfig.java:
##########
@@ -0,0 +1,281 @@
+/*
+ * 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.config;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.ranger.plugin.util.XMLUtils;
+import org.ietf.jgss.GSSCredential;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.util.Properties;
+
+/**
+ * Reads Ranger PDP configuration from {@code ranger-pdp-default.xml} (classpath)
+ * overridden by {@code ranger-pdp-site.xml} (classpath or filesystem).
+ *
+ * <p>Both files use the Hadoop {@code <configuration>} XML format, consistent
+ * with other Ranger server modules (tagsync, kms, etc.).
+ * The format is parsed directly using the JDK DOM API to avoid an early
+ * class-load dependency on Hadoop's {@code Configuration} class.
+ *
+ * <p>Authentication property names:
+ * <ul>
+ * <li>Kerberos/SPNEGO: {@code ranger.pdp.kerberos.spnego.*}
+ * <li>JWT bearer token: {@code ranger.pdp.jwt.*}
+ * <li>HTTP header: {@code ranger.pdp.authn.header.*}
+ * </ul>
Review Comment:
The class-level Javadoc lists authentication property prefixes like `ranger.pdp.kerberos.spnego.*` and `ranger.pdp.jwt.*`, but the actual configuration keys implemented in this module are under `ranger.pdp.authn.kerberos.*` and `ranger.pdp.authn.jwt.*` (see `RangerPdpConstants`). Please align the documentation with the real keys so operators don't configure the wrong properties.
##########
pdp/src/main/java/org/apache/ranger/pdp/security/KerberosAuthNHandler.java:
##########
@@ -0,0 +1,275 @@
+/*
+ * 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.commons.codec.binary.Base64;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.security.authentication.util.KerberosName;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.ietf.jgss.GSSContext;
+import org.ietf.jgss.GSSCredential;
+import org.ietf.jgss.GSSException;
+import org.ietf.jgss.GSSManager;
+import org.ietf.jgss.GSSName;
+import org.ietf.jgss.Oid;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.auth.Subject;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.auth.login.AppConfigurationEntry;
+import javax.security.auth.login.Configuration;
+import javax.security.auth.login.LoginContext;
+import javax.security.auth.login.LoginException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.security.PrivilegedExceptionAction;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Authenticates requests using Kerberos SPNEGO (HTTP Negotiate).
+ *
+ * <p>Uses the JDK's built-in GSSAPI/JGSS support – no external Kerberos library is required.
+ * The service principal and keytab must be configured via:
+ * <ul>
+ * <li>{@code ranger.pdp.kerberos.spnego.principal} – e.g. {@code HTTP/host.example.com@REALM}
+ * <li>{@code ranger.pdp.kerberos.spnego.keytab} – absolute path to the keytab file
+ * <li>{@code hadoop.security.auth_to_local} – Hadoop-style name rules (default: {@code DEFAULT})
+ * </ul>
Review Comment:
The Kerberos handler Javadoc lists configuration keys without the `authn` segment (for example `ranger.pdp.kerberos.spnego.principal/keytab`). The actual keys consumed by `init()` are `ranger.pdp.authn.kerberos.spnego.principal` and `ranger.pdp.authn.kerberos.spnego.keytab` (see `RangerPdpConstants`). Please update the Javadoc so deployments configure the correct properties.
##########
dev-support/ranger-docker/scripts/pdp/ranger-pdp-site.xml:
##########
@@ -0,0 +1,240 @@
+<?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.
+-->
+<!-- Ranger PDP Server - Docker Configuration -->
+<configuration>
+ <property>
+ <name>ranger.pdp.port</name>
+ <value>6500</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.log.dir</name>
+ <value>/var/log/ranger/pdp</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.service.*.delegation.users</name>
+ <value>ranger</value>
+ </property>
+
+ <!-- Only delegation users are allowed call on behalf of other users -->
+ <property>
+ <name>ranger.pdp.service.dev_hdfs.delegation.users</name>
+ <value>hdfs</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.service.dev_hive.delegation.users</name>
+ <value>hive</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.service.dev_hbase.delegation.users</name>
+ <value>hbase</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.service.dev_kafka.delegation.users</name>
+ <value>kafka</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.service.dev_trino.delegation.users</name>
+ <value>trino</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.service.dev_polaris.delegation.users</name>
+ <value>polaris</value>
+ </property>
+
+ <!-- SSL/TLS: disabled for Docker dev environment -->
+ <property>
+ <name>ranger.pdp.ssl.enabled</name>
+ <value>false</value>
+ </property>
+
+ <!-- HTTP/2 -->
+ <property>
+ <name>ranger.pdp.http2.enabled</name>
+ <value>true</value>
+ </property>
+
+ <!-- Connector concurrency / queue limits -->
+ <property>
+ <name>ranger.pdp.http.connector.maxThreads</name>
+ <value>200</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.http.connector.minSpareThreads</name>
+ <value>20</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.http.connector.acceptCount</name>
+ <value>100</value>
+ </property>
+
+ <property>
+ <name>ranger.pdp.http.connector.maxConnections</name>
+ <value>10000</value>
+ </property>
+
+ <!-- Authentication for incoming REST requests -->
+ <property>
+ <name>ranger.pdp.authn.types</name>
+ <value>header,jwt,kerberos</value>
+ </property>
+
+ <!-- HTTP Header authentication: disabled -->
+ <property>
+ <name>ranger.pdp.authn.header.enabled</name>
+ <value>true</value>
+ </property>
Review Comment:
The comment says "HTTP Header authentication: disabled" but the configuration sets `ranger.pdp.authn.header.enabled` to `true`. Please fix the comment (or the value) so the file is self-consistent and doesn't mislead operators.
##########
intg/src/main/python/apache_ranger/exceptions.py:
##########
@@ -33,14 +33,18 @@ def __init__(self, api, response):
self.msgDesc = None
self.messageList = None
- print(response)
-
if api is not None and response is not None:
if response.content:
try:
- respJson = response.json()
- self.msgDesc = respJson['msgDesc'] if respJson is not None and 'msgDesc' in respJson else None
- self.messageList = respJson['messageList'] if respJson is not None and 'messageList' in respJson else None
+ respJson = response.json()
+
+ if respJson:
+ self.msgDesc = respJson['msgDesc'] if 'msgDesc' in respJson else None
+ self.messageList = respJson['messageList'] if 'messageList' in respJson else None
+
+ if self.msgDesc is None:
+ self.msgDesc = respJson['message'] if 'message' in respJson else None
+
except Exception:
self.msgDesc = response.content
self.messageList = [ response.content ]
Review Comment:
Indentation in this block is inconsistent with the surrounding 4-space indentation used in the module (and makes the control flow hard to read). Please re-indent the nested `try/except` and inner `if` blocks using consistent 4-space increments.
##########
pdp/src/main/java/org/apache/ranger/pdp/security/RangerPdpAuthNFilter.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.commons.lang3.StringUtils;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_AUTHN_TYPES;
+
+/**
+ * Servlet filter that enforces authentication for all PDP REST endpoints.
+ *
+ * <p>Handlers are configured via the {@code ranger.pdp.auth.types} filter init parameter
+ * (comma-separated list of {@code header}, {@code jwt}, {@code kerberos}). Handlers are
+ * tried in the listed order; the first successful match wins.
Review Comment:
Javadoc references the init parameter as `ranger.pdp.auth.types`, but the actual config key used throughout the implementation is `ranger.pdp.authn.types` (see `RangerPdpConstants.PROP_AUTHN_TYPES`). Please update the Javadoc to the correct property name to avoid misconfiguration.
##########
pdp/src/main/java/org/apache/ranger/pdp/rest/RangerPdpREST.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * 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.rest;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.MapUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.ranger.authz.api.RangerAuthorizer;
+import org.apache.ranger.authz.api.RangerAuthzException;
+import org.apache.ranger.authz.model.RangerAccessInfo;
+import org.apache.ranger.authz.model.RangerAuthzRequest;
+import org.apache.ranger.authz.model.RangerAuthzResult;
+import org.apache.ranger.authz.model.RangerMultiAuthzRequest;
+import org.apache.ranger.authz.model.RangerMultiAuthzResult;
+import org.apache.ranger.authz.model.RangerResourceInfo;
+import org.apache.ranger.authz.model.RangerResourcePermissions;
+import org.apache.ranger.authz.model.RangerResourcePermissionsRequest;
+import org.apache.ranger.authz.model.RangerUserInfo;
+import org.apache.ranger.pdp.RangerPdpStats;
+import org.apache.ranger.pdp.config.RangerPdpConfig;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+import static javax.ws.rs.core.Response.Status.FORBIDDEN;
+import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
+import static javax.ws.rs.core.Response.Status.UNAUTHORIZED;
+import static org.apache.ranger.authz.api.RangerAuthzApiErrorCode.INVALID_REQUEST_USER_INFO_MISSING;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_PDP_SERVICE_PREFIX;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_SUFFIX_DELEGATION_USERS;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.WILDCARD_SERVICE_NAME;
+
+/**
+ * REST resource that exposes the three core {@link RangerAuthorizer} methods over HTTP.
+ *
+ * <p>All endpoints are under {@code /authz/v1} and produce/consume {@code application/json}.
+ * Authentication is enforced upstream by {@link RangerPdpAuthFilter}; the authenticated
+ * caller's identity is read from the {@link RangerPdpConstants#ATTR_AUTHENTICATED_USER}
+ * request attribute.
Review Comment:
The Javadoc links to `RangerPdpAuthFilter`, but that type doesn't exist in this module (the filter class is `RangerPdpAuthNFilter`). This produces broken Javadoc links and can fail builds that treat Javadoc warnings as errors. Update the reference to the correct filter class name.
##########
pdp/src/main/java/org/apache/ranger/pdp/security/RangerPdpAuthNFilter.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.commons.lang3.StringUtils;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_AUTHN_TYPES;
+
+/**
+ * Servlet filter that enforces authentication for all PDP REST endpoints.
+ *
+ * <p>Handlers are configured via the {@code ranger.pdp.auth.types} filter init parameter
+ * (comma-separated list of {@code header}, {@code jwt}, {@code kerberos}). Handlers are
+ * tried in the listed order; the first successful match wins.
+ *
+ * <p>On success the authenticated username is stored in the request attribute
+ * {@link RangerPdpConstants#ATTR_AUTHENTICATED_USER} so that REST resources can read it.
+ *
+ * <p>If all handlers return {@code SKIP} (no recognisable credentials found), the filter
+ * sends a {@code 401} response with {@code WWW-Authenticate} headers for every
+ * configured handler that provides a challenge.
+ */
+public class RangerPdpAuthNFilter implements Filter {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpAuthNFilter.class);
+
+ private final List<PdpAuthNHandler> handlers = new ArrayList<>();
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ Properties config = toProperties(filterConfig);
+ String authnTypes = filterConfig.getInitParameter(PROP_AUTHN_TYPES);
+
+ for (String authnType : authnTypes.split(",")) {
+ PdpAuthNHandler handler = createHandler(authnType.trim().toLowerCase(), filterConfig);
Review Comment:
`authnTypes` is read from the `ranger.pdp.authn.types` init-param but isn't validated for null/blank before calling `split(",")`. If the filter is registered without this init-param (or an empty value), `init()` will throw a NullPointerException instead of a clear `ServletException`. Add a null/blank check and fail fast with an explicit message (or fall back to a default list).
--
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: dev-unsubscribe@ranger.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] RANGER-5371: pdp server implementation [ranger]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #896:
URL: https://github.com/apache/ranger/pull/896#discussion_r3004683226
##########
intg/src/main/python/apache_ranger/exceptions.py:
##########
@@ -38,9 +38,15 @@ def __init__(self, api, response):
if api is not None and response is not None:
if response.content:
try:
- respJson = response.json()
- self.msgDesc = respJson['msgDesc'] if respJson is not None and 'msgDesc' in respJson else None
- self.messageList = respJson['messageList'] if respJson is not None and 'messageList' in respJson else None
+ respJson = response.json()
Review Comment:
`print(response)` in the exception constructor will write to stdout on every API error, which is noisy for library consumers and can leak response details into logs/terminals unexpectedly. Consider removing it or switching to `LOG.debug(...)`/`LOG.exception(...)` behind a logger.
##########
pdp/src/main/java/org/apache/ranger/pdp/RangerPdpServer.java:
##########
@@ -0,0 +1,290 @@
+/*
+ * 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 org.apache.catalina.Context;
+import org.apache.catalina.LifecycleException;
+import org.apache.catalina.connector.Connector;
+import org.apache.catalina.startup.Tomcat;
+import org.apache.catalina.valves.AccessLogValve;
+import org.apache.coyote.http2.Http2Protocol;
+import org.apache.ranger.authz.api.RangerAuthorizer;
+import org.apache.ranger.authz.api.RangerAuthzException;
+import org.apache.ranger.authz.embedded.RangerEmbeddedAuthorizer;
+import org.apache.ranger.pdp.config.RangerPdpConfig;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.apache.ranger.pdp.rest.RangerPdpApplication;
+import org.apache.ranger.pdp.security.RangerPdpAuthFilter;
+import org.apache.ranger.pdp.security.RangerPdpRequestContextFilter;
+import org.apache.tomcat.util.descriptor.web.FilterDef;
+import org.apache.tomcat.util.descriptor.web.FilterMap;
+import org.glassfish.jersey.servlet.ServletContainer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.Servlet;
+import javax.servlet.http.HttpServlet;
+
+import java.io.File;
+
+/**
+ * Main entry point for the Ranger Policy Decision Point (PDP) server.
+ *
+ * <p>Starts an embedded Apache Tomcat instance that:
+ * <ul>
+ * <li>Creates and initialises a {@link RangerEmbeddedAuthorizer} singleton
+ * <li>Exposes the three authorizer methods as REST endpoints under {@code /authz/v1/}
+ * <li>Enforces authentication via {@link RangerPdpAuthFilter} (Kerberos/JWT/HTTP-Header)
+ * <li>Optionally enables HTTP/2 ({@code Http2Protocol} upgrade on the connector)
+ * </ul>
+ *
+ * <p><b>Startup:</b> {@code java -jar ranger-pdp.jar}
+ * <br>Override config with: {@code -Dranger.pdp.conf.dir=/etc/ranger/pdp}
+ */
+public class RangerPdpServer {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpServer.class);
+
+ private final RangerPdpConfig config;
+ private final RangerPdpStats runtimeStats = new RangerPdpStats();
+ private Tomcat tomcat;
+ private RangerAuthorizer authorizer;
+
+ public RangerPdpServer() {
+ this.config = new RangerPdpConfig();
+ }
+
+ public static void main(String[] args) throws Exception {
+ new RangerPdpServer().start();
+ }
+
+ public void start() throws Exception {
+ LOG.info("Starting Ranger PDP server");
+
+ initAuthorizer();
+ startTomcat();
+ }
+
+ public void stop() {
+ LOG.info("Stopping Ranger PDP server");
+
+ runtimeStats.setAcceptingRequests(false);
+ runtimeStats.setServerStarted(false);
+
+ if (tomcat != null) {
+ try {
+ if (tomcat.getConnector() != null) {
+ tomcat.getConnector().pause();
+ }
+
+ tomcat.stop();
+ tomcat.destroy();
+ } catch (LifecycleException e) {
+ LOG.warn("Error stopping Tomcat", e);
+ }
+ }
+
+ if (authorizer != null) {
+ try {
+ authorizer.close();
+ } catch (RangerAuthzException e) {
+ LOG.warn("Error closing authorizer", e);
+ }
+ }
+ }
+
+ private void initAuthorizer() throws RangerAuthzException {
+ authorizer = new RangerEmbeddedAuthorizer(config.getAuthzProperties());
+
+ authorizer.init();
+
+ runtimeStats.setAuthorizerInitialized(true);
+
+ LOG.info("RangerEmbeddedAuthorizer initialised");
+ }
+
+ private void startTomcat() throws Exception {
+ tomcat = new Tomcat();
+
+ tomcat.setConnector(createConnector());
+
+ String docBase = new File(System.getProperty("java.io.tmpdir"), "ranger-pdp-webapps").getAbsolutePath();
+
+ new File(docBase).mkdirs();
+
+ Context ctx = tomcat.addContext("", docBase);
+
+ ctx.getServletContext().setAttribute(RangerPdpConstants.SERVLET_CTX_ATTR_AUTHORIZER, authorizer);
+ ctx.getServletContext().setAttribute(RangerPdpConstants.SERVLET_CTX_ATTR_CONFIG, config);
+ ctx.getServletContext().setAttribute(RangerPdpConstants.SERVLET_CTX_ATTR_RUNTIME_STATE, runtimeStats);
+
+ addAuthFilter(ctx);
+ addJerseyServlet(ctx);
+ addStatusEndpoints(ctx);
+ addAccessLogValve();
+
+ Runtime.getRuntime().addShutdownHook(new Thread(this::stop, "ranger-pdp-shutdown"));
+
+ tomcat.start();
+
+ runtimeStats.setServerStarted(true);
+ runtimeStats.setAcceptingRequests(true);
+
+ LOG.info("Ranger PDP server listening on port {} (SSL={}, HTTP/2={})", config.getPort(), config.isSslEnabled(), config.isHttp2Enabled());
+
+ tomcat.getServer().await();
+ }
+
+ /**
+ * Builds the Tomcat connector.
+ *
+ * <p>When SSL is enabled the connector is configured as an HTTPS endpoint.
+ * When HTTP/2 is enabled an {@link Http2Protocol} upgrade protocol is added,
+ * supporting both {@code h2} (over TLS) and {@code h2c} (cleartext upgrade) depending
+ * on whether SSL is enabled.
+ */
+ private Connector createConnector() {
+ Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
+
+ connector.setPort(config.getPort());
+ connector.setProperty("maxThreads", String.valueOf(config.getHttpConnectorMaxThreads()));
+ connector.setProperty("minSpareThreads", String.valueOf(config.getHttpConnectorMinSpareThreads()));
+ connector.setProperty("acceptCount", String.valueOf(config.getHttpConnectorAcceptCount()));
+ connector.setProperty("maxConnections", String.valueOf(config.getHttpConnectorMaxConnections()));
+
+ LOG.info("Configured HTTP connector limits: maxThreads={}, minSpareThreads={}, acceptCount={}, maxConnections={}",
+ config.getHttpConnectorMaxThreads(), config.getHttpConnectorMinSpareThreads(),
+ config.getHttpConnectorAcceptCount(), config.getHttpConnectorMaxConnections());
+
+ if (config.isSslEnabled()) {
+ connector.setSecure(true);
+ connector.setScheme("https");
+ connector.setProperty("SSLEnabled", "true");
+ connector.setProperty("protocol", "org.apache.coyote.http11.Http11NioProtocol");
+ connector.setProperty("keystoreFile", config.getKeystoreFile());
+ connector.setProperty("keystorePass", config.getKeystorePassword());
+ connector.setProperty("keystoreType", config.getKeystoreType());
+ connector.setProperty("sslProtocol", "TLS");
+
+ if (config.isTruststoreEnabled()) {
+ connector.setProperty("truststoreFile", config.getTruststoreFile());
+ connector.setProperty("truststorePass", config.getTruststorePassword());
+ connector.setProperty("truststoreType", config.getTruststoreType());
+ connector.setProperty("clientAuth", "want");
+ }
+ }
+
+ if (config.isHttp2Enabled()) {
+ connector.addUpgradeProtocol(new Http2Protocol());
+
+ LOG.info("HTTP/2 upgrade protocol registered on connector (port={})", config.getPort());
+ }
+
+ return connector;
+ }
+
+ /**
+ * Registers {@link RangerPdpAuthFilter} on all {@code /authz/*} paths.
+ * Init parameters are forwarded from the server config so the filter can
+ * instantiate and configure the auth handlers.
+ */
+ private void addAuthFilter(Context ctx) {
+ FilterDef reqCtxFilterDef = new FilterDef();
+ FilterMap reqCtxFilterMap = new FilterMap();
+ FilterDef authFilterDef = new FilterDef();
+ FilterMap authFilterMap = new FilterMap();
+
+ reqCtxFilterDef.setFilterName("rangerPdpRequestContextFilter");
+ reqCtxFilterDef.setFilter(new RangerPdpRequestContextFilter());
+
+ reqCtxFilterMap.setFilterName("rangerPdpRequestContextFilter");
+ reqCtxFilterMap.addURLPattern("/*");
+
+ authFilterDef.setFilterName("rangerPdpAuthFilter");
+ authFilterDef.setFilter(new RangerPdpAuthFilter());
+ authFilterDef.addInitParameter(RangerPdpAuthFilter.PARAM_AUTH_TYPES, config.getAuthTypes());
+ // HTTP Header auth
+ authFilterDef.addInitParameter(RangerPdpAuthFilter.PARAM_HEADER_AUTHN_ENABLED, String.valueOf(config.isHeaderAuthnEnabled()));
+ authFilterDef.addInitParameter(RangerPdpAuthFilter.PARAM_HEADER_AUTHN_USERNAME, config.getHeaderAuthnUsername());
+ // JWT bearer token auth
+ authFilterDef.addInitParameter(RangerPdpAuthFilter.PARAM_JWT_PROVIDER_URL, config.getJwtProviderUrl());
+ authFilterDef.addInitParameter(RangerPdpAuthFilter.PARAM_JWT_PUBLIC_KEY, config.getJwtPublicKey());
+ authFilterDef.addInitParameter(RangerPdpAuthFilter.PARAM_JWT_COOKIE_NAME, config.getJwtCookieName());
+ authFilterDef.addInitParameter(RangerPdpAuthFilter.PARAM_JWT_AUDIENCES, config.getJwtAudiences());
+ // Kerberos / SPNEGO
+ authFilterDef.addInitParameter(RangerPdpAuthFilter.PARAM_SPNEGO_PRINCIPAL, config.getSpnegoPrincipal());
+ authFilterDef.addInitParameter(RangerPdpAuthFilter.PARAM_SPNEGO_KEYTAB, config.getSpnegoKeytab());
+ authFilterDef.addInitParameter(RangerPdpAuthFilter.PARAM_KRB_NAME_RULES, config.getKerberosNameRules());
+ authFilterDef.addInitParameter(RangerPdpAuthFilter.PARAM_KRB_TOKEN_VALIDITY, String.valueOf(config.getKerberosTokenValiditySeconds()));
+ authFilterDef.addInitParameter(RangerPdpAuthFilter.PARAM_KRB_COOKIE_DOMAIN, config.getKerberosCookieDomain());
+ authFilterDef.addInitParameter(RangerPdpAuthFilter.PARAM_KRB_COOKIE_PATH, config.getKerberosCookiePath());
Review Comment:
The filter init parameters `ranger.pdp.authn.kerberos.token.valid.seconds`, `cookie.domain`, and `cookie.path` are forwarded here, but the current `KerberosAuthHandler` implementation doesn't read/use them. Consider either implementing the intended behavior (cookie/token handling) or removing these params from the filter wiring/config to avoid misleading configuration knobs.
```suggestion
```
##########
pdp/src/main/java/org/apache/ranger/pdp/security/RangerPdpAuthFilter.java:
##########
@@ -0,0 +1,199 @@
+/*
+ * 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.commons.lang3.StringUtils;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * Servlet filter that enforces authentication for all PDP REST endpoints.
+ *
+ * <p>Handlers are configured via the {@code ranger.pdp.auth.types} filter init parameter
+ * (comma-separated list of {@code header}, {@code jwt}, {@code kerberos}). Handlers are
+ * tried in the listed order; the first successful match wins.
+ *
+ * <p>On success the authenticated username is stored in the request attribute
+ * {@link RangerPdpConstants#ATTR_AUTHENTICATED_USER} so that REST resources can read it.
+ *
+ * <p>If all handlers return {@code SKIP} (no recognisable credentials found), the filter
+ * sends a {@code 401} response with {@code WWW-Authenticate} headers for every
+ * configured handler that provides a challenge.
+ */
+public class RangerPdpAuthFilter implements Filter {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpAuthFilter.class);
+
+ // Auth type list — pdp-specific
+ public static final String PARAM_AUTH_TYPES = RangerPdpConstants.PROP_AUTH_TYPES;
+
+ // HTTP Header auth — consistent with ranger.admin.authn.header.* in security-admin
+ public static final String PARAM_HEADER_AUTHN_ENABLED = RangerPdpConstants.PROP_AUTHN_HEADER_ENABLED;
+ public static final String PARAM_HEADER_AUTHN_USERNAME = RangerPdpConstants.PROP_AUTHN_HEADER_USERNAME;
+
+ // JWT bearer token auth
+ public static final String PARAM_JWT_PROVIDER_URL = RangerPdpConstants.PROP_AUTHN_JWT_PROVIDER_URL;
+ public static final String PARAM_JWT_PUBLIC_KEY = RangerPdpConstants.PROP_AUTHN_JWT_PUBLIC_KEY;
+ public static final String PARAM_JWT_COOKIE_NAME = RangerPdpConstants.PROP_AUTHN_JWT_COOKIE_NAME;
+ public static final String PARAM_JWT_AUDIENCES = RangerPdpConstants.PROP_AUTHN_JWT_AUDIENCES;
+
+ // Kerberos / SPNEGO
+ public static final String PARAM_SPNEGO_PRINCIPAL = RangerPdpConstants.PROP_AUTHN_KERBEROS_SPNEGO_PRINCIPAL;
+ public static final String PARAM_SPNEGO_KEYTAB = RangerPdpConstants.PROP_AUTHN_KERBEROS_SPNEGO_KEYTAB;
+ public static final String PARAM_KRB_NAME_RULES = RangerPdpConstants.PROP_KRB_NAME_RULES;
+ public static final String PARAM_KRB_TOKEN_VALIDITY = RangerPdpConstants.PROP_AUTHN_KERBEROS_KRB_TOKEN_VALIDITY;
+ public static final String PARAM_KRB_COOKIE_DOMAIN = RangerPdpConstants.PROP_AUTHN_KERBEROS_KRB_COOKIE_DOMAIN;
+ public static final String PARAM_KRB_COOKIE_PATH = RangerPdpConstants.PROP_AUTHN_KERBEROS_KRB_COOKIE_PATH;
+
+ private final List<PdpAuthHandler> handlers = new ArrayList<>();
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ Properties config = toProperties(filterConfig);
+ String types = filterConfig.getInitParameter(PARAM_AUTH_TYPES);
+ boolean headerAuthEnabled = Boolean.parseBoolean(StringUtils.defaultIfBlank(filterConfig.getInitParameter(PARAM_HEADER_AUTHN_ENABLED), "false"));
+
+ if (StringUtils.isBlank(types)) {
+ types = "jwt,kerberos";
+ }
+
+ for (String type : types.split(",")) {
+ String normalizedType = type.trim().toLowerCase();
+
+ if ("header".equals(normalizedType) && !headerAuthEnabled) {
+ LOG.info("Header auth type is configured but disabled via {}=false; skipping handler registration", PARAM_HEADER_AUTHN_ENABLED);
+
+ continue;
+ }
+
+ PdpAuthHandler handler = createHandler(normalizedType);
+
+ if (handler == null) {
+ LOG.warn("Unknown auth type ignored: {}", type);
+
+ continue;
+ }
+
+ try {
+ handler.init(config);
+ handlers.add(handler);
+
+ LOG.info("Registered auth handler: {}", normalizedType);
+ } catch (Exception e) {
+ LOG.error("Failed to initialize auth handler: {}", type, e);
+ }
Review Comment:
`init()` logs and continues when an explicitly configured auth handler fails to initialize. This can result in the server starting with a reduced (and potentially unintended) authentication surface (e.g., `jwt,kerberos` configured but Kerberos misconfigured => JWT-only). Consider failing filter initialization when any configured handler cannot be initialized (or at least when a handler listed in `ranger.pdp.auth.types` fails), so misconfiguration doesn't silently weaken auth.
##########
pdp/conf.dist/logback.xml:
##########
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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>
+ <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
+ <Target>System.out</Target>
+ <encoder>
+ <pattern>%d{ISO8601} %-5p [%X{requestId}] %c{1} - %m%n</pattern>
+ </encoder>
+ </appender>
+ <logger name="com.sun.jersey.server.wadl.generators.WadlGeneratorJAXBGrammarGenerator" level="OFF"/>
+ <logger name="org.apache.directory.server.core" level="OFF"/>
+ <logger name="org.apache.hadoop.security" level="OFF"/>
+ <logger name="org.apache.hadoop.conf" level="ERROR"/>
+ <logger name="org.apache.hadoop.crytpo.key.kms.server" level="ALL"/>
Review Comment:
Logger name `org.apache.hadoop.crytpo.key.kms.server` looks like a typo (expected `org.apache.hadoop.crypto.key.kms.server`). As written, this logger configuration won’t match the actual Hadoop KMS classes, so the intended logging level change won’t take effect.
```suggestion
<logger name="org.apache.hadoop.crypto.key.kms.server" level="ALL"/>
```
##########
intg/src/main/python/apache_ranger/model/ranger_authz.py:
##########
@@ -0,0 +1,259 @@
+#!/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.model.ranger_base import RangerBase
+from apache_ranger.utils import non_null, type_coerce, type_coerce_dict, type_coerce_list
+
+
+class RangerUserInfo(RangerBase):
+ def __init__(self, attrs=None):
+ attrs = non_null(attrs, {})
+ RangerBase.__init__(self, attrs)
+
+ self.name = attrs.get("name")
+ self.attributes = attrs.get("attributes")
+ self.groups = attrs.get("groups")
+ self.roles = attrs.get("roles")
+
+
+class RangerResourceInfo(RangerBase):
+ SCOPE_SELF = "SELF"
+ SCOPE_SELF_OR_ANY_CHILD = "SELF_OR_ANY_CHILD"
+ SCOPE_SELF_OR_ANY_DESCENDANT = "SELF_OR_ANY_DESCENDANT"
+
+ def __init__(self, attrs=None):
+ attrs = non_null(attrs, {})
+ RangerBase.__init__(self, attrs)
+
+ self.name = attrs.get("name")
+ self.subResources = attrs.get("subResources")
+ self.nameMatchScope = attrs.get("nameMatchScope")
+ self.attributes = attrs.get("attributes")
+
+
+class RangerAccessInfo(RangerBase):
+ def __init__(self, attrs=None):
+ attrs = non_null(attrs, {})
+ RangerBase.__init__(self, attrs)
+
+ self.resource = attrs.get("resource")
+ self.action = attrs.get("action")
+ self.permissions = attrs.get("permissions")
+
+ def type_coerce_attrs(self):
+ super(RangerAccessInfo, self).type_coerce_attrs()
+ self.resource = type_coerce(self.resource, RangerResourceInfo)
+
+
+class RangerAccessContext(RangerBase):
+ def __init__(self, attrs=None):
+ attrs = non_null(attrs, {})
+ RangerBase.__init__(self, attrs)
+
+ self.serviceType = attrs.get("serviceType")
+ self.serviceName = attrs.get("serviceName")
+ self.accessTime = attrs.get("accessTime")
+ self.clientIpAddress = attrs.get("clientIpAddress")
+ self.forwardedIpAddresses = attrs.get("forwardedIpAddresses")
+ self.additionalInfo = attrs.get("additionalInfo")
+
+
+class RangerAuthzRequest(RangerBase):
+ def __init__(self, attrs=None):
+ attrs = non_null(attrs, {})
+ RangerBase.__init__(self, attrs)
+
+ self.requestId = attrs.get("requestId")
+ self.user = attrs.get("user")
+ self.access = attrs.get("access")
+ self.context = attrs.get("context")
+
+ def type_coerce_attrs(self):
+ super(RangerAuthzRequest, self).type_coerce_attrs()
+ self.user = type_coerce(self.user, RangerUserInfo)
+ self.access = type_coerce(self.access, RangerAccessInfo)
+ self.context = type_coerce(self.context, RangerAccessContext)
+
+
+class RangerMultiAuthzRequest(RangerBase):
+ def __init__(self, attrs=None):
+ attrs = non_null(attrs, {})
+ RangerBase.__init__(self, attrs)
+
+ self.requestId = attrs.get("requestId")
+ self.user = attrs.get("user")
+ self.accesses = attrs.get("accesses")
+ self.context = attrs.get("context")
+
+ def type_coerce_attrs(self):
+ super(RangerMultiAuthzRequest, self).type_coerce_attrs()
+ self.user = type_coerce(self.user, RangerUserInfo)
+ self.accesses = type_coerce_list(self.accesses, RangerAccessInfo)
+ self.context = type_coerce(self.context, RangerAccessContext)
+
+
+class RangerPolicyInfo(RangerBase):
+ def __init__(self, attrs=None):
+ attrs = non_null(attrs, {})
+ RangerBase.__init__(self, attrs)
+ self.id = attrs.get("id")
+ self.version = attrs.get("version")
+
+
+class RangerAccessResult(RangerBase):
+ def __init__(self, attrs=None):
+ attrs = non_null(attrs, {})
+ RangerBase.__init__(self, attrs)
+ self.decision = attrs.get("decision")
+ self.policy = attrs.get("policy")
+
+ def type_coerce_attrs(self):
+ super(RangerAccessResult, self).type_coerce_attrs()
+ self.policy = type_coerce(self.policy, RangerPolicyInfo)
+
+
+class RangerDataMaskResult(RangerBase):
+ def __init__(self, attrs=None):
+ attrs = non_null(attrs, {})
+ RangerBase.__init__(self, attrs)
+ self.maskType = attrs.get("maskType")
+ self.maskedValue = attrs.get("maskedValue")
+ self.policy = attrs.get("policy")
+
+ def type_coerce_attrs(self):
+ super(RangerDataMaskResult, self).type_coerce_attrs()
+ self.policy = type_coerce(self.policy, RangerPolicyInfo)
+
+
+class RangerRowFilterResult(RangerBase):
+ def __init__(self, attrs=None):
+ attrs = non_null(attrs, {})
+ RangerBase.__init__(self, attrs)
+ self.filterExpr = attrs.get("filterExpr")
+ self.policy = attrs.get("policy")
+
+ def type_coerce_attrs(self):
+ super(RangerRowFilterResult, self).type_coerce_attrs()
+ self.policy = type_coerce(self.policy, RangerPolicyInfo)
+
+
+class RangerResultInfo(RangerBase):
+ def __init__(self, attrs=None):
+ attrs = non_null(attrs, {})
+ RangerBase.__init__(self, attrs)
+ self.access = attrs.get("access")
+ self.dataMask = attrs.get("dataMask")
+ self.rowFilter = attrs.get("rowFilter")
+ self.additionalInfo = attrs.get("additionalInfo")
+
+ def type_coerce_attrs(self):
+ super(RangerResultInfo, self).type_coerce_attrs()
+ self.access = type_coerce(self.access, RangerAccessResult)
+ self.dataMask = type_coerce(self.dataMask, RangerDataMaskResult)
+ self.rowFilter = type_coerce(self.rowFilter, RangerRowFilterResult)
+
+
+class RangerPermissionResult(RangerBase):
+ def __init__(self, attrs=None):
+ attrs = non_null(attrs, {})
+ RangerBase.__init__(self, attrs)
+ self.permission = attrs.get("permission")
+ self.access = attrs.get("access")
+ self.dataMask = attrs.get("dataMask")
+ self.rowFilter = attrs.get("rowFilter")
+ self.additionalInfo = attrs.get("additionalInfo")
+ self.subResources = attrs.get("subResources")
+
+ def type_coerce_attrs(self):
+ super(RangerPermissionResult, self).type_coerce_attrs()
+ self.access = type_coerce(self.access, RangerAccessResult)
+ self.dataMask = type_coerce(self.dataMask, RangerDataMaskResult)
+ self.rowFilter = type_coerce(self.rowFilter, RangerRowFilterResult)
+ self.subResources = type_coerce_dict(self.subResources, RangerResultInfo)
+
+
+class RangerAuthzResult(RangerBase):
+ DECISION_ALLOW = "ALLOW"
+ DECISION_DENY = "DENY"
+ DECISION_NOT_DETERMINED = "NOT_DETERMINED"
+ DECISION_PARTIAL = "PARTIAL"
+
+ def __init__(self, attrs=None):
+ attrs = non_null(attrs, {})
+ RangerBase.__init__(self, attrs)
+ self.requestId = attrs.get("requestId")
+ self.decision = attrs.get("decision")
+ self.permissions = attrs.get("permissions")
+
+ def type_coerce_attrs(self):
+ super(RangerAuthzResult, self).type_coerce_attrs()
+ self.permissions = type_coerce_dict(self.permissions, RangerPermissionResult)
+
+
+class RangerMultiAuthzResult(RangerBase):
+ def __init__(self, attrs=None):
+ attrs = non_null(attrs, {})
+ RangerBase.__init__(self, attrs)
+ self.requestId = attrs.get("requestId")
+ self.decision = attrs.get("decision")
+ self.accesses = attrs.get("accesses")
+
+ def type_coerce_attrs(self):
+ super(RangerMultiAuthzResult, self).type_coerce_attrs()
+ self.accesses = type_coerce_list(self.accesses, RangerAuthzResult)
+
+
+class RangerResourcePermissionsRequest(RangerBase):
+ def __init__(self, attrs=None):
+ attrs = non_null(attrs, {})
+ RangerBase.__init__(self, attrs)
+ self.resource = attrs.get("resource")
+ self.context = attrs.get("context")
+
+ def type_coerce_attrs(self):
+ super(RangerResourcePermissionsRequest, self).type_coerce_attrs()
Review Comment:
`RangerResourcePermissionsRequest` in the Java model includes a `requestId`, and the README example sets one, but the Python `RangerResourcePermissionsRequest` model doesn't define/serialize `requestId`. This makes it impossible for Python callers to send request IDs for correlation. Consider adding a `requestId` field (and type coercion if needed) to match the API model.
```suggestion
self.requestId = attrs.get("requestId")
self.resource = attrs.get("resource")
self.context = attrs.get("context")
def type_coerce_attrs(self):
super(RangerResourcePermissionsRequest, self).type_coerce_attrs()
# Ensure requestId, if provided, is represented as a string for correlation
if self.requestId is not None:
self.requestId = str(self.requestId)
```
##########
pdp/src/main/java/org/apache/ranger/pdp/config/RangerPdpConfig.java:
##########
@@ -0,0 +1,296 @@
+/*
+ * 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.config;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.ranger.plugin.util.XMLUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+/**
+ * Reads Ranger PDP configuration from {@code ranger-pdp-default.xml} (classpath)
+ * overridden by {@code ranger-pdp-site.xml} (classpath or filesystem).
+ *
+ * <p>Both files use the Hadoop {@code <configuration>} XML format, consistent
+ * with other Ranger server modules (tagsync, kms, etc.).
+ * The format is parsed directly using the JDK DOM API to avoid an early
+ * class-load dependency on Hadoop's {@code Configuration} class.
+ *
+ * <p>Authentication property names:
+ * <ul>
+ * <li>Kerberos/SPNEGO: {@code ranger.pdp.kerberos.spnego.*}
+ * <li>JWT bearer token: {@code ranger.pdp.jwt.*}
+ * <li>HTTP header: {@code ranger.pdp.authn.header.*}
+ * </ul>
+ */
+public class RangerPdpConfig {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpConfig.class);
+
+ private static final String DEFAULT_CONFIG_FILE = "ranger-pdp-default.xml";
+ private static final String SITE_CONFIG_FILE = "ranger-pdp-site.xml";
+
+ private final Properties props = new Properties();
+
+ public RangerPdpConfig() {
+ loadFromClasspath(DEFAULT_CONFIG_FILE);
+ loadFromClasspath(SITE_CONFIG_FILE);
+
+ String confDir = System.getProperty(RangerPdpConstants.PROP_CONF_DIR, "");
+
+ if (StringUtils.isNotBlank(confDir)) {
+ loadFromFile(new File(confDir, SITE_CONFIG_FILE));
+ }
+
+ applySystemPropertyOverrides();
+
+ LOG.info("RangerPdpConfig initialized (conf.dir={})", confDir);
+ }
+
+ public int getPort() {
+ return getInt(RangerPdpConstants.PROP_PORT, 6500);
+ }
+
+ public String getLogDir() {
+ return get(RangerPdpConstants.PROP_LOG_DIR, "/var/log/ranger/pdp");
+ }
+
+ public boolean isSslEnabled() {
+ return getBoolean(RangerPdpConstants.PROP_SSL_ENABLED, false);
+ }
+
+ public String getKeystoreFile() {
+ return get(RangerPdpConstants.PROP_SSL_KEYSTORE_FILE, "");
+ }
+
+ public String getKeystorePassword() {
+ return get(RangerPdpConstants.PROP_SSL_KEYSTORE_PASSWORD, "");
+ }
+
+ public String getKeystoreType() {
+ return get(RangerPdpConstants.PROP_SSL_KEYSTORE_TYPE, "JKS");
+ }
+
+ public boolean isTruststoreEnabled() {
+ return getBoolean(RangerPdpConstants.PROP_SSL_TRUSTSTORE_ENABLED, false);
+ }
+
+ public String getTruststoreFile() {
+ return get(RangerPdpConstants.PROP_SSL_TRUSTSTORE_FILE, "");
+ }
+
+ public String getTruststorePassword() {
+ return get(RangerPdpConstants.PROP_SSL_TRUSTSTORE_PASSWORD, "");
+ }
+
+ public String getTruststoreType() {
+ return get(RangerPdpConstants.PROP_SSL_TRUSTSTORE_TYPE, "JKS");
+ }
+
+ public boolean isHttp2Enabled() {
+ return getBoolean(RangerPdpConstants.PROP_HTTP2_ENABLED, true);
+ }
+
+ public int getHttpConnectorMaxThreads() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_MAX_THREADS, 200);
+ }
+
+ public int getHttpConnectorMinSpareThreads() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_MIN_SPARE_THREADS, 20);
+ }
+
+ public int getHttpConnectorAcceptCount() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_ACCEPT_COUNT, 100);
+ }
+
+ public int getHttpConnectorMaxConnections() {
+ return getInt(RangerPdpConstants.PROP_HTTP_CONNECTOR_MAX_CONNECTIONS, 10000);
+ }
+
+ public String getAuthTypes() {
+ return get(RangerPdpConstants.PROP_AUTH_TYPES, "jwt,kerberos");
+ }
+
+ // --- HTTP Header auth ---
+ public boolean isHeaderAuthnEnabled() {
+ return getBoolean(RangerPdpConstants.PROP_AUTHN_HEADER_ENABLED, false);
+ }
+
+ public String getHeaderAuthnUsername() {
+ return get(RangerPdpConstants.PROP_AUTHN_HEADER_USERNAME, "X-Forwarded-User");
+ }
+
+ // --- JWT bearer token auth ---
+ public String getJwtProviderUrl() {
+ return get(RangerPdpConstants.PROP_AUTHN_JWT_PROVIDER_URL, "");
+ }
+
+ public String getJwtPublicKey() {
+ return get(RangerPdpConstants.PROP_AUTHN_JWT_PUBLIC_KEY, "");
+ }
+
+ public String getJwtCookieName() {
+ return get(RangerPdpConstants.PROP_AUTHN_JWT_COOKIE_NAME, "hadoop-jwt");
+ }
+
+ public String getJwtAudiences() {
+ return get(RangerPdpConstants.PROP_AUTHN_JWT_AUDIENCES, "");
+ }
+
+ // --- Kerberos / SPNEGO ---
+ public String getSpnegoPrincipal() {
+ return get(RangerPdpConstants.PROP_AUTHN_KERBEROS_SPNEGO_PRINCIPAL, "");
+ }
+
+ public String getSpnegoKeytab() {
+ return get(RangerPdpConstants.PROP_AUTHN_KERBEROS_SPNEGO_KEYTAB, "");
+ }
+
+ public int getKerberosTokenValiditySeconds() {
+ return getInt(RangerPdpConstants.PROP_AUTHN_KERBEROS_KRB_TOKEN_VALIDITY, 30);
+ }
+
+ public String getKerberosCookieDomain() {
+ return get(RangerPdpConstants.PROP_AUTHN_KERBEROS_KRB_COOKIE_DOMAIN, "");
+ }
+
+ public String getKerberosCookiePath() {
+ return get(RangerPdpConstants.PROP_AUTHN_KERBEROS_KRB_COOKIE_PATH, "/");
+ }
+
+ public String getKerberosNameRules() {
+ return get(RangerPdpConstants.PROP_KRB_NAME_RULES, "DEFAULT");
+ }
+
+ /**
+ * Returns all properties for forwarding to {@code RangerEmbeddedAuthorizer}.
+ */
+ public Properties getAuthzProperties() {
+ return new Properties(props);
+ }
+
+ public String get(String key, String defaultValue) {
+ String val = props.getProperty(key);
+
+ return StringUtils.isNotBlank(val) ? val.trim() : defaultValue;
+ }
+
+ public int getInt(String key, int defaultValue) {
+ String val = props.getProperty(key);
+
+ if (StringUtils.isNotBlank(val)) {
+ try {
+ return Integer.parseInt(val.trim());
+ } catch (NumberFormatException e) {
+ LOG.warn("Invalid integer for {}: '{}'; using default {}", key, val, defaultValue);
+ }
+ }
+
+ return defaultValue;
+ }
+
+ public boolean getBoolean(String key, boolean defaultValue) {
+ String val = props.getProperty(key);
+
+ return StringUtils.isNotBlank(val) ? Boolean.parseBoolean(val.trim()) : defaultValue;
+ }
+
+ private void loadFromClasspath(String resourceName) {
+ try (InputStream in = getClass().getClassLoader().getResourceAsStream(resourceName)) {
+ if (in != null) {
+ parseHadoopXml(in, resourceName);
+ } else {
+ LOG.debug("Config resource not found on classpath: {}", resourceName);
+ }
+ } catch (IOException e) {
+ LOG.warn("Failed to close stream for classpath resource: {}", resourceName, e);
+ }
+ }
+
+ private void loadFromFile(File file) {
+ if (!file.exists() || !file.isFile()) {
+ LOG.debug("Config file not found: {}", file);
+ return;
+ }
+
+ try (InputStream in = new FileInputStream(file)) {
+ parseHadoopXml(in, file.getAbsolutePath());
+ } catch (IOException e) {
+ LOG.warn("Failed to read config file: {}", file, e);
+ }
+ }
+
+ /**
+ * Parses a Hadoop-style {@code <configuration>} XML document and merges all
+ * {@code <property>} entries into {@link #props}. Later entries override earlier
+ * ones, matching Hadoop's own override semantics.
+ *
+ * <pre>
+ * {@code
+ * <configuration>
+ * <property>
+ * <name>some.key</name>
+ * <value>some-value</value>
+ * </property>
+ * </configuration>
+ * }
+ * </pre>
+ */
+ private void parseHadoopXml(InputStream in, String source) {
+ XMLUtils.loadConfig(in, props);
+
+ LOG.info("Loaded {} properties from {}", props.size(), source);
+ }
Review Comment:
The log message in `parseHadoopXml()` reports `props.size()`, which is the cumulative size after merging properties from potentially multiple sources. This makes it look like each file loaded N properties even when it didn't. Consider tracking and logging the delta (size before/after) or logging `nList.getLength()`/properties count from the parsed document instead.
##########
pdp/src/main/java/org/apache/ranger/pdp/rest/RangerPdpREST.java:
##########
@@ -0,0 +1,498 @@
+/*
+ * 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.rest;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.MapUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.ranger.authz.api.RangerAuthorizer;
+import org.apache.ranger.authz.api.RangerAuthzException;
+import org.apache.ranger.authz.model.RangerAccessInfo;
+import org.apache.ranger.authz.model.RangerAuthzRequest;
+import org.apache.ranger.authz.model.RangerAuthzResult;
+import org.apache.ranger.authz.model.RangerMultiAuthzRequest;
+import org.apache.ranger.authz.model.RangerMultiAuthzResult;
+import org.apache.ranger.authz.model.RangerResourceInfo;
+import org.apache.ranger.authz.model.RangerResourcePermissions;
+import org.apache.ranger.authz.model.RangerResourcePermissionsRequest;
+import org.apache.ranger.authz.model.RangerUserInfo;
+import org.apache.ranger.pdp.RangerPdpStats;
+import org.apache.ranger.pdp.config.RangerPdpConfig;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+import static javax.ws.rs.core.Response.Status.FORBIDDEN;
+import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
+import static javax.ws.rs.core.Response.Status.UNAUTHORIZED;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_PDP_SERVICE_PREFIX;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.PROP_SUFFIX_DELEGATION_USERS;
+import static org.apache.ranger.pdp.config.RangerPdpConstants.WILDCARD_SERVICE_NAME;
+
+/**
+ * REST resource that exposes the three core {@link RangerAuthorizer} methods over HTTP.
+ *
+ * <p>All endpoints are under {@code /authz/v1} and produce/consume {@code application/json}.
+ * Authentication is enforced upstream by {@link RangerPdpAuthFilter}; the authenticated
+ * caller's identity is read from the {@link RangerPdpConstants#ATTR_AUTHENTICATED_USER}
+ * request attribute.
+ *
+ * <table border="1">
+ * <tr><th>Method</th><th>Path</th><th>Request body</th><th>Response body</th></tr>
+ * <tr><td>POST</td><td>/authz/v1/authorize</td>
+ * <td>{@link RangerAuthzRequest}</td><td>{@link RangerAuthzResult}</td></tr>
+ * <tr><td>POST</td><td>/authz/v1/authorizeMulti</td>
+ * <td>{@link RangerMultiAuthzRequest}</td><td>{@link RangerMultiAuthzResult}</td></tr>
+ * <tr><td>POST</td><td>/authz/v1/permissions</td>
+ * <td>{@link RangerResourcePermissionsRequest}</td>
+ * <td>{@link RangerResourcePermissions}</td></tr>
+ * </table>
+ */
+@Path("/v1")
+@Produces(MediaType.APPLICATION_JSON)
+@Consumes(MediaType.APPLICATION_JSON)
+@Singleton
+public class RangerPdpREST {
+ private static final Logger LOG = LoggerFactory.getLogger(RangerPdpREST.class);
+
+ private static final Response RESPONSE_OK = Response.ok().build();
+
+ private final Map<String, Set<String>> delegationUsersByService = new HashMap<>();
+
+ @Inject
+ private RangerAuthorizer authorizer;
+
+ @Inject
+ private RangerPdpConfig config;
+
+ @Context
+ private ServletContext servletContext;
+
+ @PostConstruct
+ public void initialize() {
+ initializeDelegationUsers();
+ }
+
+ /**
+ * Evaluates a single access request.
+ *
+ * @param request the authorization request
+ * @return {@code 200 OK} with {@link RangerAuthzResult}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/authorize")
+ public Response authorize(RangerAuthzRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String requestId = request != null ? request.getRequestId() : null;
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+ RangerUserInfo user = request != null ? request.getUser() : null;
+ RangerAccessInfo access = request != null ? request.getAccess() : null;
+
+ LOG.debug("==> authorize(requestId={}, caller={}, serviceName={})", requestId, caller, serviceName);
+
+ ret = validateCaller(caller, user, access, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
+ RangerAuthzResult result = authorizer.authorize(request);
+
+ ret = Response.ok(result).build();
+ } catch (RangerAuthzException e) {
+ LOG.warn("authorize(requestId={}): authorization error; caller={}", requestId, caller, e);
+
+ ret = badRequest(e);
+ } catch (Exception e) {
+ LOG.error("authorize(requestId={}): internal error; caller={}", requestId, caller, e);
+
+ ret = serverError();
+ }
+ }
+
+ LOG.debug("<== authorize(requestId={}, caller={}, serviceName={}): ret={}", requestId, caller, serviceName, ret != null ? ret.getStatus() : null);
+ } finally {
+ recordRequestMetrics(ret, startNanos, httpRequest);
+ }
+
+ return ret;
+ }
+
+ /**
+ * Evaluates multiple access requests in a single call.
+ *
+ * @param request the multi-access authorization request
+ * @return {@code 200 OK} with {@link RangerMultiAuthzResult}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/authorizeMulti")
+ public Response authorizeMulti(RangerMultiAuthzRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String requestId = request != null ? request.getRequestId() : null;
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+ RangerUserInfo user = request != null ? request.getUser() : null;
+ List<RangerAccessInfo> accesses = request != null ? request.getAccesses() : null;
+
+ LOG.debug("==> authorizeMulti(requestId={}, caller={}, serviceName={})", requestId, caller, serviceName);
+
+ ret = validateCaller(caller, user, accesses, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
+ RangerMultiAuthzResult result = authorizer.authorize(request);
+
+ ret = Response.ok(result).build();
+ } catch (RangerAuthzException e) {
+ LOG.warn("authorizeMulti(requestId={}): authorization error; caller={}", requestId, caller, e);
+
+ ret = badRequest(e);
+ } catch (Exception e) {
+ LOG.error("authorizeMulti(requestId={}): internal error; caller={}", requestId, caller, e);
+
+ ret = serverError();
+ }
+ }
+
+ LOG.debug("<== authorizeMulti(requestId={}, caller={}, serviceName={}): ret={}", requestId, caller, serviceName, ret != null ? ret.getStatus() : null);
+ } finally {
+ recordRequestMetrics(ret, startNanos, httpRequest);
+ }
+
+ return ret;
+ }
+
+ /**
+ * Returns the effective permissions for a resource, broken down by user/group/role.
+ *
+ * @param request wrapper containing the resource info and access context
+ * @return {@code 200 OK} with {@link RangerResourcePermissions}, or {@code 400} / {@code 500} on error
+ */
+ @POST
+ @Path("/permissions")
+ public Response getResourcePermissions(RangerResourcePermissionsRequest request, @Context HttpServletRequest httpRequest) {
+ long startNanos = System.nanoTime();
+ Response ret = null;
+
+ try {
+ String caller = getAuthenticatedUser(httpRequest);
+ String serviceName = getServiceName(request);
+
+ LOG.debug("==> getResourcePermissions(caller={}, serviceName={})", caller, serviceName);
+
+ ret = validateCaller(caller, serviceName);
+
+ if (RESPONSE_OK.equals(ret)) {
+ try {
+ RangerResourcePermissions result = authorizer.getResourcePermissions(request);
+
+ ret = Response.ok(result).build();
+ } catch (RangerAuthzException e) {
+ LOG.warn("getResourcePermissions(): validation error; caller={}", caller, e);
+
+ ret = badRequest(e);
+ } catch (Exception e) {
+ LOG.error("getResourcePermissions(): unexpected error; caller={}", caller, e);
+
+ ret = serverError();
+ }
+ }
+
+ LOG.debug("<== getResourcePermissions(caller={}, serviceName={}): ret={}", caller, serviceName, ret != null ? ret.getStatus() : null);
+ } finally {
+ recordRequestMetrics(ret, startNanos, httpRequest);
+ }
+
+ return ret;
+ }
+
+ private String getAuthenticatedUser(HttpServletRequest httpRequest) {
+ Object user = httpRequest.getAttribute(RangerPdpConstants.ATTR_AUTHENTICATED_USER);
+
+ return user != null ? user.toString() : null;
+ }
+
+ private static String getServiceName(RangerAuthzRequest request) {
+ return request != null && request.getContext() != null ? request.getContext().getServiceName() : null;
+ }
+
+ private static String getServiceName(RangerMultiAuthzRequest request) {
+ return request != null && request.getContext() != null ? request.getContext().getServiceName() : null;
+ }
+
+ private static String getServiceName(RangerResourcePermissionsRequest request) {
+ return request != null && request.getContext() != null ? request.getContext().getServiceName() : null;
+ }
+
+ private Response validateCaller(String caller, RangerUserInfo user, RangerAccessInfo access, String serviceName) {
+ final Response ret;
+
+ if (StringUtils.isBlank(caller)) {
+ ret = Response.status(UNAUTHORIZED)
+ .entity(new ErrorResponse(UNAUTHORIZED, "Authentication required"))
+ .build();
+ } else {
+ boolean needsDelegation = isDelegationNeeded(caller, user) || isDelegationNeeded(access);
+
+ if (needsDelegation) {
+ if (!isDelegationUserForService(serviceName, caller)) {
+ LOG.info("{} is not a delegation user in service {}", caller, serviceName);
+
+ ret = Response.status(FORBIDDEN)
+ .entity(new ErrorResponse(FORBIDDEN, caller + " is not authorized"))
+ .build();
+ } else {
+ ret = RESPONSE_OK;
+ }
+ } else {
+ ret = RESPONSE_OK;
+ }
+ }
+
+ return ret;
+ }
+
+ private Response validateCaller(String caller, RangerUserInfo user, List<RangerAccessInfo> accesses, String serviceName) {
+ final Response ret;
+
+ if (StringUtils.isBlank(caller)) {
+ ret = Response.status(UNAUTHORIZED)
+ .entity(new ErrorResponse(UNAUTHORIZED, "Authentication required"))
+ .build();
+ } else {
+ boolean needsDelegation = isDelegationNeeded(caller, user) || isDelegationNeeded(accesses);
+
+ if (needsDelegation) {
+ if (!isDelegationUserForService(serviceName, caller)) {
+ LOG.info("{} is not a delegation user in service {}", caller, serviceName);
+
+ ret = Response.status(FORBIDDEN)
+ .entity(new ErrorResponse(FORBIDDEN, caller + " is not authorized"))
+ .build();
+ } else {
+ ret = RESPONSE_OK;
+ }
+ } else {
+ ret = RESPONSE_OK;
+ }
+ }
+
+ return ret;
+ }
+
+ private Response validateCaller(String caller, String serviceName) {
+ final Response ret;
+
+ if (StringUtils.isBlank(caller)) {
+ ret = Response.status(UNAUTHORIZED)
+ .entity(new ErrorResponse(UNAUTHORIZED, "Authentication required"))
+ .build();
+ } else if (!isDelegationUserForService(serviceName, caller)) {
+ LOG.info("{} is not a delegation user in service {}", caller, serviceName);
+
+ ret = Response.status(FORBIDDEN)
+ .entity(new ErrorResponse(FORBIDDEN, caller + " is not authorized"))
+ .build();
+ } else {
+ ret = RESPONSE_OK;
+ }
+
+ return ret;
+ }
+
+ private boolean isDelegationNeeded(String caller, RangerUserInfo user) {
+ String userName = user != null ? user.getName() : null;
+ boolean needsDelegation = !caller.equals(userName);
+
+ if (!needsDelegation) {
+ // don't trust user-attributes/groups/roles if caller doesn't have delegation permission
+ needsDelegation = MapUtils.isNotEmpty(user.getAttributes()) || CollectionUtils.isNotEmpty(user.getGroups()) || CollectionUtils.isNotEmpty(user.getRoles());
+ }
Review Comment:
`isDelegationNeeded(String caller, RangerUserInfo user)` treats a missing/blank `user` as requiring delegation (because `caller.equals(null)` is always false). This causes invalid requests that should be rejected as `400` by `RangerAuthorizer.validateUserInfo()` to instead be rejected as `403` unless the caller is configured as a delegation user. Consider returning `false` (or skipping delegation checks) when `user == null` or `user.getName()` is blank, so request validation errors surface as `BAD_REQUEST` instead of `FORBIDDEN`.
##########
pdp/src/main/java/org/apache/ranger/pdp/security/KerberosAuthHandler.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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.commons.codec.binary.Base64;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.security.authentication.util.KerberosName;
+import org.apache.ranger.pdp.config.RangerPdpConstants;
+import org.ietf.jgss.GSSContext;
+import org.ietf.jgss.GSSCredential;
+import org.ietf.jgss.GSSException;
+import org.ietf.jgss.GSSManager;
+import org.ietf.jgss.Oid;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.auth.Subject;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.auth.login.AppConfigurationEntry;
+import javax.security.auth.login.Configuration;
+import javax.security.auth.login.LoginContext;
+import javax.security.auth.login.LoginException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.io.IOException;
+import java.security.PrivilegedExceptionAction;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Authenticates requests using Kerberos SPNEGO (HTTP Negotiate).
+ *
+ * <p>Uses the JDK's built-in GSSAPI/JGSS support – no external Kerberos library is required.
+ * The service principal and keytab must be configured via:
+ * <ul>
+ * <li>{@code ranger.pdp.kerberos.spnego.principal} – e.g. {@code HTTP/host.example.com@REALM}
+ * <li>{@code ranger.pdp.kerberos.spnego.keytab} – absolute path to the keytab file
+ * <li>{@code hadoop.security.auth_to_local} – Hadoop-style name rules (default: {@code DEFAULT})
+ * </ul>
+ *
+ * <p>Authentication flow:
+ * <ol>
+ * <li>If no {@code Authorization: Negotiate} header is present the handler returns {@code SKIP}.
+ * <li>The SPNEGO token is extracted, validated via GSSAPI, and – if a response token is
+ * produced (mutual authentication) – written to {@code WWW-Authenticate: Negotiate <token>}.
+ * <li>On success the short-form principal name (strip {@literal @REALM} and host components)
+ * is returned as the authenticated user.
+ * <li>On failure a {@code 401 Negotiate} challenge is sent and {@code CHALLENGE} is returned.
+ * </ol>
+ */
+public class KerberosAuthHandler implements PdpAuthHandler {
+ private static final Logger LOG = LoggerFactory.getLogger(KerberosAuthHandler.class);
+
+ public static final String AUTH_TYPE = "KERBEROS";
+
+ private static final String NEGOTIATE_PREFIX = "Negotiate ";
+ private static final String WWW_AUTHENTICATE = "WWW-Authenticate";
+ private static final String AUTHORIZATION = "Authorization";
+ private static final Oid SPNEGO_OID;
+ private static final Oid KRB5_OID;
+
+ static {
+ try {
+ SPNEGO_OID = new Oid("1.3.6.1.5.5.2");
+ KRB5_OID = new Oid("1.2.840.113554.1.2.2");
+ } catch (GSSException e) {
+ throw new ExceptionInInitializerError(e);
+ }
+ }
+
+ private Subject serviceSubject;
+
+ @Override
+ public void init(Properties config) throws Exception {
+ String principal = config.getProperty(RangerPdpAuthFilter.PARAM_SPNEGO_PRINCIPAL);
+ String keytab = config.getProperty(RangerPdpAuthFilter.PARAM_SPNEGO_KEYTAB);
+
+ if (StringUtils.isBlank(principal) || StringUtils.isBlank(keytab)) {
+ throw new IllegalArgumentException("Kerberos auth requires configurations " + RangerPdpConstants.PROP_AUTHN_KERBEROS_SPNEGO_PRINCIPAL + " and " + RangerPdpConstants.PROP_AUTHN_KERBEROS_SPNEGO_KEYTAB);
+ }
+
+ String configuredNameRules = config.getProperty(RangerPdpAuthFilter.PARAM_KRB_NAME_RULES, "DEFAULT");
+
+ serviceSubject = loginWithKeytab(principal, keytab);
+
+ initializeKerberosNameRules(configuredNameRules);
+
+ LOG.info("KerberosAuthHandler initialized; principal={}", principal);
+ }
+
+ @Override
+ public Result authenticate(HttpServletRequest request, HttpServletResponse response) throws IOException {
+ String authHeader = request.getHeader(AUTHORIZATION);
+
+ if (authHeader == null || !authHeader.startsWith(NEGOTIATE_PREFIX)) {
+ return Result.skip();
+ }
+
+ byte[] inputToken = Base64.decodeBase64(authHeader.substring(NEGOTIATE_PREFIX.length()).trim());
+
+ try {
+ return Subject.doAs(serviceSubject, (PrivilegedExceptionAction<Result>) () -> validateSpnegoToken(inputToken, response));
+ } catch (Exception e) {
+ LOG.warn("authenticate(): SPNEGO validation error", e);
+
+ sendUnauthorized(response, null);
+
+ return Result.challenge();
+ }
+ }
+
+ @Override
+ public String getChallengeHeader() {
+ return NEGOTIATE_PREFIX.trim();
+ }
+
+ private Result validateSpnegoToken(byte[] inputToken, HttpServletResponse response) throws GSSException, IOException {
+ GSSManager manager = GSSManager.getInstance();
+ GSSCredential serverCred = manager.createCredential(null, GSSCredential.INDEFINITE_LIFETIME, new Oid[] {SPNEGO_OID, KRB5_OID}, GSSCredential.ACCEPT_ONLY);
+ GSSContext gssCtx = manager.createContext(serverCred);
+
Review Comment:
`validateSpnegoToken()` creates a new `GSSCredential` (and `GSSContext`) on every request. Creating server credentials can be relatively expensive; consider caching the acceptor `GSSCredential` (created after keytab login) and reusing it across requests, while still creating a fresh `GSSContext` per request.
--
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: dev-unsubscribe@ranger.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] RANGER-5371: pdp server implementation [ranger]
Posted by "mneethiraj (via GitHub)" <gi...@apache.org>.
mneethiraj closed pull request #896: RANGER-5371: pdp server implementation
URL: https://github.com/apache/ranger/pull/896
--
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: dev-unsubscribe@ranger.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org