Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…tion-library-for-java into avdunn/cache-refresh-metadata
  • Loading branch information
Avery-Dunn committed Jan 29, 2025
2 parents ddad77b + 15da510 commit e312512
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
23 changes: 21 additions & 2 deletions msal4j-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<version>3.5.2</version>
<configuration>
<argLine>-noverify</argLine>
<argLine>@{argLine} -noverify</argLine>
</configuration>
</plugin>

Expand Down Expand Up @@ -339,6 +339,25 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class ServiceFabricManagedIdentitySource extends AbstractManagedIdentitySource {
//Service Fabric requires a special check for an environment variable containing a certificate thumbprint used for validating requests.
//No other flow need this and an app developer may not be aware of it, so it was decided that for the Service Fabric flow we will simply override
// any HttpClient that may have been set by the app developer with our own client which performs the validation logic.
private final IHttpClient httpClient = new DefaultHttpClientManagedIdentity(null, null, null, null);
private final HttpHelper httpHelper = new HttpHelper(httpClient);
private static IHttpClient httpClient = new DefaultHttpClientManagedIdentity(null, null, null, null);
private static HttpHelper httpHelper = new HttpHelperManagedIdentity(httpClient);

@Override
public void createManagedIdentityRequest(String resource) {
Expand Down Expand Up @@ -117,4 +117,10 @@ private static URI validateAndGetUri(String msiEndpoint)
}
}

//The HttpClient is not normally customizable in this flow, as it requires special behavior for certificate validation.
//However, unit tests often need to mock HttpClient and need a way to inject the mocked object into this class.
static void setHttpClient(IHttpClient client) {
httpClient = client;
httpHelper = new HttpHelperManagedIdentity(httpClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ private HttpRequest expectedRequest(ManagedIdentitySourceType source, String res
String endpoint = null;
Map<String, String> headers = new HashMap<>();
Map<String, List<String>> queryParameters = new HashMap<>();
Map<String, List<String>> bodyParameters = new HashMap<>();

switch (source) {
case APP_SERVICE: {
Expand All @@ -89,10 +88,8 @@ private HttpRequest expectedRequest(ManagedIdentitySourceType source, String res
headers.put("ContentType", "application/x-www-form-urlencoded");
headers.put("Metadata", "true");

bodyParameters.put("resource", singletonList(resource));

queryParameters.put("resource", singletonList(resource));
return new HttpRequest(HttpMethod.GET, computeUri(endpoint, queryParameters), headers, URLUtils.serializeParameters(bodyParameters));
break;
}
case IMDS: {
endpoint = IMDS_ENDPOINT;
Expand All @@ -110,11 +107,14 @@ private HttpRequest expectedRequest(ManagedIdentitySourceType source, String res
headers.put("Metadata", "true");
break;
}
case SERVICE_FABRIC:
case SERVICE_FABRIC: {
endpoint = serviceFabricEndpoint;
queryParameters.put("api-version", singletonList("2019-07-01-preview"));
queryParameters.put("resource", singletonList(resource));

headers.put("secret", "secret");
break;
}
}

switch (id.getIdType()) {
Expand Down Expand Up @@ -172,6 +172,9 @@ void managedIdentityTest_SystemAssigned_SuccessfulResponse(ManagedIdentitySource
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
if (source == SERVICE_FABRIC) {
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
}

when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));

Expand Down Expand Up @@ -206,6 +209,9 @@ void managedIdentityTest_UserAssigned_SuccessfulResponse(ManagedIdentitySourceTy
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
if (source == SERVICE_FABRIC) {
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
}

when(httpClientMock.send(expectedRequest(source, resource, id))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));

Expand Down Expand Up @@ -294,6 +300,9 @@ void managedIdentityTest_DifferentScopes_RequestsNewToken(ManagedIdentitySourceT
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
if (source == SERVICE_FABRIC) {
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
}

when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
when(httpClientMock.send(expectedRequest(source, anotherResource))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
Expand Down Expand Up @@ -327,6 +336,9 @@ void managedIdentityTest_WrongScopes(ManagedIdentitySourceType source, String en
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
if (source == SERVICE_FABRIC) {
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
}

if (environmentVariables.getEnvironmentVariable("SourceType").equals(ManagedIdentitySourceType.CLOUD_SHELL.toString())) {
when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(500, getMsiErrorResponseCloudShell()));
Expand Down Expand Up @@ -364,7 +376,11 @@ void managedIdentityTest_WrongScopes(ManagedIdentitySourceType source, String en
void managedIdentityTest_Retry(ManagedIdentitySourceType source, String endpoint, String resource) throws Exception {
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);

DefaultHttpClientManagedIdentity httpClientMock = mock(DefaultHttpClientManagedIdentity.class);
if (source == SERVICE_FABRIC) {
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
}

miApp = ManagedIdentityApplication
.builder(ManagedIdentityId.systemAssigned())
Expand Down Expand Up @@ -415,6 +431,9 @@ void managedIdentity_RequestFailed_NoPayload(ManagedIdentitySourceType source, S
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
if (source == SERVICE_FABRIC) {
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
}

when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(500, ""));

Expand Down Expand Up @@ -449,7 +468,9 @@ void managedIdentity_RequestFailed_NullResponse(ManagedIdentitySourceType source
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);

if (source == SERVICE_FABRIC) {
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
}
when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(200, ""));

miApp = ManagedIdentityApplication
Expand Down Expand Up @@ -483,6 +504,9 @@ void managedIdentity_RequestFailed_UnreachableNetwork(ManagedIdentitySourceType
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
if (source == SERVICE_FABRIC) {
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
}

when(httpClientMock.send(expectedRequest(source, resource))).thenThrow(new SocketException("A socket operation was attempted to an unreachable network."));

Expand Down Expand Up @@ -517,6 +541,9 @@ void managedIdentity_SharedCache(ManagedIdentitySourceType source, String endpoi
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
if (source == SERVICE_FABRIC) {
ServiceFabricManagedIdentitySource.setHttpClient(httpClientMock);
}

when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));

Expand Down

0 comments on commit e312512

Please sign in to comment.