This is an automated email from the ASF dual-hosted git repository.

smengcl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 268654683b4 Handle missing RPC user in OzoneManager and 
OMClientRequest.
268654683b4 is described below

commit 268654683b495fcf43c0f1335e1cc380282c9ee6
Author: Siyao Meng <[email protected]>
AuthorDate: Sat Oct 17 01:03:38 2026 -0700

    Handle missing RPC user in OzoneManager and OMClientRequest.
---
 .../ozone/om/TestOMAdminRpcAuthorization.java      |  41 +++++++++
 .../om/TestOMHALeaderSpecificACLEnforcement.java   |   2 +-
 .../hadoop/ozone/MiniOzoneHAClusterImpl.java       |   6 +-
 .../org/apache/hadoop/ozone/om/OzoneManager.java   |  29 ++++--
 .../hadoop/ozone/om/request/OMClientRequest.java   |   5 +-
 .../ozone/om/TestOzoneManagerPrivilegeCheck.java   | 100 +++++++++++++++++++++
 .../request/TestOMClientRequestWithUserInfo.java   |  14 +++
 7 files changed, 183 insertions(+), 14 deletions(-)

diff --git 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOMAdminRpcAuthorization.java
 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOMAdminRpcAuthorization.java
index 02b214af9bc..a0cf9c2b07f 100644
--- 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOMAdminRpcAuthorization.java
+++ 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOMAdminRpcAuthorization.java
@@ -23,6 +23,7 @@
 import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ACL_ENABLED;
 import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ADMINISTRATORS;
 import static 
org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_CLIENT_FAILOVER_MAX_ATTEMPTS_KEY;
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -52,6 +53,8 @@
 import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.EchoRPCRequest;
 import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest;
 import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMResponse;
+import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.SafeMode;
+import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.SetSafeModeRequest;
 import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.Status;
 import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.Type;
 import org.apache.hadoop.security.UserGroupInformation;
@@ -212,6 +215,25 @@ void getOMConfigurationAdminIsAllowed() throws Exception {
     assertFalse(omConfig.getCurrentPeerList().isEmpty());
   }
 
+  @Test
+  void listTenantWithoutRpcUserIsDenied() {
+    OMException ex = assertThrows(OMException.class, () -> 
cluster.getOzoneManager().listTenant());
+    assertEquals(OMException.ResultCodes.PERMISSION_DENIED, ex.getResult());
+  }
+
+  @Test
+  void listTenantAdminIsAllowed() throws Exception {
+    Server.Call call = mock(Server.Call.class);
+    when(call.getRemoteUser())
+        .thenReturn(UserGroupInformation.createUserForTesting("admin", new 
String[] {"admins"}));
+    Server.getCurCall().set(call);
+    try {
+      
assertThat(cluster.getOzoneManager().listTenant().getTenantStateList()).isEmpty();
+    } finally {
+      Server.getCurCall().remove();
+    }
+  }
+
   @Test
   void bootstrapNonAdminIsDenied() {
     UserGroupInformation nonAdmin =
@@ -265,4 +287,23 @@ void echoRPCWithoutRpcUserIsDenied(boolean writeToRatis) 
throws Exception {
       Server.getCurCall().remove();
     }
   }
+
+  @Test
+  void forceExitSafeModeWithoutRpcUserIsDenied() throws Exception {
+    OMRequest request = OMRequest.newBuilder()
+        .setCmdType(Type.SetSafeMode)
+        .setClientId(ClientId.randomId().toString())
+        
.setSetSafeModeRequest(SetSafeModeRequest.newBuilder().setSafeMode(SafeMode.FORCE_EXIT))
+        .build();
+
+    // The call context that OzoneManagerServiceGrpc sets up: a client ID, but 
no connection and so no user.
+    Server.getCurCall().set(new Server.Call(1, 1, null, null, 
RPC.RpcKind.RPC_PROTOCOL_BUFFER,
+        ClientId.randomId().toByteString().toByteArray()));
+    try {
+      OMResponse response = 
cluster.getOzoneManager().getOmServerProtocol().submitRequest(null, request);
+      assertEquals(Status.PERMISSION_DENIED, response.getStatus());
+    } finally {
+      Server.getCurCall().remove();
+    }
+  }
 }
diff --git 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOMHALeaderSpecificACLEnforcement.java
 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOMHALeaderSpecificACLEnforcement.java
index 43acb0f823d..22553de8ca5 100644
--- 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOMHALeaderSpecificACLEnforcement.java
+++ 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOMHALeaderSpecificACLEnforcement.java
@@ -110,7 +110,7 @@ public void shutdown() {
   public void restoreLeadership() throws IOException, InterruptedException, 
TimeoutException {
     OzoneManager currentLeader = cluster.getOMLeader();
     if (!currentLeader.getOMNodeId().equals(theLeaderOM.getOMNodeId())) {
-      currentLeader.transferLeadership(theLeaderOM.getOMNodeId());
+      
client.getProxy().getOzoneManagerClient().transferLeadership(theLeaderOM.getOMNodeId());
       GenericTestUtils.waitFor(() -> {
         try {
           OzoneManager currentLeaderCheck = cluster.getOMLeader();
diff --git 
a/hadoop-ozone/mini-cluster/src/main/java/org/apache/hadoop/ozone/MiniOzoneHAClusterImpl.java
 
b/hadoop-ozone/mini-cluster/src/main/java/org/apache/hadoop/ozone/MiniOzoneHAClusterImpl.java
index 8df0f587c60..21ba4a74732 100644
--- 
a/hadoop-ozone/mini-cluster/src/main/java/org/apache/hadoop/ozone/MiniOzoneHAClusterImpl.java
+++ 
b/hadoop-ozone/mini-cluster/src/main/java/org/apache/hadoop/ozone/MiniOzoneHAClusterImpl.java
@@ -1000,8 +1000,10 @@ public OzoneManager 
transferOMLeadershipToAnotherNode(OzoneManager currentLeader
     OzoneManager targetOM = omList.get(0);
     String targetNodeId = targetOM.getOMNodeId();
 
-    // Transfer leadership
-    currentLeader.transferLeadership(targetNodeId);
+    // Transfer leadership over RPC, so the admin check sees the caller
+    try (OzoneClient client = createClient()) {
+      
client.getProxy().getOzoneManagerClient().transferLeadership(targetNodeId);
+    }
 
     // Wait for leadership transfer to complete
     GenericTestUtils.waitFor(() -> {
diff --git 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneManager.java
 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneManager.java
index a19a054dd99..99eed192ae6 100644
--- 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneManager.java
+++ 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneManager.java
@@ -2721,6 +2721,17 @@ private boolean isAllowedDelegationTokenOp() throws 
IOException {
         || (authMethod == AuthenticationMethod.CERTIFICATE);
   }
 
+  /**
+   * Returns the caller of a delegation token operation. With security enabled 
the call must carry an RPC user.
+   */
+  private UserGroupInformation getDelegationTokenCaller() throws IOException {
+    if (ProtobufRpcEngine.Server.getRemoteUser() == null && 
isSecurityEnabled()) {
+      throw new OMException("Delegation token operations require an RPC user."
+          + " Send the request over the OM RPC transport.", PERMISSION_DENIED);
+    }
+    return getRemoteUser();
+  }
+
   /**
    * Returns authentication method used to establish the connection.
    *
@@ -2728,7 +2739,7 @@ private boolean isAllowedDelegationTokenOp() throws 
IOException {
    */
   private AuthenticationMethod getConnectionAuthenticationMethod()
       throws IOException {
-    UserGroupInformation ugi = getRemoteUser();
+    UserGroupInformation ugi = getDelegationTokenCaller();
     AuthenticationMethod authMethod = ugi.getAuthenticationMethod();
     if (authMethod == AuthenticationMethod.PROXY) {
       authMethod = ugi.getRealUser().getAuthenticationMethod();
@@ -2756,7 +2767,7 @@ public Token<OzoneTokenIdentifier> 
getDelegationToken(Text renewer)
         return null;
       }
 
-      UserGroupInformation ugi = getRemoteUser();
+      UserGroupInformation ugi = getDelegationTokenCaller();
       String user = ugi.getUserName();
       Text owner = new Text(user);
       Text realUser = null;
@@ -2792,7 +2803,7 @@ public long 
renewDelegationToken(Token<OzoneTokenIdentifier> token)
             + "kerberos or web authentication",
             INVALID_AUTH_METHOD);
       }
-      String renewer = getRemoteUser().getShortUserName();
+      String renewer = getDelegationTokenCaller().getShortUserName();
       expiryTime = delegationTokenMgr.renewToken(token, renewer);
 
     } catch (OMException oex) {
@@ -2820,7 +2831,7 @@ public long 
renewDelegationToken(Token<OzoneTokenIdentifier> token)
   public void cancelDelegationToken(Token<OzoneTokenIdentifier> token)
       throws OMException {
     try {
-      String canceller = getRemoteUser().getUserName();
+      String canceller = getDelegationTokenCaller().getUserName();
       final OzoneTokenIdentifier id = delegationTokenMgr.cancelToken(token, 
canceller);
       LOG.trace("Delegation token cancelled for dt: {}", id);
     } catch (OMException oex) {
@@ -4068,7 +4079,7 @@ public boolean triggerRangerBGSync(boolean noWait) throws 
IOException {
       throw new OMException("OM is not leader or not ready", INVALID_REQUEST);
     }
 
-    final UserGroupInformation ugi = getRemoteUser();
+    final UserGroupInformation ugi = ProtobufRpcEngine.Server.getRemoteUser();
     // Check Ozone admin privilege
     if (isAdminAuthorizationEnabled() && !isAdmin(ugi)) {
       throw new OMException("Only Ozone admins are allowed to trigger "
@@ -4108,7 +4119,7 @@ public boolean triggerSnapshotDefrag(boolean noWait) 
throws IOException {
 
     // Note: Any OM (leader or follower) can run snapshot defrag
 
-    final UserGroupInformation ugi = getRemoteUser();
+    final UserGroupInformation ugi = ProtobufRpcEngine.Server.getRemoteUser();
     // Check Ozone admin privilege
     if (isAdminAuthorizationEnabled() && !isAdmin(ugi)) {
       throw new OMException("Only Ozone admins are allowed to trigger "
@@ -4222,7 +4233,7 @@ public TenantStateList listTenant() throws IOException {
 
     metrics.incNumTenantLists();
 
-    final UserGroupInformation ugi = getRemoteUser();
+    final UserGroupInformation ugi = ProtobufRpcEngine.Server.getRemoteUser();
     if (isAdminAuthorizationEnabled() && !isAdmin(ugi)) {
       final OMException omEx = new OMException(
           "Only Ozone admins are allowed to list tenants.", PERMISSION_DENIED);
@@ -5362,7 +5373,7 @@ public void checkAdminUserPrivilege(String operation) 
throws IOException {
       return;
     }
     
-    final UserGroupInformation ugi = getRemoteUser();
+    final UserGroupInformation ugi = ProtobufRpcEngine.Server.getRemoteUser();
     if (!isAdmin(ugi)) {
       throw new OMException("Only Ozone admins are allowed to " + operation,
           PERMISSION_DENIED);
@@ -5382,7 +5393,7 @@ private void checkGetDBUpdatesPrivilege() throws 
IOException {
       return;
     }
 
-    final UserGroupInformation ugi = getRemoteUser();
+    final UserGroupInformation ugi = ProtobufRpcEngine.Server.getRemoteUser();
     if (!isAdmin(ugi) && !isReadOnlyAdmin(ugi) && !isReconUser(ugi)) {
       throw new OMException("Only Ozone admins, read-only admins and Recon are 
allowed to "
           + "access the OM metadata database via getDBUpdates.", 
PERMISSION_DENIED);
diff --git 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/OMClientRequest.java
 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/OMClientRequest.java
index 55ef24bbbb9..00781d0c3ce 100644
--- 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/OMClientRequest.java
+++ 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/OMClientRequest.java
@@ -264,7 +264,8 @@ private static OzoneManagerProtocolProtos.UserInfo 
getUserInfo(
   /**
    * For non-rpc internal calls Server.getRemoteUser()
    * and Server.getRemoteIp() will be null.
-   * Passing getCurrentUser() and Ip of the Om node that started it.
+   * Passing getCurrentUser() and Ip of the Om node that started it,
+   * unless getUserInfo() already resolved a user name.
    * @return User Info.
    */
   public OzoneManagerProtocolProtos.UserInfo getUserIfNotExists(
@@ -283,7 +284,7 @@ public OzoneManagerProtocolProtos.UserInfo 
getUserIfNotExists(
         LOG.debug("Couldn't get om Rpc server address", e);
         return getUserInfo();
       }
-      newuserInfo.setUserName(user.getUserName());
+      newuserInfo.setUserName(userInfo.hasUserName() ? userInfo.getUserName() 
: user.getUserName());
       newuserInfo.setHostName(remoteAddress.getHostName());
       newuserInfo.setRemoteAddress(remoteAddress.getHostAddress());
       return newuserInfo.build();
diff --git 
a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/TestOzoneManagerPrivilegeCheck.java
 
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/TestOzoneManagerPrivilegeCheck.java
new file mode 100644
index 00000000000..888cf3cc050
--- /dev/null
+++ 
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/TestOzoneManagerPrivilegeCheck.java
@@ -0,0 +1,100 @@
+/*
+ * 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.hadoop.ozone.om;
+
+import static org.junit.jupiter.api.Assertions.assertAll;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.Mockito.doCallRealMethod;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.ipc_.Server;
+import org.apache.hadoop.ozone.om.exceptions.OMException;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.function.Executable;
+
+/**
+ * Test OzoneManager operations that depend on the RPC user of the call.
+ */
+class TestOzoneManagerPrivilegeCheck {
+
+  private OzoneManager om;
+
+  @BeforeEach
+  void setUp() throws Exception {
+    om = mock(OzoneManager.class);
+    when(om.isSecurityEnabled()).thenReturn(true);
+    when(om.isAdminAuthorizationEnabled()).thenReturn(true);
+    when(om.isLeaderReady()).thenReturn(true);
+    // Every user is an admin. any(Class) does not match null.
+    when(om.isAdmin(any(UserGroupInformation.class))).thenReturn(true);
+
+    doCallRealMethod().when(om).checkAdminUserPrivilege(any());
+    when(om.getQuotaRepairStatus()).thenCallRealMethod();
+    when(om.getDBUpdates(any())).thenCallRealMethod();
+    when(om.triggerRangerBGSync(anyBoolean())).thenCallRealMethod();
+    when(om.triggerSnapshotDefrag(anyBoolean())).thenCallRealMethod();
+    when(om.getDelegationToken(any())).thenCallRealMethod();
+    when(om.renewDelegationToken(any())).thenCallRealMethod();
+    doCallRealMethod().when(om).cancelDelegationToken(any());
+  }
+
+  @AfterEach
+  void tearDown() {
+    Server.getCurCall().remove();
+  }
+
+  @Test
+  void testAdminOperationsWithoutRpcUser() {
+    assertAll(
+        () -> assertPermissionDenied(() -> om.getQuotaRepairStatus()),
+        () -> assertPermissionDenied(() -> om.getDBUpdates(null)),
+        () -> assertPermissionDenied(() -> om.triggerRangerBGSync(true)),
+        () -> assertPermissionDenied(() -> om.triggerSnapshotDefrag(true)));
+  }
+
+  @Test
+  void testDelegationTokenOperationsWithoutRpcUser() {
+    assertAll(
+        () -> assertPermissionDenied(() -> om.getDelegationToken(new 
Text("renewer"))),
+        () -> assertPermissionDenied(() -> om.renewDelegationToken(null)),
+        () -> assertPermissionDenied(() -> om.cancelDelegationToken(null)));
+  }
+
+  @Test
+  void testAdminOperationWithRpcUser() {
+    Server.Call call = mock(Server.Call.class);
+    
when(call.getRemoteUser()).thenReturn(UserGroupInformation.createRemoteUser("alice"));
+    Server.getCurCall().set(call);
+
+    assertDoesNotThrow(() -> om.getQuotaRepairStatus());
+  }
+
+  private static void assertPermissionDenied(Executable operation) {
+    OMException e = assertThrows(OMException.class, operation);
+    assertEquals(OMException.ResultCodes.PERMISSION_DENIED, e.getResult());
+  }
+}
diff --git 
a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/TestOMClientRequestWithUserInfo.java
 
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/TestOMClientRequestWithUserInfo.java
index 6915d9b59a5..9dc8bb408c7 100644
--- 
a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/TestOMClientRequestWithUserInfo.java
+++ 
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/TestOMClientRequestWithUserInfo.java
@@ -31,6 +31,7 @@
 import io.grpc.Context;
 import java.io.IOException;
 import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.nio.file.Path;
 import java.util.UUID;
 import org.apache.hadoop.hdds.conf.OzoneConfiguration;
@@ -184,6 +185,19 @@ public void 
testClientSuppliedUserNameDoesNotOverrideS3AuthIdentity()
     assertEquals("AccessId", omClientRequest.getUserInfo().getUserName());
   }
 
+  @Test
+  public void testUserInfoWithoutRemoteAddress() throws Exception {
+    when(ozoneManager.getOmRpcServerAddr()).thenReturn(new 
InetSocketAddress(inetAddress, 9862));
+
+    OMRequest request = createRequestWithS3Credentials("AccessId", 
"Signature", "StringToSign");
+    OMClientRequest omClientRequest = new OMKeyCommitRequest(request, 
mock(BucketLayout.class));
+
+    OzoneManagerProtocolProtos.UserInfo userInfo = 
omClientRequest.getUserIfNotExists(ozoneManager);
+
+    assertEquals("AccessId", userInfo.getUserName());
+    assertEquals(inetAddress.getHostAddress(), userInfo.getRemoteAddress());
+  }
+
   @Test
   public void testUserInfoWithSTSToken() throws IOException {
     final String accessId = "ASIA12345";


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to