Skip to content

Commit 3b4f0a2

Browse files
authored
Fix | MSI expiry date format fix for Azure Functions (#1308)
Fix | MSI expiry date format fix for Azure Functions
1 parent 639d22c commit 3b4f0a2

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSecurityUtility.java

+13-26
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
import java.net.URL;
1616
import java.security.InvalidKeyException;
1717
import java.security.NoSuchAlgorithmException;
18-
import java.text.DateFormat;
1918
import java.text.MessageFormat;
20-
import java.text.SimpleDateFormat;
2119
import java.util.ArrayList;
2220
import java.util.Calendar;
2321
import java.util.Date;
@@ -270,7 +268,7 @@ static SqlFedAuthToken getMSIAuthToken(String resource, String msiClientId) thro
270268
&& !msiSecret.isEmpty();
271269

272270
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);
274272
} else {
275273
urlString.append(ActiveDirectoryAuthentication.AZURE_REST_MSI_URL).append("&resource=").append(resource);
276274
// 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
283281

284282
// Append Client Id if available
285283
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);
291285
}
292286

293287
// Loop while maxRetry reaches its limit
@@ -299,7 +293,7 @@ static SqlFedAuthToken getMSIAuthToken(String resource, String msiClientId) thro
299293
connection.setRequestMethod("GET");
300294

301295
if (isAzureFunction) {
302-
connection.setRequestProperty("Secret", msiSecret);
296+
connection.setRequestProperty("X-IDENTITY-HEADER", msiSecret);
303297
if (connectionlogger.isLoggable(Level.FINER)) {
304298
connectionlogger.finer("Using Azure Function/App Service MSI auth: " + urlString);
305299
}
@@ -315,7 +309,7 @@ static SqlFedAuthToken getMSIAuthToken(String resource, String msiClientId) thro
315309
try (InputStream stream = connection.getInputStream()) {
316310

317311
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, UTF_8), 100);
318-
String result = reader.readLine();
312+
StringBuilder result = new StringBuilder(reader.readLine());
319313

320314
int startIndex_AT = result.indexOf(ActiveDirectoryAuthentication.ACCESS_TOKEN_IDENTIFIER)
321315
+ ActiveDirectoryAuthentication.ACCESS_TOKEN_IDENTIFIER.length();
@@ -324,30 +318,23 @@ static SqlFedAuthToken getMSIAuthToken(String resource, String msiClientId) thro
324318

325319
Calendar cal = new Calendar.Builder().setInstant(new Date()).build();
326320

321+
int startIndex_ATX;
322+
323+
// Fetch expires_on
327324
if (isAzureFunction) {
328-
// Fetch expires_on
329-
int startIndex_ATX = result
325+
startIndex_ATX = result
330326
.indexOf(ActiveDirectoryAuthentication.ACCESS_TOKEN_EXPIRES_ON_IDENTIFIER)
331327
+ 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();
341328
} else {
342-
// Fetch expires_in
343-
int startIndex_ATX = result
329+
startIndex_ATX = result
344330
.indexOf(ActiveDirectoryAuthentication.ACCESS_TOKEN_EXPIRES_IN_IDENTIFIER)
345331
+ 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));
349332
}
350333

334+
String accessTokenExpiry = result.substring(startIndex_ATX,
335+
result.indexOf("\"", startIndex_ATX + 1));
336+
cal.add(Calendar.SECOND, Integer.parseInt(accessTokenExpiry));
337+
351338
return new SqlFedAuthToken(accessToken, cal.getTime());
352339
}
353340
} catch (Exception e) {

0 commit comments

Comments
 (0)