-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add server certificate renewal for Edge Hub (#412)
* Add server certificate renewal to Edge Hub * Add log line for no cert renewal * Remove percentage * Fix datetime subtraction * Fix string formatting * Add buffer for renewal * Address review comments * Update log line
- Loading branch information
Showing
4 changed files
with
73 additions
and
6 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Service/CertificateRenewal.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
namespace Microsoft.Azure.Devices.Edge.Hub.Service | ||
{ | ||
using System; | ||
using System.Threading; | ||
using Microsoft.Azure.Devices.Edge.Util; | ||
using Microsoft.Extensions.Logging; | ||
|
||
public class CertificateRenewal : IDisposable | ||
{ | ||
readonly static TimeSpan TimeBuffer = TimeSpan.FromMinutes(5); | ||
readonly CancellationTokenSource cts; | ||
|
||
/// <summary> | ||
/// This cancellation token will expire when certificate renewal is required. | ||
/// </summary> | ||
public CancellationToken Token => this.cts.Token; | ||
|
||
public CertificateRenewal(EdgeHubCertificates certificates, ILogger logger) | ||
{ | ||
Preconditions.CheckNotNull(certificates, nameof(certificates)); | ||
Preconditions.CheckNotNull(logger, nameof(logger)); | ||
|
||
TimeSpan timeToExpire = certificates.ServerCertificate.NotAfter - DateTime.UtcNow; | ||
if (timeToExpire > TimeBuffer) | ||
{ | ||
var renewAfter = timeToExpire - TimeBuffer; | ||
logger.LogInformation("Scheduling server certificate renewal for {0}.", DateTime.UtcNow.Add(renewAfter).ToString("o")); | ||
this.cts = new CancellationTokenSource(renewAfter); | ||
this.cts.Token.Register(l => ((ILogger)l).LogInformation("Restarting process to perform server certificate renewal."), logger); | ||
} | ||
else | ||
{ | ||
this.cts = new CancellationTokenSource(); | ||
logger.LogWarning("Server certificate is expired ({0}). Not scheduling renewal.", timeToExpire.ToString("c")); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
this.Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
try | ||
{ | ||
this.cts.Dispose(); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
// ignore by design | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters