Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added lib/azure-authentication-msi-token-provider.jar
Binary file not shown.
Binary file added lib/mssql-jdbc-7.1.3-SNAPSHOT.jar
Binary file not shown.
29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,39 @@
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${stagingDirectory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>runtime</includeScope>
<excludeArtifactIds>azure-functions-java-library</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.microsoft.azure.msi_auth_token_provider</groupId>
<artifactId>azure-authentication-msi-token-provider</artifactId>
<version>1.0.0-beta</version>
<scope>system</scope>
<systemPath>${basedir}/lib/azure-authentication-msi-token-provider.jar</systemPath>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
Expand Down
117 changes: 65 additions & 52 deletions src/main/java/com/test/aadmsi/msiexample/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,73 @@
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.microsoft.azure.msiAuthTokenProvider.*;

/**
* Sample Application that runs without driver support to test VM configuration.
*/
public class App extends CredentialManager {

public static void main(String[] args) throws Exception {
System.out.println("\nTesting System Assigned Managed Identity for Database");
App.getToken(AZURE_SQL_SPN, null);

System.out.println("\nTesting System Assigned Managed Identity for Azure Management");
App.getToken(AZURE_RESOURCE_SPN, null);

System.out.println("\nTesting User Assigned Managed Identity");
App.getToken(AZURE_SQL_SPN, userMSIClientId);

}

static String getToken(String resource, String clientId) throws Exception {
String urlString = "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource="
+ resource;

if (null != clientId) {
urlString += "&client_id=" + clientId;
}

HttpURLConnection connection = null;

try {
connection = (HttpURLConnection) new URL(urlString).openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Metadata", "true");
connection.connect();

try (InputStream stream = connection.getInputStream()) {

BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"), 100);
String result = reader.readLine();
String accessTokenIdentifier = "\"access_token\":\"";

int startIndex = result.indexOf(accessTokenIdentifier) + accessTokenIdentifier.length();
String accessToken = result.substring(startIndex, result.indexOf("\"", startIndex));

System.out.println("Access Token: " + accessToken);

return accessToken;
}
} catch (Exception e) {
throw e;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
public class App {

public static void main(String[] args) throws Exception {
System.out.println("\nTesting System Assigned Managed Identity for Database");
App.getTokenUsingJar("https://database.windows.net", null);

System.out.println("\nTesting System Assigned Managed Identity for Azure Management");
App.getTokenUsingJar("https://management.azure.com", null);

System.out.println("\nTesting User Assigned Managed Identity");
App.getTokenUsingJar("https://database.windows.net/", ""); // Pass Object ID of User Assigned MSI
}

static String getTokenUsingJar(String resourceId, String objectId) throws Exception
{
MSICredentials credsProvider = MSICredentials.getMSICredentials();
if (objectId != null && !objectId.isEmpty()) {
credsProvider.updateObjectId(objectId);
}

String token = credsProvider.getToken(resourceId).accessToken();

System.out.println("Access Token Using Jar file for resource : " + resourceId + ", Token : "+ token);

return token;
}

static String getToken(String resource, String objectId) throws Exception {
String urlString = "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource="
+ resource;

if (null != objectId) {
urlString += "&object_id=" + objectId;
}

HttpURLConnection connection = null;

try {
connection = (HttpURLConnection) new URL(urlString).openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Metadata", "true");
connection.connect();

try (InputStream stream = connection.getInputStream()) {

BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"), 100);
String result = reader.readLine();
String accessTokenIdentifier = "\"access_token\":\"";

int startIndex = result.indexOf(accessTokenIdentifier) + accessTokenIdentifier.length();
String accessToken = result.substring(startIndex, result.indexOf("\"", startIndex));

System.out.println("Access Token: " + accessToken);

return accessToken;
}
} catch (Exception e) {
throw e;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/test/aadmsi/msiexample/TestMSIAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.sql.Statement;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.sqlserver.jdbc.SQLServerException;


/**
Expand Down Expand Up @@ -41,5 +42,45 @@ public static void main(String[] args) throws SQLException, InterruptedException
System.out.println(rs.getString(1));
}
}

try (Connection con = ds.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT Distinct TABLE_NAME FROM information_schema.TABLES")) {
System.out.println("Connected with SYSTEM MSI Authentication, Tables : ");
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception ex) {
System.out.println("\n\nCaught Exception : " + ex);
}

ds = new SQLServerDataSource();
ds.setServerName(serverName);
ds.setDatabaseName(databaseName);
ds.setAuthentication(ActiveDirectoryMSI);
ds.setHostNameInCertificate(hostNameInCertificate);
ds.setMSIObjectId(userMSIObjectId);

try (Connection con = ds.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT @@VERSION")) {
System.out.println("\n\nConnected with User MSI Authentication");
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception ex) {
System.out.println("\n\nCaught Exception : " + ex);
}

try (Connection con = ds.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT Distinct TABLE_NAME FROM information_schema.TABLES")) {
System.out.println("\n\nConnected with User MSI Authentication, Tables : ");
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception ex) {
System.out.println("\n\nCaught Exception : " + ex);
}
}
}