Example #1
0
        static void Main(string[] args)
        {
            ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            // run the vault recovery samples
            // soft delete flow with new vault
            Console.WriteLine("\n\n** Running recovery/purge sample for a new vault..");
            Task.Run(() => KeyVaultRecoverySamples.DemonstrateRecoveryAndPurgeForNewVaultAsync()).ConfigureAwait(false).GetAwaiter().GetResult();

            // enabling soft delete on existing vault + soft delete flow
            Console.WriteLine("\n\n** Running recovery/purge sample for an existing vault..");
            Task.Run(() => KeyVaultRecoverySamples.DemonstrateRecoveryAndPurgeForExistingVaultAsync()).ConfigureAwait(false).GetAwaiter().GetResult();

            // soft delete flow for a vault entity
            Console.WriteLine("\n\n** Running recovery/purge sample for a vault entity..");
            Task.Run(() => KeyVaultEntityRecoverySamples.DemonstrateRecoveryAndPurgeAsync()).ConfigureAwait(false).GetAwaiter().GetResult();

            // backup/restore flow for a vault entity
            Console.WriteLine("\n\n** Running backup/restore sample for a vault entity..");
            Task.Run(() => KeyVaultEntityRecoverySamples.DemonstrateBackupAndRestoreAsync()).ConfigureAwait(false).GetAwaiter().GetResult();
        }
        /// <summary>
        /// Demonstrates how to enable soft delete on an existing vault, and then proceeds to delete, recover and purge the vault.
        /// Assumes the caller has the KeyVaultContributor role in the subscription.
        /// </summary>
        /// <returns>Task representing this functionality.</returns>
        public static async Task DemonstrateRecoveryAndPurgeForNewVaultAsync()
        {
            // instantiate the samples object
            var sample = new KeyVaultRecoverySamples();

            var rgName = sample.context.ResourceGroupName;

            // derive a unique vault name for this sample
            var vaultName = sample.context.VaultName + "new";

            DeletedVaultInner deletedVault = null;

            try
            {
                var vaultParameters = sample.CreateVaultParameters(rgName, vaultName, sample.context.PreferredLocation, enableSoftDelete: true, enablePurgeProtection: false);
                Console.WriteLine("Operating with vault name '{0}' in resource group '{1}' and location '{2}'", vaultName, rgName, vaultParameters.Location);

                // create new soft-delete-enabled vault
                Console.Write("Creating vault...");
                var vault = await sample.ManagementClient.Vaults.CreateOrUpdateAsync(rgName, vaultName, vaultParameters).ConfigureAwait(false);

                Console.WriteLine("done.");

                // wait for the DNS record to propagate; verify properties
                Console.Write("Waiting for DNS propagation..");
                Thread.Sleep(10 * 1000);
                Console.WriteLine("done.");

                Console.Write("Retrieving newly created vault...");
                var retrievedVault = await sample.ManagementClient.Vaults.GetAsync(rgName, vaultName).ConfigureAwait(false);

                Console.WriteLine("done.");

                // delete vault
                Console.Write("Deleting vault...");
                await sample.ManagementClient.Vaults.DeleteAsync(rgName, vaultName).ConfigureAwait(false);

                Console.WriteLine("done.");

                // confirm the existence of the deleted vault
                Console.Write("Retrieving deleted vault...");
                deletedVault = await sample.ManagementClient.Vaults.GetDeletedAsync(vaultName, retrievedVault.Location).ConfigureAwait(false);

                Console.WriteLine("done; '{0}' deleted on: {1}, scheduled for purge on: {2}", deletedVault.Id, deletedVault.Properties.DeletionDate, deletedVault.Properties.ScheduledPurgeDate);

                // recover; set the creation mode as 'recovery' in the vault parameters
                Console.Write("Recovering deleted vault...");
                vaultParameters.Properties.CreateMode = CreateMode.Recover;
                await sample.ManagementClient.Vaults.CreateOrUpdateAsync(rgName, vaultName, vaultParameters).ConfigureAwait(false);

                Console.WriteLine("done.");

                // confirm recovery
                Console.Write("Verifying the existence of recovered vault...");
                var recoveredVault = await sample.ManagementClient.Vaults.GetAsync(rgName, vaultName).ConfigureAwait(false);

                Console.WriteLine("done.");

                // delete vault
                Console.Write("Deleting vault...");
                await sample.ManagementClient.Vaults.DeleteAsync(rgName, vaultName).ConfigureAwait(false);

                Console.WriteLine("done.");

                // purge vault
                Console.Write("Purging vault...");
                deletedVault = await sample.ManagementClient.Vaults.GetDeletedAsync(vaultName, recoveredVault.Location).ConfigureAwait(false);

                await sample.ManagementClient.Vaults.PurgeDeletedAsync(vaultName, recoveredVault.Location).ConfigureAwait(false);

                Console.WriteLine("done.");
            }
            catch (Exception e)
            {
                Console.WriteLine("unexpected exception encountered running the test: {message}", e.Message);
                throw;
            }

            // verify purge
            try
            {
                Console.Write("Verifying vault deletion succeeded...");
                await sample.ManagementClient.Vaults.GetAsync(rgName, vaultName);
            }
            catch (Exception e)
            {
                // no op; expected
                VerifyExpectedARMException(e, HttpStatusCode.NotFound);
                Console.WriteLine("done.");
            }

            try
            {
                Console.Write("Verifying vault purging succeeded...");
                await sample.ManagementClient.Vaults.GetDeletedAsync(vaultName, deletedVault.Properties.Location).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                // no op; expected
                VerifyExpectedARMException(e, HttpStatusCode.NotFound);
                Console.WriteLine("done.");
            }
        }