Lua Requires Chilkat v9.5.0.96+
Lua
Azure Key Vault Find Certificate
See more Azure Key Vault Examples
Let's say you have the certificate locally, but not with the private key. You only have the certificate, such as in a .cer file, but not the .pfx. The purpose of this example is to show how to find the same certificate in Azure Key Vault, and return the Azure Key Vault's name for the certificate.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- We have a .cer file locally, and we want to find this same certificate in Azure Key Vault
-- because we'll need Azure Key Vault's name (and version) for the certificate if we are going to ask
-- Key Vault to sign using the cert's private key.
local cert = chilkat.Cert.new()
local success = cert:load_from_file("qa_data/certs/myCert.cer")
if not success then
print(cert.last_error_text)
return
end
-- Let's the the SHA1 thumbprint for our cert in base64url format. This is the "x5t" member that we'll
-- be seeking in the list of certificates returned from Azure Key Vault.
local bdThumbprint = chilkat.BinData.new()
bdThumbprint:append_encoded(cert.sha1_thumbprint, "hex")
local seek_x5t = bdThumbprint:get_encoded("base64url")
print("Seeking the cert with x5t = " .. seek_x5t)
-- Provide information needed for Chilkat to automatically get an OAuth2 access token as needed.
local json = chilkat.JsonObject.new()
json:update_string("client_id", "APP_ID")
json:update_string("client_secret", "APP_PASSWORD")
json:update_string("resource", "https://vault.azure.net")
json:update_string("token_endpoint", "https://login.microsoftonline.com/TENANT_ID/oauth2/token")
local http = chilkat.Http.new()
-- Instead of providing an actual access token, we give Chilkat the information that allows it to
-- automatically fetch the access token using the OAuth2 client credentials flow.
http.auth_token = json:emit()
-- Download JSON containing information about the certs in the Azure Key Vault.
-- Replace VAULT_NAME with the name of your Azure Key Vault.
local sbResponse = chilkat.StringBuilder.new()
success = http:quick_get_sb("https://VAULT_NAME.vault.azure.net/certificates?api-version=7.4", sbResponse)
if not success then
local statusCode = http.last_status
if statusCode == 0 then
-- We did not get a response from the server..
print(http.last_error_text)
else
-- We received a response, but it was an error.
print("Error response status code: " .. statusCode)
print("Error response:")
print(sbResponse:get_as_string())
end
return
end
local jsonResp = chilkat.JsonObject.new()
jsonResp:load_sb(sbResponse)
jsonResp.emit_compact = false
-- The JSON will contain an array of certs like this:
-- {
-- "value": [
-- {
-- "id": "https://kvchilkat.vault.azure.net/certificates/BadSSL",
-- "x5t": "U04xLnb8Ww7BKkW9dD7P1cCHNDY",
-- "attributes": {
-- "enabled": true,
-- "nbf": 1674409014,
-- "exp": 1737481014,
-- "created": 1697294224,
-- "updated": 1697294224
-- },
-- "subject": ""
-- },
-- ...
-- ...
-- Find the record having an "x5t" value equal to the one we're seeking.
local jsonRec = jsonResp:find_record("value", "x5t", seek_x5t, true)
if not jsonResp.last_method_success then
print("Did not find a matching certificate.")
else
print("Found the matching certificate.")
-- The id is a value such as https://kvchilkat.vault.azure.net/certificates/BadSSL
print("id: " .. jsonRec:string_of("id"))
-- The name of the certificate is the last word after the final "/", such as "BadSSL"
local sbId = chilkat.StringBuilder.new()
sbId:append(jsonRec:string_of("id"))
local certName = sbId:get_after_final("/", false)
print("name: " .. certName)
end