Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue where the token acquisition timeout is not set via the property azure.accessTokenTimeoutInSeconds #43641

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion sdk/identity/azure-identity-extensions/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class TokenCredentialProviderOptions {
private boolean managedIdentityEnabled;
private String tokenCredentialProviderClassName;
private String tokenCredentialBeanName;
private String accessTokenTimeoutInSeconds;

public TokenCredentialProviderOptions() {

Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ 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);
"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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -27,6 +28,8 @@ public class AzureAuthenticationTemplate {

private AccessTokenResolver accessTokenResolver;

private long accessTokenTimeoutInSeconds;

/**
* Default constructor for AzureAuthenticationTemplate
*/
Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -110,7 +119,7 @@ TokenCredentialProvider getTokenCredentialProvider() {
}

Duration getBlockTimeout() {
return Duration.ofSeconds(30);
return Duration.ofSeconds(accessTokenTimeoutInSeconds);
}

AtomicBoolean getIsInitialized() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Loading