Copilot commented on code in PR #899: URL: https://github.com/apache/ranger/pull/899#discussion_r3010637117
########## pdp/src/main/java/org/apache/ranger/pdp/rest/RangerPdpREST.java: ########## @@ -0,0 +1,491 @@ +/* + * 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.OK; +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 Review Comment: `RESPONSE_OK` is a shared static `Response` instance used as a sentinel value during request processing. JAX-RS `Response` implementations are not guaranteed to be thread-safe for reuse, and this also makes it easier to accidentally return the sentinel (or record metrics against it) in future refactors. Consider replacing this with a boolean (e.g., `callerValid`) or creating a new OK response when needed. ########## 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: it says the filter init "skips" the header handler when disabled, but the assertion expects init() to throw ServletException (because no handlers get registered). Consider renaming the test to reflect the expected failure mode (e.g., init fails when all configured handlers are disabled). ########## pdp/scripts/ranger-pdp.sh: ########## @@ -0,0 +1,76 @@ +#!/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. + +### BEGIN INIT INFO +# Provides: ranger-pdp +# Required-Start: $local_fs $remote_fs $network $named $syslog $time +# Required-Stop: $local_fs $remote_fs $network $named $syslog $time +# Default-Start: 2 3 4 5 +# Default-Stop: +# Short-Description: Start/Stop Ranger PDP Server +### END INIT INFO + +LINUX_USER=ranger +BIN_PATH=/usr/bin +MOD_NAME=ranger-pdp-services.sh +pidf=/var/run/ranger/pdp.pid +pid="" + +if [ -f "${pidf}" ]; then + pid=$(cat "$pidf") +fi + +case $1 in + start) + if [ "${pid}" != "" ]; then + echo "Ranger PDP Service is already running [pid=${pid}]" + exit 1 + else + echo "Starting Ranger PDP Service." + /bin/su --login "${LINUX_USER}" -c "${BIN_PATH}/${MOD_NAME} start" + fi Review Comment: The init wrapper only checks whether the pidfile contains any value; it doesn't verify the PID is actually running. A stale pidfile will block `start` (and can misreport `status`). Consider validating `ps -p "$pid"` before treating it as running, and removing the pidfile if the PID is not alive (or delegating the check to ranger-pdp-services.sh). ########## 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 755 "$RANGER_PDP_LOG_DIR" + fi + + cp="${cdir}/conf:${cdir}/dist/*:${cdir}/lib/*" + + if [ -f "$pidf" ]; then + pid=$(cat "$pidf") + if ps -p "$pid" > /dev/null 2>&1; then + echo "Ranger PDP Service is already running [pid=${pid}]" + exit 0 + else + rm -f "$pidf" + fi + fi + + if [ -z "${RANGER_PDP_CONF_DIR}" ]; then + RANGER_PDP_CONF_DIR=${cdir}/conf + fi + + mkdir -p "${RANGER_PDP_PID_DIR_PATH}" + + SLEEP_TIME_AFTER_START=5 + nohup java -Dproc_rangerpdp ${JAVA_OPTS} \ + -Dlogdir="${RANGER_PDP_LOG_DIR}" \ + -Dlogback.configurationFile="file:${RANGER_PDP_CONF_DIR}/logback.xml" \ + -Dranger.pdp.conf.dir="${RANGER_PDP_CONF_DIR}" \ + -Duser="${USER}" \ + -Dhostname="${HOSTNAME}" \ + -cp "${cp}" \ + org.apache.ranger.pdp.RangerPdpServer \ + > "${RANGER_PDP_LOG_DIR}/pdp.out" 2>&1 & + + VALUE_OF_PID=$! + echo "Starting Ranger PDP Service" + sleep $SLEEP_TIME_AFTER_START + + if ps -p $VALUE_OF_PID > /dev/null 2>&1; then + echo $VALUE_OF_PID > "${pidf}" + chown "${UNIX_PDP_USER}" "${pidf}" 2>/dev/null || true + chmod 660 "${pidf}" + pid=$(cat "$pidf") + echo "Ranger PDP Service with pid ${pid} has started." + else + echo "Ranger PDP Service failed to start!" + exit 1 + fi + exit 0 + +elif [ "${action}" == "RUN" ]; 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 755 "$RANGER_PDP_LOG_DIR" + fi + + if [ -z "${RANGER_PDP_CONF_DIR}" ]; then + RANGER_PDP_CONF_DIR=${cdir}/conf + fi + + cp="${cdir}/conf:${cdir}/dist/*:${cdir}/lib/*" + exec java -Dproc_rangerpdp ${JAVA_OPTS} \ + -Dlogdir="${RANGER_PDP_LOG_DIR}" \ + -Dlogback.configurationFile="file:${RANGER_PDP_CONF_DIR}/logback.xml" \ + -Dranger.pdp.conf.dir="${RANGER_PDP_CONF_DIR}" \ + -Duser="${USER}" \ + -Dhostname="${HOSTNAME}" \ + -cp "${cp}" \ + org.apache.ranger.pdp.RangerPdpServer + +elif [ "${action}" == "STOP" ]; then + + WAIT_TIME_FOR_SHUTDOWN=2 + NR_ITER_FOR_SHUTDOWN_CHECK=15 + + if [ -f "$pidf" ]; then + pid=$(cat "$pidf") + else + pid=$(ps -ef | grep java | grep -- '-Dproc_rangerpdp' | grep -v grep | awk '{ print $2 }') + if [ "$pid" != "" ]; then + echo "pid file (${pidf}) not found; taking pid from 'ps' output." + else + echo "Ranger PDP Service is not running." + exit 0 + fi + fi + + echo "Stopping Ranger PDP Service (pid=${pid})..." + kill -15 "$pid" + + for ((i=0; i<NR_ITER_FOR_SHUTDOWN_CHECK; i++)); do + sleep $WAIT_TIME_FOR_SHUTDOWN + if ps -p "$pid" > /dev/null 2>&1; then + echo "Shutdown in progress. Checking again in ${WAIT_TIME_FOR_SHUTDOWN}s..." Review Comment: In the STOP path when the pidfile is missing, `pid=$(ps ... awk ...)` can expand to multiple PIDs (or include newlines). Because the script later runs `kill -15 "$pid"` and `ps -p "$pid"`, a multi-PID value will be treated as a single argument and can cause stop to fail. Consider selecting a single PID (e.g., first match) or iterating over all matches, and avoid quoting a whitespace-separated PID list if the intent is to handle multiple PIDs. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
