Azure Key Vault: Set a Rotation Policy and Rotate a Key
See more Azure Key Vault Examples
Requires Chilkat 11.6.0 or later. Keep qa_data/azure/keyvault.json and token/PFX files private, outside web content and source control, with access restricted to the application identity. Use placeholders replaced by your configuration, never commit credentials. Create output directories before running file-writing examples. These examples target Azure public-cloud vaults, not Managed HSM or sovereign-cloud endpoints.
Uses HTTPS and Key Vault data-plane API 2026-04-01. Configure vault_name and first run the client-credentials token example. It writes access_token and an expires_at UTC timestamp to qa_data/tokens/azureKeyVaultToken.json. Each request checks expiration; refresh is explicit. The sample stops on errors and does not retry. Production callers should handle 429/Retry-After and transient failures with bounded backoff, and reconcile uncertain writes. Redirects are disabled.
Set key_name. Reads the current policy, replaces it with expiry P90D and rotation after P60D, then requests one immediate rotation. Requires keys/getrotationpolicy, keys/setrotationpolicy and keys/rotate. Review the policy replacement before running; existing notification actions are replaced. Keep old versioned IDs for old ciphertext/signatures. Do not blindly retry rotation after uncertain completion.
Chilkat VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
set stringCompare = CreateObject("Chilkat.StringBuilder")
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Keep this configuration private, outside public web content and source control.
set config = CreateObject("Chilkat.JsonObject")
config.Strict = 1
success = config.LoadFile("qa_data/azure/keyvault.json")
If (success <> 1) Then
outFile.WriteLine("Could not load private Key Vault configuration.")
WScript.Quit
End If
success = stringCompare.SetString(config.StringOf("vault_name"))
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("vault_name") <> 1) Or (stringMatch1 = 1)) Then
outFile.WriteLine("Missing or invalid vault_name.")
WScript.Quit
End If
set validate1 = CreateObject("Chilkat.StringBuilder")
set alphabet1 = CreateObject("Chilkat.StringBuilder")
success = validate1.Append(config.StringOf("vault_name"))
If (success <> 1) Then
outFile.WriteLine("Could not validate vault_name.")
WScript.Quit
End If
success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-")
If (success <> 1) Then
outFile.WriteLine("Could not initialize validation.")
WScript.Quit
End If
If ((validate1.Length < 3) Or (validate1.Length > 24)) Then
outFile.WriteLine("Invalid vault_name length.")
WScript.Quit
End If
charIndex1 = 0
Do While (charIndex1 < validate1.Length)
If (alphabet1.Contains(validate1.GetRange(charIndex1,1,0),1) <> 1) Then
outFile.WriteLine("Invalid character in vault_name.")
WScript.Quit
End If
charIndex1 = charIndex1 + 1
Loop
set vaultName = CreateObject("Chilkat.StringBuilder")
success = vaultName.Append(config.StringOf("vault_name"))
set letters = CreateObject("Chilkat.StringBuilder")
success = letters.Append("abcdefghijklmnopqrstuvwxyz")
If ((letters.Contains(vaultName.GetRange(0,1,0),1) <> 1) Or (vaultName.EndsWith("-",1) = 1) Or (vaultName.Contains("--",1) = 1)) Then
outFile.WriteLine("Invalid vault_name.")
WScript.Quit
End If
set origin = CreateObject("Chilkat.StringBuilder")
success = origin.Append("https://")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = origin.Append(config.StringOf("vault_name"))
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = origin.Append(".vault.azure.net")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
set http = CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.FollowRedirects = 0
http.ConnectTimeout = 20
http.ReadTimeout = 60
http.VerboseLogging = 0
set request = CreateObject("Chilkat.HttpRequest")
request.HttpVerb = "GET"
set response = CreateObject("Chilkat.HttpResponse")
set result = CreateObject("Chilkat.JsonObject")
result.Strict = 1
' First run Get a Key Vault access token.
set token = CreateObject("Chilkat.JsonObject")
token.Strict = 1
success = token.LoadFile("qa_data/tokens/azureKeyVaultToken.json")
If (success <> 1) Then
outFile.WriteLine("Could not read the private token file.")
WScript.Quit
End If
success = stringCompare.SetString(token.StringOf("access_token"))
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((token.JsonTypeOf("access_token") <> 1) Or (stringMatch2 = 1)) Then
outFile.WriteLine("Missing or invalid access_token.")
WScript.Quit
End If
success = stringCompare.SetString(token.StringOf("expires_at"))
stringMatch3 = stringCompare.ContentsEqual("",1)
If ((token.JsonTypeOf("expires_at") <> 1) Or (stringMatch3 = 1)) Then
outFile.WriteLine("Missing or invalid expires_at.")
WScript.Quit
End If
set expires = CreateObject("Chilkat.CkDateTime")
set now = CreateObject("Chilkat.CkDateTime")
success = expires.SetFromTimestamp(token.StringOf("expires_at"))
If (success <> 1) Then
outFile.WriteLine("Invalid token expiration.")
WScript.Quit
End If
http.AuthToken = token.StringOf("access_token")
success = stringCompare.SetString(config.StringOf("key_name"))
stringMatch4 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("key_name") <> 1) Or (stringMatch4 = 1)) Then
outFile.WriteLine("Missing or invalid key_name.")
WScript.Quit
End If
set validate37 = CreateObject("Chilkat.StringBuilder")
set alphabet37 = CreateObject("Chilkat.StringBuilder")
success = validate37.Append(config.StringOf("key_name"))
If (success <> 1) Then
outFile.WriteLine("Could not validate key_name.")
WScript.Quit
End If
success = alphabet37.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If (success <> 1) Then
outFile.WriteLine("Could not initialize validation.")
WScript.Quit
End If
If ((validate37.Length < 1) Or (validate37.Length > 127)) Then
outFile.WriteLine("Invalid key_name length.")
WScript.Quit
End If
charIndex37 = 0
Do While (charIndex37 < validate37.Length)
If (alphabet37.Contains(validate37.GetRange(charIndex37,1,0),1) <> 1) Then
outFile.WriteLine("Invalid character in key_name.")
WScript.Quit
End If
charIndex37 = charIndex37 + 1
Loop
set resourceBase = CreateObject("Chilkat.StringBuilder")
success = resourceBase.Append(origin.GetAsString())
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = resourceBase.Append("/keys/")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = resourceBase.Append(config.StringOf("key_name"))
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
set policyUrl = CreateObject("Chilkat.StringBuilder")
success = policyUrl.Append(resourceBase.GetAsString())
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = policyUrl.Append("/rotationpolicy?api-version=2026-04-01")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
outFile.WriteLine("Could not read current time.")
WScript.Quit
End If
If (expires.DiffSeconds(now) <= 60) Then
outFile.WriteLine("Token is expired or near expiration. Obtain a new token before continuing.")
WScript.Quit
End If
request.HttpVerb = "GET"
success = http.HttpReq(policyUrl.GetAsString(),request,response)
If (success <> 1) Then
outFile.WriteLine("Request did not complete; inspect diagnostics privately.")
WScript.Quit
End If
If (response.StatusCode <> 200) Then
outFile.WriteLine("Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.")
WScript.Quit
End If
success = result.Load(response.BodyStr)
If (success <> 1) Then
outFile.WriteLine("Invalid JSON response.")
WScript.Quit
End If
set body = CreateObject("Chilkat.JsonObject")
success = body.UpdateString("attributes.expiryTime","P90D")
If (success <> 1) Then
outFile.WriteLine("Could not set attributes.expiryTime.")
WScript.Quit
End If
success = body.UpdateString("lifetimeActions[0].trigger.timeAfterCreate","P60D")
If (success <> 1) Then
outFile.WriteLine("Could not set lifetimeActions[0].trigger.timeAfterCreate.")
WScript.Quit
End If
success = body.UpdateString("lifetimeActions[0].action.type","Rotate")
If (success <> 1) Then
outFile.WriteLine("Could not set lifetimeActions[0].action.type.")
WScript.Quit
End If
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
outFile.WriteLine("Could not read current time.")
WScript.Quit
End If
If (expires.DiffSeconds(now) <= 60) Then
outFile.WriteLine("Token is expired or near expiration. Obtain a new token before continuing.")
WScript.Quit
End If
success = http.HttpJson("PUT",policyUrl.GetAsString(),body,"application/json",response)
If (success <> 1) Then
outFile.WriteLine("Request failed; reconcile uncertain writes before retrying.")
WScript.Quit
End If
If (response.StatusCode <> 200) Then
outFile.WriteLine("Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.")
WScript.Quit
End If
success = result.Load(response.BodyStr)
If (success <> 1) Then
outFile.WriteLine("Invalid JSON response.")
WScript.Quit
End If
success = stringCompare.SetString(result.StringOf("attributes.expiryTime"))
stringMatch5 = stringCompare.ContentsEqual("P90D",1)
If (stringMatch5 <> 1) Then
outFile.WriteLine("Unexpected returned rotation policy.")
WScript.Quit
End If
body.Clear
set rotateUrl = CreateObject("Chilkat.StringBuilder")
success = rotateUrl.Append(resourceBase.GetAsString())
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = rotateUrl.Append("/rotate?api-version=2026-04-01")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
outFile.WriteLine("Could not read current time.")
WScript.Quit
End If
If (expires.DiffSeconds(now) <= 60) Then
outFile.WriteLine("Token is expired or near expiration. Obtain a new token before continuing.")
WScript.Quit
End If
success = http.HttpJson("POST",rotateUrl.GetAsString(),body,"application/json",response)
If (success <> 1) Then
outFile.WriteLine("Request failed; reconcile uncertain writes before retrying.")
WScript.Quit
End If
If (response.StatusCode <> 200) Then
outFile.WriteLine("Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.")
WScript.Quit
End If
success = result.Load(response.BodyStr)
If (success <> 1) Then
outFile.WriteLine("Invalid JSON response.")
WScript.Quit
End If
success = stringCompare.SetString(result.StringOf("key.kid"))
stringMatch6 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("key.kid") <> 1) Or (stringMatch6 = 1)) Then
outFile.WriteLine("Missing or invalid key.kid.")
WScript.Quit
End If
set returnedVersion38Prefix = CreateObject("Chilkat.StringBuilder")
success = returnedVersion38Prefix.Append(resourceBase.GetAsString())
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = returnedVersion38Prefix.Append("/")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
set returnedVersion38Id = CreateObject("Chilkat.StringBuilder")
success = returnedVersion38Id.Append(result.StringOf("key.kid"))
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
If (returnedVersion38Id.StartsWith(returnedVersion38Prefix.GetAsString(),1) <> 1) Then
outFile.WriteLine("Returned identifier is outside the expected vault/resource.")
WScript.Quit
End If
returnedVersion38 = returnedVersion38Id.GetRange(returnedVersion38Prefix.Length,returnedVersion38Id.Length - returnedVersion38Prefix.Length,0)
set validate39 = CreateObject("Chilkat.StringBuilder")
set alphabet39 = CreateObject("Chilkat.StringBuilder")
success = validate39.Append(returnedVersion38)
If (success <> 1) Then
outFile.WriteLine("Could not validate resource identifier.")
WScript.Quit
End If
success = alphabet39.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If (success <> 1) Then
outFile.WriteLine("Could not initialize validation.")
WScript.Quit
End If
If ((validate39.Length < 1) Or (validate39.Length > 127)) Then
outFile.WriteLine("Invalid resource identifier length.")
WScript.Quit
End If
charIndex39 = 0
Do While (charIndex39 < validate39.Length)
If (alphabet39.Contains(validate39.GetRange(charIndex39,1,0),1) <> 1) Then
outFile.WriteLine("Invalid character in resource identifier.")
WScript.Quit
End If
charIndex39 = charIndex39 + 1
Loop
outFile.WriteLine("New key version: " & result.StringOf("key.kid"))
outFile.WriteLine("Example completed.")
outFile.Close