fmorg-git commented on code in PR #9372:
URL: https://github.com/apache/ozone/pull/9372#discussion_r2587390315


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/security/STSSecurityUtil.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.security;
+
+import static 
org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.INVALID_TOKEN;
+
+import com.google.protobuf.InvalidProtocolBufferException;
+import java.io.IOException;
+import java.time.Instant;
+import java.util.UUID;
+import org.apache.hadoop.hdds.annotation.InterfaceAudience;
+import org.apache.hadoop.hdds.annotation.InterfaceStability;
+import org.apache.hadoop.hdds.security.symmetric.ManagedSecretKey;
+import org.apache.hadoop.hdds.security.symmetric.SecretKeyClient;
+import org.apache.hadoop.ozone.om.exceptions.OMException;
+import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMTokenProto;
+import org.apache.hadoop.security.token.SecretManager;
+import org.apache.hadoop.security.token.Token;
+
+/**
+ * Utility class with methods to validate and decrypt STS tokens.
+ */
[email protected]
[email protected]
+public final class STSSecurityUtil {
+  private STSSecurityUtil() {
+  }
+
+  /**
+   * Constructs, validates and decrypts STS session token.
+   * @param sessionToken        the session token from the 
x-amz-security-token header
+   * @param secretKeyClient     the Ozone Manager secretKeyClient
+   * @return the STSTokenIdentifier with decrypted secretAccessKey
+   * @throws OMException        if the token is not valid or processing failed 
otherwise
+   */
+  public static STSTokenIdentifier constructValidateAndDecryptSTSToken(String 
sessionToken,
+      SecretKeyClient secretKeyClient) throws OMException {
+    try {
+      final Token<STSTokenIdentifier> token = 
decodeTokenFromString(sessionToken);
+      return verifyAndDecryptToken(token, secretKeyClient);
+    } catch (SecretManager.InvalidToken e) {
+      throw new OMException("Invalid STS token format: " + e.getMessage(), e, 
INVALID_TOKEN);
+    }
+  }
+
+  /**
+   * Verifies an STS Token by performing multiple checks.
+   *
+   * @param token                         the token to verify
+   * @return the STSTokenIdentifier with decrypted secretAccessKey
+   * @throws SecretManager.InvalidToken   if the token is invalid
+   */
+  private static STSTokenIdentifier 
verifyAndDecryptToken(Token<STSTokenIdentifier> token,
+      SecretKeyClient secretKeyClient) throws SecretManager.InvalidToken {
+    if (!STSTokenIdentifier.KIND_NAME.equals(token.getKind())) {
+      throw new SecretManager.InvalidToken("Invalid STS token - kind is 
incorrect: " + token.getKind());
+    }
+
+    if (!STSTokenIdentifier.STS_SERVICE.equals(token.getService().toString())) 
{
+      throw new SecretManager.InvalidToken("Invalid STS token - service is 
incorrect: " + token.getService());
+    }
+
+    final byte[] tokenBytes = token.getIdentifier();
+    final OMTokenProto proto;
+    try {
+      proto = OMTokenProto.parseFrom(tokenBytes);
+    } catch (InvalidProtocolBufferException e) {
+      throw new SecretManager.InvalidToken("Invalid STS token - could not 
parse protocol buffer: " + e.getMessage());
+    }
+    final UUID secretKeyId;
+    try {
+      secretKeyId = UUID.fromString(proto.getSecretKeyId());
+    } catch (IllegalArgumentException e) {
+      throw new SecretManager.InvalidToken("Invalid STS token - secretKeyId 
was not valid: " + proto.getSecretKeyId());
+    }
+
+    final STSTokenIdentifier tokenId = new STSTokenIdentifier();
+    final ManagedSecretKey secretKey;
+    try {
+      secretKey = getValidatedSecretKey(secretKeyId, secretKeyClient);
+      tokenId.setEncryptionKey(secretKey.getSecretKey().getEncoded());
+      tokenId.readFromByteArray(tokenBytes);
+    } catch (IOException e) {
+      throw new SecretManager.InvalidToken("Invalid STS token - could not 
readFromByteArray: " + e.getMessage());
+    }
+
+    // Check expiration
+    if (tokenId.isExpired(Instant.now())) {

Review Comment:
   updated



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

Reply via email to