|
| 1 | +package com.microsoft.aad.msal4j; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import static com.microsoft.aad.msal4j.ManagedIdentitySourceType.SERVICE_FABRIC; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 12 | + |
| 13 | +import com.microsoft.aad.msal4j.Shortcuts.TestConfig; |
| 14 | +import com.microsoft.aad.msal4j.Shortcuts.TestObject; |
| 15 | +import com.microsoft.aad.msal4j.Shortcuts.TestAction; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | + |
| 19 | +public class RunnerHelper { |
| 20 | + private static final Logger LOG = LoggerFactory.getLogger(RunnerHelper.class); |
| 21 | + |
| 22 | + /** |
| 23 | + * Create Managed Identity applications from the test configuration. |
| 24 | + * This method processes the "arrange" section of the test configuration. |
| 25 | + */ |
| 26 | + static Map<String, ManagedIdentityApplication> createAppsFromConfig(TestConfig config) { |
| 27 | + Map<String, ManagedIdentityApplication> apps = new HashMap<>(); |
| 28 | + |
| 29 | + for (String appName : config.getAllArrangeObjects()) { |
| 30 | + TestObject appObject = config.getArrangeObject(appName); |
| 31 | + if ("ManagedIdentityClient".equals(appObject.getType())) { |
| 32 | + ManagedIdentityId identityId = createManagedIdentityId(appObject); |
| 33 | + List<String> capabilities = extractClientCapabilities(appObject); |
| 34 | + IEnvironmentVariables envVars = createEnvironmentVariables(config); |
| 35 | + // TODO: other application properties |
| 36 | + |
| 37 | + ManagedIdentityApplication app = ManagedIdentityApplication.builder(identityId) |
| 38 | + .clientCapabilities(capabilities) |
| 39 | + .build(); |
| 40 | + |
| 41 | + ManagedIdentityApplication.setEnvironmentVariables(envVars); |
| 42 | + |
| 43 | + apps.put(appName, app); |
| 44 | + } //TODO: Confidential and public clients |
| 45 | + } |
| 46 | + |
| 47 | + return apps; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Execute an action and return the result |
| 52 | + * This method uses the "act" section of the test configuration. |
| 53 | + */ |
| 54 | + static IAuthenticationResult executeAction(ManagedIdentityApplication app, TestAction action) throws Exception { |
| 55 | + if (action.getMethodName().equals("AcquireTokenForManagedIdentity")) { |
| 56 | + LOG.info(String.format("Executing action: %s", action.getMethodName())); |
| 57 | + |
| 58 | + ManagedIdentityParameters params = buildManagedIdentityParameters(action); |
| 59 | + |
| 60 | + IAuthenticationResult result = app.acquireTokenForManagedIdentity(params).get(); |
| 61 | + |
| 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())); |
| 67 | + |
| 68 | + return result; |
| 69 | + } else { |
| 70 | + //TODO: other token calls and apps |
| 71 | + throw new UnsupportedOperationException("Unsupported action: " + action.getMethodName()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Validate assertions against a result. |
| 77 | + * This method uses the "assert" section of the test configuration. |
| 78 | + */ |
| 79 | + static void validateAssertions(IAuthenticationResult result, Map<String, JsonNode> assertions) { |
| 80 | + assertions.forEach((key, value) -> { |
| 81 | + switch (key) { |
| 82 | + case "token_source": |
| 83 | + LOG.info("Validating token source"); |
| 84 | + validateTokenSource(value.asText(), result); |
| 85 | + break; |
| 86 | + //TODO: other assertions |
| 87 | + default: |
| 88 | + // Optional: Handle unknown assertion types |
| 89 | + break; |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Create managed identity ID from test object |
| 96 | + */ |
| 97 | + 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; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Extract client capabilities from test object |
| 110 | + */ |
| 111 | + static List<String> extractClientCapabilities(TestObject testObject) { |
| 112 | + List<String> capabilities = new ArrayList<>(); |
| 113 | + JsonNode capabilitiesNode = testObject.getProperty("client_capabilities"); |
| 114 | + |
| 115 | + if (capabilitiesNode != null && capabilitiesNode.isArray()) { |
| 116 | + capabilitiesNode.forEach(node -> capabilities.add(node.asText())); |
| 117 | + } |
| 118 | + |
| 119 | + LOG.info(String.format("Extracted client capabilities: %s", capabilities)); |
| 120 | + |
| 121 | + return capabilities; |
| 122 | + } |
| 123 | + |
| 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")); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Build parameters for token acquisition |
| 133 | + */ |
| 134 | + static ManagedIdentityParameters buildManagedIdentityParameters(TestAction action) { |
| 135 | + String resource = action.getParameter("resource").asText(); |
| 136 | + |
| 137 | + LOG.info(String.format("Building ManagedIdentityParameters with resource: %s", resource)); |
| 138 | + |
| 139 | + ManagedIdentityParameters.ManagedIdentityParametersBuilder builder = |
| 140 | + ManagedIdentityParameters.builder(resource); |
| 141 | + |
| 142 | + // Add optional claims challenge |
| 143 | + if (action.hasParameter("claims_challenge")) { |
| 144 | + builder.claims(action.getParameter("claims_challenge").asText()); |
| 145 | + } |
| 146 | + |
| 147 | + //TODO: other parameters |
| 148 | + |
| 149 | + return builder.build(); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Validate token source assertion, either cache or identity provider |
| 154 | + */ |
| 155 | + static void validateTokenSource(String expectedSource, IAuthenticationResult result) { |
| 156 | + TokenSource expected = "identity_provider".equals(expectedSource) ? |
| 157 | + 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())); |
| 160 | + |
| 161 | + assertEquals(expected, result.metadata().tokenSource()); |
| 162 | + } |
| 163 | +} |
0 commit comments