Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server certificate renewal for Edge Hub #412

Merged
merged 9 commits into from
Oct 11, 2018

Conversation

myagley
Copy link
Contributor

@myagley myagley commented Oct 11, 2018

Adds server cert renewal for the Edge Hub.
Triggers a restart of the Edge Hub when expiration of the cert is about to occur.

@myagley
Copy link
Contributor Author

myagley commented Oct 11, 2018

I'm not sure how to write tests for this?

{
logger.LogInformation("Scheduling server certificate renewal for {0}.", DateTime.UtcNow.Add(timeToExpire).ToString("o"));
this.cts = new CancellationTokenSource(timeToExpire);
this.cts.Token.Register(l => ((ILogger)l).LogInformation("Performing server certificate renewal."), logger);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We should probably also add "restarting edgeHub.." to the log..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. Perhaps "Restarting Edge for certificate renewal..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I can add a log in main to indicate we are shutting down. However, most of the logs are in ShutdownHandler and guarded by a semaphore. Do you know the history here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a couple logs to main?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, that works...

else
{
this.cts = new CancellationTokenSource();
logger.LogWarning("Time to server certificate expiration is {0}. Not scheduling renewal.", timeToExpire.ToString("c"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we instead say "Certificate is expired".. might be better?
Also, in this case we don't do anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm worried about it getting in a crash loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if just started and it gets an expired certificate there isn't much a restart will accomplish.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I see.. I was thinking purely form this class's standpoint..
About the crash loop, isn't that what will happen anyways? Will the servers be able to start properly?

{
if (disposing)
{
this.cts.Dispose();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this cause the ugly "A task was cancelled" exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this handles it?

varunpuranik
varunpuranik previously approved these changes Oct 11, 2018
mrohera
mrohera previously approved these changes Oct 11, 2018
{
logger.LogInformation("Scheduling server certificate renewal for {0}.", DateTime.UtcNow.Add(timeToExpire).ToString("o"));
this.cts = new CancellationTokenSource(timeToExpire);
this.cts.Token.Register(l => ((ILogger)l).LogInformation("Performing server certificate renewal."), logger);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. Perhaps "Restarting Edge for certificate renewal..

Preconditions.CheckNotNull(logger, nameof(logger));

TimeSpan timeToExpire = certificates.ServerCertificate.NotAfter - DateTime.UtcNow;
if (timeToExpire > TimeSpan.Zero)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer if we leave a small buffer here of a few mins before expiration instead of waiting till it is expired. This is so that clients log any TLS validation errors and so on.

@myagley myagley dismissed stale reviews from mrohera and varunpuranik via 4d93f39 October 11, 2018 21:32
Copy link
Member

@mrohera mrohera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@myagley myagley merged commit f557fc3 into Azure:master Oct 11, 2018
@myagley myagley deleted the miyagley/cert-renewal branch October 11, 2018 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants