-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/dev' into dev
- Loading branch information
Showing
4 changed files
with
67 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,3 +51,6 @@ data.etcd/ | |
# vscode | ||
.vscode | ||
|
||
# asdf | ||
.tool-versions | ||
|
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,50 @@ | ||
package cmd | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
type CertReloader struct { | ||
CertFile string // path to the x509 certificate for https | ||
KeyFile string // path to the x509 private key matching | ||
mutex *sync.Mutex | ||
cachedCert *tls.Certificate | ||
} | ||
|
||
func (cr *CertReloader) reloadCertificate() error { | ||
pair, err := tls.LoadX509KeyPair(cr.CertFile, cr.KeyFile) | ||
if err != nil { | ||
return fmt.Errorf("failed loading tls key pair: %w", err) | ||
} | ||
cr.mutex.Lock() | ||
cr.cachedCert = &pair | ||
cr.mutex.Unlock() | ||
return err | ||
} | ||
|
||
func (cr *CertReloader) GetCertificate() *tls.Certificate { | ||
cr.mutex.Lock() | ||
defer cr.mutex.Unlock() | ||
return cr.cachedCert | ||
} | ||
|
||
func (cr *CertReloader) Listen() { | ||
ticker := time.NewTicker(3 * time.Second) | ||
|
||
go func() { | ||
for { | ||
select { | ||
case <-ticker.C: | ||
err := cr.reloadCertificate() | ||
if err != nil { | ||
zap.L().Error("Error in reloading ssl certificates", zap.Error(err)) | ||
} | ||
} | ||
} | ||
}() | ||
} |
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