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 token acquisition when using azure-json #42860

Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions sdk/keyvault/azure-security-keyvault-jca/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ Please replace `${KEY_VAULT}` with your key vault name and replace `${MANAGED_ID

## Troubleshooting

### Debug Key Vault Provider

Remote debugger can be used to troubleshoot complex issues, add below parameter for `jarsigner` command to enable remote debug:

```shell
-J-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=y
moarychan marked this conversation as resolved.
Show resolved Hide resolved
```

Create a Remote JVM Debug configuration in your IDE tool, such as in Intellij IDEA:

![remote-jvm-debug.png](./resources/remote-jvm-debug.png.png)

## Configure logging
This module uses JUL (`java.util.logging`), so to configure things like the logging level you can directly modify the JUL configuration.

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static AccessToken fromJson(JsonReader jsonReader) throws IOException {
if ("access_token".equals(fieldName)) {
deserializedAccessToken.accessToken = reader.getString();
} else if ("expires_in".equals(fieldName)) {
deserializedAccessToken.expiresIn = reader.getLong();
deserializedAccessToken.expiresIn = Long.parseLong(reader.getString());
moarychan marked this conversation as resolved.
Show resolved Hide resolved
} else {
reader.skipChildren();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
exports com.azure.security.keyvault.jca.implementation.signature to java.base;

provides java.security.Provider with com.azure.security.keyvault.jca.KeyVaultJcaProvider;
uses com.azure.security.keyvault.jca.implementation.shaded.com.azure.json.JsonProvider;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,30 @@

package com.azure.security.keyvault.jca.implementation.utils;

import com.azure.json.JsonProviders;
import com.azure.json.JsonReader;
import com.azure.json.JsonToken;
import com.azure.json.ReadValueCallback;
import com.azure.json.implementation.jackson.core.JsonParseException;
import com.azure.security.keyvault.jca.implementation.model.AccessToken;
import com.azure.security.keyvault.jca.implementation.model.CertificateBundle;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* The JUnit tests for the {@link JsonConverterUtil} class.
*/
public class JsonConverterUtilTest {

/**
* Test the {@link JsonConverterUtil#fromJson(ReadValueCallback, String)} method.
*/
Expand All @@ -43,4 +53,44 @@ public void testToJson() {
assertTrue(string.contains("cer"));
assertTrue(string.contains("\"value\""));
}

@Test
void testFromJsonWithInvalidTokenResponseBody() {
final String accessTokenBody = getAccessTokenBody("src/test/resources/aad/invalid-access-token-response.json");
moarychan marked this conversation as resolved.
Show resolved Hide resolved
assertThrowsExactly(JsonParseException.class, () -> {
try (JsonReader reader = JsonProviders.createReader(accessTokenBody)) {
while (reader.nextToken() != JsonToken.END_OBJECT) {
String fieldName = reader.getFieldName();
reader.nextToken();
if ("expires_in".equals(fieldName)) {
reader.getLong();
} else {
reader.skipChildren();
}
}
}
});
}

@Test
void testFromJsonWithTokenResponseBody() {
String accessTokenBody = getAccessTokenBody("src/test/resources/aad/access-token-response.json");
AccessToken accessToken = null;
try {
accessToken = JsonConverterUtil.fromJson(AccessToken::fromJson, accessTokenBody);
} catch (IOException e) {
throw new RuntimeException(e);
}
assertNotNull(accessToken);
assertEquals("test_access_token_value", accessToken.getAccessToken());
}

private static String getAccessTokenBody(String filePath) {
moarychan marked this conversation as resolved.
Show resolved Hide resolved
String accessTokenBody = null;
try {
accessTokenBody = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
} catch (IOException ignored) {
}
return accessTokenBody;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"token_type": "Bearer",
"expires_in": "3599",
"ext_expires_in": "3599",
"expires_on": "1731052824",
"not_before": "1731048924",
"resource": "https://vault.azure.net",
"access_token": "test_access_token_value"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"token_type": "Bearer",
"expires_in": "3599",
moarychan marked this conversation as resolved.
Show resolved Hide resolved
"ext_expires_in": 3599,
"expires_on": "1731052824",
"not_before": "1731048924",
"resource": "https://vault.azure.net",
"access_token": "test_invalid_access_token_value"
}