somandal commented on code in PR #16709: URL: https://github.com/apache/pinot/pull/16709#discussion_r2308754823
########## pinot-common/src/main/java/org/apache/pinot/common/audit/AuditIdentityResolver.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.pinot.common.audit; + +import com.nimbusds.jwt.JWT; +import com.nimbusds.jwt.JWTClaimsSet; +import com.nimbusds.jwt.JWTParser; +import javax.inject.Inject; +import javax.inject.Singleton; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.core.HttpHeaders; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +@Singleton Review Comment: nit: javadocs? ########## pinot-common/src/main/java/org/apache/pinot/common/audit/AuditIdentityResolver.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.pinot.common.audit; + +import com.nimbusds.jwt.JWT; +import com.nimbusds.jwt.JWTClaimsSet; +import com.nimbusds.jwt.JWTParser; +import javax.inject.Inject; +import javax.inject.Singleton; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.core.HttpHeaders; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +@Singleton +public class AuditIdentityResolver { + + private static final Logger LOG = LoggerFactory.getLogger(AuditIdentityResolver.class); + private static final String BEARER_PREFIX = "Bearer "; + + private final AuditConfigManager _configManager; + + @Inject + public AuditIdentityResolver(AuditConfigManager configManager) { + _configManager = configManager; + } + + public AuditEvent.UserIdentity resolveIdentity(ContainerRequestContext requestContext) { + AuditConfig config = _configManager.getCurrentConfig(); + + // Priority 1: Check custom identity header + String identityHeader = config.getUseridHeader(); + if (StringUtils.isNotBlank(identityHeader)) { + String principal = requestContext.getHeaderString(identityHeader); + if (StringUtils.isNotBlank(principal)) { + return new AuditEvent.UserIdentity().setPrincipal(principal); + } + } + + // Priority 2: Check JWT in Authorization header + String authHeader = requestContext.getHeaderString(HttpHeaders.AUTHORIZATION); + if (StringUtils.isNotBlank(authHeader) && authHeader.startsWith(BEARER_PREFIX)) { + String token = authHeader.substring(BEARER_PREFIX.length()).trim(); + String principal = extractJwtPrincipal(token, config.getUseridJwtClaimName()); + if (principal != null) { Review Comment: can principal be empty/blank? should the check be `StringUtils.isNotBlank` instead of null check? ########## pinot-common/src/main/java/org/apache/pinot/common/audit/AuditConfig.java: ########## @@ -84,10 +90,27 @@ public void setExcludedEndpoints(String excludedEndpoints) { _excludedEndpoints = excludedEndpoints; } + public String getUseridHeader() { + return _useridHeader; + } + + public void setUseridHeader(String useridHeader) { + _useridHeader = useridHeader; + } + + public String getUseridJwtClaimName() { + return _useridJwtClaimName; + } + + public void setUseridJwtClaimName(String useridJwtClaimName) { + _useridJwtClaimName = useridJwtClaimName; + } + @Override public String toString() { return "AuditConfig{" + "enabled=" + _enabled + ", captureRequestPayload=" + _captureRequestPayload + ", captureRequestHeaders='" + _captureRequestHeaders + "', maxPayloadSize=" + _maxPayloadSize - + ", excludedEndpoints='" + _excludedEndpoints + "'}"; + + ", excludedEndpoints='" + _excludedEndpoints + "', identityHeader='" + _useridHeader Review Comment: nit: should it be `useridHeader` instead of `identityHeader`? -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
