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 bfe90a9764a HDDS-17177. OM renew delegation token apply can write a 
canceled token back and can lower a token's stored expiry (#11642)
bfe90a9764a is described below

commit bfe90a9764ad193a12348e6e0ed90c5cc90a94b7
Author: Siyao Meng <[email protected]>
AuthorDate: Wed Oct 21 16:35:24 2026 -0700

    HDDS-17177. OM renew delegation token apply can write a canceled token back 
and can lower a token's stored expiry (#11642)
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../security/OMRenewDelegationTokenRequest.java    |  11 ++
 .../OzoneDelegationTokenSecretManager.java         |  21 ++-
 .../TestOzoneDelegationTokenSecretManager.java     | 153 +++++++++++++++++++++
 3 files changed, 184 insertions(+), 1 deletion(-)

diff --git 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/security/OMRenewDelegationTokenRequest.java
 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/security/OMRenewDelegationTokenRequest.java
index 38e86c3ce6f..794aa3ab1d7 100644
--- 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/security/OMRenewDelegationTokenRequest.java
+++ 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/security/OMRenewDelegationTokenRequest.java
@@ -18,6 +18,7 @@
 package org.apache.hadoop.ozone.om.request.security;
 
 import static org.apache.hadoop.ozone.om.OzoneManagerUtils.buildTokenAuditMap;
+import static 
org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.TOKEN_ERROR_OTHER;
 
 import java.io.IOException;
 import java.nio.file.InvalidPathException;
@@ -29,6 +30,7 @@
 import org.apache.hadoop.ozone.audit.OMAction;
 import org.apache.hadoop.ozone.om.OMMetadataManager;
 import org.apache.hadoop.ozone.om.OzoneManager;
+import org.apache.hadoop.ozone.om.exceptions.OMException;
 import org.apache.hadoop.ozone.om.execution.flowcontrol.ExecutionContext;
 import org.apache.hadoop.ozone.om.request.OMClientRequest;
 import org.apache.hadoop.ozone.om.request.util.OmResponseUtil;
@@ -156,6 +158,15 @@ public OMClientResponse 
validateAndUpdateCache(OzoneManager ozoneManager, Execut
           ozoneTokenIdentifier.getRenewer() == null ? "" :
               ozoneTokenIdentifier.getRenewer().toString());
 
+      // The token was validated in preExecute, but a cancel or another renew 
may have been applied since then.
+      if 
(ozoneManager.getDelegationTokenMgr().isCanceled(ozoneTokenIdentifier)) {
+        throw new OMException("Delegation token has been canceled: " + 
ozoneTokenIdentifier, TOKEN_ERROR_OTHER);
+      }
+      Long storedRenewTime = 
omMetadataManager.getDelegationTokenTable().get(ozoneTokenIdentifier);
+      if (storedRenewTime != null) {
+        renewTime = Math.min(ozoneTokenIdentifier.getMaxDate(), 
Math.max(renewTime, storedRenewTime));
+      }
+
       // Update in memory map of token.
       ozoneManager.getDelegationTokenMgr()
           .updateRenewToken(ozoneTokenIdentifierToken, ozoneTokenIdentifier,
diff --git 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/security/OzoneDelegationTokenSecretManager.java
 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/security/OzoneDelegationTokenSecretManager.java
index acce4015962..1e1243bf6f9 100644
--- 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/security/OzoneDelegationTokenSecretManager.java
+++ 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/security/OzoneDelegationTokenSecretManager.java
@@ -30,6 +30,7 @@
 import java.security.cert.X509Certificate;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
 import java.util.UUID;
 import java.util.concurrent.ConcurrentHashMap;
 import org.apache.commons.lang3.StringUtils;
@@ -73,6 +74,13 @@ public class OzoneDelegationTokenSecretManager
   private static final Logger LOG = LoggerFactory
       .getLogger(OzoneDelegationTokenSecretManager.class);
   private final Map<OzoneTokenIdentifier, TokenInfo> currentTokens;
+  /**
+   * Tokens removed by a cancel applied since this secret manager was created, 
kept in memory until the remover scan
+   * after their max date. Unlike {@link #currentTokens}, the expired token 
remover does not drop an entry before the
+   * token's max date. The set is not persisted: after a restart or a 
checkpoint install it is empty, so a renew
+   * applied after a cancel from before the restart or install is not rejected.
+   */
+  private final Set<OzoneTokenIdentifier> canceledTokens = 
ConcurrentHashMap.newKeySet();
   private final OzoneSecretStore store;
   private final S3SecretManager s3SecretManager;
   private Thread tokenRemoverThread;
@@ -351,10 +359,19 @@ public OzoneTokenIdentifier 
cancelToken(Token<OzoneTokenIdentifier> token,
   }
 
   /**
-   * Remove the expired token from in-memory map.
+   * Remove the canceled token from in-memory map and record it as canceled, 
so that a renew applied later is rejected.
    */
   public void removeToken(OzoneTokenIdentifier ozoneTokenIdentifier) {
     currentTokens.remove(ozoneTokenIdentifier);
+    canceledTokens.add(ozoneTokenIdentifier);
+  }
+
+  /**
+   * @return true if a cancel of the token was applied since this secret 
manager was created and the remover scan
+   * after the token's max date has not dropped it yet.
+   */
+  public boolean isCanceled(OzoneTokenIdentifier ozoneTokenIdentifier) {
+    return canceledTokens.contains(ozoneTokenIdentifier);
   }
 
   @Override
@@ -620,6 +637,8 @@ public void setSecretKeyClient(SecretKeyClient client) {
    */
   private void removeExpiredToken() {
     long now = Time.now();
+    // A renew can not extend a token past its max date, so a canceled token 
is no longer needed after it.
+    canceledTokens.removeIf(id -> id.getMaxDate() < now);
     synchronized (noInterruptsLock) {
       Iterator<Map.Entry<OzoneTokenIdentifier,
           TokenInfo>> i = currentTokens.entrySet().iterator();
diff --git 
a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/security/TestOzoneDelegationTokenSecretManager.java
 
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/security/TestOzoneDelegationTokenSecretManager.java
index 184c8f9983d..82d104924b3 100644
--- 
a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/security/TestOzoneDelegationTokenSecretManager.java
+++ 
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/security/TestOzoneDelegationTokenSecretManager.java
@@ -53,8 +53,12 @@
 import org.apache.hadoop.hdds.security.symmetric.SecretKeyClient;
 import org.apache.hadoop.hdds.security.x509.certificate.utils.CertificateCodec;
 import org.apache.hadoop.hdds.server.ServerUtils;
+import org.apache.hadoop.hdds.utils.db.BatchOperation;
+import org.apache.hadoop.hdds.utils.db.Table;
 import org.apache.hadoop.io.Text;
 import org.apache.hadoop.ozone.OzoneConsts;
+import org.apache.hadoop.ozone.audit.AuditLogger;
+import org.apache.hadoop.ozone.audit.AuditLoggerType;
 import org.apache.hadoop.ozone.om.OMMetadataManager;
 import org.apache.hadoop.ozone.om.OMStorage;
 import org.apache.hadoop.ozone.om.OmMetadataManagerImpl;
@@ -66,8 +70,20 @@
 import org.apache.hadoop.ozone.om.exceptions.OMLeaderNotReadyException;
 import org.apache.hadoop.ozone.om.exceptions.OMNotLeaderException;
 import org.apache.hadoop.ozone.om.helpers.S3SecretValue;
+import 
org.apache.hadoop.ozone.om.request.security.OMCancelDelegationTokenRequest;
+import 
org.apache.hadoop.ozone.om.request.security.OMRenewDelegationTokenRequest;
+import org.apache.hadoop.ozone.om.response.OMClientResponse;
 import org.apache.hadoop.ozone.om.upgrade.OMVersionManager;
 import org.apache.hadoop.ozone.om.upgrade.OMVersionManagerTestUtils;
+import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest;
+import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenewDelegationTokenResponseProto;
+import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.Status;
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.Type;
+import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.UpdateRenewDelegationTokenRequest;
+import org.apache.hadoop.ozone.protocolPB.OMPBHelper;
+import org.apache.hadoop.ozone.security.proto.SecurityProtos;
+import 
org.apache.hadoop.ozone.security.proto.SecurityProtos.CancelDelegationTokenRequestProto;
+import 
org.apache.hadoop.ozone.security.proto.SecurityProtos.RenewDelegationTokenRequestProto;
 import org.apache.hadoop.security.AccessControlException;
 import org.apache.hadoop.security.ssl.KeyStoreTestUtil;
 import org.apache.hadoop.security.token.SecretManager;
@@ -406,6 +422,85 @@ public void testCancelTokenFailure() throws Exception {
         .startsWith("rougeUser is not authorized to cancel the token"));
   }
 
+  /**
+   * A renew that was validated while the token existed must not restore the 
token when a cancel is applied first.
+   */
+  @Test
+  public void testRenewAppliedAfterCancelDoesNotRestoreToken() throws 
Exception {
+    Token<OzoneTokenIdentifier> token = createTokenForApply();
+    OzoneTokenIdentifier id = 
OzoneTokenIdentifier.readProtoBuf(token.getIdentifier());
+    long newExpiryTime = secretManager.renewToken(token, TEST_USER.toString());
+
+    OMClientResponse cancel = applyCancel(token, 1);
+    OMClientResponse renew = applyRenew(token, newExpiryTime, 2);
+    commitToDB(cancel, renew);
+
+    assertThat(cancel.getOMResponse().getStatus()).isEqualTo(Status.OK);
+    
assertThat(renew.getOMResponse().getStatus()).isEqualTo(Status.TOKEN_ERROR_OTHER);
+    Table<OzoneTokenIdentifier, Long> table = 
om.getMetadataManager().getDelegationTokenTable();
+    assertThat(table.get(id)).isNull();
+    assertThat(table.getSkipCache(id)).isNull();
+    assertThrows(SecretManager.InvalidToken.class, () -> 
secretManager.retrievePassword(id));
+  }
+
+  /**
+   * The expired token remover deletes tokens outside Ratis, at a different 
time on each OM. A renew applied after
+   * such a removal, for example while a restarted OM catches up, must be 
applied as it is on the other OMs.
+   */
+  @Test
+  public void testRenewAppliedAfterRemovalOutsideRatisIsApplied() throws 
Exception {
+    Token<OzoneTokenIdentifier> token = createTokenForApply();
+    OzoneTokenIdentifier id = 
OzoneTokenIdentifier.readProtoBuf(token.getIdentifier());
+    long newExpiryTime = secretManager.renewToken(token, TEST_USER.toString());
+    Table<OzoneTokenIdentifier, Long> table = 
om.getMetadataManager().getDelegationTokenTable();
+    table.delete(id);
+    secretManager.stop();
+    secretManager = createSecretManager(conf, TOKEN_MAX_LIFETIME, 
TOKEN_MAX_LIFETIME / 2, TOKEN_REMOVER_SCAN_INTERVAL);
+    secretManager.start(certificateClient);
+    mockRequestDependencies();
+
+    assertThat(applyRenew(token, newExpiryTime, 
1).getOMResponse().getStatus()).isEqualTo(Status.OK);
+    assertThat(table.get(id)).isEqualTo(newExpiryTime);
+    
assertThat(secretManager.retrievePassword(id)).isEqualTo(token.getPassword());
+  }
+
+  /**
+   * A renew applied with an earlier expiry than the stored one must not 
shorten the token's lifetime.
+   */
+  @Test
+  public void testRenewAppliedWithEarlierExpiryKeepsStoredExpiry() throws 
Exception {
+    Token<OzoneTokenIdentifier> token = createTokenForApply();
+    OzoneTokenIdentifier id = 
OzoneTokenIdentifier.readProtoBuf(token.getIdentifier());
+    Table<OzoneTokenIdentifier, Long> table = 
om.getMetadataManager().getDelegationTokenTable();
+    long earlier = table.get(id) + 1000;
+    long later = earlier + 1000;
+
+    assertThat(applyRenew(token, later, 
1).getOMResponse().getStatus()).isEqualTo(Status.OK);
+    assertThat(table.get(id)).isEqualTo(later);
+    assertThat(applyRenew(token, earlier, 
2).getOMResponse().getStatus()).isEqualTo(Status.OK);
+    assertThat(table.get(id)).isEqualTo(later);
+    
assertThat(secretManager.retrievePassword(id)).isEqualTo(token.getPassword());
+  }
+
+  /**
+   * A renew never extends the stored expiry past the token's max date, even 
if the stored expiry was beyond it.
+   */
+  @Test
+  public void testRenewAppliedCapsExpiryAtMaxDate() throws Exception {
+    secretManager = createSecretManager(conf, TOKEN_MAX_LIFETIME, expiryTime, 
TOKEN_REMOVER_SCAN_INTERVAL);
+    secretManager.start(certificateClient);
+    mockRequestDependencies();
+    Token<OzoneTokenIdentifier> token = secretManager.createToken(TEST_USER, 
TEST_USER, TEST_USER);
+    addToTokenStore(token);
+    OzoneTokenIdentifier id = 
OzoneTokenIdentifier.readProtoBuf(token.getIdentifier());
+    Table<OzoneTokenIdentifier, Long> table = 
om.getMetadataManager().getDelegationTokenTable();
+    assertThat(table.get(id)).isGreaterThan(id.getMaxDate());
+
+    long newExpiryTime = secretManager.renewToken(token, TEST_USER.toString());
+    assertThat(applyRenew(token, newExpiryTime, 
1).getOMResponse().getStatus()).isEqualTo(Status.OK);
+    
assertThat(table.get(id)).isEqualTo(newExpiryTime).isLessThanOrEqualTo(id.getMaxDate());
+  }
+
   @Test
   public void testVerifySignatureSuccess() throws Exception {
     secretManager = createSecretManager(conf, TOKEN_MAX_LIFETIME,
@@ -551,4 +646,62 @@ private void addToTokenStore(Token<OzoneTokenIdentifier> 
token) throws IOExcepti
     long renewDate = secretManager.updateToken(token, ozoneTokenIdentifier, 
expiryTime);
     
om.getMetadataManager().getDelegationTokenTable().put(ozoneTokenIdentifier, 
renewDate);
   }
+
+  private void mockRequestDependencies() {
+    when(om.getDelegationTokenMgr()).thenReturn(secretManager);
+    when(om.getAuditLogger()).thenReturn(new 
AuditLogger(AuditLoggerType.OMLOGGER));
+  }
+
+  /**
+   * Creates a token whose renew interval is shorter than its max lifetime and 
stores it like an applied
+   * GetDelegationToken request does.
+   */
+  private Token<OzoneTokenIdentifier> createTokenForApply() throws IOException 
{
+    long renewInterval = TOKEN_MAX_LIFETIME / 2;
+    secretManager = createSecretManager(conf, TOKEN_MAX_LIFETIME, 
renewInterval, TOKEN_REMOVER_SCAN_INTERVAL);
+    secretManager.start(certificateClient);
+    mockRequestDependencies();
+    Token<OzoneTokenIdentifier> token = secretManager.createToken(TEST_USER, 
TEST_USER, TEST_USER);
+    OzoneTokenIdentifier id = 
OzoneTokenIdentifier.readProtoBuf(token.getIdentifier());
+    long renewDate = secretManager.updateToken(token, id, renewInterval);
+    om.getMetadataManager().getDelegationTokenTable().put(id, renewDate);
+    return token;
+  }
+
+  private OMClientResponse applyCancel(Token<OzoneTokenIdentifier> token, long 
index) {
+    OMRequest request = OMRequest.newBuilder()
+        .setCmdType(Type.CancelDelegationToken)
+        .setClientId(UUID.randomUUID().toString())
+        
.setCancelDelegationTokenRequest(CancelDelegationTokenRequestProto.newBuilder()
+            .setToken(OMPBHelper.protoFromToken(token)))
+        .build();
+    return new 
OMCancelDelegationTokenRequest(request).validateAndUpdateCache(om, index);
+  }
+
+  /**
+   * Applies a renew as the leader submits it, carrying the expiry the leader 
computed when it validated the renew.
+   */
+  private OMClientResponse applyRenew(Token<OzoneTokenIdentifier> token, long 
newExpiryTime, long index) {
+    OMRequest request = OMRequest.newBuilder()
+        .setCmdType(Type.RenewDelegationToken)
+        .setClientId(UUID.randomUUID().toString())
+        
.setUpdatedRenewDelegationTokenRequest(UpdateRenewDelegationTokenRequest.newBuilder()
+            
.setRenewDelegationTokenRequest(RenewDelegationTokenRequestProto.newBuilder()
+                .setToken(OMPBHelper.protoFromToken(token)))
+            
.setRenewDelegationTokenResponse(RenewDelegationTokenResponseProto.newBuilder()
+                
.setResponse(SecurityProtos.RenewDelegationTokenResponseProto.newBuilder()
+                    .setNewExpiryTime(newExpiryTime))))
+        .build();
+    return new 
OMRenewDelegationTokenRequest(request).validateAndUpdateCache(om, index);
+  }
+
+  private void commitToDB(OMClientResponse... responses) throws IOException {
+    OMMetadataManager metadataManager = om.getMetadataManager();
+    try (BatchOperation batch = 
metadataManager.getStore().initBatchOperation()) {
+      for (OMClientResponse response : responses) {
+        response.checkAndUpdateDB(metadataManager, batch);
+      }
+      metadataManager.getStore().commitBatchOperation(batch);
+    }
+  }
 }


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

Reply via email to