Esempio n. 1
0
        public virtual Response <Vault> Update(VaultPatchParameters parameters, CancellationToken cancellationToken = default)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            using var scope = _vaultClientDiagnostics.CreateScope("Vault.Update");
            scope.Start();
            try
            {
                var response = _vaultRestClient.Update(Id.SubscriptionId, Id.ResourceGroupName, Id.Name, parameters, cancellationToken);
                return(Response.FromValue(new Vault(ArmClient, response.Value), response.GetRawResponse()));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
Esempio n. 2
0
        public async virtual Task <Response <Vault> > UpdateAsync(VaultPatchParameters parameters, CancellationToken cancellationToken = default)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            using var scope = _clientDiagnostics.CreateScope("Vault.Update");
            scope.Start();
            try
            {
                var response = await _vaultsRestClient.UpdateAsync(Id.SubscriptionId, Id.ResourceGroupName, Id.Name, parameters, cancellationToken).ConfigureAwait(false);

                return(Response.FromValue(new Vault(this, response.Value), response.GetRawResponse()));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
        internal HttpMessage CreateUpdateRequest(string resourceGroupName, string vaultName, IDictionary <string, string> tags, VaultPatchProperties properties)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;

            request.Method = RequestMethod.Patch;
            var uri = new RawRequestUriBuilder();

            uri.Reset(endpoint);
            uri.AppendPath("/subscriptions/", false);
            uri.AppendPath(subscriptionId, true);
            uri.AppendPath("/resourceGroups/", false);
            uri.AppendPath(resourceGroupName, true);
            uri.AppendPath("/providers/Microsoft.KeyVault/vaults/", false);
            uri.AppendPath(vaultName, true);
            uri.AppendQuery("api-version", apiVersion, true);
            request.Uri = uri;
            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("Content-Type", "application/json");
            VaultPatchParameters vaultPatchParameters = new VaultPatchParameters()
            {
                Properties = properties
            };

            if (tags != null)
            {
                foreach (var value in tags)
                {
                    vaultPatchParameters.Tags.Add(value);
                }
            }
            var model   = vaultPatchParameters;
            var content = new Utf8JsonRequestContent();

            content.JsonWriter.WriteObjectValue(model);
            request.Content = content;
            message.SetProperty("UserAgentOverride", _userAgent);
            return(message);
        }
 public virtual Response <Vault> Update(string resourceGroupName, string vaultName, VaultPatchParameters parameters, CancellationToken cancellationToken = default)
 {
     using var scope = _clientDiagnostics.CreateScope("VaultsOperations.Update");
     scope.Start();
     try
     {
         return(RestClient.Update(resourceGroupName, vaultName, parameters, cancellationToken));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
Esempio n. 5
0
        //Azure Key Vault sample for managing key vaults -
        //   - Create a key vault
        //   - Authorize an application
        //   - Update a key vault
        //     - alter configurations
        //     - change permissions
        //   - Create another key vault
        //   - List key vaults
        //   - Delete a key vault.
        public static async Task RunSample(TokenCredential credential)
        {
            string subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
            Guid   tenantId       = new Guid(Environment.GetEnvironmentVariable("AZURE_TENANT_ID"));
            // Please pre-define the Client's Object in Environment Variable settings
            string objectId   = Environment.GetEnvironmentVariable("AZURE_OBJECT_ID");
            string vaultName1 = Utilities.RandomResourceName("vault1", 20);
            string vaultName2 = Utilities.RandomResourceName("vault2", 20);
            string rgName     = Utilities.RandomResourceName("rgNEMV", 24);
            string region     = "eastus";

            var keyVaultManagementClient = new KeyVaultManagementClient(subscriptionId, credential);
            var vaults = keyVaultManagementClient.Vaults;

            try
            {
                await ResourceGroupHelper.CreateOrUpdateResourceGroup(rgName, region);

                // Create a key vault with empty access policy

                Utilities.Log("Creating a key vault...");

                var vaultProperties = new VaultProperties(tenantId, new Sku(SkuName.Standard))
                {
                    AccessPolicies = new[] { new AccessPolicyEntry(tenantId, objectId, new Permissions()) }
                };
                var vaultParameters = new VaultCreateOrUpdateParameters(region, vaultProperties);

                var rawResult = await vaults.StartCreateOrUpdateAsync(rgName, vaultName1, vaultParameters);

                var vault1 = (await rawResult.WaitForCompletionAsync()).Value;

                Utilities.Log("Created key vault");
                Utilities.PrintVault(vault1);

                // Authorize an application

                Utilities.Log("Authorizing the application associated with the current service principal...");

                var permissions = new Permissions
                {
                    Keys    = new KeyPermissions[] { new KeyPermissions("all") },
                    Secrets = new SecretPermissions[] { new SecretPermissions("get"), new SecretPermissions("list") },
                };
                var accessPolicyEntry      = new AccessPolicyEntry(tenantId, objectId, permissions);
                var accessPolicyProperties = new VaultAccessPolicyProperties(new[] { accessPolicyEntry });
                var accessPolicyParameters = new VaultAccessPolicyParameters(accessPolicyProperties);

                await vaults.UpdateAccessPolicyAsync(rgName, vaultName1, AccessPolicyUpdateKind.Add, accessPolicyParameters);

                vault1 = (await vaults.GetAsync(rgName, vaultName1)).Value;

                Utilities.Log("Updated key vault");
                Utilities.PrintVault(vault1);

                // Update a key vault

                Utilities.Log("Update a key vault to enable deployments and add permissions to the application...");

                permissions = new Permissions
                {
                    Secrets = new SecretPermissions[] { new SecretPermissions("all") }
                };
                accessPolicyEntry = new AccessPolicyEntry(tenantId, objectId, permissions);
                var vaultPatchProperties = new VaultPatchProperties
                {
                    EnabledForDeployment         = true,
                    EnabledForTemplateDeployment = true,
                    AccessPolicies = new[] { accessPolicyEntry }
                };
                var vaultPatchParameters = new VaultPatchParameters
                {
                    Properties = vaultPatchProperties
                };
                await vaults.UpdateAsync(rgName, vaultName1, vaultPatchParameters);

                vault1 = (await vaults.GetAsync(rgName, vaultName1)).Value;

                Utilities.Log("Updated key vault");
                // Print the network security group
                Utilities.PrintVault(vault1);

                // Create another key vault

                Utilities.Log("Create another key vault");

                permissions = new Permissions
                {
                    Keys    = new KeyPermissions[] { new KeyPermissions("list"), new KeyPermissions("get"), new KeyPermissions("decrypt") },
                    Secrets = new SecretPermissions[] { new SecretPermissions("get") },
                };
                accessPolicyEntry = new AccessPolicyEntry(tenantId, objectId, permissions);
                vaultProperties   = new VaultProperties(tenantId, new Sku(SkuName.Standard))
                {
                    AccessPolicies = new[] { accessPolicyEntry }
                };
                vaultParameters = new VaultCreateOrUpdateParameters(region, vaultProperties);

                rawResult = await vaults.StartCreateOrUpdateAsync(rgName, vaultName2, vaultParameters);

                var vault2 = (await rawResult.WaitForCompletionAsync()).Value;

                Utilities.Log("Created key vault");
                // Print the network security group
                Utilities.PrintVault(vault2);

                // List key vaults

                Utilities.Log("Listing key vaults...");

                foreach (var vault in (await vaults.ListByResourceGroupAsync(rgName).ToEnumerableAsync()))
                {
                    Utilities.PrintVault(vault);
                }

                // Delete key vaults
                Utilities.Log("Deleting the key vaults");
                await vaults.DeleteAsync(rgName, vaultName1);

                await vaults.DeleteAsync(rgName, vaultName2);

                Utilities.Log("Deleted the key vaults");
            }
            finally
            {
                try
                {
                    await ResourceGroupHelper.DeleteResourceGroup(rgName);
                }
                catch (NullReferenceException)
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
                catch (Exception ex)
                {
                    Utilities.Log(ex);
                }
            }
        }
 /// <summary>
 /// Update a key vault in the specified subscription.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the Resource Group to which the server belongs.
 /// </param>
 /// <param name='vaultName'>
 /// Name of the vault
 /// </param>
 /// <param name='parameters'>
 /// Parameters to patch the vault
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <VaultInner> UpdateAsync(this IVaultsOperations operations, string resourceGroupName, string vaultName, VaultPatchParameters parameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.UpdateWithHttpMessagesAsync(resourceGroupName, vaultName, parameters, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Esempio n. 7
0
 /// <summary>
 /// Update a key vault in the specified subscription.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the Resource Group to which the server belongs.
 /// </param>
 /// <param name='vaultName'>
 /// Name of the vault
 /// </param>
 /// <param name='parameters'>
 /// Parameters to patch the vault
 /// </param>
 public static Vault Update(this IVaultsOperations operations, string resourceGroupName, string vaultName, VaultPatchParameters parameters)
 {
     return(operations.UpdateAsync(resourceGroupName, vaultName, parameters).GetAwaiter().GetResult());
 }
 public Task <AzureOperationResponse <Vault> > UpdateWithHttpMessagesAsync(string resourceGroupName, string vaultName, VaultPatchParameters parameters, Dictionary <string, List <string> > customHeaders = null, CancellationToken cancellationToken = default)
 {
     throw new NotImplementedException();
 }
 public virtual async Task <Response <Vault> > UpdateAsync(string resourceGroupName, string vaultName, VaultPatchParameters parameters, CancellationToken cancellationToken = default)
 {
     using var scope = _clientDiagnostics.CreateScope("VaultsClient.Update");
     scope.Start();
     try
     {
         return(await RestClient.UpdateAsync(resourceGroupName, vaultName, parameters, cancellationToken).ConfigureAwait(false));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }