-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reloading of changed TLS key/cert
- Loading branch information
1 parent
86378c9
commit 6c16b4d
Showing
4 changed files
with
56 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// see https://stackoverflow.com/questions/37473201/is-there-a-way-to-update-the-tls-certificates-in-a-net-http-server-without-any-d | ||
package internal | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"os" | ||
"time" | ||
) | ||
|
||
type CertLoader struct { | ||
CertFile string | ||
KeyFile string | ||
cachedCert *tls.Certificate | ||
cachedCertModTime time.Time | ||
} | ||
|
||
func (cr *CertLoader) GetCertificate(h *tls.ClientHelloInfo) (*tls.Certificate, error) { | ||
stat, err := os.Stat(cr.KeyFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed checking key file modification time: %w", err) | ||
} | ||
|
||
if cr.cachedCert == nil || stat.ModTime().After(cr.cachedCertModTime) { | ||
pair, err := tls.LoadX509KeyPair(cr.CertFile, cr.KeyFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed loading tls key pair: %w", err) | ||
} | ||
|
||
cr.cachedCert = &pair | ||
cr.cachedCertModTime = stat.ModTime() | ||
} | ||
|
||
return cr.cachedCert, nil | ||
} |
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