From 8a6c59e6e8f835b7a32f9c4a17e9e6d00329f1f5 Mon Sep 17 00:00:00 2001 From: Moary Chen Date: Fri, 27 Dec 2024 16:44:56 +0800 Subject: [PATCH 1/4] Fix the issue where the token acquisition timeout is not set via the property `azure.accessTokenTimeoutInSeconds` and update to use the new minor version 1.2 --- .../azure-identity-extensions/CHANGELOG.md | 51 ++++++++++++++++++- .../TokenCredentialProviderOptions.java | 9 ++++ .../implementation/enums/AuthProperty.java | 11 +++- .../template/AzureAuthenticationTemplate.java | 13 ++++- .../AzureAuthenticationTemplateTest.java | 23 +++++++++ 5 files changed, 102 insertions(+), 5 deletions(-) diff --git a/sdk/identity/azure-identity-extensions/CHANGELOG.md b/sdk/identity/azure-identity-extensions/CHANGELOG.md index 63bf441ee75c..38751608824b 100644 --- a/sdk/identity/azure-identity-extensions/CHANGELOG.md +++ b/sdk/identity/azure-identity-extensions/CHANGELOG.md @@ -2,11 +2,52 @@ ## 1.2.0-beta.2 (Unreleased) +#### Bugs Fixed +- Fix the issue where the token acquisition timeout is not set via the property `azure.accessTokenTimeoutInSeconds`. [#43512](https://github.com/Azure/azure-sdk-for-java/issues/43512). + ### Other Changes #### Dependency Updates -- Upgraded `mysql-connector-j` from `8.0.33` to version `9.0.0`. +## 1.1.22 (2024-12-04) + +### Other Changes + +#### Dependency Updates + +- Upgraded `azure-identity` from `1.14.0` to version `1.14.2`. + +## 1.1.21 (2024-10-28) + +### Other Changes + +#### Dependency Updates + +- Upgraded `azure-identity` from `1.13.3` to version `1.14.0`. + +## 1.1.20 (2024-09-28) + +### Other Changes + +#### Dependency Updates + +- Upgraded `azure-identity` from `1.13.2` to version `1.13.3`. + +## 1.1.19 (2024-08-25) + +### Other Changes + +#### Dependency Updates + +- Upgraded `azure-identity` from `1.13.1` to version `1.13.2`. + +## 1.1.18 (2024-07-27) + +### Other Changes + +#### Dependency Updates + +- Upgraded `azure-identity` from `1.13.0` to version `1.13.1`. ## 1.1.17 (2024-06-25) @@ -153,6 +194,14 @@ - Improve the performance of DefaultTokenCredentialProvider's `get()` method. +## 1.2.0-beta.1 (2023-02-04) + +### Other Changes + +#### Dependency Updates + +- Upgraded `mysql-connector-j` from `8.0.33` to version `9.0.0`. + ## 1.2.0-beta.1 (2023-02-06) ### Other Changes diff --git a/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/credential/TokenCredentialProviderOptions.java b/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/credential/TokenCredentialProviderOptions.java index af785be09d75..d7dbfe64c40e 100644 --- a/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/credential/TokenCredentialProviderOptions.java +++ b/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/credential/TokenCredentialProviderOptions.java @@ -25,6 +25,7 @@ public class TokenCredentialProviderOptions { private boolean managedIdentityEnabled; private String tokenCredentialProviderClassName; private String tokenCredentialBeanName; + private String accessTokenTimeoutInSeconds; public TokenCredentialProviderOptions() { @@ -41,6 +42,7 @@ public TokenCredentialProviderOptions(Properties properties) { this.managedIdentityEnabled = Boolean.TRUE.equals(AuthProperty.MANAGED_IDENTITY_ENABLED.getBoolean(properties)); this.tokenCredentialProviderClassName = AuthProperty.TOKEN_CREDENTIAL_PROVIDER_CLASS_NAME.get(properties); this.tokenCredentialBeanName = AuthProperty.TOKEN_CREDENTIAL_BEAN_NAME.get(properties); + this.accessTokenTimeoutInSeconds = AuthProperty.TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS.get(properties); this.authorityHost = AuthProperty.AUTHORITY_HOST.get(properties); } @@ -132,4 +134,11 @@ public void setAuthorityHost(String authorityHost) { this.authorityHost = authorityHost; } + public String getAccessTokenTimeoutInSeconds() { + return accessTokenTimeoutInSeconds; + } + + public void setAccessTokenTimeoutInSeconds(String accessTokenTimeoutInSeconds) { + this.accessTokenTimeoutInSeconds = accessTokenTimeoutInSeconds; + } } diff --git a/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/enums/AuthProperty.java b/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/enums/AuthProperty.java index 1daf1c7383dd..829fdbf5d9c5 100644 --- a/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/enums/AuthProperty.java +++ b/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/enums/AuthProperty.java @@ -76,8 +76,15 @@ public enum AuthProperty { /** * The given bean name of a TokenCredential bean in the Spring context. */ - TOKEN_CREDENTIAL_BEAN_NAME("azure.tokenCredentialBeanName", "springCloudAzureDefaultCredential", - "The given bean name of a TokenCredential bean in the Spring context.", false); + TOKEN_CREDENTIAL_BEAN_NAME("azure.tokenCredentialBeanName", + "The given bean name of a TokenCredential bean in the Spring context.", false), + + /** + * Max time to get an access token. + * @since 1.2.0 + */ + TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS("azure.accessTokenTimeoutInSeconds", "30", + "Max time to get an access token.", false); String propertyKey; String defaultValue; diff --git a/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplate.java b/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplate.java index 6fbf69c85cec..33cebf0c727f 100644 --- a/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplate.java +++ b/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplate.java @@ -9,10 +9,11 @@ import com.azure.identity.extensions.implementation.credential.TokenCredentialProviderOptions; import com.azure.identity.extensions.implementation.token.AccessTokenResolver; import com.azure.identity.extensions.implementation.token.AccessTokenResolverOptions; -import reactor.core.publisher.Mono; import java.time.Duration; import java.util.Properties; import java.util.concurrent.atomic.AtomicBoolean; +import reactor.core.publisher.Mono; +import static com.azure.identity.extensions.implementation.enums.AuthProperty.TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS; /** * Template class can be extended to get password from access token. @@ -27,6 +28,8 @@ public class AzureAuthenticationTemplate { private AccessTokenResolver accessTokenResolver; + private long accessTokenTimeoutInSeconds; + /** * Default constructor for AzureAuthenticationTemplate */ @@ -66,6 +69,12 @@ public void init(Properties properties) { = AccessTokenResolver.createDefault(new AccessTokenResolverOptions(properties)); } + if (properties.containsKey(TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS.getPropertyKey())) { + accessTokenTimeoutInSeconds = Long.parseLong(TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS.get(properties)); + } else { + accessTokenTimeoutInSeconds = 30; + LOGGER.verbose("Use default access token timeout: {} seconds.", accessTokenTimeoutInSeconds); + } LOGGER.verbose("Initialized AzureAuthenticationTemplate."); } else { LOGGER.info("AzureAuthenticationTemplate has already initialized."); @@ -110,7 +119,7 @@ TokenCredentialProvider getTokenCredentialProvider() { } Duration getBlockTimeout() { - return Duration.ofSeconds(30); + return Duration.ofSeconds(accessTokenTimeoutInSeconds); } AtomicBoolean getIsInitialized() { diff --git a/sdk/identity/azure-identity-extensions/src/test/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplateTest.java b/sdk/identity/azure-identity-extensions/src/test/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplateTest.java index 05b1d1578e1c..b10e7247dbcc 100644 --- a/sdk/identity/azure-identity-extensions/src/test/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplateTest.java +++ b/sdk/identity/azure-identity-extensions/src/test/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplateTest.java @@ -12,10 +12,12 @@ import org.mockito.MockedConstruction; import reactor.core.publisher.Mono; +import java.lang.reflect.Field; import java.time.OffsetDateTime; import java.util.Properties; import java.util.concurrent.TimeUnit; +import static com.azure.identity.extensions.implementation.enums.AuthProperty.TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; @@ -109,4 +111,25 @@ void testGetTokenAsPassword() throws InterruptedException { } } + @Test + void useDefaultTokenTimeout() throws NoSuchFieldException, IllegalAccessException { + AzureAuthenticationTemplate template = new AzureAuthenticationTemplate(); + Properties properties = new Properties(); + template.init(properties); + assertNotNull(template.getBlockTimeout()); + Field defaultValueField = TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS.getClass().getDeclaredField("defaultValue"); + defaultValueField.setAccessible(true); + String defaultVault = (String) defaultValueField.get(TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS); + assertEquals(template.getBlockTimeout().getSeconds() + "", defaultVault); + } + + @Test + void useCustomTokenTimeout() { + AzureAuthenticationTemplate template = new AzureAuthenticationTemplate(); + Properties properties = new Properties(); + properties.setProperty(AuthProperty.TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS.getPropertyKey(), "35"); + template.init(properties); + assertNotNull(template.getBlockTimeout()); + assertEquals(template.getBlockTimeout().getSeconds() , 35); + } } From 656d3b4c6693c390bf963b20dbade1760ad111c6 Mon Sep 17 00:00:00 2001 From: Moary Chen Date: Fri, 27 Dec 2024 16:59:22 +0800 Subject: [PATCH 2/4] Revert --- .../identity/extensions/implementation/enums/AuthProperty.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/enums/AuthProperty.java b/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/enums/AuthProperty.java index 829fdbf5d9c5..65a39ce52df8 100644 --- a/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/enums/AuthProperty.java +++ b/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/enums/AuthProperty.java @@ -76,7 +76,7 @@ public enum AuthProperty { /** * The given bean name of a TokenCredential bean in the Spring context. */ - TOKEN_CREDENTIAL_BEAN_NAME("azure.tokenCredentialBeanName", + TOKEN_CREDENTIAL_BEAN_NAME("azure.tokenCredentialBeanName", "springCloudAzureDefaultCredential", "The given bean name of a TokenCredential bean in the Spring context.", false), /** From 82c1ea33e861a1227f0b58c0eacd654e250db7cd Mon Sep 17 00:00:00 2001 From: Moary Chen Date: Mon, 30 Dec 2024 08:19:44 +0800 Subject: [PATCH 3/4] Fix code smell --- .../template/AzureAuthenticationTemplateTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/identity/azure-identity-extensions/src/test/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplateTest.java b/sdk/identity/azure-identity-extensions/src/test/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplateTest.java index b10e7247dbcc..451fa57ce548 100644 --- a/sdk/identity/azure-identity-extensions/src/test/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplateTest.java +++ b/sdk/identity/azure-identity-extensions/src/test/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplateTest.java @@ -130,6 +130,6 @@ void useCustomTokenTimeout() { properties.setProperty(AuthProperty.TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS.getPropertyKey(), "35"); template.init(properties); assertNotNull(template.getBlockTimeout()); - assertEquals(template.getBlockTimeout().getSeconds() , 35); + assertEquals(template.getBlockTimeout().getSeconds(), 35); } } From cf7661fbe9732c910271e9cd4910adc2444ebacd Mon Sep 17 00:00:00 2001 From: Moary Chen Date: Mon, 30 Dec 2024 08:55:58 +0800 Subject: [PATCH 4/4] Use GET_TOKEN_TIMEOUT instead ACCESS_TOKEN_TIMEOUT_IN_SECONDS --- .../credential/TokenCredentialProviderOptions.java | 2 +- .../implementation/enums/AuthProperty.java | 11 ++--------- .../template/AzureAuthenticationTemplate.java | 6 +++--- .../template/AzureAuthenticationTemplateTest.java | 12 ++++++------ 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/credential/TokenCredentialProviderOptions.java b/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/credential/TokenCredentialProviderOptions.java index d7dbfe64c40e..68bf84b85840 100644 --- a/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/credential/TokenCredentialProviderOptions.java +++ b/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/credential/TokenCredentialProviderOptions.java @@ -42,7 +42,7 @@ public TokenCredentialProviderOptions(Properties properties) { this.managedIdentityEnabled = Boolean.TRUE.equals(AuthProperty.MANAGED_IDENTITY_ENABLED.getBoolean(properties)); this.tokenCredentialProviderClassName = AuthProperty.TOKEN_CREDENTIAL_PROVIDER_CLASS_NAME.get(properties); this.tokenCredentialBeanName = AuthProperty.TOKEN_CREDENTIAL_BEAN_NAME.get(properties); - this.accessTokenTimeoutInSeconds = AuthProperty.TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS.get(properties); + this.accessTokenTimeoutInSeconds = AuthProperty.GET_TOKEN_TIMEOUT.get(properties); this.authorityHost = AuthProperty.AUTHORITY_HOST.get(properties); } diff --git a/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/enums/AuthProperty.java b/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/enums/AuthProperty.java index 65a39ce52df8..198524d909e7 100644 --- a/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/enums/AuthProperty.java +++ b/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/enums/AuthProperty.java @@ -67,7 +67,7 @@ public enum AuthProperty { /** * Max time to get an access token. */ - GET_TOKEN_TIMEOUT("azure.accessTokenTimeoutInSeconds", "Max time to get an access token.", false), + GET_TOKEN_TIMEOUT("azure.accessTokenTimeoutInSeconds", "30", "Max time to get an access token.", false), /** * The canonical class name of a class that implements 'TokenCredentialProvider'. */ @@ -77,14 +77,7 @@ public enum AuthProperty { * The given bean name of a TokenCredential bean in the Spring context. */ TOKEN_CREDENTIAL_BEAN_NAME("azure.tokenCredentialBeanName", "springCloudAzureDefaultCredential", - "The given bean name of a TokenCredential bean in the Spring context.", false), - - /** - * Max time to get an access token. - * @since 1.2.0 - */ - TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS("azure.accessTokenTimeoutInSeconds", "30", - "Max time to get an access token.", false); + "The given bean name of a TokenCredential bean in the Spring context.", false); String propertyKey; String defaultValue; diff --git a/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplate.java b/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplate.java index 33cebf0c727f..4240aa50313f 100644 --- a/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplate.java +++ b/sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplate.java @@ -13,7 +13,7 @@ import java.util.Properties; import java.util.concurrent.atomic.AtomicBoolean; import reactor.core.publisher.Mono; -import static com.azure.identity.extensions.implementation.enums.AuthProperty.TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS; +import static com.azure.identity.extensions.implementation.enums.AuthProperty.GET_TOKEN_TIMEOUT; /** * Template class can be extended to get password from access token. @@ -69,8 +69,8 @@ public void init(Properties properties) { = AccessTokenResolver.createDefault(new AccessTokenResolverOptions(properties)); } - if (properties.containsKey(TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS.getPropertyKey())) { - accessTokenTimeoutInSeconds = Long.parseLong(TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS.get(properties)); + if (properties.containsKey(GET_TOKEN_TIMEOUT.getPropertyKey())) { + accessTokenTimeoutInSeconds = Long.parseLong(GET_TOKEN_TIMEOUT.get(properties)); } else { accessTokenTimeoutInSeconds = 30; LOGGER.verbose("Use default access token timeout: {} seconds.", accessTokenTimeoutInSeconds); diff --git a/sdk/identity/azure-identity-extensions/src/test/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplateTest.java b/sdk/identity/azure-identity-extensions/src/test/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplateTest.java index 451fa57ce548..b7750f5cd4c1 100644 --- a/sdk/identity/azure-identity-extensions/src/test/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplateTest.java +++ b/sdk/identity/azure-identity-extensions/src/test/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplateTest.java @@ -17,7 +17,7 @@ import java.util.Properties; import java.util.concurrent.TimeUnit; -import static com.azure.identity.extensions.implementation.enums.AuthProperty.TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS; +import static com.azure.identity.extensions.implementation.enums.AuthProperty.GET_TOKEN_TIMEOUT; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; @@ -112,22 +112,22 @@ void testGetTokenAsPassword() throws InterruptedException { } @Test - void useDefaultTokenTimeout() throws NoSuchFieldException, IllegalAccessException { + void useDefaultAccessTokenTimeout() throws NoSuchFieldException, IllegalAccessException { AzureAuthenticationTemplate template = new AzureAuthenticationTemplate(); Properties properties = new Properties(); template.init(properties); assertNotNull(template.getBlockTimeout()); - Field defaultValueField = TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS.getClass().getDeclaredField("defaultValue"); + Field defaultValueField = GET_TOKEN_TIMEOUT.getClass().getDeclaredField("defaultValue"); defaultValueField.setAccessible(true); - String defaultVault = (String) defaultValueField.get(TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS); + String defaultVault = (String) defaultValueField.get(GET_TOKEN_TIMEOUT); assertEquals(template.getBlockTimeout().getSeconds() + "", defaultVault); } @Test - void useCustomTokenTimeout() { + void useCustomAccessTokenTimeout() { AzureAuthenticationTemplate template = new AzureAuthenticationTemplate(); Properties properties = new Properties(); - properties.setProperty(AuthProperty.TOKEN_ACCESS_TOKEN_TIMEOUT_IN_SECONDS.getPropertyKey(), "35"); + properties.setProperty(AuthProperty.GET_TOKEN_TIMEOUT.getPropertyKey(), "35"); template.init(properties); assertNotNull(template.getBlockTimeout()); assertEquals(template.getBlockTimeout().getSeconds(), 35);