laserninja commented on code in PR #10668: URL: https://github.com/apache/gravitino/pull/10668#discussion_r3035160283
########## iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergAuthenticationFilter.java: ########## @@ -0,0 +1,75 @@ +/* + * 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.gravitino.iceberg.service; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.ImmutableMap; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import javax.servlet.http.HttpServletResponse; +import org.apache.gravitino.server.authentication.AuthenticationFilter; +import org.apache.iceberg.rest.responses.ErrorResponse; +import org.eclipse.jetty.http.HttpStatus; + +/** + * An {@link AuthenticationFilter} subclass for the Iceberg REST server that produces JSON error + * responses conforming to the Iceberg REST API specification. + * + * <p>When authentication fails, the default {@link AuthenticationFilter} calls {@code + * resp.sendError()} which produces HTML error pages via Jetty's default error handler. This + * subclass overrides the error response to write a proper Iceberg {@link ErrorResponse} JSON body, + * which Iceberg REST clients (e.g., the Java {@code RESTCatalog}) expect. + */ +public class IcebergAuthenticationFilter extends AuthenticationFilter { + + private static final ObjectMapper MAPPER = IcebergObjectMapper.getInstance(); + + // Error type names matching the Iceberg REST API specification examples. + // See https://github.com/apache/iceberg/blob/main/open-api/rest-catalog-open-api.yaml + static final Map<Integer, String> ERROR_TYPE_NAMES = Review Comment: Refactored `sendAuthErrorResponse` to accept an `Exception` instead of `(int status, String message)`: - `AuthenticationFilter.sendAuthErrorResponse(HttpServletResponse, Exception)` — base impl resolves `UnauthorizedException` → 401, others → 500 - `IcebergAuthenticationFilter` overrides it and calls `IcebergExceptionMapper.getErrorCode(exception)` to resolve the status code, reusing the same `EXCEPTION_ERROR_CODES` mapping used by the JAX-RS path - Added `UnauthorizedException` → 401 to `EXCEPTION_ERROR_CODES` and extracted a `public static getErrorCode(Exception)` method so the filter can share it - Tests updated to pass exception objects; added a `ForbiddenException` → 403 test case This unifies the exception → status code → type name resolution between both paths. Adding `AuthenticationTimeoutException` for expired tokens (as mentioned in #10672) can build on top of this, just add the exception class to `EXCEPTION_ERROR_CODES` and `ERROR_TYPE_NAMES`. -- 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]
