|
| 1 | +"""End-to-end tests for Federated Managed Identity (FMI) functionality. |
| 2 | +
|
| 3 | +These tests verify: |
| 4 | +1. Tokens can be acquired using certificate authentication with FMI path |
| 5 | +2. Tokens are properly cached and returned from cache on subsequent calls |
| 6 | +3. Tokens can be acquired using an assertion callback (RMA pattern) with FMI path |
| 7 | +
|
| 8 | +""" |
| 9 | + |
| 10 | +import logging |
| 11 | +import os |
| 12 | +import sys |
| 13 | +import unittest |
| 14 | + |
| 15 | +import msal |
| 16 | +from tests.http_client import MinimalHttpClient |
| 17 | +from tests.lab_config import get_client_certificate |
| 18 | +from tests.test_e2e import LabBasedTestCase |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | +logging.basicConfig(level=logging.DEBUG if "-v" in sys.argv else logging.INFO) |
| 22 | + |
| 23 | +# Test configuration |
| 24 | +_FMI_TENANT_ID = "f645ad92-e38d-4d1a-b510-d1b09a74a8ca" |
| 25 | +_FMI_CLIENT_ID = "4df2cbbb-8612-49c1-87c8-f334d6d065ad" |
| 26 | +_FMI_SCOPE = "3091264c-7afb-45d4-b527-39737ee86187/.default" |
| 27 | +_FMI_PATH = "SomeFmiPath/FmiCredentialPath" |
| 28 | +_FMI_CLIENT_ID_URN = "urn:microsoft:identity:fmi" |
| 29 | +_FMI_SCOPE_FOR_RMA = "api://AzureFMITokenExchange/.default" |
| 30 | +_AUTHORITY_URL = "https://login.microsoftonline.com/" + _FMI_TENANT_ID |
| 31 | + |
| 32 | + |
| 33 | +def _get_fmi_credential_from_rma(): |
| 34 | + """Acquire an FMI token from RMA service using certificate credentials. |
| 35 | +
|
| 36 | + This mirrors the Go function GetFmiCredentialFromRma: |
| 37 | + 1. Create a confidential client with certificate credential |
| 38 | + 2. Acquire a token for the FMI scope with the FMI path |
| 39 | + 3. Return the access token as an assertion string |
| 40 | + """ |
| 41 | + |
| 42 | + app = msal.ConfidentialClientApplication( |
| 43 | + _FMI_CLIENT_ID, |
| 44 | + client_credential=get_client_certificate(), |
| 45 | + authority=_AUTHORITY_URL, |
| 46 | + http_client=MinimalHttpClient(), |
| 47 | + ) |
| 48 | + result = app.acquire_token_for_client_with_fmi_path( |
| 49 | + [_FMI_SCOPE_FOR_RMA], _FMI_PATH) |
| 50 | + if "access_token" not in result: |
| 51 | + raise RuntimeError( |
| 52 | + "Failed to acquire FMI token from RMA: {}: {}".format( |
| 53 | + result.get("error"), result.get("error_description"))) |
| 54 | + return result["access_token"] |
| 55 | + |
| 56 | + |
| 57 | +class TestFMIBasicFunctionality(LabBasedTestCase): |
| 58 | + """Test basic FMI token acquisition with certificate credential. |
| 59 | +
|
| 60 | + Mirrors TestFMIBasicFunctionality from Go: |
| 61 | + 1. Acquire token by credential with FMI path |
| 62 | + 2. Verify silent (cached) token acquisition works |
| 63 | + 3. Validate tokens match (proving cache was used) |
| 64 | + """ |
| 65 | + |
| 66 | + def test_acquire_and_cache_with_fmi_path(self): |
| 67 | + app = msal.ConfidentialClientApplication( |
| 68 | + _FMI_CLIENT_ID, |
| 69 | + client_credential=get_client_certificate(), |
| 70 | + authority=_AUTHORITY_URL, |
| 71 | + http_client=MinimalHttpClient(), |
| 72 | + ) |
| 73 | + scopes = [_FMI_SCOPE] |
| 74 | + |
| 75 | + # 1. Acquire token by credential with FMI path |
| 76 | + result = app.acquire_token_for_client_with_fmi_path(scopes, _FMI_PATH) |
| 77 | + self.assertIn("access_token", result, |
| 78 | + "acquire_token_for_client_with_fmi_path() failed: {}: {}".format( |
| 79 | + result.get("error"), result.get("error_description"))) |
| 80 | + self.assertNotEqual("", result["access_token"], |
| 81 | + "acquire_token_for_client_with_fmi_path() returned empty access token") |
| 82 | + |
| 83 | + first_token = result["access_token"] |
| 84 | + |
| 85 | + # 2. Verify silent token acquisition works (should retrieve from cache) |
| 86 | + cache_result = app.acquire_token_for_client_with_fmi_path(scopes, _FMI_PATH) |
| 87 | + self.assertIn("access_token", cache_result, |
| 88 | + "Second call failed: {}: {}".format( |
| 89 | + cache_result.get("error"), cache_result.get("error_description"))) |
| 90 | + self.assertNotEqual("", cache_result["access_token"], |
| 91 | + "Second call returned empty access token") |
| 92 | + self.assertEqual( |
| 93 | + cache_result.get("token_source"), "cache", |
| 94 | + "Second call should return token from cache") |
| 95 | + |
| 96 | + # 3. Validate tokens match (proving cache was used) |
| 97 | + self.assertEqual(first_token, cache_result["access_token"], |
| 98 | + "Token comparison failed - tokens don't match, " |
| 99 | + "cache might not be working correctly") |
| 100 | + |
| 101 | +class TestFMIIntegration(LabBasedTestCase): |
| 102 | + """Test FMI with assertion callback (RMA pattern). |
| 103 | +
|
| 104 | + Mirrors TestFMIIntegration from Go: |
| 105 | + 1. Get credentials from RMA via assertion callback |
| 106 | + 2. Acquire token by credential with FMI path |
| 107 | + 3. Verify cached token acquisition works |
| 108 | + 4. Compare tokens to verify cache was used |
| 109 | + """ |
| 110 | + |
| 111 | + def test_acquire_with_assertion_callback_and_fmi_path(self): |
| 112 | + # Create credential from assertion callback (mirrors Go's NewCredFromAssertionCallback) |
| 113 | + client_credential = { |
| 114 | + "client_assertion": lambda: _get_fmi_credential_from_rma(), |
| 115 | + } |
| 116 | + |
| 117 | + app = msal.ConfidentialClientApplication( |
| 118 | + _FMI_CLIENT_ID_URN, |
| 119 | + client_credential=client_credential, |
| 120 | + authority=_AUTHORITY_URL, |
| 121 | + http_client=MinimalHttpClient(), |
| 122 | + ) |
| 123 | + scopes = [_FMI_SCOPE] |
| 124 | + fmi_path = "SomeFmiPath/Path" |
| 125 | + |
| 126 | + # 1. Acquire token by credential with FMI path |
| 127 | + result = app.acquire_token_for_client_with_fmi_path(scopes, fmi_path) |
| 128 | + self.assertIn("access_token", result, |
| 129 | + "acquire_token_for_client_with_fmi_path() failed: {}: {}".format( |
| 130 | + result.get("error"), result.get("error_description"))) |
| 131 | + self.assertNotEqual("", result["access_token"], |
| 132 | + "acquire_token_for_client_with_fmi_path() returned empty access token") |
| 133 | + first_token = result["access_token"] |
| 134 | + |
| 135 | + # 2. Verify cached token acquisition works |
| 136 | + cache_result = app.acquire_token_for_client_with_fmi_path(scopes, fmi_path) |
| 137 | + self.assertIn("access_token", cache_result, |
| 138 | + "Second call failed: {}: {}".format( |
| 139 | + cache_result.get("error"), cache_result.get("error_description"))) |
| 140 | + self.assertNotEqual("", cache_result["access_token"], |
| 141 | + "Second call returned empty access token") |
| 142 | + self.assertEqual( |
| 143 | + cache_result.get("token_source"), "cache", |
| 144 | + "Second call should return token from cache") |
| 145 | + |
| 146 | + # 3. Compare tokens to verify cache was used |
| 147 | + self.assertEqual(first_token, cache_result["access_token"], |
| 148 | + "Token comparison failed - tokens don't match, " |
| 149 | + "cache might not be working correctly") |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + unittest.main() |
0 commit comments