Copilot commented on code in PR #10968:
URL: https://github.com/apache/gravitino/pull/10968#discussion_r3182405957


##########
authenticators/authenticator-basic/src/main/java/org/apache/gravitino/auth/local/BasicAuthenticator.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.gravitino.auth.local;
+
+import java.nio.charset.StandardCharsets;
+import java.security.Principal;
+import java.util.Base64;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.UserPrincipal;
+import org.apache.gravitino.auth.AuthConstants;
+import org.apache.gravitino.exceptions.BadRequestException;
+import org.apache.gravitino.exceptions.UnauthorizedException;
+import org.apache.gravitino.server.authentication.Authenticator;
+
+/**
+ * Basic authenticator wiring for the local authentication module.
+ *
+ * <p>The credential verification flow will be implemented in follow-up 
subtasks. For now, keep the
+ * current Basic header parsing behavior so the module can be loaded safely 
when {@code
+ * gravitino.authenticators=basic}.
+ */
+public class BasicAuthenticator implements Authenticator {
+  private static final String BASIC_AUTH_CHALLENGE = "Basic";
+
+  @Override
+  public boolean isDataFromToken() {
+    return true;
+  }
+
+  @Override
+  public Principal authenticateToken(byte[] tokenData) {
+    if (tokenData == null) {
+      throw new UnauthorizedException("Empty token authorization header", 
BASIC_AUTH_CHALLENGE);
+    }
+
+    String authData = new String(tokenData, StandardCharsets.UTF_8);
+    if (authData.trim().isEmpty()) {
+      throw new UnauthorizedException("Empty token authorization header", 
BASIC_AUTH_CHALLENGE);
+    }
+
+    if (!authData.startsWith(AuthConstants.AUTHORIZATION_BASIC_HEADER)) {
+      throw new UnauthorizedException("Invalid token authorization header", 
BASIC_AUTH_CHALLENGE);
+    }
+
+    String credential = 
authData.substring(AuthConstants.AUTHORIZATION_BASIC_HEADER.length());
+    if (credential.trim().isEmpty()) {
+      throw new BadRequestException("Malformed Basic authorization header: 
missing credentials");
+    }
+
+    try {
+      String decodedCredential =
+          new String(Base64.getDecoder().decode(credential), 
StandardCharsets.UTF_8);
+      int separatorIndex = decodedCredential.indexOf(':');
+      if (separatorIndex < 0) {
+        throw new BadRequestException(
+            "Malformed Basic authorization header: credentials must be in 
username:password format");
+      }
+
+      String userName = decodedCredential.substring(0, separatorIndex);
+      if (userName.isEmpty()) {
+        throw new BadRequestException(
+            "Malformed Basic authorization header: username must not be 
empty");
+      }
+
+      return new UserPrincipal(userName, authData);
+    } catch (IllegalArgumentException e) {
+      throw new BadRequestException(e, "Malformed Basic authorization header: 
invalid base64");
+    }
+  }
+
+  @Override
+  public void initialize(Config config) {
+    // no-op
+  }
+
+  @Override
+  public boolean supportsToken(byte[] tokenData) {
+    return tokenData != null
+        && new String(tokenData, StandardCharsets.UTF_8)
+            .startsWith(AuthConstants.AUTHORIZATION_BASIC_HEADER);

Review Comment:
   `supportsToken()` returns false when the Authorization header is missing, so 
`AuthenticationFilter` never calls `authenticateToken()` for an unauthenticated 
request. With `basic` configured as the only authenticator, missing credentials 
therefore fall through to the generic 401 path without a `WWW-Authenticate: 
Basic` challenge, which breaks standard Basic-auth negotiation and contradicts 
the behavior tested in `authenticateToken(null)`.
   



##########
authenticators/authenticator-basic/src/main/java/org/apache/gravitino/auth/local/BasicAuthenticator.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.gravitino.auth.local;
+
+import java.nio.charset.StandardCharsets;
+import java.security.Principal;
+import java.util.Base64;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.UserPrincipal;
+import org.apache.gravitino.auth.AuthConstants;
+import org.apache.gravitino.exceptions.BadRequestException;
+import org.apache.gravitino.exceptions.UnauthorizedException;
+import org.apache.gravitino.server.authentication.Authenticator;
+
+/**
+ * Basic authenticator wiring for the local authentication module.
+ *
+ * <p>The credential verification flow will be implemented in follow-up 
subtasks. For now, keep the
+ * current Basic header parsing behavior so the module can be loaded safely 
when {@code
+ * gravitino.authenticators=basic}.
+ */
+public class BasicAuthenticator implements Authenticator {
+  private static final String BASIC_AUTH_CHALLENGE = "Basic";
+
+  @Override
+  public boolean isDataFromToken() {
+    return true;
+  }
+
+  @Override
+  public Principal authenticateToken(byte[] tokenData) {
+    if (tokenData == null) {
+      throw new UnauthorizedException("Empty token authorization header", 
BASIC_AUTH_CHALLENGE);
+    }
+
+    String authData = new String(tokenData, StandardCharsets.UTF_8);
+    if (authData.trim().isEmpty()) {
+      throw new UnauthorizedException("Empty token authorization header", 
BASIC_AUTH_CHALLENGE);
+    }
+
+    if (!authData.startsWith(AuthConstants.AUTHORIZATION_BASIC_HEADER)) {
+      throw new UnauthorizedException("Invalid token authorization header", 
BASIC_AUTH_CHALLENGE);
+    }
+
+    String credential = 
authData.substring(AuthConstants.AUTHORIZATION_BASIC_HEADER.length());
+    if (credential.trim().isEmpty()) {
+      throw new BadRequestException("Malformed Basic authorization header: 
missing credentials");
+    }
+
+    try {
+      String decodedCredential =
+          new String(Base64.getDecoder().decode(credential), 
StandardCharsets.UTF_8);
+      int separatorIndex = decodedCredential.indexOf(':');
+      if (separatorIndex < 0) {
+        throw new BadRequestException(
+            "Malformed Basic authorization header: credentials must be in 
username:password format");
+      }
+
+      String userName = decodedCredential.substring(0, separatorIndex);
+      if (userName.isEmpty()) {
+        throw new BadRequestException(
+            "Malformed Basic authorization header: username must not be 
empty");
+      }
+
+      return new UserPrincipal(userName, authData);

Review Comment:
   This stores the raw `Basic ...` header in `UserPrincipal`. That value 
contains the user's reusable username/password and is later forwarded verbatim 
by `UserPrincipalForwardingAuthManager`, so enabling access-token forwarding 
would leak local credentials to downstream services. Unlike bearer or negotiate 
tokens, Basic credentials should not be propagated this way.
   



##########
server/build.gradle.kts:
##########
@@ -24,6 +24,7 @@ plugins {
 }
 
 dependencies {
+  implementation(project(":authenticators:authenticator-basic"))

Review Comment:
   Only the main `server` module gets the new authenticator on its runtime 
classpath. `ServerAuthenticator` is also initialized by the Iceberg REST server 
and Lance REST server, but neither of those modules depends on 
`:authenticators:authenticator-basic`; configuring 
`gravitino.authenticators=basic` there will make `AuthenticatorFactory` fail 
with `ClassNotFoundException` at startup.



##########
authenticators/authenticator-basic/src/main/java/org/apache/gravitino/auth/local/BasicAuthenticator.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.gravitino.auth.local;
+
+import java.nio.charset.StandardCharsets;
+import java.security.Principal;
+import java.util.Base64;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.UserPrincipal;
+import org.apache.gravitino.auth.AuthConstants;
+import org.apache.gravitino.exceptions.BadRequestException;
+import org.apache.gravitino.exceptions.UnauthorizedException;
+import org.apache.gravitino.server.authentication.Authenticator;
+
+/**
+ * Basic authenticator wiring for the local authentication module.
+ *
+ * <p>The credential verification flow will be implemented in follow-up 
subtasks. For now, keep the
+ * current Basic header parsing behavior so the module can be loaded safely 
when {@code
+ * gravitino.authenticators=basic}.
+ */
+public class BasicAuthenticator implements Authenticator {
+  private static final String BASIC_AUTH_CHALLENGE = "Basic";
+
+  @Override
+  public boolean isDataFromToken() {
+    return true;
+  }
+
+  @Override
+  public Principal authenticateToken(byte[] tokenData) {
+    if (tokenData == null) {
+      throw new UnauthorizedException("Empty token authorization header", 
BASIC_AUTH_CHALLENGE);
+    }
+
+    String authData = new String(tokenData, StandardCharsets.UTF_8);
+    if (authData.trim().isEmpty()) {
+      throw new UnauthorizedException("Empty token authorization header", 
BASIC_AUTH_CHALLENGE);
+    }
+
+    if (!authData.startsWith(AuthConstants.AUTHORIZATION_BASIC_HEADER)) {
+      throw new UnauthorizedException("Invalid token authorization header", 
BASIC_AUTH_CHALLENGE);
+    }
+
+    String credential = 
authData.substring(AuthConstants.AUTHORIZATION_BASIC_HEADER.length());
+    if (credential.trim().isEmpty()) {
+      throw new BadRequestException("Malformed Basic authorization header: 
missing credentials");
+    }
+
+    try {
+      String decodedCredential =
+          new String(Base64.getDecoder().decode(credential), 
StandardCharsets.UTF_8);
+      int separatorIndex = decodedCredential.indexOf(':');
+      if (separatorIndex < 0) {
+        throw new BadRequestException(
+            "Malformed Basic authorization header: credentials must be in 
username:password format");
+      }
+
+      String userName = decodedCredential.substring(0, separatorIndex);
+      if (userName.isEmpty()) {
+        throw new BadRequestException(
+            "Malformed Basic authorization header: username must not be 
empty");
+      }
+
+      return new UserPrincipal(userName, authData);

Review Comment:
   The new Basic-auth parser only has negative-path tests. There is no test 
covering a valid `Basic <base64(username:password)>` header or the 
`username:password` delimiter checks, so regressions in the main authentication 
path can slip through unnoticed.



-- 
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]

Reply via email to