Skip to content

Commit 36dfe18

Browse files
committed
Add sample app to test msi on cloud shell or VM
1 parent 0913e04 commit 36dfe18

File tree

4 files changed

+208
-33
lines changed

4 files changed

+208
-33
lines changed

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AbstractManagedIdentitySource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public ManagedIdentityResponse handleResponse(
9090
throw new MsalManagedIdentityException(AuthenticationErrorCode.MANAGED_IDENTITY_REQUEST_FAILED, message, managedIdentitySourceType);
9191
}
9292
} catch (Exception e) {
93-
if (!(e instanceof MsalServiceException)) {
93+
if (!(e instanceof MsalManagedIdentityException)) {
9494
LOG.error(
9595
String.format("[Managed Identity] Exception: %s Http status code: %s", e.getMessage(),
9696
response != null ? response.statusCode() : ""));

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/ManagedIdentityErrorResponse.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
package com.microsoft.aad.msal4j;
55

66
import com.fasterxml.jackson.annotation.JsonProperty;
7+
import lombok.Getter;
78

9+
@Getter
810
public class ManagedIdentityErrorResponse {
911

1012
@JsonProperty("message")
@@ -18,36 +20,4 @@ public class ManagedIdentityErrorResponse {
1820

1921
@JsonProperty("error_description")
2022
private String errorDescription;
21-
22-
public String getMessage() {
23-
return message;
24-
}
25-
26-
public void setMessage(String message) {
27-
this.message = message;
28-
}
29-
30-
public String getCorrelationId() {
31-
return correlationId;
32-
}
33-
34-
public void setCorrelationId(String correlationId) {
35-
this.correlationId = correlationId;
36-
}
37-
38-
public String getError() {
39-
return error;
40-
}
41-
42-
public void setError(String error) {
43-
this.error = error;
44-
}
45-
46-
public String getErrorDescription() {
47-
return errorDescription;
48-
}
49-
50-
public void setErrorDescription(String errorDescription) {
51-
this.errorDescription = errorDescription;
52-
}
5323
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.microsoft.msi</groupId>
5+
<artifactId>msi-sample-jar</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0.1</version>
8+
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<groupId>org.apache.maven.plugins</groupId>
13+
<artifactId>maven-compiler-plugin</artifactId>
14+
<version>3.7.0</version>
15+
<configuration>
16+
<source>8</source>
17+
<target>8</target>
18+
</configuration>
19+
</plugin>
20+
<plugin>
21+
<groupId>org.apache.maven.plugins</groupId>
22+
<artifactId>maven-jar-plugin</artifactId>
23+
<version>2.5</version>
24+
<configuration>
25+
<archive>
26+
<manifest>
27+
<addClasspath>true</addClasspath>
28+
<classpathPrefix>lib/</classpathPrefix>
29+
<mainClass>com.microsoft.msi.App</mainClass>
30+
</manifest>
31+
</archive>
32+
</configuration>
33+
</plugin>
34+
<plugin>
35+
<artifactId>maven-assembly-plugin</artifactId>
36+
<configuration>
37+
<archive>
38+
<manifest>
39+
<addClasspath>true</addClasspath>
40+
<classpathPrefix>lib/</classpathPrefix>
41+
<mainClass>com.microsoft.msi.App</mainClass>
42+
</manifest>
43+
</archive>
44+
<descriptorRefs>
45+
<descriptorRef>jar-with-dependencies</descriptorRef>
46+
</descriptorRefs>
47+
</configuration>
48+
<executions>
49+
<execution>
50+
<id>make-assembly</id>
51+
<phase>package</phase>
52+
<goals>
53+
<goal>single</goal>
54+
</goals>
55+
</execution>
56+
</executions>
57+
</plugin>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-source-plugin</artifactId>
61+
<version>2.2.1</version>
62+
<executions>
63+
<execution>
64+
<id>attach-sources</id>
65+
<goals>
66+
<goal>jar</goal>
67+
</goals>
68+
</execution>
69+
</executions>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
74+
<dependencies>
75+
<dependency>
76+
<groupId>junit</groupId>
77+
<artifactId>junit</artifactId>
78+
<version>3.8.1</version>
79+
<scope>test</scope>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.slf4j</groupId>
83+
<artifactId>slf4j-simple</artifactId>
84+
<version>1.7.36</version>
85+
</dependency>
86+
<dependency>
87+
<groupId>com.microsoft.azure</groupId>
88+
<artifactId>msal4j</artifactId>
89+
<version>1.13.11-msi</version>
90+
<scope>compile</scope>
91+
</dependency>
92+
</dependencies>
93+
</project>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.microsoft.msi;
2+
3+
import com.microsoft.aad.msal4j.IAuthenticationResult;
4+
import com.microsoft.aad.msal4j.ManagedIdentityApplication;
5+
import com.microsoft.aad.msal4j.ManagedIdentityId;
6+
import com.microsoft.aad.msal4j.ManagedIdentityParameters;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
import java.io.BufferedReader;
11+
import java.io.IOException;
12+
import java.io.InputStreamReader;
13+
14+
/**
15+
* Sample app to test MSI using a single jar.
16+
* To create a jar run mvn install -Dmaven.test.skip=true for msal4j pom and then msi-sample-jar pom.
17+
* Copy the jar with dependencies and run on a VM or cloud shell using java -jar msi-sample-jar-1.0.1-jar-with-dependencies.jar
18+
*/
19+
public class App
20+
{
21+
public static void main( String[] args ) throws IOException {
22+
String response;
23+
String resource = "https://management.azure.com";
24+
int option = 1;
25+
BufferedReader reader = new BufferedReader(
26+
new InputStreamReader(System.in));
27+
final Logger logger = LoggerFactory.getLogger(App.class);
28+
29+
while (option != 0) {
30+
System.out.println("Enter one of the following options to create a managed identity application:");
31+
System.out.println("1: System assigned managed identity");
32+
System.out.println("2: User assigned managed identity");
33+
System.out.println("0: Quit");
34+
35+
option = Integer.parseInt(reader.readLine());
36+
37+
switch (option) {
38+
case 1:
39+
acquireTokenWithSAMI(resource, logger, reader);
40+
break;
41+
case 2:
42+
acquireTokenWithUAMI(resource, logger, reader);
43+
break;
44+
case 0:
45+
return;
46+
default:
47+
System.out.println("Invalid option, try again.");
48+
}
49+
50+
}
51+
}
52+
53+
private static void acquireTokenWithSAMI(String resource, Logger logger, BufferedReader reader) throws IOException {
54+
System.out.println("Enter a scope to acquire token for.");
55+
resource = reader.readLine();
56+
57+
ManagedIdentityApplication msiApp = ManagedIdentityApplication
58+
.builder(ManagedIdentityId.systemAssigned())
59+
.logPii(true)
60+
.build();
61+
62+
try {
63+
logger.info("Trying to acquire a token for system assigned managed identity with provided resource.");
64+
IAuthenticationResult result = msiApp.acquireTokenForManagedIdentity(ManagedIdentityParameters.builder(resource).build()).get();
65+
logger.info("Access token recieved: " + result.accessToken().substring(0, 10) + "\nScopes: " + result.scopes());
66+
} catch (Exception e) {
67+
e.printStackTrace();
68+
}
69+
}
70+
71+
private static void acquireTokenWithUAMI(String resource, Logger logger, BufferedReader reader) throws IOException {
72+
System.out.println("Enter the options to create a user assigned managed identity");
73+
System.out.println("1. User assigned client id.");
74+
System.out.println("2. User assigned resource id.");
75+
76+
int option = Integer.parseInt(reader.readLine());
77+
78+
ManagedIdentityId msiId;
79+
80+
switch (option) {
81+
case 1:
82+
System.out.println("Enter client id of the user assigned managed identity");
83+
String clientId = reader.readLine();
84+
msiId = ManagedIdentityId.userAssignedClientId(clientId);
85+
break;
86+
case 2:
87+
System.out.println("Enter resource id of the user assigned managed identity");
88+
String resourceId = reader.readLine();
89+
msiId = ManagedIdentityId.userAssignedResourceId(resourceId);
90+
break;
91+
default:
92+
System.out.println("Invalid option");
93+
return;
94+
}
95+
96+
System.out.println("Enter a scope to acquire token for. ");
97+
resource = reader.readLine();
98+
99+
ManagedIdentityApplication msiApp = ManagedIdentityApplication
100+
.builder(msiId)
101+
.logPii(true)
102+
.build();
103+
104+
try {
105+
logger.info("Trying to acquire a token for system assigned managed identity with provided resource.");
106+
IAuthenticationResult result = msiApp.acquireTokenForManagedIdentity(ManagedIdentityParameters.builder(resource).build()).get();
107+
logger.info("Access token received: " + result.accessToken().substring(0, 10) + "\nScopes: " + result.scopes());
108+
} catch (Exception e) {
109+
e.printStackTrace();
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)