lnd+cert: recreate TLS files if IPs or DNS changed

This commit makes lnd recreate its TLS certificate if the config's
tlsextradomains or tlsextraips changed. This is useful, since earlier
user would have to manually delete the files to trigger lnd to recreate
them.

To ensure users don't accidentally have their TLS certificate recreated,
we gate it behind a flag --tlsautorefresh that defaults to false.
This commit is contained in:
Johan T. Halseth
2020-02-05 13:51:48 +01:00
parent 83dcf95f92
commit f7a85e07b0
2 changed files with 33 additions and 15 deletions

23
lnd.go
View File

@@ -717,10 +717,25 @@ func getTLSConfig(tlsCertPath string, tlsKeyPath string, tlsExtraIPs,
return nil, nil, "", err
}
// If the certificate expired, delete it and the TLS key and generate a
// new pair.
if time.Now().After(parsedCert.NotAfter) {
ltndLog.Info("TLS certificate is expired, generating a new one")
// We check whether the certifcate we have on disk match the IPs and
// domains specified by the config. If the extra IPs or domains have
// changed from when the certificate was created, we will refresh the
// certificate if auto refresh is active.
refresh := false
if cfg.TLSAutoRefresh {
refresh, err = cert.IsOutdated(
parsedCert, tlsExtraIPs, tlsExtraDomains,
)
if err != nil {
return nil, nil, "", err
}
}
// If the certificate expired or it was outdated, delete it and the TLS
// key and generate a new pair.
if time.Now().After(parsedCert.NotAfter) || refresh {
ltndLog.Info("TLS certificate is expired or outdated, " +
"generating a new one")
err := os.Remove(tlsCertPath)
if err != nil {