onprem 机器到 Azure Active Directory,这样我们就可以访问 ActiveDirectoryMSI 身份验证以及 IMDS 服务器
onprem machines to Azure Active Directory so we can access ActiveDirectoryMSI authentication as well as IMDS SERVER
我们希望将 AzureSqlServer 与 ActiveDirectoryMSI 身份验证以及基于令牌的身份验证结合使用,并且
我们能够从在 Azure 网络中创建并添加为 Azure AD 组成员的 VM 成功执行。
为此,我们按照 link
创建了 Contained 用户
并将虚拟机添加为 AzureActiveDirectory 的一部分
按照此 link
com.microsoft.sqlserver.jdbc.SQLServerException: MSI Token failure: Failed to acquire token from MSI Endpoint
并且我们能够使用 IMDS 服务器访问 SQL 数据而无需提供用户名和密码,并且能够使用
http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fdatabase.windows.net%2F
和
ActiveDirectorMSI
URIString jdbc:sqlserver://azuresqlserverNAME:1433;databaseName=DatabaseNAME;Authentication=ActiveDirectoryMsi;
.
但是当涉及到从 Onprem Windows/Linux/Mac 机器访问时,我们无法访问 Azure SQL 服务器
有人可以就内部部署向我建议需要做什么,以便我们可以使用 ActiveDirectoryMSI 身份验证以及基于令牌的身份验证访问 AzureSqlServer 吗?
根据我的测试,如果要连接AzureSQL本地机,请参考以下步骤
- 创建服务主体
az ad sp create-for-rbac -n 'name' --skip-assignment
Add the service principal as Azure SQL database contained user.
设置环境变量。请将以下变量设置为环境变量
AZURE_TENANT_ID: ID of the service principal's tenant. Also called its 'directory' ID.
AZURE_CLIENT_ID: the service principal's client ID
AZURE_CLIENT_SECRET: one of the service principal's client secrets
- SDK
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.0.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.2.2.jre8</version>
</dependency>
- 代码
public static void main( String[] args )
{
AccessToken token= GetAccessToken();
SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName("<>.database.windows.net"); // Replace with your server name.
ds.setDatabaseName("demo"); // Replace with your database name.
ds.setAccessToken(token.getToken());
try (Connection connection = ds.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT SUSER_SNAME()")) {
if (rs.next()) {
System.out.println("You have successfully logged on as: " + rs.getString(1));
}
}
}
public static AccessToken GetAccessToken() {
DefaultAzureCredential creds = new DefaultAzureCredentialBuilder()
.build();
TokenRequestContext request = new TokenRequestContext();
System.out.println("444");
request.addScopes("https://database.windows.net//.default");
String token;
AccessToken accesstoken=creds.getToken(request).block();
return accesstoken;
}
我们希望将 AzureSqlServer 与 ActiveDirectoryMSI 身份验证以及基于令牌的身份验证结合使用,并且 我们能够从在 Azure 网络中创建并添加为 Azure AD 组成员的 VM 成功执行。 为此,我们按照 link
创建了 Contained 用户并将虚拟机添加为 AzureActiveDirectory 的一部分 按照此 link
com.microsoft.sqlserver.jdbc.SQLServerException: MSI Token failure: Failed to acquire token from MSI Endpoint
并且我们能够使用 IMDS 服务器访问 SQL 数据而无需提供用户名和密码,并且能够使用 http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fdatabase.windows.net%2F
和
ActiveDirectorMSI URIString jdbc:sqlserver://azuresqlserverNAME:1433;databaseName=DatabaseNAME;Authentication=ActiveDirectoryMsi; .
但是当涉及到从 Onprem Windows/Linux/Mac 机器访问时,我们无法访问 Azure SQL 服务器
有人可以就内部部署向我建议需要做什么,以便我们可以使用 ActiveDirectoryMSI 身份验证以及基于令牌的身份验证访问 AzureSqlServer 吗?
根据我的测试,如果要连接AzureSQL本地机,请参考以下步骤
- 创建服务主体
az ad sp create-for-rbac -n 'name' --skip-assignment
Add the service principal as Azure SQL database contained user.
设置环境变量。请将以下变量设置为环境变量
AZURE_TENANT_ID: ID of the service principal's tenant. Also called its 'directory' ID.
AZURE_CLIENT_ID: the service principal's client ID
AZURE_CLIENT_SECRET: one of the service principal's client secrets
- SDK
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.0.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.2.2.jre8</version>
</dependency>
- 代码
public static void main( String[] args )
{
AccessToken token= GetAccessToken();
SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName("<>.database.windows.net"); // Replace with your server name.
ds.setDatabaseName("demo"); // Replace with your database name.
ds.setAccessToken(token.getToken());
try (Connection connection = ds.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT SUSER_SNAME()")) {
if (rs.next()) {
System.out.println("You have successfully logged on as: " + rs.getString(1));
}
}
}
public static AccessToken GetAccessToken() {
DefaultAzureCredential creds = new DefaultAzureCredentialBuilder()
.build();
TokenRequestContext request = new TokenRequestContext();
System.out.println("444");
request.addScopes("https://database.windows.net//.default");
String token;
AccessToken accesstoken=creds.getToken(request).block();
return accesstoken;
}