Search code examples
azureazure-storageazure-blob-storageazure-management-api

Unable to regenerate storage key with Azure Management API


I can't use /regenerateKey [1] to regenerate keys for a Storage Account with the Azure Management API.

I'm using the following code in JavaScript (the resource has the subscription removed)

const { ClientSecretCredential } = require('@azure/identity');
const { SecretClient } = require('@azure/keyvault-secrets');
const MSRestAzure = require('ms-rest-azure');

const keyVaultName = process.env.KEY_VAULT_NAME;
const KVUri = `https://${keyVaultName}.vault.azure.net`;

const credential = new ClientSecretCredential(
  process.env.AZURE_TENANT_ID,
  process.env.AZURE_CLIENT_ID,
  process.env.AZURE_CLIENT_SECRET,
);

const vault = new SecretClient(KVUri, credential);

function getCreds() {
  return new Promise((res, rej) => {
    MSRestAzure.loginWithServicePrincipalSecret(
      process.env.AZURE_CLIENT_ID,
      process.env.AZURE_CLIENT_SECRET,
      process.env.AZURE_TENANT_ID,
      (err, creds) => {
        if (err) {
          rej(err);
          return;
        }
        res(creds);
      },
    );
  });
}

const getResourceUrl = (resource, action) => `https://management.azure.com${resource}/${action}?api-version=2019-04-01`;
const resource = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myRg/providers/Microsoft.Storage/storageAccounts/MyStore


const creds = await getCreds();
const client = new MSRestAzure.AzureServiceClient(creds);

const regenUrl = getResourceUrl(resource, 'regenerateKey');

await client.sendRequest({ method: 'POST', url: regenUrl }).then(console.log);

I'm getting an UnexpectedException response -

{
    "error": {
        "code": "UnexpectedException",
        "message": "The server was unable to complete your request."
    }
}

The Client ID/Secret belongs to an app registration that has access to the storage account, as well as Contributor and Storage Account Key Operator over that subscription.

I'm lead to think that I've not formed the request properly.


Solution

  • I am able to reproduce the error if I don't specify the request body.

    Please provide the request body in the following format:

    {
        keyName: "key1 or key2 (basically which key you want to regenerate)"
    }