Azure KeyVault expiration date update

Viewed 3206

Is it possible to update a secret's expiration date without creating a new version of the secret within KeyVault via the Microsoft.Azure.KeyVault package? I'm able to do so within the Azure portal, but need to be able to do this programmatically.

1 Answers

Yes it's possible to update the expiration date for an existing secret without creating a new version.

Here is quick and dirty sample C# code. Look closely at the SecretAttributes and client.UpdateSecretAsync method being called.

Expires is an attribute for the secret that you need to set.

I am making use of KeyVaultClientExtensions.UpdateSecretAsync Method

using Microsoft.Azure.KeyVault;
using Microsoft.Azure.KeyVault.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

namespace UpdateKeyVaultSecret
{
    class Program
    {
        static void Main(string[] args)
        {
            UpdateSecretAttributes("https://rohitvault1.vault.azure.net/secrets/mysecret1").GetAwaiter().GetResult();

            Console.ReadLine();
        }


        private static async Task<string> GetAccessTokenAsync(string authority, string resource, string scope)
        {
            var authContext = new AuthenticationContext(authority);
            ClientCredential clientCred = new ClientCredential("<my-app-clientid>", "<my-app-client-secret>");
            AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientCred);

            if (result == null)
                throw new InvalidOperationException("Failed to obtain the JWT token");

            return result.AccessToken;
        }

        public static async Task<string> GetSecretFromVault(string secretKeyIdentifier)
        {
            var client = new KeyVaultClient(
                new KeyVaultClient.AuthenticationCallback(GetAccessTokenAsync),
                new System.Net.Http.HttpClient());

            var secret = await client.GetSecretAsync(secretKeyIdentifier).ConfigureAwait(false);

            return secret.Value;
        }

        public static async Task<string> UpdateSecretAttributes(string secretKeyIdentifier)
        {
            var client = new KeyVaultClient(
                new KeyVaultClient.AuthenticationCallback(GetAccessTokenAsync),
                new System.Net.Http.HttpClient());

            SecretAttributes attributes = new SecretAttributes();
        attributes.Expires = DateTime.UtcNow.AddDays(15);

            var secret = await client.UpdateSecretAsync(secretKeyIdentifier, null, attributes, null).ConfigureAwait(false);

            return secret.Value;
        }
    }
}

On a side note, there are other programmatic options as well. I'm just mentioning these briefly as the question is pretty generic and someone might land here looking for ways other than C# too:

Related