55
66import com .azure .core .credential .TokenRequestContext ;
77import com .azure .core .exception .ClientAuthenticationException ;
8+ import com .azure .core .http .HttpClient ;
9+ import com .azure .core .test .http .MockHttpResponse ;
810import com .azure .core .test .utils .TestConfigurationSource ;
911import com .azure .core .util .Configuration ;
1012import com .azure .identity .implementation .IdentityClient ;
13+ import com .azure .identity .implementation .IdentityClientOptions ;
1114import com .azure .identity .util .TestUtils ;
1215import org .junit .jupiter .api .Assertions ;
1316import org .junit .jupiter .api .Test ;
17+ import org .junit .jupiter .params .ParameterizedTest ;
18+ import org .junit .jupiter .params .provider .ValueSource ;
1419import org .mockito .MockedConstruction ;
1520import reactor .test .StepVerifier ;
1621
22+ import java .nio .charset .StandardCharsets ;
1723import java .time .OffsetDateTime ;
1824import java .time .ZoneOffset ;
1925import java .util .UUID ;
2026
2127import static org .hamcrest .MatcherAssert .assertThat ;
2228import static org .hamcrest .core .IsInstanceOf .instanceOf ;
29+ import static org .junit .jupiter .api .Assertions .assertEquals ;
30+ import static org .junit .jupiter .api .Assertions .assertThrows ;
2331import static org .mockito .Mockito .mockConstruction ;
2432import static org .mockito .Mockito .when ;
2533
@@ -31,7 +39,39 @@ public class ManagedIdentityCredentialTest {
3139 @ Test
3240 public void testVirtualMachineMSICredentialConfigurations () {
3341 ManagedIdentityCredential credential = new ManagedIdentityCredentialBuilder ().clientId ("foo" ).build ();
34- Assertions .assertEquals ("foo" , credential .getClientId ());
42+ assertEquals ("foo" , credential .getClientId ());
43+ }
44+
45+ @ ParameterizedTest
46+ @ ValueSource (booleans = { true , false })
47+ public void testInvalidJsonResponse (boolean isChained ) {
48+ HttpClient client = TestUtils .getMockHttpClient (
49+ getMockResponse (400 ,
50+ "{\" error\" :\" invalid_request\" ,\" error_description\" :\" Required metadata header not specified\" }" ),
51+ getMockResponse (200 , "invalid json" ));
52+
53+ String endpoint = "http://localhost" ;
54+ String secret = "secret" ;
55+
56+ Configuration configuration
57+ = TestUtils .createTestConfiguration (new TestConfigurationSource ().put ("MSI_ENDPOINT" , endpoint ) // This must stay to signal we are in an app service context
58+ .put ("MSI_SECRET" , secret )
59+ .put ("IDENTITY_ENDPOINT" , endpoint )
60+ .put ("IDENTITY_HEADER" , secret ));
61+
62+ IdentityClientOptions options
63+ = new IdentityClientOptions ().setChained (isChained ).setHttpClient (client ).setConfiguration (configuration );
64+ ManagedIdentityCredential cred = new ManagedIdentityCredential ("clientId" , null , null , options );
65+ StepVerifier .create (cred .getToken (new TokenRequestContext ().addScopes ("https://management.azure.com" )))
66+ .expectErrorMatches (t -> {
67+ if (isChained ) {
68+ return t instanceof CredentialUnavailableException ;
69+ } else {
70+ return t instanceof ClientAuthenticationException ;
71+ }
72+ })
73+ .verify ();
74+
3575 }
3676
3777 @ Test
@@ -117,7 +157,7 @@ public void testCloudshellUserAssigned() {
117157 ManagedIdentityCredential credential
118158 = new ManagedIdentityCredentialBuilder ().configuration (configuration ).objectId (OBJECT_ID ).build ();
119159 StepVerifier .create (credential .getToken (request ))
120- .expectErrorMatches (t -> t instanceof CredentialUnavailableException )
160+ .expectErrorMatches (t -> t instanceof ClientAuthenticationException )
121161 .verify ();
122162 }
123163
@@ -129,82 +169,22 @@ public void testInvalidIdCombination() {
129169 String objectId = "2323-sd2323s-32323-32334-34343" ;
130170
131171 // test
132- Assertions . assertThrows (IllegalStateException .class ,
172+ assertThrows (IllegalStateException .class ,
133173 () -> new ManagedIdentityCredentialBuilder ().clientId (CLIENT_ID ).resourceId (resourceId ).build ());
134174
135- Assertions . assertThrows (IllegalStateException .class ,
175+ assertThrows (IllegalStateException .class ,
136176 () -> new ManagedIdentityCredentialBuilder ().clientId (CLIENT_ID )
137177 .resourceId (resourceId )
138178 .objectId (objectId )
139179 .build ());
140180
141- Assertions . assertThrows (IllegalStateException .class ,
181+ assertThrows (IllegalStateException .class ,
142182 () -> new ManagedIdentityCredentialBuilder ().clientId (CLIENT_ID ).objectId (objectId ).build ());
143183
144- Assertions . assertThrows (IllegalStateException .class ,
184+ assertThrows (IllegalStateException .class ,
145185 () -> new ManagedIdentityCredentialBuilder ().resourceId (resourceId ).objectId (objectId ).build ());
146186 }
147187
148- @ Test
149- public void testArcIdentityCredentialCreated () {
150- Configuration configuration
151- = TestUtils
152- .createTestConfiguration (new TestConfigurationSource ().put ("IDENTITY_ENDPOINT" , "http://localhost" )
153- .put ("IMDS_ENDPOINT" , "http://localhost" ))
154- .put ("USE_AZURE_IDENTITY_CLIENT_LIBRARY_LEGACY_MI" , "true" );
155-
156- ManagedIdentityCredential cred = new ManagedIdentityCredentialBuilder ().configuration (configuration ).build ();
157- assertThat ("Received class " + cred .managedIdentityServiceCredential .getClass ().toString (),
158- cred .managedIdentityServiceCredential , instanceOf (ManagedIdentityMsalCredential .class ));
159- }
160-
161- @ Test
162- public void testServiceFabricMsiCredentialCreated () {
163- Configuration configuration = TestUtils
164- .createTestConfiguration (new TestConfigurationSource ().put ("IDENTITY_ENDPOINT" , "http://localhost" )
165- .put ("IDENTITY_SERVER_THUMBPRINT" , "thumbprint" )
166- .put ("IDENTITY_HEADER" , "header" ))
167- .put ("USE_AZURE_IDENTITY_CLIENT_LIBRARY_LEGACY_MI" , "true" );
168-
169- ManagedIdentityCredential cred = new ManagedIdentityCredentialBuilder ().configuration (configuration ).build ();
170- assertThat ("Received class " + cred .managedIdentityServiceCredential .getClass ().toString (),
171- cred .managedIdentityServiceCredential , instanceOf (ManagedIdentityMsalCredential .class ));
172- }
173-
174- @ Test
175- public void testAppServiceMsi2019CredentialCreated () {
176- Configuration configuration = TestUtils .createTestConfiguration (
177- new TestConfigurationSource ().put ("IDENTITY_ENDPOINT" , "http://localhost" ).put ("IDENTITY_HEADER" , "header" ))
178- .put ("USE_AZURE_IDENTITY_CLIENT_LIBRARY_LEGACY_MI" , "true" );
179-
180- ManagedIdentityCredential cred = new ManagedIdentityCredentialBuilder ().configuration (configuration ).build ();
181- assertThat ("Received class " + cred .managedIdentityServiceCredential .getClass ().toString (),
182- cred .managedIdentityServiceCredential , instanceOf (ManagedIdentityMsalCredential .class ));
183- }
184-
185- @ Test
186- public void testAppServiceMsi2017CredentialCreated () {
187- Configuration configuration = TestUtils
188- .createTestConfiguration (
189- new TestConfigurationSource ().put ("MSI_ENDPOINT" , "http://localhost" ).put ("MSI_SECRET" , "secret" ))
190- .put ("USE_AZURE_IDENTITY_CLIENT_LIBRARY_LEGACY_MI" , "true" );
191-
192- ManagedIdentityCredential cred = new ManagedIdentityCredentialBuilder ().configuration (configuration ).build ();
193- assertThat ("Received class " + cred .managedIdentityServiceCredential .getClass ().toString (),
194- cred .managedIdentityServiceCredential , instanceOf (ManagedIdentityMsalCredential .class ));
195- }
196-
197- @ Test
198- public void testCloudShellCredentialCreated () {
199- Configuration configuration
200- = TestUtils .createTestConfiguration (new TestConfigurationSource ().put ("MSI_ENDPOINT" , "http://localhost" ))
201- .put ("USE_AZURE_IDENTITY_CLIENT_LIBRARY_LEGACY_MI" , "true" );
202-
203- ManagedIdentityCredential cred = new ManagedIdentityCredentialBuilder ().configuration (configuration ).build ();
204- assertThat ("Received class " + cred .managedIdentityServiceCredential .getClass ().toString (),
205- cred .managedIdentityServiceCredential , instanceOf (ManagedIdentityMsalCredential .class ));
206- }
207-
208188 @ Test
209189 public void testAksExchangeTokenCredentialCreated () {
210190 Configuration configuration
@@ -217,13 +197,7 @@ public void testAksExchangeTokenCredentialCreated() {
217197 cred .managedIdentityServiceCredential , instanceOf (AksExchangeTokenCredential .class ));
218198 }
219199
220- @ Test
221- public void testIMDSPodIdentityV1Credential () {
222- Configuration configuration = TestUtils .createTestConfiguration (new TestConfigurationSource ());
223- configuration .put ("USE_AZURE_IDENTITY_CLIENT_LIBRARY_LEGACY_MI" , "true" );
224-
225- ManagedIdentityCredential cred = new ManagedIdentityCredentialBuilder ().configuration (configuration ).build ();
226- assertThat ("Received class " + cred .managedIdentityServiceCredential .getClass ().toString (),
227- cred .managedIdentityServiceCredential , instanceOf (ManagedIdentityMsalCredential .class ));
200+ private MockHttpResponse getMockResponse (int code , String body ) {
201+ return new MockHttpResponse (null , code , body .getBytes (StandardCharsets .UTF_8 ));
228202 }
229203}
0 commit comments