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

tls: Add healthcheck to shutdown if certificate is expired #4792

Conversation

murtyjones
Copy link
Contributor

@murtyjones murtyjones commented Nov 21, 2020

In certain container set ups, it's useful to optionally have lnd just shutdown if it detects that its certs are expired, as assuming there's a hypervisor to restart the container/pod, then upon restart, lnd will have fully up to date certs.

Adds a configurable healthcheck that shuts down if an expired certificate is found.

Reference issue

sample-lnd.conf Outdated Show resolved Hide resolved
@murtyjones murtyjones marked this pull request as ready for review November 21, 2020 19:37
@murtyjones murtyjones force-pushed the certificate-expiration-shutdown-option branch from e35f020 to 7e72bb1 Compare November 21, 2020 20:30
@carlaKC carlaKC requested review from carlaKC and removed request for Roasbeef November 23, 2020 06:59
Copy link
Collaborator

@carlaKC carlaKC left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @murtyjones, this is going to be a really helpful one for a lot of people.

I don't think the IsOutdated function is the right check for this, because it just checks ip addresses and dns names are in sync with our existing config - confusingly named, I also thought it would check expiry!

Would also be nice to have some default values for this check, and it needs to be validated in lncfg.

server.go Outdated Show resolved Hide resolved
sample-lnd.conf Show resolved Hide resolved
server.go Show resolved Hide resolved
server.go Outdated
if err != nil {
return err
}
outdated, err := cert.IsOutdated(
Copy link
Collaborator

Choose a reason for hiding this comment

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

IsOutdated checks the ip addresses + dns names on the cert, rather than its expiry. This can be replaced with a check on cert.NotAfter to detect expiry.

I think we can leave IsOutdated out of this check entirely because we already have the tlsautorefresh flag which will sync the cert with our config at startup if enabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch! I didn't even think to look at the source method 😅

@murtyjones murtyjones force-pushed the certificate-expiration-shutdown-option branch from e2bf1a3 to 9a2b65c Compare November 30, 2020 13:45
@murtyjones
Copy link
Contributor Author

@carlaKC Thanks for reviewing!

Incorporated your feedback. Admittedly I'm not sure what the workflow to test this change looks like, so I've just compiled it.

Copy link
Collaborator

@carlaKC carlaKC left a comment

Choose a reason for hiding this comment

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

Almost there, just need to bump the default interval and we are gg!

I'm not sure what the workflow to test this change looks like

I've tested locally by deleting my existing cert and updating the expiry time to 5 minutes, then run lnd and check that it shuts down once it's expired.

Goods are as advertised:

2020-12-01 09:13:48.992 [INF] HLCK: Health check: tls, call: 1 failed with: TLS certificate is expired, backing off for: 1m0s
2020-12-01 09:14:48.993 [CRT] SRVR: Health check: tls failed after 2 calls
2020-12-01 09:14:48.993 [INF] SRVR: Sending request for shutdown

server.go Outdated
// If the current time is passed the certificate's
// expiry time, then it is considered expired
if time.Now().After(parsedCert.NotAfter) {
return fmt.Errorf("TLS certificate is expired")
Copy link
Collaborator

Choose a reason for hiding this comment

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

nitty-nit: include the cert expiry in the error (nice to have, non-blocking)

config.go Outdated
// is not expired. Although this check is off by default (not all setups
// require it), we still set the other default values so that the health
// check can be easily enabled with sane defaults.
defaultTLSInterval = time.Second * 30
Copy link
Collaborator

Choose a reason for hiding this comment

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

As is, interval needs to be at least 1 minute, so defaultTLSInterval fails validation as is - bump to 1 min?

The 1 min minimum is arbitrarily picked, so can be changed if we see a pressing reason to do so.

Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Very useful feature, LGTM (barring @carlaKC's comments) 🎉

if err != nil {
return err
}
// If the current time is passed the certificate's
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: add newlines before comments.

@murtyjones murtyjones force-pushed the certificate-expiration-shutdown-option branch from 9a2b65c to 83787ee Compare December 1, 2020 13:38
Copy link
Collaborator

@carlaKC carlaKC left a comment

Choose a reason for hiding this comment

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

Great stuff, thanks for the quick turnaround @murtyjones 🙌

In certain container set ups, it's useful to optionally have lnd just shutdown if it detects that its certs are expired, as assuming there's a hypervisor to restart the container/pod, then upon restart, lnd will have fully up to date certs.
@murtyjones murtyjones force-pushed the certificate-expiration-shutdown-option branch from 83787ee to bad4e9a Compare December 1, 2020 13:48

; The amount of time we should wait between certificate expiration health checks.
; This value must be >= 1m.
; healthcheck.tls.interval=1m
Copy link
Contributor Author

Choose a reason for hiding this comment

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

added one last change to make this >=1m (was 30s)

@carlaKC carlaKC added the v0.12 label Dec 1, 2020
@carlaKC carlaKC added this to In progress in v0.12.0-beta via automation Dec 1, 2020
@carlaKC carlaKC moved this from In progress to Reviewer approved in v0.12.0-beta Dec 1, 2020
@Roasbeef Roasbeef added this to the 0.12.0 milestone Dec 2, 2020
@Roasbeef Roasbeef merged commit c047739 into lightningnetwork:master Dec 2, 2020
v0.12.0-beta automation moved this from Reviewer approved to Done Dec 2, 2020
@murtyjones murtyjones deleted the certificate-expiration-shutdown-option branch December 5, 2020 17:48
matheusd pushed a commit to matheusd/dcrlnd that referenced this pull request Jul 19, 2023
…gnetwork#4792)

In certain container set ups, it's useful to optionally have lnd just shutdown if it detects that its certs are expired, as assuming there's a hypervisor to restart the container/pod, then upon restart, lnd will have fully up to date certs.
matheusd pushed a commit to matheusd/dcrlnd that referenced this pull request Jul 19, 2023
…gnetwork#4792)

In certain container set ups, it's useful to optionally have lnd just shutdown if it detects that its certs are expired, as assuming there's a hypervisor to restart the container/pod, then upon restart, lnd will have fully up to date certs.
matheusd pushed a commit to matheusd/dcrlnd that referenced this pull request Jul 20, 2023
…gnetwork#4792)

In certain container set ups, it's useful to optionally have lnd just shutdown if it detects that its certs are expired, as assuming there's a hypervisor to restart the container/pod, then upon restart, lnd will have fully up to date certs.
matheusd pushed a commit to matheusd/dcrlnd that referenced this pull request Jul 20, 2023
…gnetwork#4792)

In certain container set ups, it's useful to optionally have lnd just shutdown if it detects that its certs are expired, as assuming there's a hypervisor to restart the container/pod, then upon restart, lnd will have fully up to date certs.
matheusd pushed a commit to matheusd/dcrlnd that referenced this pull request Jul 20, 2023
…gnetwork#4792)

In certain container set ups, it's useful to optionally have lnd just shutdown if it detects that its certs are expired, as assuming there's a hypervisor to restart the container/pod, then upon restart, lnd will have fully up to date certs.
matheusd pushed a commit to matheusd/dcrlnd that referenced this pull request Jul 21, 2023
…gnetwork#4792)

In certain container set ups, it's useful to optionally have lnd just shutdown if it detects that its certs are expired, as assuming there's a hypervisor to restart the container/pod, then upon restart, lnd will have fully up to date certs.
matheusd pushed a commit to matheusd/dcrlnd that referenced this pull request Jul 24, 2023
…gnetwork#4792)

In certain container set ups, it's useful to optionally have lnd just shutdown if it detects that its certs are expired, as assuming there's a hypervisor to restart the container/pod, then upon restart, lnd will have fully up to date certs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
v0.12.0-beta
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants