Skip to content

Commit

Permalink
Fix the issue where the token acquisition timeout is not set via the …
Browse files Browse the repository at this point in the history
…property `azure.accessTokenTimeoutInSeconds` (#43641)
  • Loading branch information
moarychan authored Dec 30, 2024
1 parent 9658db9 commit 0fe407a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 4 deletions.
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.GET_TOKEN_TIMEOUT.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 @@ -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'.
*/
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.GET_TOKEN_TIMEOUT;

/**
* 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(GET_TOKEN_TIMEOUT.getPropertyKey())) {
accessTokenTimeoutInSeconds = Long.parseLong(GET_TOKEN_TIMEOUT.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.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;
Expand Down Expand Up @@ -109,4 +111,25 @@ void testGetTokenAsPassword() throws InterruptedException {
}
}

@Test
void useDefaultAccessTokenTimeout() throws NoSuchFieldException, IllegalAccessException {
AzureAuthenticationTemplate template = new AzureAuthenticationTemplate();
Properties properties = new Properties();
template.init(properties);
assertNotNull(template.getBlockTimeout());
Field defaultValueField = GET_TOKEN_TIMEOUT.getClass().getDeclaredField("defaultValue");
defaultValueField.setAccessible(true);
String defaultVault = (String) defaultValueField.get(GET_TOKEN_TIMEOUT);
assertEquals(template.getBlockTimeout().getSeconds() + "", defaultVault);
}

@Test
void useCustomAccessTokenTimeout() {
AzureAuthenticationTemplate template = new AzureAuthenticationTemplate();
Properties properties = new Properties();
properties.setProperty(AuthProperty.GET_TOKEN_TIMEOUT.getPropertyKey(), "35");
template.init(properties);
assertNotNull(template.getBlockTimeout());
assertEquals(template.getBlockTimeout().getSeconds(), 35);
}
}

0 comments on commit 0fe407a

Please sign in to comment.