|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.resourcemanager.kubernetescluster.samples; |
| 5 | + |
| 6 | +import com.azure.core.credential.TokenCredential; |
| 7 | +import com.azure.core.http.policy.HttpLogDetailLevel; |
| 8 | +import com.azure.core.management.AzureEnvironment; |
| 9 | +import com.azure.core.management.Region; |
| 10 | +import com.azure.core.management.profile.AzureProfile; |
| 11 | +import com.azure.core.util.Configuration; |
| 12 | +import com.azure.identity.DefaultAzureCredentialBuilder; |
| 13 | +import com.azure.resourcemanager.AzureResourceManager; |
| 14 | +import com.azure.resourcemanager.compute.models.DiskEncryptionSet; |
| 15 | +import com.azure.resourcemanager.compute.models.DiskEncryptionSetType; |
| 16 | +import com.azure.resourcemanager.containerservice.models.AgentPoolMode; |
| 17 | +import com.azure.resourcemanager.containerservice.models.ContainerServiceVMSizeTypes; |
| 18 | +import com.azure.resourcemanager.containerservice.models.KubernetesCluster; |
| 19 | +import com.azure.resourcemanager.keyvault.models.Key; |
| 20 | +import com.azure.resourcemanager.keyvault.models.KeyPermissions; |
| 21 | +import com.azure.resourcemanager.keyvault.models.Vault; |
| 22 | +import com.azure.resourcemanager.samples.Utils; |
| 23 | +import com.azure.security.keyvault.keys.models.KeyType; |
| 24 | + |
| 25 | +/** |
| 26 | + * Azure Container Service (AKS) sample for managing a Kubernetes cluster with customer-managed key. |
| 27 | + * - Create a key vault with purge-protection enabled |
| 28 | + * - Create a key in key vault |
| 29 | + * - Create a disk encryption set using the created key as the encryption key with automatic key-rotation |
| 30 | + * - Grant the des access to the key vault |
| 31 | + * - Create an Azure Container Service (AKS) with managed Kubernetes cluster, os disk encrypted using customer-managed key |
| 32 | + */ |
| 33 | +public class ManageKubernetesClusterWithCustomerManagedKey { |
| 34 | + |
| 35 | + /** |
| 36 | + * Main function which runs the actual sample. |
| 37 | + * |
| 38 | + * @param azureResourceManager instance of the azure client |
| 39 | + * @param clientId clientId of the app |
| 40 | + * @return true if sample runs successfully |
| 41 | + */ |
| 42 | + public static boolean runSample(AzureResourceManager azureResourceManager, String clientId) { |
| 43 | + final String vaultName = Utils.randomResourceName(azureResourceManager, "v", 15); |
| 44 | + final String keyName = Utils.randomResourceName(azureResourceManager, "vk", 15); |
| 45 | + final String desName = Utils.randomResourceName(azureResourceManager, "des", 15); |
| 46 | + final String rgName = Utils.randomResourceName(azureResourceManager, "rgaks", 15); |
| 47 | + final String aksName = Utils.randomResourceName(azureResourceManager, "akssample", 30); |
| 48 | + final Region region = Region.US_EAST; |
| 49 | + |
| 50 | + try { |
| 51 | + //============================================================= |
| 52 | + // Create a key vault with purge-protection enabled |
| 53 | + |
| 54 | + Vault vault = azureResourceManager.vaults() |
| 55 | + .define(vaultName) |
| 56 | + .withRegion(region) |
| 57 | + .withNewResourceGroup(rgName) |
| 58 | + .defineAccessPolicy() |
| 59 | + .forServicePrincipal(clientId) |
| 60 | + .allowKeyPermissions(KeyPermissions.CREATE) |
| 61 | + .attach() |
| 62 | + .withPurgeProtectionEnabled() |
| 63 | + .create(); |
| 64 | + |
| 65 | + System.out.println("Created key vault: " + vault.name()); |
| 66 | + |
| 67 | + //============================================================= |
| 68 | + // Create a key in key vault |
| 69 | + |
| 70 | + Key vaultKey = vault.keys() |
| 71 | + .define(keyName) |
| 72 | + .withKeyTypeToCreate(KeyType.RSA) |
| 73 | + .withKeySize(4096) |
| 74 | + .create(); |
| 75 | + |
| 76 | + System.out.println("Created key vault key: " + vaultKey.id()); |
| 77 | + |
| 78 | + //============================================================= |
| 79 | + // Create a disk encryption set using the created key as the encryption key with automatic key-rotation |
| 80 | + DiskEncryptionSet des = azureResourceManager.diskEncryptionSets() |
| 81 | + .define(desName) |
| 82 | + .withRegion(region) |
| 83 | + .withExistingResourceGroup(rgName) |
| 84 | + .withEncryptionType(DiskEncryptionSetType.ENCRYPTION_AT_REST_WITH_CUSTOMER_KEY) |
| 85 | + .withExistingKeyVault(vault.id()) |
| 86 | + .withExistingKey(vaultKey.id()) |
| 87 | + .withSystemAssignedManagedServiceIdentity() |
| 88 | + .withAutomaticKeyRotation() |
| 89 | + .create(); |
| 90 | + |
| 91 | + System.out.println("Created disk encryption set with automatic key-rotation: " + des.name()); |
| 92 | + |
| 93 | + //============================================================= |
| 94 | + // Grant the des access to the key vault |
| 95 | + |
| 96 | + vault.update() |
| 97 | + .defineAccessPolicy() |
| 98 | + .forObjectId(des.systemAssignedManagedServiceIdentityPrincipalId()) |
| 99 | + .allowKeyPermissions(KeyPermissions.GET, KeyPermissions.WRAP_KEY, KeyPermissions.UNWRAP_KEY) |
| 100 | + .attach() |
| 101 | + .apply(); |
| 102 | + |
| 103 | + System.out.println("Granted des access to the key vault."); |
| 104 | + |
| 105 | + //============================================================= |
| 106 | + // Create an Azure Container Service (AKS) with managed Kubernetes cluster, os disk encrypted using customer-managed key |
| 107 | + |
| 108 | + KubernetesCluster kubernetesCluster = azureResourceManager |
| 109 | + .kubernetesClusters() |
| 110 | + .define(aksName) |
| 111 | + .withRegion(region) |
| 112 | + .withExistingResourceGroup(rgName) |
| 113 | + .withDefaultVersion() |
| 114 | + .withSystemAssignedManagedServiceIdentity() |
| 115 | + .withDiskEncryptionSet(des.id()) |
| 116 | + .defineAgentPool("agentpool") |
| 117 | + .withVirtualMachineSize(ContainerServiceVMSizeTypes.STANDARD_D2_V3) |
| 118 | + .withAgentPoolVirtualMachineCount(1) |
| 119 | + .withAgentPoolMode(AgentPoolMode.SYSTEM) |
| 120 | + .withOSDiskSizeInGB(30) |
| 121 | + .attach() |
| 122 | + .withDnsPrefix("mp1" + aksName) |
| 123 | + .create(); |
| 124 | + |
| 125 | + System.out.println("Created Azure Container Service (AKS) with managed Kubernetes cluster, " |
| 126 | + + "os disk encrypted using customer-managed key"); |
| 127 | + Utils.print(kubernetesCluster); |
| 128 | + |
| 129 | + return true; |
| 130 | + } finally { |
| 131 | + try { |
| 132 | + System.out.println("Deleting Resource Group: " + rgName); |
| 133 | + azureResourceManager.resourceGroups().beginDeleteByName(rgName); |
| 134 | + System.out.println("Deleted Resource Group: " + rgName); |
| 135 | + } catch (NullPointerException npe) { |
| 136 | + System.out.println("Did not create any resources in Azure. No clean up is necessary"); |
| 137 | + } catch (Exception g) { |
| 138 | + g.printStackTrace(); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Main entry point. |
| 145 | + * |
| 146 | + * @param args the parameters |
| 147 | + */ |
| 148 | + public static void main(String[] args) { |
| 149 | + try { |
| 150 | + //============================================================= |
| 151 | + // Authenticate |
| 152 | + |
| 153 | + final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); |
| 154 | + final TokenCredential credential = new DefaultAzureCredentialBuilder() |
| 155 | + .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint()) |
| 156 | + .build(); |
| 157 | + |
| 158 | + AzureResourceManager azureResourceManager = AzureResourceManager |
| 159 | + .configure() |
| 160 | + .withLogLevel(HttpLogDetailLevel.BASIC) |
| 161 | + .authenticate(credential, profile) |
| 162 | + .withDefaultSubscription(); |
| 163 | + |
| 164 | + // Print selected subscription |
| 165 | + System.out.println("Selected subscription: " + azureResourceManager.subscriptionId()); |
| 166 | + |
| 167 | + runSample(azureResourceManager, |
| 168 | + Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_CLIENT_ID)); |
| 169 | + } catch (Exception e) { |
| 170 | + System.out.println(e.getMessage()); |
| 171 | + e.printStackTrace(); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments