MSAL Java Web API 用于 DNS 和记录

huangapple go评论112阅读模式
英文:

MSAL Java web API for DNS and records

问题

  1. 如何在使用最新的MSAL库而不是基于ADAL的情况下通过Azure Web服务APIAzure服务器上创建区域DNS和记录然而DNS库支持https://github.com/Azure-Samples/dns-java-host-and-manage-your-domains,但未提到任何使用MSAL访问令牌的方式。例如,
  2. ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(client, tenant, key, AzureEnvironment.AZURE);
  3. azure = Azure.authenticate(credentials).withSubscription(subscriptionId);
  4. ResourceGroup resourceGroup = azure.resourceGroups().define(rgName)
  5. .withRegion(Region.US_EAST2)
  6. .create();
  7. System.out.println("Creating root DNS zone " + customDomainName + "...");
  8. DnsZone rootDnsZone = azure.dnsZones().define(customDomainName)
  9. .withExistingResourceGroup(resourceGroup)
  10. .create();

但它是使用密钥而不是由MSAL提供的访问令牌。这在旧的方式中已经可以通过Azure在内部使用ADAL来实现。

  1. <details>
  2. <summary>英文:</summary>
  3. How can we create zone DNS and records on Azure server using Azure web services API with latest &quot;MSAL&quot; library not ADAL based? However DNS library support https://github.com/Azure-Samples/dns-java-host-and-manage-your-domains does not mentioned any way to utilized using MSAL access token. For example
  4. ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(client, tenant, key, AzureEnvironment.AZURE);
  5. azure = Azure.authenticate(credentials).withSubscription(subscriptionId);
  6. ResourceGroup resourceGroup = azure.resourceGroups().define(rgName)
  7. .withRegion(Region.US_EAST2)
  8. .create();
  9. System.out.println(&quot;Creating root DNS zone &quot; + customDomainName + &quot;...&quot;);
  10. DnsZone rootDnsZone = azure.dnsZones().define(customDomainName)
  11. .withExistingResourceGroup(resourceGroup)
  12. .create();
  13. But it is using with keys instead of access tokens provided by MSAL. This can be already achieved in old ways which is using ADAL internally by Azure.
  14. </details>
  15. # 答案1
  16. **得分**: 1

如果您想使用 Azure Java 管理 SDK 使用 AD 访问令牌管理 Azure DNS,请参考以下代码:

a. 创建服务主体(我使用 Azure CLI 完成此步骤)

  1. az login
  2. az account set --subscription "<your subscription id>"
  3. # 服务主体将具有 Azure Contributor 角色
  4. az ad sp create-for-rbac -n "readMetric"

MSAL Java Web API 用于 DNS 和记录

  1. 代码
  1. public void test() throws MalformedURLException, ExecutionException, InterruptedException {
  2. AzureTokenCredentials tokenCredentials = new AzureTokenCredentials(AzureEnvironment.AZURE, ADProperty.tenantId) {
  3. @Override
  4. public String getToken(String resource) throws IOException {
  5. String token = null;
  6. // 使用 msal 获取 Azure AD 访问令牌
  7. ConfidentialClientApplication app = ConfidentialClientApplication.builder(
  8. ADProperty.clientId, // 服务主体应用程序 ID
  9. ClientCredentialFactory.createFromSecret(ADProperty.clientKey)) // 服务主体密码
  10. .authority(ADProperty.authority) // "https://login.microsoftonline.com/" + 服务主体租户 ID
  11. .build();
  12. ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(
  13. Collections.singleton("https://management.azure.com/.default"))
  14. .build();
  15. CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
  16. try {
  17. token = future.get().accessToken();
  18. } catch (InterruptedException e) {
  19. e.printStackTrace();
  20. } catch (ExecutionException e) {
  21. e.printStackTrace();
  22. }
  23. return token;
  24. }
  25. };
  26. Azure azure = Azure.authenticate(tokenCredentials)
  27. .withSubscription(ADProperty.subscriptionId); // 服务主体订阅 ID
  28. DnsZone rootDnsZone = azure.dnsZones().define("mydevchat.com")
  29. .withExistingResourceGroup("jimtest")
  30. .create();
  31. System.out.println("成功创建 DNSZone " + rootDnsZone.name());
  32. }

MSAL Java Web API 用于 DNS 和记录

  1. <details>
  2. <summary>英文:</summary>
  3. If you want to use Azure java management SDK to manage Azure DNS with AD access token, please refer to the following code
  4. a. create a service principal (I use Azure CLI to do that)

az login
az account set --subscription "<your subscription id>"

the sp will have Azure Contributor role

az ad sp create-for-rbac -n "readMetric"

  1. [![enter image description here][1]][1]
  2. 2. Code

public void test() throws MalformedURLException, ExecutionException, InterruptedException {

  1. AzureTokenCredentials tokenCredentials = new AzureTokenCredentials(AzureEnvironment.AZURE,ADProperty.tenantId) {
  2. @Override
  3. public String getToken(String resource) throws IOException {
  4. String token =null;
  5. // use msal to get Azure AD access token
  6. ConfidentialClientApplication app = ConfidentialClientApplication.builder(
  7. ADProperty.clientId, // sp appid
  8. ClientCredentialFactory.createFromSecret(ADProperty.clientKey)) // sp password
  9. .authority(ADProperty.authority) // &quot;https://login.microsoftonline.com/&quot; + sp tenant id
  10. .build();
  11. ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(
  12. Collections.singleton(&quot;https://management.azure.com/.default&quot;))
  13. .build();
  14. CompletableFuture&lt;IAuthenticationResult&gt; future = app.acquireToken(clientCredentialParam);
  15. try {
  16. token =future.get().accessToken();
  17. } catch (InterruptedException e) {
  18. e.printStackTrace();
  19. } catch (ExecutionException e) {
  20. e.printStackTrace();
  21. }
  22. return token;
  23. }
  24. };
  25. Azure azure = Azure.authenticate(tokenCredentials)
  26. .withSubscription(ADProperty.subscriptionId); // sp subscription id
  27. DnsZone rootDnsZone = azure.dnsZones().define(&quot;mydevchat.com&quot;)
  28. .withExistingResourceGroup(&quot;jimtest&quot;)
  29. .create();
  30. System.out.println(&quot;create DNSZone &quot; + rootDnsZone.name() + &quot; successfully&quot;);

}

  1. [![enter image description here][2]][2]
  2. [1]: https://i.stack.imgur.com/1kOsX.png
  3. [2]: https://i.stack.imgur.com/EhtdV.png
  4. </details>

huangapple
  • 本文由 发表于 2020年4月9日 17:44:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/61118265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定