java - Azure 使用 Java SDK 将角色分配给 VM
问题描述
我正在编写一个 Java 程序,它创建一个 VM 并从存储中访问文件。但是,我无法为该 VM 分配角色“存储贡献者/所有者”,以便它可以。
我目前有这段代码,但我不确定它是否是我需要的,而且我不知道在某些地方写什么:
rbacManager = GraphRbacManager.authenticate( credentials );
rbacManager.roleAssignments()
.define("roletest")
// which object? and where to find the ID?
.forObjectId("/subscription/" + subscription + "?")
.withBuiltInRole(com.microsoft.azure.management.graphrbac.BuiltInRole.STORAGE_ACCOUNT_CONTRIBUTOR)
// what should go as resource scope?
.withResourceScope(?)
.createAsync();
先感谢您!
解决方案
关于这个问题,请参考以下步骤
- 创建服务主体并将
Owner
角色分配给 sp
az login
az ad sp create-for-rbac -n "MyApp" --role "Owner"\
--scopes /subscriptions/{SubID} \
--sdk-auth
- 项目
一种。sdk
<dependency>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.2.0</version>
</dependency>
湾。代码
AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
String clientId="<sp appid>";
String clientSecret="<sp password>";
String tenant="";
String subscriptionId=""
TokenCredential credential = new ClientSecretCredentialBuilder()
.clientId(clientId)
.clientSecret(clientSecret)
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
.tenantId(tenant)
.build();
AzureResourceManager azureResourceManager = AzureResourceManager
.configure()
.withLogLevel(HttpLogDetailLevel.BASIC)
.authenticate(credential, profile)
.withSubscription(subscriptionId);
// get storage account
String accountGroup="";
String accountName="";
StorageAccount account = azureResourceManager.storageAccounts().getByResourceGroup(accountGroup,accountName);
// get vm
String vmGroup="";
String vmName="test";
VirtualMachine virtualMachine = azureResourceManager.virtualMachines().getByResourceGroup(vmGroup,vmName);
virtualMachine.update()
.withSystemAssignedManagedServiceIdentity()
.withSystemAssignedIdentityBasedAccessTo(account.id(), BuiltInRole.fromString("Storage Blob Data Owner"))
.apply();
}
推荐阅读
- azure-devops - 即使由于服务器上的空间问题而导致 Octopus 构建失败,AzureDevops 构建步骤也会通过
- java - 在没有 OpenID Connect 服务器的情况下使用 Jhipster 的社交登录
- json - 如何根据使用角度 7 中的键的条件检查来过滤 json 响应中的数组
- javascript - React 尝试编辑输入但出现错误
- c# - 如何为 Hangfire RecurringJob 进行集成测试?
- apache-kafka - 是否应该在 Kafka 集群中的所有代理节点上复制主题分区?
- android - 如何在 Flutter 中下载视频 (mp4) 文件?
- java - 关于docker上的多个springboot服务实例可访问性
- python - 更有效地获得两个的最小向量
- .net-core - Asp.Net Core 自定义转换器 appsettings JSON for IDictionary