15
15
import java .net .URL ;
16
16
import java .security .InvalidKeyException ;
17
17
import java .security .NoSuchAlgorithmException ;
18
- import java .text .DateFormat ;
19
18
import java .text .MessageFormat ;
20
- import java .text .SimpleDateFormat ;
21
19
import java .util .ArrayList ;
22
20
import java .util .Calendar ;
23
21
import java .util .Date ;
@@ -270,7 +268,7 @@ static SqlFedAuthToken getMSIAuthToken(String resource, String msiClientId) thro
270
268
&& !msiSecret .isEmpty ();
271
269
272
270
if (isAzureFunction ) {
273
- urlString .append (msiEndpoint ).append ("?api-version=2017-09 -01&resource=" ).append (resource );
271
+ urlString .append (msiEndpoint ).append ("?api-version=2019-08 -01&resource=" ).append (resource );
274
272
} else {
275
273
urlString .append (ActiveDirectoryAuthentication .AZURE_REST_MSI_URL ).append ("&resource=" ).append (resource );
276
274
// Retry acquiring access token upto 20 times due to possible IMDS upgrade (Applies to VM only)
@@ -283,11 +281,7 @@ static SqlFedAuthToken getMSIAuthToken(String resource, String msiClientId) thro
283
281
284
282
// Append Client Id if available
285
283
if (null != msiClientId && !msiClientId .isEmpty ()) {
286
- if (isAzureFunction ) {
287
- urlString .append ("&clientid=" ).append (msiClientId );
288
- } else {
289
- urlString .append ("&client_id=" ).append (msiClientId );
290
- }
284
+ urlString .append ("&client_id=" ).append (msiClientId );
291
285
}
292
286
293
287
// Loop while maxRetry reaches its limit
@@ -299,7 +293,7 @@ static SqlFedAuthToken getMSIAuthToken(String resource, String msiClientId) thro
299
293
connection .setRequestMethod ("GET" );
300
294
301
295
if (isAzureFunction ) {
302
- connection .setRequestProperty ("Secret " , msiSecret );
296
+ connection .setRequestProperty ("X-IDENTITY-HEADER " , msiSecret );
303
297
if (connectionlogger .isLoggable (Level .FINER )) {
304
298
connectionlogger .finer ("Using Azure Function/App Service MSI auth: " + urlString );
305
299
}
@@ -315,7 +309,7 @@ static SqlFedAuthToken getMSIAuthToken(String resource, String msiClientId) thro
315
309
try (InputStream stream = connection .getInputStream ()) {
316
310
317
311
BufferedReader reader = new BufferedReader (new InputStreamReader (stream , UTF_8 ), 100 );
318
- String result = reader .readLine ();
312
+ StringBuilder result = new StringBuilder ( reader .readLine () );
319
313
320
314
int startIndex_AT = result .indexOf (ActiveDirectoryAuthentication .ACCESS_TOKEN_IDENTIFIER )
321
315
+ ActiveDirectoryAuthentication .ACCESS_TOKEN_IDENTIFIER .length ();
@@ -324,30 +318,23 @@ static SqlFedAuthToken getMSIAuthToken(String resource, String msiClientId) thro
324
318
325
319
Calendar cal = new Calendar .Builder ().setInstant (new Date ()).build ();
326
320
321
+ int startIndex_ATX ;
322
+
323
+ // Fetch expires_on
327
324
if (isAzureFunction ) {
328
- // Fetch expires_on
329
- int startIndex_ATX = result
325
+ startIndex_ATX = result
330
326
.indexOf (ActiveDirectoryAuthentication .ACCESS_TOKEN_EXPIRES_ON_IDENTIFIER )
331
327
+ ActiveDirectoryAuthentication .ACCESS_TOKEN_EXPIRES_ON_IDENTIFIER .length ();
332
- String accessTokenExpiry = result .substring (startIndex_ATX ,
333
- result .indexOf ("\" " , startIndex_ATX + 1 ));
334
- if (connectionlogger .isLoggable (Level .FINER )) {
335
- connectionlogger .finer ("MSI auth token expires on: " + accessTokenExpiry );
336
- }
337
-
338
- DateFormat df = new SimpleDateFormat (
339
- ActiveDirectoryAuthentication .ACCESS_TOKEN_EXPIRES_ON_DATE_FORMAT );
340
- cal = new Calendar .Builder ().setInstant (df .parse (accessTokenExpiry )).build ();
341
328
} else {
342
- // Fetch expires_in
343
- int startIndex_ATX = result
329
+ startIndex_ATX = result
344
330
.indexOf (ActiveDirectoryAuthentication .ACCESS_TOKEN_EXPIRES_IN_IDENTIFIER )
345
331
+ ActiveDirectoryAuthentication .ACCESS_TOKEN_EXPIRES_IN_IDENTIFIER .length ();
346
- String accessTokenExpiry = result .substring (startIndex_ATX ,
347
- result .indexOf ("\" " , startIndex_ATX + 1 ));
348
- cal .add (Calendar .SECOND , Integer .parseInt (accessTokenExpiry ));
349
332
}
350
333
334
+ String accessTokenExpiry = result .substring (startIndex_ATX ,
335
+ result .indexOf ("\" " , startIndex_ATX + 1 ));
336
+ cal .add (Calendar .SECOND , Integer .parseInt (accessTokenExpiry ));
337
+
351
338
return new SqlFedAuthToken (accessToken , cal .getTime ());
352
339
}
353
340
} catch (Exception e ) {
0 commit comments