PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/sdk/compute/mgmt/src/test/java/com/azure/management/compute/VirtualMachineManagedServiceIdentityOperationsTests.java

http://github.com/WindowsAzure/azure-sdk-for-java
Java | 369 lines | 292 code | 39 blank | 38 comment | 35 complexity | 0866d076a8100ad296e2cb2e511f8d48 MD5 | raw file
Possible License(s): MIT
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. package com.azure.management.compute;
  4. import com.azure.core.http.HttpPipeline;
  5. import com.azure.core.http.rest.PagedIterable;
  6. import com.azure.management.graphrbac.BuiltInRole;
  7. import com.azure.management.graphrbac.RoleAssignment;
  8. import com.azure.management.resources.ResourceGroup;
  9. import com.azure.management.resources.fluentcore.arm.Region;
  10. import com.azure.management.resources.fluentcore.dag.TaskGroup;
  11. import com.azure.management.resources.fluentcore.model.Indexable;
  12. import com.azure.management.resources.fluentcore.profile.AzureProfile;
  13. import com.azure.management.storage.StorageAccount;
  14. import org.junit.jupiter.api.Assertions;
  15. import org.junit.jupiter.api.Test;
  16. import reactor.core.publisher.Flux;
  17. public class VirtualMachineManagedServiceIdentityOperationsTests extends ComputeManagementTest {
  18. private String rgName = "";
  19. private final Region region = Region.US_SOUTH_CENTRAL;
  20. private final String vmName = "javavm";
  21. @Override
  22. protected void initializeClients(HttpPipeline httpPipeline, AzureProfile profile) {
  23. rgName = generateRandomResourceName("javacsmrg", 15);
  24. super.initializeClients(httpPipeline, profile);
  25. }
  26. @Override
  27. protected void cleanUpResources() {
  28. resourceManager.resourceGroups().beginDeleteByName(rgName);
  29. }
  30. @Test
  31. public void canSetMSIOnNewOrExistingVMWithoutRoleAssignment() throws Exception {
  32. // Create a virtual machine with just MSI enabled without role and scope.
  33. //
  34. VirtualMachine virtualMachine =
  35. computeManager
  36. .virtualMachines()
  37. .define(vmName)
  38. .withRegion(region)
  39. .withNewResourceGroup(rgName)
  40. .withNewPrimaryNetwork("10.0.0.0/28")
  41. .withPrimaryPrivateIPAddressDynamic()
  42. .withoutPrimaryPublicIPAddress()
  43. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  44. .withRootUsername("Foo12")
  45. .withRootPassword("abc!@#F0orL")
  46. .withSize(VirtualMachineSizeTypes.STANDARD_DS2_V2)
  47. .withOSDiskCaching(CachingTypes.READ_WRITE)
  48. .withSystemAssignedManagedServiceIdentity()
  49. .create();
  50. Assertions.assertNotNull(virtualMachine);
  51. Assertions.assertNotNull(virtualMachine.inner());
  52. Assertions.assertTrue(virtualMachine.isManagedServiceIdentityEnabled());
  53. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId());
  54. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityTenantId());
  55. // Ensure NO role assigned for resource group
  56. //
  57. ResourceGroup resourceGroup =
  58. this.resourceManager.resourceGroups().getByName(virtualMachine.resourceGroupName());
  59. PagedIterable<RoleAssignment> rgRoleAssignments1 =
  60. rbacManager.roleAssignments().listByScope(resourceGroup.id());
  61. Assertions.assertNotNull(rgRoleAssignments1);
  62. boolean found = false;
  63. for (RoleAssignment roleAssignment : rgRoleAssignments1) {
  64. if (roleAssignment.principalId() != null
  65. && roleAssignment
  66. .principalId()
  67. .equalsIgnoreCase(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId())) {
  68. found = true;
  69. break;
  70. }
  71. }
  72. Assertions
  73. .assertFalse(found, "Resource group should not have a role assignment with virtual machine MSI principal");
  74. virtualMachine = virtualMachine.update().withSystemAssignedManagedServiceIdentity().apply();
  75. Assertions.assertNotNull(virtualMachine);
  76. Assertions.assertNotNull(virtualMachine.inner());
  77. Assertions.assertTrue(virtualMachine.isManagedServiceIdentityEnabled());
  78. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId());
  79. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityTenantId());
  80. // Ensure NO role assigned for resource group
  81. //
  82. rgRoleAssignments1 = rbacManager.roleAssignments().listByScope(resourceGroup.id());
  83. Assertions.assertNotNull(rgRoleAssignments1);
  84. found = false;
  85. for (RoleAssignment roleAssignment : rgRoleAssignments1) {
  86. if (roleAssignment.principalId() != null
  87. && roleAssignment
  88. .principalId()
  89. .equalsIgnoreCase(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId())) {
  90. found = true;
  91. break;
  92. }
  93. }
  94. Assertions
  95. .assertFalse(found, "Resource group should not have a role assignment with virtual machine MSI principal");
  96. }
  97. @Test
  98. public void canSetMSIOnNewVMWithRoleAssignedToCurrentResourceGroup() throws Exception {
  99. Flux<Indexable> resources =
  100. computeManager
  101. .virtualMachines()
  102. .define(vmName)
  103. .withRegion(region)
  104. .withNewResourceGroup(rgName)
  105. .withNewPrimaryNetwork("10.0.0.0/28")
  106. .withPrimaryPrivateIPAddressDynamic()
  107. .withoutPrimaryPublicIPAddress()
  108. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  109. .withRootUsername("Foo12")
  110. .withRootPassword("abc!@#F0orL")
  111. .withSize(VirtualMachineSizeTypes.STANDARD_DS2_V2)
  112. .withOSDiskCaching(CachingTypes.READ_WRITE)
  113. .withSystemAssignedManagedServiceIdentity()
  114. .withSystemAssignedIdentityBasedAccessToCurrentResourceGroup(BuiltInRole.CONTRIBUTOR)
  115. .createAsync();
  116. final VirtualMachine[] virtualMachines = new VirtualMachine[1];
  117. final RoleAssignment[] roleAssignments = new RoleAssignment[1];
  118. resources
  119. .collectList()
  120. .block()
  121. .forEach(
  122. indexable -> {
  123. if (indexable instanceof VirtualMachine) {
  124. virtualMachines[0] = (VirtualMachine) indexable;
  125. }
  126. if (indexable instanceof RoleAssignment) {
  127. roleAssignments[0] = (RoleAssignment) indexable;
  128. }
  129. });
  130. Assertions.assertNotNull(virtualMachines[0]);
  131. Assertions.assertNotNull(roleAssignments[0]);
  132. final VirtualMachine virtualMachine = virtualMachines[0];
  133. Assertions.assertNotNull(virtualMachine);
  134. Assertions.assertNotNull(virtualMachine.inner());
  135. Assertions.assertTrue(virtualMachine.isManagedServiceIdentityEnabled());
  136. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId());
  137. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityTenantId());
  138. // Validate service created service principal
  139. // TODO: Renable the below code snippet: https://github.com/Azure/azure-libraries-for-net/issues/739
  140. // ServicePrincipal servicePrincipal = rbacManager
  141. // .servicePrincipals()
  142. // .getById(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId());
  143. //
  144. // Assertions.assertNotNull(servicePrincipal);
  145. // Assertions.assertNotNull(servicePrincipal.inner());
  146. // Ensure role assigned
  147. //
  148. ResourceGroup resourceGroup =
  149. this.resourceManager.resourceGroups().getByName(virtualMachine.resourceGroupName());
  150. PagedIterable<RoleAssignment> rgRoleAssignments = rbacManager.roleAssignments().listByScope(resourceGroup.id());
  151. boolean found = false;
  152. for (RoleAssignment rgRoleAssignment : rgRoleAssignments) {
  153. if (rgRoleAssignment.principalId() != null
  154. && rgRoleAssignment
  155. .principalId()
  156. .equalsIgnoreCase(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId())) {
  157. found = true;
  158. break;
  159. }
  160. }
  161. Assertions.assertTrue(found, "Resource group should have a role assignment with virtual machine MSI principal");
  162. // Below we tests internal functionality to ensure a call for RoleAssignment is not happening.
  163. // NOT a pattern applications/customer should use
  164. //
  165. RoleAssignment savedRoleAssignment = roleAssignments[0];
  166. roleAssignments[0] = null;
  167. TaskGroup.HasTaskGroup hasTaskGroup = (TaskGroup.HasTaskGroup) virtualMachine;
  168. Assertions.assertNotNull(hasTaskGroup);
  169. TaskGroup vmTaskGroup = hasTaskGroup.taskGroup();
  170. vmTaskGroup
  171. .invokeAsync(vmTaskGroup.newInvocationContext())
  172. .collectList()
  173. .block()
  174. .forEach(
  175. indexable -> {
  176. if (indexable instanceof RoleAssignment) {
  177. roleAssignments[0] = (RoleAssignment) indexable;
  178. }
  179. });
  180. Assertions.assertNotNull(roleAssignments[0]);
  181. Assertions.assertTrue((roleAssignments[0]).key().equalsIgnoreCase(savedRoleAssignment.key()));
  182. }
  183. @Test
  184. public void canSetMSIOnNewVMWithMultipleRoleAssignments() throws Exception {
  185. String storageAccountName = generateRandomResourceName("javacsrg", 15);
  186. StorageAccount storageAccount =
  187. storageManager
  188. .storageAccounts()
  189. .define(storageAccountName)
  190. .withRegion(Region.US_EAST2)
  191. .withNewResourceGroup(rgName)
  192. .create();
  193. ResourceGroup resourceGroup =
  194. this.resourceManager.resourceGroups().getByName(storageAccount.resourceGroupName());
  195. VirtualMachine virtualMachine =
  196. computeManager
  197. .virtualMachines()
  198. .define(vmName)
  199. .withRegion(region)
  200. .withExistingResourceGroup(rgName)
  201. .withNewPrimaryNetwork("10.0.0.0/28")
  202. .withPrimaryPrivateIPAddressDynamic()
  203. .withoutPrimaryPublicIPAddress()
  204. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  205. .withRootUsername("Foo12")
  206. .withRootPassword("abc!@#F0orL")
  207. .withSize(VirtualMachineSizeTypes.STANDARD_DS2_V2)
  208. .withOSDiskCaching(CachingTypes.READ_WRITE)
  209. .withSystemAssignedManagedServiceIdentity()
  210. .withSystemAssignedIdentityBasedAccessTo(resourceGroup.id(), BuiltInRole.CONTRIBUTOR)
  211. .withSystemAssignedIdentityBasedAccessTo(storageAccount.id(), BuiltInRole.CONTRIBUTOR)
  212. .create();
  213. // Validate service created service principal
  214. //
  215. // TODO: Renable the below code snippet: https://github.com/Azure/azure-libraries-for-net/issues/739
  216. // ServicePrincipal servicePrincipal = rbacManager
  217. // .servicePrincipals()
  218. // .getById(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId());
  219. //
  220. // Assertions.assertNotNull(servicePrincipal);
  221. // Assertions.assertNotNull(servicePrincipal.inner());
  222. // Ensure role assigned for resource group
  223. //
  224. PagedIterable<RoleAssignment> rgRoleAssignments = rbacManager.roleAssignments().listByScope(resourceGroup.id());
  225. Assertions.assertNotNull(rgRoleAssignments);
  226. boolean found = false;
  227. for (RoleAssignment roleAssignment : rgRoleAssignments) {
  228. if (roleAssignment.principalId() != null
  229. && roleAssignment
  230. .principalId()
  231. .equalsIgnoreCase(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId())) {
  232. found = true;
  233. break;
  234. }
  235. }
  236. Assertions.assertTrue(found, "Resource group should have a role assignment with virtual machine MSI principal");
  237. // Ensure role assigned for storage account
  238. //
  239. PagedIterable<RoleAssignment> stgRoleAssignments =
  240. rbacManager.roleAssignments().listByScope(storageAccount.id());
  241. Assertions.assertNotNull(stgRoleAssignments);
  242. found = false;
  243. for (RoleAssignment roleAssignment : stgRoleAssignments) {
  244. if (roleAssignment.principalId() != null
  245. && roleAssignment
  246. .principalId()
  247. .equalsIgnoreCase(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId())) {
  248. found = true;
  249. break;
  250. }
  251. }
  252. Assertions
  253. .assertTrue(found, "Storage account should have a role assignment with virtual machine MSI principal");
  254. }
  255. @Test
  256. public void canSetMSIOnExistingVMWithRoleAssignments() throws Exception {
  257. VirtualMachine virtualMachine =
  258. computeManager
  259. .virtualMachines()
  260. .define(vmName)
  261. .withRegion(region)
  262. .withNewResourceGroup(rgName)
  263. .withNewPrimaryNetwork("10.0.0.0/28")
  264. .withPrimaryPrivateIPAddressDynamic()
  265. .withoutPrimaryPublicIPAddress()
  266. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  267. .withRootUsername("Foo12")
  268. .withRootPassword("abc!@#F0orL")
  269. .withSize(VirtualMachineSizeTypes.STANDARD_DS2_V2)
  270. .withOSDiskCaching(CachingTypes.READ_WRITE)
  271. .withSystemAssignedManagedServiceIdentity()
  272. .create();
  273. Assertions.assertNotNull(virtualMachine);
  274. Assertions.assertNotNull(virtualMachine.inner());
  275. Assertions.assertTrue(virtualMachine.isManagedServiceIdentityEnabled());
  276. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId());
  277. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityTenantId());
  278. Assertions.assertNotNull(virtualMachine.managedServiceIdentityType());
  279. Assertions.assertTrue(virtualMachine.managedServiceIdentityType().equals(ResourceIdentityType.SYSTEM_ASSIGNED));
  280. // Ensure NO role assigned for resource group
  281. //
  282. ResourceGroup resourceGroup =
  283. this.resourceManager.resourceGroups().getByName(virtualMachine.resourceGroupName());
  284. PagedIterable<RoleAssignment> rgRoleAssignments1 =
  285. rbacManager.roleAssignments().listByScope(resourceGroup.id());
  286. Assertions.assertNotNull(rgRoleAssignments1);
  287. boolean found = false;
  288. for (RoleAssignment roleAssignment : rgRoleAssignments1) {
  289. if (roleAssignment.principalId() != null
  290. && roleAssignment
  291. .principalId()
  292. .equalsIgnoreCase(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId())) {
  293. found = true;
  294. break;
  295. }
  296. }
  297. Assertions
  298. .assertFalse(found, "Resource group should not have a role assignment with virtual machine MSI principal");
  299. virtualMachine
  300. .update()
  301. .withSystemAssignedManagedServiceIdentity()
  302. .withSystemAssignedIdentityBasedAccessToCurrentResourceGroup(BuiltInRole.CONTRIBUTOR)
  303. .apply();
  304. // Ensure role assigned for resource group
  305. //
  306. PagedIterable<RoleAssignment> roleAssignments2 = rbacManager.roleAssignments().listByScope(resourceGroup.id());
  307. Assertions.assertNotNull(roleAssignments2);
  308. for (RoleAssignment roleAssignment : roleAssignments2) {
  309. if (roleAssignment.principalId() != null
  310. && roleAssignment
  311. .principalId()
  312. .equalsIgnoreCase(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId())) {
  313. found = true;
  314. break;
  315. }
  316. }
  317. Assertions.assertTrue(found, "Resource group should have a role assignment with virtual machine MSI principal");
  318. }
  319. private static Integer objectToInteger(Object obj) {
  320. Integer result = null;
  321. if (obj != null) {
  322. if (obj instanceof Integer) {
  323. result = (Integer) obj;
  324. } else {
  325. result = Integer.valueOf((String) obj);
  326. }
  327. }
  328. return result;
  329. }
  330. }