node.jsazureazure-functionspublic-key

Azure Functions (Node.js): How to read the Public Key (.cer), uploaded into the Certificates Blade on Azure, from codes?


I have Azure Functions running on Node.js. (Also, please note that underlying OS via App Service plan is Windows)


Let me do a little detour first.

I've been accessing Azure Key Vault from Node.js codes all along, so that I know how to access Key Vault from Node.js codes, so that it would look like this:

const { DefaultAzureCredential } = require("@azure/identity");
const { SecretClient } = require("@azure/keyvault-secrets");
const AzureCredential = new DefaultAzureCredential();
const kvEndpoint = "Key Vault Endpoint Url";
const kvClient = new SecretClient(kvEndpoint, AzureCredential);
const API_KEY = (await kvClient.getSecret("Key Name")).value;

But that was just Azure Key Vault, which is NOT what I'm looking for. The reason I shared above codes is to highlight that it is quite straight forward to even connect to Azure Key Vault from Node.js. And the Node.js packages for this is complete.


Now.

I want to read the "Public Key" (.cer) which was directly uploaded into the Certificates blade of the Azure Function App dashboard itself. (Not the one from Key Vault). Which looks like this after it was uploaded:

enter image description here

QUESTION

How do I read that Public key (.cer) from the Node.js please? I really tried reading the available documentations from Microsoft but all they mentioned were for C# and Java ONLY.

All the helps are greatly appreciated.


Solution

  • I have taken code from the document to fetch the certificates available under the Azure function App=>Certificates=>Public Key Certificates(.cer) :

    enter image description here

    enter image description here

    Code Snippet:

    const http = require('http');
    const ca = require('win-ca');
    
    // Create an instance of the http server to handle HTTP requests
    let app = http.createServer((req, res) => {
    
    // Set a response type of plain text for the response
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    
    // fetching certificates
    let certificates = []
    
    ca({
      format:ca.der2.pem,
        store: ['My'],
        ondata: crt => certificates.push(crt)
    })
      res.end("Certificate count under 'My' store is" + certificates);
      
    });
    
    let port = process.env.PORT || 3000;
    app.listen(port);
    
    console.log('Node server running on port ' + port);
    

    Output:

    enter image description here