|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hadoop.ozone.security; |
| 19 | + |
| 20 | +import static org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.REVOKED_TOKEN; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 24 | +import static org.mockito.ArgumentMatchers.any; |
| 25 | +import static org.mockito.ArgumentMatchers.anyString; |
| 26 | +import static org.mockito.ArgumentMatchers.eq; |
| 27 | +import static org.mockito.Mockito.CALLS_REAL_METHODS; |
| 28 | +import static org.mockito.Mockito.mock; |
| 29 | +import static org.mockito.Mockito.mockStatic; |
| 30 | +import static org.mockito.Mockito.when; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.time.Clock; |
| 34 | +import java.time.Instant; |
| 35 | +import java.util.UUID; |
| 36 | +import java.util.concurrent.ThreadLocalRandom; |
| 37 | +import org.apache.hadoop.hdds.security.symmetric.SecretKeyClient; |
| 38 | +import org.apache.hadoop.hdds.utils.db.InMemoryTestTable; |
| 39 | +import org.apache.hadoop.hdds.utils.db.Table; |
| 40 | +import org.apache.hadoop.ozone.om.OMMetadataManager; |
| 41 | +import org.apache.hadoop.ozone.om.OzoneManager; |
| 42 | +import org.apache.hadoop.ozone.om.exceptions.OMException; |
| 43 | +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest; |
| 44 | +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.S3Authentication; |
| 45 | +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.Type; |
| 46 | +import org.junit.jupiter.api.Test; |
| 47 | +import org.mockito.MockedStatic; |
| 48 | + |
| 49 | +/** |
| 50 | + * Tests for STS revocation handling in {@link S3SecurityUtil}. |
| 51 | + */ |
| 52 | +public class TestS3SecurityUtil { |
| 53 | + private static final byte[] ENCRYPTION_KEY = new byte[5]; |
| 54 | + |
| 55 | + { |
| 56 | + ThreadLocalRandom.current().nextBytes(ENCRYPTION_KEY); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testValidateS3CredentialFailsWhenTokenRevoked() throws Exception { |
| 61 | + // If the revoked STS token table contains an entry for the temporary access key id extracted from the session |
| 62 | + // token, validateS3Credential should reject the request with REVOKED_TOKEN |
| 63 | + final String sessionToken = "session-token-a"; |
| 64 | + final String tempAccessKeyId = "ASIA123456789"; |
| 65 | + |
| 66 | + try (OzoneManager ozoneManager = mock(OzoneManager.class)) { |
| 67 | + when(ozoneManager.isSecurityEnabled()).thenReturn(true); |
| 68 | + when(ozoneManager.getSecretKeyClient()).thenReturn(mock(SecretKeyClient.class)); |
| 69 | + |
| 70 | + final OMMetadataManager metadataManager = mock(OMMetadataManager.class); |
| 71 | + when(ozoneManager.getMetadataManager()).thenReturn(metadataManager); |
| 72 | + |
| 73 | + final Table<String, String> revokedSTSTokenTable = new InMemoryTestTable<>(); |
| 74 | + when(metadataManager.getS3RevokedStsTokenTable()).thenReturn(revokedSTSTokenTable); |
| 75 | + |
| 76 | + // Mock STSSecurityUtil to return a token whose tempAccessKeyId matches the one that's revoked. |
| 77 | + final STSTokenIdentifier stsTokenIdentifier = new STSTokenIdentifier( |
| 78 | + tempAccessKeyId, "original-access-key-id", "arn:aws:iam::123456789012:role/test-role", |
| 79 | + Instant.now().plusSeconds(3600), "secret-access-key", "session-policy", |
| 80 | + ENCRYPTION_KEY); |
| 81 | + |
| 82 | + try (MockedStatic<STSSecurityUtil> stsSecurityUtilMock = mockStatic(STSSecurityUtil.class, CALLS_REAL_METHODS)) { |
| 83 | + stsSecurityUtilMock.when( |
| 84 | + () -> STSSecurityUtil.constructValidateAndDecryptSTSToken( |
| 85 | + eq(sessionToken), any(SecretKeyClient.class), any(Clock.class))) |
| 86 | + .thenReturn(stsTokenIdentifier); |
| 87 | + |
| 88 | + // Revoke the tempAccessKeyId |
| 89 | + revokedSTSTokenTable.put(tempAccessKeyId, sessionToken); |
| 90 | + |
| 91 | + final OMRequest omRequest = createRequestWithSessionToken(sessionToken); |
| 92 | + final OMException ex = assertThrows( |
| 93 | + OMException.class, () -> S3SecurityUtil.validateS3Credential(omRequest, ozoneManager)); |
| 94 | + assertEquals(REVOKED_TOKEN, ex.getResult()); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testValidateS3CredentialWhenMetadataUnavailable() { |
| 101 | + // If the metadata manager is not available, the revocation check should not cause the request to be rejected. |
| 102 | + final String sessionToken = "session-token-b"; |
| 103 | + |
| 104 | + try (OzoneManager ozoneManager = mock(OzoneManager.class)) { |
| 105 | + when(ozoneManager.isSecurityEnabled()).thenReturn(true); |
| 106 | + when(ozoneManager.getMetadataManager()).thenReturn(null); |
| 107 | + when(ozoneManager.getSecretKeyClient()).thenReturn(mock(SecretKeyClient.class)); |
| 108 | + |
| 109 | + final OMRequest omRequest = createRequestWithSessionToken(sessionToken); |
| 110 | + |
| 111 | + try (MockedStatic<STSSecurityUtil> stsSecurityUtilMock = mockStatic(STSSecurityUtil.class, CALLS_REAL_METHODS); |
| 112 | + MockedStatic<AWSV4AuthValidator> awsV4AuthValidatorMock = mockStatic( |
| 113 | + AWSV4AuthValidator.class, CALLS_REAL_METHODS)) { |
| 114 | + |
| 115 | + final STSTokenIdentifier stsTokenIdentifier = new STSTokenIdentifier( |
| 116 | + "temp-access-key-id", "original-access-key-id", "arn:aws:iam::123456789012:role/test-role", |
| 117 | + Instant.now().plusSeconds(3600), "secret-access-key", "session-policy", |
| 118 | + ENCRYPTION_KEY); |
| 119 | + |
| 120 | + stsSecurityUtilMock.when( |
| 121 | + () -> STSSecurityUtil.constructValidateAndDecryptSTSToken( |
| 122 | + eq(sessionToken), any(SecretKeyClient.class), any(Clock.class))) |
| 123 | + .thenReturn(stsTokenIdentifier); |
| 124 | + |
| 125 | + // Mock AWS V4 signature validation |
| 126 | + awsV4AuthValidatorMock.when(() -> AWSV4AuthValidator.validateRequest(anyString(), anyString(), anyString())) |
| 127 | + .thenReturn(true); |
| 128 | + |
| 129 | + assertDoesNotThrow(() -> S3SecurityUtil.validateS3Credential(omRequest, ozoneManager)); |
| 130 | + } |
| 131 | + } catch (IOException e) { |
| 132 | + throw new RuntimeException(e); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testValidateS3CredentialSuccessWhenNotRevoked() { |
| 138 | + // Normal case: token is NOT revoked and request is accepted |
| 139 | + final String sessionToken = "session-token-c"; |
| 140 | + |
| 141 | + try (OzoneManager ozoneManager = mock(OzoneManager.class)) { |
| 142 | + when(ozoneManager.isSecurityEnabled()).thenReturn(true); |
| 143 | + when(ozoneManager.getSecretKeyClient()).thenReturn(mock(SecretKeyClient.class)); |
| 144 | + |
| 145 | + final OMMetadataManager metadataManager = mock(OMMetadataManager.class); |
| 146 | + when(ozoneManager.getMetadataManager()).thenReturn(metadataManager); |
| 147 | + |
| 148 | + final Table<String, String> revokedSTSTokenTable = new InMemoryTestTable<>(); |
| 149 | + when(metadataManager.getS3RevokedStsTokenTable()).thenReturn(revokedSTSTokenTable); |
| 150 | + |
| 151 | + // Not revoked -> getIfExist returns null by default in InMemoryTestTable |
| 152 | + final OMRequest omRequest = createRequestWithSessionToken(sessionToken); |
| 153 | + final STSTokenIdentifier stsTokenIdentifier = new STSTokenIdentifier( |
| 154 | + "temp-access-key-id", "original-access-key-id", "arn:aws:iam::123456789012:role/test-role", |
| 155 | + Instant.now().plusSeconds(3600), "secret-access-key", "session-policy", |
| 156 | + ENCRYPTION_KEY); |
| 157 | + |
| 158 | + try (MockedStatic<STSSecurityUtil> stsSecurityUtilMock = mockStatic(STSSecurityUtil.class, CALLS_REAL_METHODS); |
| 159 | + MockedStatic<AWSV4AuthValidator> awsV4AuthValidatorMock = mockStatic( |
| 160 | + AWSV4AuthValidator.class, CALLS_REAL_METHODS)) { |
| 161 | + |
| 162 | + stsSecurityUtilMock.when( |
| 163 | + () -> STSSecurityUtil.constructValidateAndDecryptSTSToken( |
| 164 | + eq(sessionToken), any(SecretKeyClient.class), any(Clock.class))) |
| 165 | + .thenReturn(stsTokenIdentifier); |
| 166 | + awsV4AuthValidatorMock.when(() -> AWSV4AuthValidator.validateRequest(anyString(), anyString(), anyString())) |
| 167 | + .thenReturn(true); |
| 168 | + |
| 169 | + assertDoesNotThrow(() -> S3SecurityUtil.validateS3Credential(omRequest, ozoneManager)); |
| 170 | + } |
| 171 | + } catch (IOException e) { |
| 172 | + throw new RuntimeException(e); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private static OMRequest createRequestWithSessionToken(String sessionToken) { |
| 177 | + final S3Authentication s3Authentication = S3Authentication.newBuilder() |
| 178 | + .setAccessId("accessKeyId") |
| 179 | + .setStringToSign("string-to-sign") |
| 180 | + .setSignature("signature") |
| 181 | + .setSessionToken(sessionToken) |
| 182 | + .build(); |
| 183 | + |
| 184 | + return OMRequest.newBuilder() |
| 185 | + .setClientId(UUID.randomUUID().toString()) |
| 186 | + .setCmdType(Type.CreateVolume) |
| 187 | + .setS3Authentication(s3Authentication) |
| 188 | + .build(); |
| 189 | + } |
| 190 | +} |
0 commit comments