seropian commented on code in PR #2409: URL: https://github.com/apache/jackrabbit-oak/pull/2409#discussion_r2428194010
########## oak-blob-cloud-azure/src/test/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/v8/AzureBlobContainerProviderV8AuthenticationTest.java: ########## @@ -0,0 +1,337 @@ +/* + * 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.jackrabbit.oak.blob.cloud.azure.blobstorage.v8; + +import org.apache.jackrabbit.core.data.DataStoreException; +import org.junit.After; +import org.junit.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +import java.lang.reflect.Method; + +import static org.junit.Assert.*; +import static org.mockito.Answers.CALLS_REAL_METHODS; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import com.microsoft.aad.msal4j.MsalServiceException; +import com.microsoft.azure.storage.blob.CloudBlobContainer; + +/** + * Test class focused on AzureBlobContainerProviderV8 authentication functionality. + * Tests authentication methods including service principal, connection string, SAS token, and account key. + */ +public class AzureBlobContainerProviderV8AuthenticationTest { + + private static final String CONTAINER_NAME = "test-container"; + private static final String ACCOUNT_NAME = "testaccount"; + private static final String TENANT_ID = "test-tenant-id"; + private static final String CLIENT_ID = "test-client-id"; + private static final String CLIENT_SECRET = "test-client-secret"; + private static final String CONNECTION_STRING = "DefaultEndpointsProtocol=https;AccountName=testaccount;AccountKey=dGVzdC1hY2NvdW50LWtleQ==;EndpointSuffix=core.windows.net"; + private static final String SAS_TOKEN = "?sv=2020-08-04&ss=b&srt=sco&sp=rwdlacx&se=2023-12-31T23:59:59Z&st=2023-01-01T00:00:00Z&spr=https&sig=test"; + private static final String ACCOUNT_KEY = "dGVzdC1hY2NvdW50LWtleQ=="; + private static final String BLOB_ENDPOINT = "https://testaccount.blob.core.windows.net"; + + private AzureBlobContainerProviderV8 provider; + + @After + public void tearDown() { + if (provider != null) { + provider.close(); + } + } + + @Test + public void testAuthenticationPriorityConnectionStringOverSasToken() throws DataStoreException { + // Test that connection string takes priority over all other authentication methods + provider = AzureBlobContainerProviderV8.Builder + .builder(CONTAINER_NAME) + .withAzureConnectionString(CONNECTION_STRING) + .withAccountName(ACCOUNT_NAME) + .withTenantId(TENANT_ID) + .withClientId(CLIENT_ID) + .withClientSecret(CLIENT_SECRET) + .withSasToken(SAS_TOKEN) + .withAccountKey(ACCOUNT_KEY) + .build(); + + //spy UtilsV8 + try (MockedStatic<UtilsV8> mockedUtils = mockStatic(UtilsV8.class)) { + mockedUtils.when(() -> UtilsV8.getBlobContainer(anyString(), anyString(), any())) + .thenReturn(mock(CloudBlobContainer.class)); + + provider.getBlobContainer(); + + mockedUtils.verify(() -> UtilsV8.getBlobContainer(CONNECTION_STRING, CONTAINER_NAME, null), times(1)); + mockedUtils.verifyNoMoreInteractions(); + } + } + + @Test + public void testAuthenticationPrioritySasTokenOverAccountKey() throws DataStoreException { + // Test that SAS token takes priority over account key when no connection string or service principal + provider = AzureBlobContainerProviderV8.Builder + .builder(CONTAINER_NAME) + .withAccountName(ACCOUNT_NAME) + .withSasToken(SAS_TOKEN) + .withAccountKey(ACCOUNT_KEY) + .withBlobEndpoint(BLOB_ENDPOINT) + .build(); + + //spy UtilsV8 + try (MockedStatic<UtilsV8> mockedUtils = mockStatic(UtilsV8.class)) { + mockedUtils.when(() -> UtilsV8.getConnectionStringForSas(SAS_TOKEN, BLOB_ENDPOINT, ACCOUNT_NAME)) + .thenReturn(CONNECTION_STRING); + mockedUtils.when(() -> UtilsV8.getBlobContainer(CONNECTION_STRING, CONTAINER_NAME, null)) + .thenReturn(mock(CloudBlobContainer.class)); + + provider.getBlobContainer(); + + mockedUtils.verify(() -> UtilsV8.getConnectionStringForSas(SAS_TOKEN, BLOB_ENDPOINT, ACCOUNT_NAME), times(1)); + mockedUtils.verify(() -> UtilsV8.getBlobContainer(CONNECTION_STRING, CONTAINER_NAME, null), times(1)); + mockedUtils.verifyNoMoreInteractions(); + } + } + + @Test + public void testAuthenticationPriorityServicePrincipalOverAccountKey() throws DataStoreException { + // Test that service principal takes priority over account key when no connection string or SAS token + provider = AzureBlobContainerProviderV8.Builder + .builder(CONTAINER_NAME) + .withAccountName(ACCOUNT_NAME) + .withTenantId(TENANT_ID) + .withClientId(CLIENT_ID) + .withClientSecret(CLIENT_SECRET) + .withAccountKey(ACCOUNT_KEY) + .build(); + + //spy UtilsV8 + try (MockedStatic<UtilsV8> mockedUtils = mockStatic(UtilsV8.class, CALLS_REAL_METHODS)) { + // This should use service principal authentication + try { + provider.getBlobContainer(); + } catch (Exception e) { + // Expected + } + + // Verify that UtilsV8.getBlobContainer was not called + // This means service principal authentication was attempted + mockedUtils.verifyNoInteractions(); + } + } + + @Test + public void testAuthenticationFallbackToAccountKey() throws DataStoreException { + // Test fallback to account key when no other authentication methods are available + provider = AzureBlobContainerProviderV8.Builder + .builder(CONTAINER_NAME) + .withAccountName(ACCOUNT_NAME) + .withAccountKey(ACCOUNT_KEY) + .withBlobEndpoint(BLOB_ENDPOINT) + .build(); + + try (MockedStatic<UtilsV8> mockedUtils = mockStatic(UtilsV8.class, CALLS_REAL_METHODS)) { + // This should use service principal authentication + provider.getBlobContainer(); + + mockedUtils.verify(() -> UtilsV8.getConnectionString(ACCOUNT_NAME, ACCOUNT_KEY, BLOB_ENDPOINT), times(1)); + mockedUtils.verify(() -> UtilsV8.getBlobContainer(anyString(), eq(CONTAINER_NAME), eq(null)), times(1)); + } + } + + @Test + public void testServicePrincipalAuthenticationMissingAccountName() throws Exception { + // Test service principal authentication detection with missing account name + provider = AzureBlobContainerProviderV8.Builder + .builder(CONTAINER_NAME) + .withTenantId(TENANT_ID) + .withClientId(CLIENT_ID) + .withClientSecret(CLIENT_SECRET) + .build(); + + Method authenticateMethod = AzureBlobContainerProviderV8.class + .getDeclaredMethod("authenticateViaServicePrincipal"); + authenticateMethod.setAccessible(true); + + boolean result = (Boolean) authenticateMethod.invoke(provider); Review Comment: fixed ########## oak-blob-cloud-azure/src/test/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/v8/AzureBlobContainerProviderV8AuthenticationTest.java: ########## @@ -0,0 +1,337 @@ +/* + * 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.jackrabbit.oak.blob.cloud.azure.blobstorage.v8; + +import org.apache.jackrabbit.core.data.DataStoreException; +import org.junit.After; +import org.junit.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +import java.lang.reflect.Method; + +import static org.junit.Assert.*; +import static org.mockito.Answers.CALLS_REAL_METHODS; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import com.microsoft.aad.msal4j.MsalServiceException; +import com.microsoft.azure.storage.blob.CloudBlobContainer; + +/** + * Test class focused on AzureBlobContainerProviderV8 authentication functionality. + * Tests authentication methods including service principal, connection string, SAS token, and account key. + */ +public class AzureBlobContainerProviderV8AuthenticationTest { + + private static final String CONTAINER_NAME = "test-container"; + private static final String ACCOUNT_NAME = "testaccount"; + private static final String TENANT_ID = "test-tenant-id"; + private static final String CLIENT_ID = "test-client-id"; + private static final String CLIENT_SECRET = "test-client-secret"; + private static final String CONNECTION_STRING = "DefaultEndpointsProtocol=https;AccountName=testaccount;AccountKey=dGVzdC1hY2NvdW50LWtleQ==;EndpointSuffix=core.windows.net"; + private static final String SAS_TOKEN = "?sv=2020-08-04&ss=b&srt=sco&sp=rwdlacx&se=2023-12-31T23:59:59Z&st=2023-01-01T00:00:00Z&spr=https&sig=test"; + private static final String ACCOUNT_KEY = "dGVzdC1hY2NvdW50LWtleQ=="; + private static final String BLOB_ENDPOINT = "https://testaccount.blob.core.windows.net"; + + private AzureBlobContainerProviderV8 provider; + + @After + public void tearDown() { + if (provider != null) { + provider.close(); + } + } + + @Test + public void testAuthenticationPriorityConnectionStringOverSasToken() throws DataStoreException { + // Test that connection string takes priority over all other authentication methods + provider = AzureBlobContainerProviderV8.Builder + .builder(CONTAINER_NAME) + .withAzureConnectionString(CONNECTION_STRING) + .withAccountName(ACCOUNT_NAME) + .withTenantId(TENANT_ID) + .withClientId(CLIENT_ID) + .withClientSecret(CLIENT_SECRET) + .withSasToken(SAS_TOKEN) + .withAccountKey(ACCOUNT_KEY) + .build(); + + //spy UtilsV8 + try (MockedStatic<UtilsV8> mockedUtils = mockStatic(UtilsV8.class)) { + mockedUtils.when(() -> UtilsV8.getBlobContainer(anyString(), anyString(), any())) + .thenReturn(mock(CloudBlobContainer.class)); + + provider.getBlobContainer(); + + mockedUtils.verify(() -> UtilsV8.getBlobContainer(CONNECTION_STRING, CONTAINER_NAME, null), times(1)); + mockedUtils.verifyNoMoreInteractions(); + } + } + + @Test + public void testAuthenticationPrioritySasTokenOverAccountKey() throws DataStoreException { + // Test that SAS token takes priority over account key when no connection string or service principal + provider = AzureBlobContainerProviderV8.Builder + .builder(CONTAINER_NAME) + .withAccountName(ACCOUNT_NAME) + .withSasToken(SAS_TOKEN) + .withAccountKey(ACCOUNT_KEY) + .withBlobEndpoint(BLOB_ENDPOINT) + .build(); + + //spy UtilsV8 + try (MockedStatic<UtilsV8> mockedUtils = mockStatic(UtilsV8.class)) { + mockedUtils.when(() -> UtilsV8.getConnectionStringForSas(SAS_TOKEN, BLOB_ENDPOINT, ACCOUNT_NAME)) + .thenReturn(CONNECTION_STRING); + mockedUtils.when(() -> UtilsV8.getBlobContainer(CONNECTION_STRING, CONTAINER_NAME, null)) + .thenReturn(mock(CloudBlobContainer.class)); + + provider.getBlobContainer(); + + mockedUtils.verify(() -> UtilsV8.getConnectionStringForSas(SAS_TOKEN, BLOB_ENDPOINT, ACCOUNT_NAME), times(1)); + mockedUtils.verify(() -> UtilsV8.getBlobContainer(CONNECTION_STRING, CONTAINER_NAME, null), times(1)); + mockedUtils.verifyNoMoreInteractions(); + } + } + + @Test + public void testAuthenticationPriorityServicePrincipalOverAccountKey() throws DataStoreException { + // Test that service principal takes priority over account key when no connection string or SAS token + provider = AzureBlobContainerProviderV8.Builder + .builder(CONTAINER_NAME) + .withAccountName(ACCOUNT_NAME) + .withTenantId(TENANT_ID) + .withClientId(CLIENT_ID) + .withClientSecret(CLIENT_SECRET) + .withAccountKey(ACCOUNT_KEY) + .build(); + + //spy UtilsV8 + try (MockedStatic<UtilsV8> mockedUtils = mockStatic(UtilsV8.class, CALLS_REAL_METHODS)) { + // This should use service principal authentication + try { + provider.getBlobContainer(); + } catch (Exception e) { + // Expected + } + + // Verify that UtilsV8.getBlobContainer was not called + // This means service principal authentication was attempted + mockedUtils.verifyNoInteractions(); + } + } + + @Test + public void testAuthenticationFallbackToAccountKey() throws DataStoreException { + // Test fallback to account key when no other authentication methods are available + provider = AzureBlobContainerProviderV8.Builder + .builder(CONTAINER_NAME) + .withAccountName(ACCOUNT_NAME) + .withAccountKey(ACCOUNT_KEY) + .withBlobEndpoint(BLOB_ENDPOINT) + .build(); + + try (MockedStatic<UtilsV8> mockedUtils = mockStatic(UtilsV8.class, CALLS_REAL_METHODS)) { + // This should use service principal authentication + provider.getBlobContainer(); + + mockedUtils.verify(() -> UtilsV8.getConnectionString(ACCOUNT_NAME, ACCOUNT_KEY, BLOB_ENDPOINT), times(1)); + mockedUtils.verify(() -> UtilsV8.getBlobContainer(anyString(), eq(CONTAINER_NAME), eq(null)), times(1)); + } + } + + @Test + public void testServicePrincipalAuthenticationMissingAccountName() throws Exception { + // Test service principal authentication detection with missing account name + provider = AzureBlobContainerProviderV8.Builder + .builder(CONTAINER_NAME) + .withTenantId(TENANT_ID) + .withClientId(CLIENT_ID) + .withClientSecret(CLIENT_SECRET) + .build(); + + Method authenticateMethod = AzureBlobContainerProviderV8.class + .getDeclaredMethod("authenticateViaServicePrincipal"); + authenticateMethod.setAccessible(true); + + boolean result = (Boolean) authenticateMethod.invoke(provider); + assertFalse("Should not authenticate via service principal when account name is missing", result); + } + + @Test + public void testServicePrincipalAuthenticationMissingClientId() throws Exception { + // Test service principal authentication detection with missing client ID + provider = AzureBlobContainerProviderV8.Builder + .builder(CONTAINER_NAME) + .withAccountName(ACCOUNT_NAME) + .withTenantId(TENANT_ID) + .withClientSecret(CLIENT_SECRET) + .build(); + + Method authenticateMethod = AzureBlobContainerProviderV8.class + .getDeclaredMethod("authenticateViaServicePrincipal"); + authenticateMethod.setAccessible(true); + + boolean result = (Boolean) authenticateMethod.invoke(provider); Review Comment: fixed -- 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]
