22
33import com .fasterxml .jackson .databind .JsonNode ;
44
5+ import java .io .IOException ;
56import java .util .ArrayList ;
67import java .util .HashMap ;
78import java .util .List ;
89import java .util .Map ;
9-
10- import static com .microsoft .aad .msal4j .ManagedIdentitySourceType .SERVICE_FABRIC ;
1110import static org .junit .jupiter .api .Assertions .assertEquals ;
1211
1312import com .microsoft .aad .msal4j .Shortcuts .TestConfig ;
@@ -31,7 +30,7 @@ static Map<String, ManagedIdentityApplication> createAppsFromConfig(TestConfig c
3130 if ("ManagedIdentityClient" .equals (appObject .getType ())) {
3231 ManagedIdentityId identityId = createManagedIdentityId (appObject );
3332 List <String > capabilities = extractClientCapabilities (appObject );
34- IEnvironmentVariables envVars = createEnvironmentVariables (config );
33+ IEnvironmentVariables envVars = setEnvironmentVariables (config );
3534 // TODO: other application properties
3635
3736 ManagedIdentityApplication app = ManagedIdentityApplication .builder (identityId )
@@ -53,21 +52,19 @@ static Map<String, ManagedIdentityApplication> createAppsFromConfig(TestConfig c
5352 */
5453 static IAuthenticationResult executeAction (ManagedIdentityApplication app , TestAction action ) throws Exception {
5554 if (action .getMethodName ().equals ("AcquireTokenForManagedIdentity" )) {
56- LOG .info (String .format ("Executing action: %s" , action .getMethodName ()));
57-
55+ LOG .info (String .format ("===Executing action: %s" , action .getMethodName ()));
5856 ManagedIdentityParameters params = buildManagedIdentityParameters (action );
59-
6057 IAuthenticationResult result = app .acquireTokenForManagedIdentity (params ).get ();
6158
62- LOG .info ("Action result: " );
63- LOG .info (String .format ("Access Token: %s" , result .accessToken ()));
64- LOG .info (String .format ("ID Token : %s" , result .idToken ()));
65- LOG .info (String .format ("Account : %s" , result .account ()));
66- LOG .info (String .format ("Token Source: %s" , result .metadata ().tokenSource ()));
59+ LOG .info ("--- Action result" );
60+ LOG .info (String .format ("- Access Token: %s" , result .accessToken ()));
61+ LOG .info (String .format ("- ID Token : %s" , result .idToken ()));
62+ LOG .info (String .format ("- Account : %s" , result .account ()));
63+ LOG .info (String .format ("- Token Source: %s" , result .metadata ().tokenSource ()));
6764
6865 return result ;
6966 } else {
70- //TODO: other token calls and apps
67+ //TODO: other token calls and confidential/public client apps
7168 throw new UnsupportedOperationException ("Unsupported action: " + action .getMethodName ());
7269 }
7370 }
@@ -80,10 +77,10 @@ static void validateAssertions(IAuthenticationResult result, Map<String, JsonNod
8077 assertions .forEach ((key , value ) -> {
8178 switch (key ) {
8279 case "token_source" :
83- LOG .info ("Validating token source" );
80+ LOG .info ("=== Validating token source" );
8481 validateTokenSource (value .asText (), result );
8582 break ;
86- //TODO: other assertions
83+ //TODO: other assertions, such as exceptions checks, token content, etc.
8784 default :
8885 // Optional: Handle unknown assertion types
8986 break ;
@@ -95,13 +92,23 @@ static void validateAssertions(IAuthenticationResult result, Map<String, JsonNod
9592 * Create managed identity ID from test object
9693 */
9794 static ManagedIdentityId createManagedIdentityId (TestObject appObject ) {
98- String idType = appObject .getProperty ("managed_identity" ).get ("ManagedIdentityIdType" ).asText ();
99-
100- if ("SystemAssigned" .equals (idType )) {
101- return ManagedIdentityId .systemAssigned ();
102- } else {
103- // TODO: handle user assertions
104- return null ;
95+ JsonNode managedIdentityNode = appObject .getProperty ("managed_identity" );
96+ String idType = managedIdentityNode .get ("ManagedIdentityIdType" ).asText ();
97+
98+ switch (idType ) {
99+ case "SystemAssigned" :
100+ return ManagedIdentityId .systemAssigned ();
101+ case "ClientId" :
102+ String clientId = managedIdentityNode .get ("Id" ).asText ();
103+ return ManagedIdentityId .userAssignedClientId (clientId );
104+ case "ObjectId" :
105+ String objectId = managedIdentityNode .get ("Id" ).asText ();
106+ return ManagedIdentityId .userAssignedObjectId (objectId );
107+ case "ResourceId" :
108+ String resourceId = managedIdentityNode .get ("Id" ).asText ();
109+ return ManagedIdentityId .userAssignedResourceId (resourceId );
110+ default :
111+ throw new IllegalArgumentException ("Unsupported ManagedIdentityIdType: " + idType );
105112 }
106113 }
107114
@@ -116,16 +123,29 @@ static List<String> extractClientCapabilities(TestObject testObject) {
116123 capabilitiesNode .forEach (node -> capabilities .add (node .asText ()));
117124 }
118125
119- LOG .info (String .format ("Extracted client capabilities: %s" , capabilities ));
126+ LOG .info (String .format ("--- Extracted client capabilities: %s" , capabilities ));
120127
121128 return capabilities ;
122129 }
123130
124- //TODO: Re-used from other Managed Identity tests, specific to this proof-of-concept but should be more generic
125- static IEnvironmentVariables createEnvironmentVariables (TestConfig config ) {
126- return new EnvironmentVariablesHelper (
127- SERVICE_FABRIC ,
128- config .getEnvironmentVariable ("IDENTITY_ENDPOINT" ));
131+ /**
132+ * Creates provider for mocked environment variables using the test configuration.
133+ *
134+ * @param config The test configuration containing the environment variables
135+ * @return An IEnvironmentVariables implementation with the configured variables
136+ */
137+ static IEnvironmentVariables setEnvironmentVariables (TestConfig config ) {
138+ // Get all environment variables from the config
139+ final Map <String , String > envVars = config .getAllEnvironmentVariables ();
140+
141+ LOG .info (String .format ("---Configured environment variables: %s" , envVars .keySet ()));
142+
143+ return new IEnvironmentVariables () {
144+ @ Override
145+ public String getEnvironmentVariable (String envVariable ) {
146+ return envVars .get (envVariable );
147+ }
148+ };
129149 }
130150
131151 /**
@@ -141,7 +161,8 @@ static ManagedIdentityParameters buildManagedIdentityParameters(TestAction actio
141161
142162 // Add optional claims challenge
143163 if (action .hasParameter ("claims_challenge" )) {
144- builder .claims (action .getParameter ("claims_challenge" ).asText ());
164+ String validatedClaimsChallenge = Shortcuts .validateAndGetClaimsChallenge (action );
165+ builder .claims (validatedClaimsChallenge );
145166 }
146167
147168 //TODO: other parameters
@@ -155,9 +176,42 @@ static ManagedIdentityParameters buildManagedIdentityParameters(TestAction actio
155176 static void validateTokenSource (String expectedSource , IAuthenticationResult result ) {
156177 TokenSource expected = "identity_provider" .equals (expectedSource ) ?
157178 TokenSource .IDENTITY_PROVIDER : TokenSource .CACHE ;
158- LOG .info (String .format ("Expected token source: %s" , expected ));
159- LOG .info (String .format ("Actual token source : %s" , result .metadata ().tokenSource ()));
179+ LOG .info (String .format ("--- Expected token source: %s" , expected ));
180+ LOG .info (String .format ("--- Actual token source : %s" , result .metadata ().tokenSource ()));
160181
161182 assertEquals (expected , result .metadata ().tokenSource ());
162183 }
184+
185+ /**
186+ * Complete workflow to get all test case configs
187+ *
188+ * @param indexEndpoint The URL of the index containing test case URLs
189+ * @return Map of test case names to their JSON configurations
190+ */
191+ static Map <String , JsonNode > getAllTestCaseConfigs (String indexEndpoint ) throws IOException {
192+ // Get list of SML test case URLs
193+ List <String > smlUrls = RunnerJsonHelper .getTestCaseUrlsFromEndpoint (indexEndpoint );
194+
195+ // Convert SML URLs to JSON URLs
196+ List <String > jsonUrls = Shortcuts .convertSmlUrlsToJsonUrls (smlUrls );
197+
198+ // Fetch content for each JSON URL
199+ Map <String , JsonNode > testCaseConfigs = new HashMap <>();
200+ for (String jsonUrl : jsonUrls ) {
201+ String testCaseName = extractTestCaseName (jsonUrl );
202+ JsonNode config = RunnerJsonHelper .fetchJsonContent (jsonUrl );
203+ testCaseConfigs .put (testCaseName , config );
204+ }
205+
206+ return testCaseConfigs ;
207+ }
208+
209+ /**
210+ * Extract test case name from URL
211+ */
212+ private static String extractTestCaseName (String url ) {
213+ String [] parts = url .split ("/" );
214+ String fileName = parts [parts .length - 1 ];
215+ return fileName .substring (0 , fileName .lastIndexOf ('.' ));
216+ }
163217}
0 commit comments