rameeshm commented on code in PR #847: URL: https://github.com/apache/ranger/pull/847#discussion_r2893042823
########## ranger-audit-server/ranger-audit-server-service/src/main/java/org/apache/ranger/audit/rest/AuditREST.java: ########## @@ -0,0 +1,373 @@ +/* + * 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.audit.rest; + +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.security.authentication.util.KerberosName; +import org.apache.ranger.audit.model.AuthzAuditEvent; +import org.apache.ranger.audit.producer.AuditDestinationMgr; +import org.apache.ranger.audit.provider.MiscUtil; +import org.apache.ranger.audit.server.AuditServerConfig; +import org.apache.ranger.audit.server.AuditServerConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +@Path("/audit") +@Component +@Scope("request") +public class AuditREST { + private static final Logger LOG = LoggerFactory.getLogger(AuditREST.class); + private static final Set<String> allowedServiceUsers; + + static { + allowedServiceUsers = initializeAllowedUsers(); + initializeAuthToLocal(); + } + + @Autowired + AuditDestinationMgr auditDestinationMgr; + + /** + * Health check endpoint + */ + @GET + @Path("/health") + @Produces("application/json") + public Response healthCheck() { + LOG.debug("==> AuditREST.healthCheck()"); + Response ret; + String jsonString; + + try { + // Check if audit destination manager is available and healthy + if (auditDestinationMgr != null) { + Map<String, Object> resp = new HashMap<>(); + resp.put("status", "UP"); + resp.put("service", "ranger-audit-server"); + jsonString = buildResponse(resp); + ret = Response.ok() + .entity(jsonString) + .build(); + } else { + Map<String, Object> resp = new HashMap<>(); + resp.put("status", "DOWN"); + resp.put("service", "ranger-audit-server"); + resp.put("reason", "AuditDestinationMgr not available"); + jsonString = buildResponse(resp); + ret = Response.status(Response.Status.SERVICE_UNAVAILABLE) + .entity(jsonString) + .build(); + } + } catch (Exception e) { + LOG.error("Health check failed", e); + Map<String, Object> resp = new HashMap<>(); + resp.put("status", "DOWN"); + resp.put("service", "ranger-audit-server"); + resp.put("reason", e.getMessage()); + jsonString = buildResponse(resp); + ret = Response.status(Response.Status.SERVICE_UNAVAILABLE) + .entity(jsonString) + .build(); + } + + LOG.debug("<== AuditREST.healthCheck(): {}", ret); + + return ret; + } + + /** + * Status endpoint for monitoring + */ + @GET + @Path("/status") + @Produces("application/json") + public Response getStatus() { + LOG.debug("==> AuditREST.getStatus()"); + + Response ret; + String jsonString; + + try { + String status = (auditDestinationMgr != null) ? "READY" : "NOT_READY"; + + Map<String, Object> resp = new HashMap<>(); + resp.put("status", status); + resp.put("timestamp", System.currentTimeMillis()); + resp.put("service", "ranger-audit-server"); + jsonString = buildResponse(resp); + ret = Response.status(Response.Status.OK) + .entity(jsonString) + .build(); + } catch (Exception e) { + LOG.error("Status check failed", e); + ret = Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity("{\"status\":\"ERROR\",\"error\":\"" + e.getMessage() + "\"}") + .build(); + } + + LOG.debug("<== AuditREST.getStatus(): {}", ret); + + return ret; + } + + /** + * Access Audits producer endpoint. + * @param serviceName Required query parameter to identify the source service (hdfs, hive, kafka, solr, etc.) + * @param appId Optional query parameter for batch processing - identifies the application instance + * @param accessAudits List of audit events to process + * @param request HTTP request to extract authenticated user + */ + @POST + @Path("/access") + @Consumes("application/json") + @Produces("application/json") + public Response logAccessAudit(@QueryParam("serviceName") String serviceName, @QueryParam("appId") String appId, List<AuthzAuditEvent> accessAudits, @Context HttpServletRequest request) { + String authenticatedUser = getAuthenticatedUser(request); + + LOG.debug("==> AuditREST.accessAudit(): received {} audit events from service: {}, appId: {}, authenticatedUser: {}", accessAudits != null ? accessAudits.size() : 0, StringUtils.isNotEmpty(serviceName) ? serviceName : "unknown", StringUtils.isNotEmpty(appId) ? appId : "none", authenticatedUser); + + Response ret; + + if (StringUtils.isEmpty(serviceName)) { + LOG.error("serviceName query parameter is required. Rejecting audit request."); + ret = Response.status(Response.Status.BAD_REQUEST) + .entity(buildErrorResponse("serviceName query parameter is required")) + .build(); + return ret; + } + + if (StringUtils.isEmpty(authenticatedUser)) { + LOG.error("No authenticated user found in request for service: {}. Rejecting audit request.", serviceName); + ret = Response.status(Response.Status.UNAUTHORIZED) + .entity(buildErrorResponse("Authentication required to send audit events")) + .build(); + return ret; + } + + if (!serviceName.equals(authenticatedUser)) { Review Comment: @mneethiraj I have removed this check as this may not be good idea like you mentioned. Already a check is there with a config in audit-server "ranger.audit.service.allowed.users" which will allow only those users ( service user) who are authenticated and also in this list will be the one allowed to commit audits into audit server. I think we don't need any more config in ranger-admin for it. -- 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]
