<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ohe Shogo</title>
    <description>The latest articles on DEV Community by Ohe Shogo (@shogoohe47).</description>
    <link>https://dev.to/shogoohe47</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F471450%2F7a9d7055-c7ba-4d05-a591-ad6b2fd2acb1.png</url>
      <title>DEV Community: Ohe Shogo</title>
      <link>https://dev.to/shogoohe47</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shogoohe47"/>
    <language>en</language>
    <item>
      <title>Monitoring App Service certificate expiration dates using Azure Functions</title>
      <dc:creator>Ohe Shogo</dc:creator>
      <pubDate>Sun, 29 Jan 2023 13:29:51 +0000</pubDate>
      <link>https://dev.to/shogoohe47/monitoring-app-service-certificate-expiration-dates-using-azure-functions-5f87</link>
      <guid>https://dev.to/shogoohe47/monitoring-app-service-certificate-expiration-dates-using-azure-functions-5f87</guid>
      <description>&lt;p&gt;On Azure, We can bought TLS/SSL certificate as &lt;a href="https://docs.microsoft.com/en-us/azure/app-service/configure-ssl-certificate"&gt;App Service Certificate&lt;/a&gt;. and it has auto-renew function, so we no need to worry about expiration dates.&lt;br&gt;
other hands, App Service Certificate (=GoDaddy) issue policy require domain verification every 395 days. We need to know when it expires to ensure that we renew our certificates(*1).&lt;/p&gt;

&lt;p&gt;*1…App Service Certificate notifies the expiration date (=domain verification require case), but the destination is fixed. for example: if you issue certificate for example.com, Only the following five addresses will be notified.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="mailto:webmaster@example.com"&gt;webmaster@example.com&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="mailto:hostmaster@example.com"&gt;hostmaster@example.com&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="mailto:postmaster@example.com"&gt;postmaster@example.com&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="mailto:admin@example.com"&gt;admin@example.com&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="mailto:administrator@example.com"&gt;administrator@example.com&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As an alternative, I would like to introduce a system that uses Azure Functions to check the expiration date of certificates and notify by e-mail with sample code.&lt;/p&gt;
&lt;h1&gt;
  
  
  Overview of the system
&lt;/h1&gt;

&lt;p&gt;system is very simple. Azure Functions accessing Key Vault and evaluate remaining date.&lt;br&gt;
The evaluation results are notified via email using SendGrid.&lt;br&gt;
Using the &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=csharp"&gt;Timer trigger&lt;/a&gt; in Functions, you can check the expiration status periodically, such as once a week or once a month.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W6hPY6sS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8fizegxl00u05luwy7x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W6hPY6sS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8fizegxl00u05luwy7x.png" alt="Image description" width="446" height="340"&gt;&lt;/a&gt;&lt;br&gt;
processing flow:&lt;br&gt;
(1) Access KeyVault from Azure Functions. Check certificate (secret) expiration date&lt;br&gt;
(2) Create emails with expiration date information categorized into three types: active, expires soon, and expired.&lt;br&gt;
(3) Email notification via SendGrid&lt;/p&gt;

&lt;p&gt;The following resources are required to build this mechanism:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Azure Key Vault (it's contains Certificate)&lt;/li&gt;
&lt;li&gt;Azure Functions
-- Windows consumption, Runtime v4.x, Node.js 16LTS
-- If you have a App Service Plan (Standard SKU or Premium), you can able to run this app with same App Service Plan.&lt;/li&gt;
&lt;li&gt;SendGrid: Free 100 (2022) plan, it is able to send 100 mails/day&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Resource creation
&lt;/h1&gt;

&lt;p&gt;Configure the information necessary to create and access each resource.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prepare of SendGrid
&lt;/h2&gt;

&lt;p&gt;Create a SendGrid account, obtain an API Key, and register an email address to be used as the sender (Sender Verification).&lt;/p&gt;

&lt;p&gt;For information on creating a SendGrid account, please refer to the following document&lt;br&gt;
&lt;a href="https://docs.sendgrid.com/for-developers/partners/microsoft-azure-2021#create-a-twilio-sendgrid-account"&gt;Create a Twilio SendGrid account&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For information on obtaining an API Key, please refer to the following document&lt;br&gt;
&lt;a href="https://docs.sendgrid.com/for-developers/partners/microsoft-azure-2021#api-keys"&gt;API Keys&lt;/a&gt;&lt;br&gt;
The SendGrid API Key is displayed only once when it is created. Be sure to record it in notepad.&lt;/p&gt;

&lt;p&gt;The obtained SendGrid API Key will be registered in the application settings later with the following name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AzureWebJobsSendGridApiKey = SendGrid API Key (example: SG.xxxx...)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, the registration of the email address to be used as the sender (Sender Identity): SendGrid allows you to send email to a specified email address as the sender. However, since there is a problem of spoofing, the email address is registered and verified in advance.&lt;br&gt;
If you have just created a Sendgrid account, the email address to be used as the sender is not registered.&lt;br&gt;
Open [Setup Guide] from the Sendgrid icon in the upper left corner, or go to [Settings] &amp;gt; [Sender Authentication] to register the email address to be used as the sender.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qtZsEWSo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4k9gkc2jvfbogvrebien.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qtZsEWSo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4k9gkc2jvfbogvrebien.png" alt="Image description" width="880" height="330"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WEGOjYG7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pd0xqzizgvz6vzay2mxn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WEGOjYG7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pd0xqzizgvz6vzay2mxn.png" alt="Image description" width="880" height="473"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enter the email address and sender information to be used as the sender.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VJkOhoEm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kvpaj4pj1bj4ps993s9p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VJkOhoEm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kvpaj4pj1bj4ps993s9p.png" alt="Image description" width="741" height="992"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you have completed the form and click the "Create" button, a confirmation e-mail will be sent to the e-mail address you specified as the sender.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Lrfn8wT9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mfzqjnwcce22tndvfg46.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Lrfn8wT9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mfzqjnwcce22tndvfg46.png" alt="Image description" width="548" height="586"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A confirmation e-mail will be sent to the e-mail address provided. If you recognize it, click the link specified in the [Verify Single Sender] section.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RbTsyGPQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdiaex0gtjjcp70n1tgv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RbTsyGPQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdiaex0gtjjcp70n1tgv.png" alt="Image description" width="824" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go to Sendgrid site, and if a confirmation message appears, sender verification is complete.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H0PgekLn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2oisl02dyi97cinqduj1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H0PgekLn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2oisl02dyi97cinqduj1.png" alt="Image description" width="455" height="195"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The specified e-mail address can now be used as the sender when using Sendgrid.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prepare Key Vault
&lt;/h2&gt;

&lt;p&gt;If you are using an App Service certificate, you should have a KeyVault for storage.&lt;/p&gt;

&lt;p&gt;If you have no Key Vault resource, &lt;a href="https://learn.microsoft.com/ja-jp/azure/key-vault/certificates/quick-create-portal"&gt;create new Key Vault&lt;/a&gt; and create an appropriate self-signed certificate in "Certificate".&lt;/p&gt;

&lt;h2&gt;
  
  
  Azure Functions Resource
&lt;/h2&gt;

&lt;p&gt;Create Azure Functions resource. There are several ways to create resources, but we will create a Java Script language, Windows, Consumption plan.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-function-app-portal"&gt;Create your first function in the Azure portal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-node"&gt;Quickstart: Create a JavaScript function in Azure using Visual Studio Code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When creating Azure Functions in an existing App Service Plan, AlwaysOn must be enabled. If AlwaysOn is disabled, mostly functions trigger not work fine. for details please check following documents.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/azure-functions/dedicated-plan#always-on"&gt;Always On&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/Azure/azure-functions-host/wiki/Investigating-and-reporting-issues-with-timer-triggered-functions-not-firing"&gt;Investigating and reporting issues with timer triggered functions not firing&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Set Application Settings in Azure Functions
&lt;/h2&gt;

&lt;p&gt;Add SendGrid API keys, etc. to Azure Functions. This sample code reference Application Settings as environment value.&lt;br&gt;
Add the following application settings from the portal's [Configuration] &amp;gt; [Application Settings].&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;KEYVAULT_NAME&lt;/td&gt;
&lt;td&gt;Name of Azure Key Vault&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;EXPIRATION_THRESHOLD_INDAYS&lt;/td&gt;
&lt;td&gt;Number of days subject to expiration date warning&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AzureWebJobsSendGridApiKey&lt;/td&gt;
&lt;td&gt;SendGrid API Key. &lt;a href="https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-sendgrid?tabs=in-process%2Cfunctionsv2&amp;amp;pivots=programming-language-javascript#configuration"&gt;AzureWebJobsSendGridApiKey is default value&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SendGrid_email_from&lt;/td&gt;
&lt;td&gt;e-mail address. Email from. Confirmed email address for Sender Authentication by Sendgrid.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SendGrid_email_to&lt;/td&gt;
&lt;td&gt;e-mail address. Email to. if you separated with ','(comma), it is able to specify multiple e-mail address.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Enable Managed ID of Azure Functions
&lt;/h2&gt;

&lt;p&gt;Authentication is required to access Azure Key Vault from Azure Functions.&lt;br&gt;
Using Managed ID in this scenario.&lt;/p&gt;

&lt;p&gt;The configuration step is as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://learn.microsoft.com/en-us/azure/app-service/overview-managed-identity?tabs=portal%2Chttp"&gt;enable a system-assigned identity&lt;/a&gt; in Azure Functions.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://learn.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/tutorial-windows-vm-access-nonaad#grant-access"&gt;Granting access rights from Functions in the access policy&lt;/a&gt; in Azure Key Vault.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The permissions required by the Key Vault [Access Plicy] are List and Get Secrets and List and Get Certificates.&lt;/p&gt;

&lt;h1&gt;
  
  
  Deploy application code
&lt;/h1&gt;

&lt;p&gt;Sample code are shared in following GitHub repository. Please copy and publish by git clone etc. &lt;br&gt;
&lt;a href="https://github.com/ShogoOhe47/azure-keyvault-secret-expirationdate-checker"&gt;https://github.com/ShogoOhe47/azure-keyvault-secret-expirationdate-checker&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you run code in local development environment, need to set local.settings.json. A sample can be found in &lt;a href="https://github.com/ShogoOhe47/azure-keyvault-secret-expirationdate-checker/blob/main/local.settings.sample.json"&gt;local.settings.sample.json&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;About deploy codes, local development, Timer Trigger schedule (NCRON expression), please read follow docs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-node?tabs=azure-cli%2Cbrowser#run-the-function-locally"&gt;Run the function locally&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-node?tabs=azure-cli%2Cbrowser#deploy-the-function-project-to-azure"&gt;Deploy the function project to Azure&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=in-process&amp;amp;pivots=programming-language-python#ncrontab-expressions"&gt;NCRONTAB expressions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Example of Execution Result
&lt;/h1&gt;

&lt;p&gt;If successfully executed, you will receive the following email.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--liswOt7N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93ztae8jl75hcdhszec1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--liswOt7N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93ztae8jl75hcdhszec1.png" alt="Image description" width="704" height="645"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see Key Vault Secrets and Certificate, Expiration date is near (Days remaining &amp;lt; EXPIRATION_THRESHOLD_INDAYS) or not. The App Service Certificate Resource ID and Common Name of the certificate are also listed, so please refer to them when searching for the target resource.&lt;/p&gt;

&lt;p&gt;These formats are written in code. You change the code, able to customize as you like.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I shared sample code for a mechanism to know the expiration date of certificates managed by Key Vault. I am not a javascript expert, so the logic could use improvement. The data collection, processing, and writing email format is &lt;a href="https://github.com/ShogoOhe47/azure-keyvault-secret-expirationdate-checker/blob/main/kv-expirationdate-checker/index.js#L124"&gt;written as is&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I would be happy to help you in your work.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
