Skip to content

Commit f20c8ff

Browse files
fsul7oWindzCUHK
andauthored
Fixing a bug where the token server startup check fails when mTLS is enabled (#174)
* use config variable Signed-off-by: wfan <[email protected]> * add forceful shutdown logic Signed-off-by: wfan <[email protected]> * fix all shutdown Signed-off-by: wfan <[email protected]> * add comment Signed-off-by: wfan <[email protected]> * fix: server check with tls Signed-off-by: fum1h1to <[email protected]> * fix: check mtls Signed-off-by: fum1h1to <[email protected]> * fix: comment Signed-off-by: fum1h1to <[email protected]> * fix: variable name Signed-off-by: fum1h1to <[email protected]> * refactor Signed-off-by: fum1h1to <[email protected]> * refactor Signed-off-by: fum1h1to <[email protected]> Signed-off-by: wfan <[email protected]> --------- Signed-off-by: wfan <[email protected]> Signed-off-by: fum1h1to <[email protected]> Signed-off-by: Windz <[email protected]> Co-authored-by: wfan <[email protected]> Co-authored-by: Windz <[email protected]>
1 parent 3d8691d commit f20c8ff

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

pkg/daemon/httpchecker.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package daemon
1616

1717
import (
1818
"crypto/tls"
19+
"errors"
1920
"net/http"
2021
"time"
2122

@@ -25,27 +26,36 @@ import (
2526
)
2627

2728
// WaitForServerReady waits until the HTTP(S) server can respond to a GET request. Should NOT allow cancelling the retry as shuting down non-ready server may cause deadlock.
28-
func WaitForServerReady(serverAddr string, insecureSkipVerify bool) error {
29+
func WaitForServerReady(serverAddr string, insecureSkipVerify bool, clientCertEnabled bool) error {
2930

3031
t := http.DefaultTransport.(*http.Transport).Clone()
3132
t.TLSClientConfig = &tls.Config{}
3233
client := &http.Client{Transport: t}
3334

34-
var url string
35+
var targetUrl string
3536
if insecureSkipVerify {
3637
t.TLSClientConfig.InsecureSkipVerify = true
37-
url = "https://" + serverAddr
38+
targetUrl = "https://" + serverAddr
3839
} else {
39-
url = "http://" + serverAddr
40+
targetUrl = "http://" + serverAddr
4041
}
4142

4243
get := func() error {
43-
resp, err := client.Get(url)
44-
if err == nil {
45-
resp.Body.Close()
46-
log.Debugf("Server started at %s", url)
44+
resp, err := client.Get(targetUrl)
45+
if err != nil {
46+
// if client certificate disabled, return ALL errors.
47+
// if client certificate enabled, return ALL errors but exclude client certificate verification error.
48+
errCause := errors.Unwrap(err)
49+
if !clientCertEnabled || errCause == nil || errCause.Error() != "remote error: tls: certificate required" {
50+
return err
51+
}
52+
log.Debugf("Server started at %s (can response certificate required error)", targetUrl)
53+
return nil
4754
}
48-
return err
55+
56+
resp.Body.Close()
57+
log.Debugf("Server started at %s", targetUrl)
58+
return nil
4959
}
5060

5161
getExponentialBackoff := func() backoff.BackOff {

pkg/healthcheck/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (hs *hcService) Start(ctx context.Context) error {
8484
log.Info("Stopped health check server")
8585
}()
8686

87-
if err := daemon.WaitForServerReady(hs.hcServer.Addr, false); err != nil {
87+
if err := daemon.WaitForServerReady(hs.hcServer.Addr, false, false); err != nil {
8888
log.Errorf("Failed to confirm health check server ready: %s", err.Error())
8989
return err
9090
}

pkg/metrics/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (ms *metricsService) Start(ctx context.Context) error {
125125
log.Info("Stopped metrics exporter server")
126126
}()
127127

128-
if err := daemon.WaitForServerReady(ms.exporter.ListenAddress, false); err != nil {
128+
if err := daemon.WaitForServerReady(ms.exporter.ListenAddress, false, false); err != nil {
129129
log.Errorf("Failed to confirm metrics exporter server ready: %s", err.Error())
130130
return err
131131
}

pkg/token/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (ts *tokenService) Start(ctx context.Context) error {
190190
log.Info("Stopped token provider server")
191191
}()
192192

193-
if err := daemon.WaitForServerReady(ts.tokenServer.Addr, ts.idCfg.TokenServer.TLS.Use); err != nil {
193+
if err := daemon.WaitForServerReady(ts.tokenServer.Addr, ts.idCfg.TokenServer.TLS.Use, ts.idCfg.TokenServer.TLS.CAPath != ""); err != nil {
194194
log.Errorf("Failed to confirm token provider server ready: %s", err.Error())
195195
return err
196196
}

0 commit comments

Comments
 (0)