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

Add TCP+TLS Healthchecks #18381

Merged
merged 36 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6ff0e7d
Begin adding TCPUseTLS
pgporada Aug 2, 2023
c6d8eaa
More TCP with TLS plumbing
pgporada Aug 3, 2023
729f671
Making forward progress
pgporada Aug 3, 2023
a382b2f
Keep on adding TCP+TLS support for healthchecks
pgporada Aug 4, 2023
14a0d49
Removed too many lines
pgporada Aug 4, 2023
8ee986c
Unit tests for TCP+TLS
pgporada Aug 11, 2023
7c7db53
Update tlsutil/config.go
pgporada Aug 11, 2023
5b0dfc6
Working on the tcp+tls unit test
pgporada Aug 15, 2023
9620347
Updated the runtime integration tests
pgporada Aug 15, 2023
c76008b
Progress
pgporada Aug 15, 2023
562c0bc
Revert this file back to HEAD
pgporada Aug 15, 2023
a11c310
Remove debugging lines
pgporada Aug 16, 2023
9e2012c
Implement TLS enabled TCP socket server and make a successful TCP+TLS…
pgporada Aug 16, 2023
fd9f37c
Merge branch 'main' into healthcheck-tls
pgporada Aug 16, 2023
dafbf48
Update docs
pgporada Aug 16, 2023
c76e1b9
Merge branch 'healthcheck-tls' of github.com:pgporada/consul into hea…
pgporada Aug 16, 2023
41e572d
Merge branch 'main' into healthcheck-tls
pgporada Aug 16, 2023
af6ba92
Update agent/agent_test.go
pgporada Aug 16, 2023
8a2f5ed
Update website/content/docs/ecs/configuration-reference.mdx
pgporada Aug 16, 2023
f786916
Update website/content/docs/ecs/configuration-reference.mdx
pgporada Aug 16, 2023
4395cd1
Update agent/checks/check.go
pgporada Aug 16, 2023
2864f24
Address comments
pgporada Aug 16, 2023
eb72235
Remove extraneous bracket
pgporada Aug 16, 2023
67c60ee
Update agent/agent_test.go
pgporada Aug 16, 2023
4ca7dcc
Update agent/agent_test.go
pgporada Aug 16, 2023
8e8b035
Update website/content/docs/ecs/configuration-reference.mdx
pgporada Aug 16, 2023
9c53567
Update the mockTLSServer
pgporada Aug 16, 2023
67d5add
Remove trailing newline
pgporada Aug 16, 2023
576bcc4
Merge branch 'main' into healthcheck-tls
pgporada Aug 21, 2023
0705235
Address comments
pgporada Aug 21, 2023
b73cec2
Merge branch 'healthcheck-tls' of github.com:pgporada/consul into hea…
pgporada Aug 21, 2023
7d4b533
Merge branch 'main' into healthcheck-tls
pgporada Aug 31, 2023
97ab75f
Fix merge problem
pgporada Aug 31, 2023
dad8aca
Add changelog entry
pgporada Sep 1, 2023
1e4fa20
Merge branch 'main' into healthcheck-tls
pgporada Sep 1, 2023
116be8b
Merge branch 'main' into healthcheck-tls
pgporada Sep 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3021,14 +3021,20 @@ func (a *Agent) addCheck(check *structs.HealthCheck, chkType *structs.CheckType,
chkType.Interval = checks.MinInterval
}

var tlsClientConfig *tls.Config
if chkType.TCPUseTLS {
tlsClientConfig = a.tlsConfigurator.OutgoingTLSConfigForCheck(chkType.TLSSkipVerify, chkType.TLSServerName)
}

tcp := &checks.CheckTCP{
CheckID: cid,
ServiceID: sid,
TCP: chkType.TCP,
Interval: chkType.Interval,
Timeout: chkType.Timeout,
Logger: a.logger,
StatusHandler: statusHandler,
CheckID: cid,
ServiceID: sid,
TCP: chkType.TCP,
Interval: chkType.Interval,
Timeout: chkType.Timeout,
Logger: a.logger,
TLSClientConfig: tlsClientConfig,
StatusHandler: statusHandler,
}
tcp.Start()
a.checkTCPs[cid] = tcp
Expand Down
49 changes: 32 additions & 17 deletions agent/checks/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,13 +631,14 @@ func (c *CheckH2PING) Start() {
// The check is critical if the connection returns an error
// Supports failures_before_critical and success_before_passing.
type CheckTCP struct {
CheckID structs.CheckID
ServiceID structs.ServiceID
TCP string
Interval time.Duration
Timeout time.Duration
Logger hclog.Logger
StatusHandler *StatusHandler
CheckID structs.CheckID
ServiceID structs.ServiceID
TCP string
Interval time.Duration
Timeout time.Duration
Logger hclog.Logger
TLSClientConfig *tls.Config
StatusHandler *StatusHandler

dialer *net.Dialer
stop bool
Expand Down Expand Up @@ -694,17 +695,31 @@ func (c *CheckTCP) run() {

// check is invoked periodically to perform the TCP check
func (c *CheckTCP) check() {
conn, err := c.dialer.Dial(`tcp`, c.TCP)
pgporada marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
c.Logger.Warn("Check socket connection failed",
"check", c.CheckID.String(),
"error", err,
)
c.StatusHandler.updateCheck(c.CheckID, api.HealthCritical, err.Error())
return
logAndUpdate := func(checkType string, err error) {
if err != nil {
c.Logger.Warn(fmt.Sprintf("Check %s socket connection failed", checkType),
"check", c.CheckID.String(),
"error", err,
)
c.StatusHandler.updateCheck(c.CheckID, api.HealthCritical, err.Error())
return
}
c.StatusHandler.updateCheck(c.CheckID, api.HealthPassing, fmt.Sprintf("%s connect %s: Success", checkType, c.TCP))
}

if c.TLSClientConfig != nil {
tlsConn, err := tls.DialWithDialer(c.dialer, `tcp`, c.TCP, c.TLSClientConfig)
logAndUpdate("TCP+TLS", err)
if err == nil {
defer tlsConn.Close()
}
} else {
conn, err := c.dialer.Dial(`tcp`, c.TCP)
logAndUpdate("TCP", err)
if err == nil {
defer conn.Close()
}
pgporada marked this conversation as resolved.
Show resolved Hide resolved
}
conn.Close()
c.StatusHandler.updateCheck(c.CheckID, api.HealthPassing, fmt.Sprintf("TCP connect %s: Success", c.TCP))
}

// CheckUDP is used to periodically send a UDP datagram to determine the health of a given check.
Expand Down
1 change: 1 addition & 0 deletions agent/config/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,7 @@ func (b *builder) checkVal(v *CheckDefinition) *structs.CheckDefinition {
Body: stringVal(v.Body),
DisableRedirects: boolVal(v.DisableRedirects),
TCP: stringVal(v.TCP),
TCPUseTLS: boolVal(v.TCPUseTLS),
UDP: stringVal(v.UDP),
Interval: b.durationVal(fmt.Sprintf("check[%s].interval", id), v.Interval),
DockerContainerID: stringVal(v.DockerContainerID),
Expand Down
1 change: 1 addition & 0 deletions agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ type CheckDefinition struct {
DisableRedirects *bool `mapstructure:"disable_redirects"`
OutputMaxSize *int `mapstructure:"output_max_size"`
TCP *string `mapstructure:"tcp"`
TCPUseTLS *bool `mapstructure:"tcp_use_tls"`
UDP *string `mapstructure:"udp"`
Interval *string `mapstructure:"interval"`
DockerContainerID *string `mapstructure:"docker_container_id" alias:"dockercontainerid"`
Expand Down
6 changes: 6 additions & 0 deletions agent/structs/check_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type CheckDefinition struct {
Body string
DisableRedirects bool
TCP string
TCPUseTLS bool
UDP string
Interval time.Duration
DockerContainerID string
Expand Down Expand Up @@ -76,6 +77,7 @@ func (t *CheckDefinition) UnmarshalJSON(data []byte) (err error) {
DockerContainerIDSnake string `json:"docker_container_id"`
TLSServerNameSnake string `json:"tls_server_name"`
TLSSkipVerifySnake bool `json:"tls_skip_verify"`
TCPUseTLSSnake bool `json:"tcp_use_tls"`
GRPCUseTLSSnake bool `json:"grpc_use_tls"`
ServiceIDSnake string `json:"service_id"`
H2PingUseTLSSnake bool `json:"h2ping_use_tls"`
Expand Down Expand Up @@ -119,6 +121,9 @@ func (t *CheckDefinition) UnmarshalJSON(data []byte) (err error) {
if aux.TLSSkipVerifySnake {
t.TLSSkipVerify = aux.TLSSkipVerifySnake
}
if aux.TCPUseTLSSnake {
t.TCPUseTLS = aux.TCPUseTLSSnake
}
if aux.GRPCUseTLSSnake {
t.GRPCUseTLS = aux.GRPCUseTLSSnake
}
Expand Down Expand Up @@ -220,6 +225,7 @@ func (c *CheckDefinition) CheckType() *CheckType {
DisableRedirects: c.DisableRedirects,
OutputMaxSize: c.OutputMaxSize,
TCP: c.TCP,
TCPUseTLS: c.TCPUseTLS,
UDP: c.UDP,
Interval: c.Interval,
DockerContainerID: c.DockerContainerID,
Expand Down
5 changes: 5 additions & 0 deletions agent/structs/check_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type CheckType struct {
Body string
DisableRedirects bool
TCP string
TCPUseTLS bool
UDP string
Interval time.Duration
AliasNode string
Expand Down Expand Up @@ -87,6 +88,7 @@ func (t *CheckType) UnmarshalJSON(data []byte) (err error) {
DockerContainerIDSnake string `json:"docker_container_id"`
TLSServerNameSnake string `json:"tls_server_name"`
TLSSkipVerifySnake bool `json:"tls_skip_verify"`
TCPUseTLSSnake bool `json:"tcp_use_tls"`
GRPCUseTLSSnake bool `json:"grpc_use_tls"`
H2PingUseTLSSnake bool `json:"h2ping_use_tls"`

Expand Down Expand Up @@ -131,6 +133,9 @@ func (t *CheckType) UnmarshalJSON(data []byte) (err error) {
if aux.TLSSkipVerifySnake {
t.TLSSkipVerify = aux.TLSSkipVerifySnake
}
if aux.TCPUseTLSSnake {
t.TCPUseTLS = aux.TCPUseTLSSnake
}
if aux.GRPCUseTLSSnake {
t.GRPCUseTLS = aux.GRPCUseTLSSnake
}
Expand Down
2 changes: 2 additions & 0 deletions agent/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,7 @@ type HealthCheckDefinition struct {
Body string `json:",omitempty"`
DisableRedirects bool `json:",omitempty"`
TCP string `json:",omitempty"`
TCPUseTLS bool `json:",omitempty"`
UDP string `json:",omitempty"`
H2PING string `json:",omitempty"`
OSService string `json:",omitempty"`
Expand Down Expand Up @@ -2031,6 +2032,7 @@ func (c *HealthCheck) CheckType() *CheckType {
Body: c.Definition.Body,
DisableRedirects: c.Definition.DisableRedirects,
TCP: c.Definition.TCP,
TCPUseTLS: c.Definition.TCPUseTLS,
UDP: c.Definition.UDP,
H2PING: c.Definition.H2PING,
OSService: c.Definition.OSService,
Expand Down
1 change: 1 addition & 0 deletions agent/txn_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ func (s *HTTPHandlers) convertOps(resp http.ResponseWriter, req *http.Request) (
Method: check.Definition.Method,
Body: check.Definition.Body,
TCP: check.Definition.TCP,
TCPUseTLS: check.Definition.TCPUseTLS,
GRPC: check.Definition.GRPC,
GRPCUseTLS: check.Definition.GRPCUseTLS,
OSService: check.Definition.OSService,
Expand Down
1 change: 1 addition & 0 deletions api/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ type AgentServiceCheck struct {
Method string `json:",omitempty"`
Body string `json:",omitempty"`
TCP string `json:",omitempty"`
TCPUseTLS bool `json:",omitempty"`
UDP string `json:",omitempty"`
Status string `json:",omitempty"`
Notes string `json:",omitempty"`
Expand Down
1 change: 1 addition & 0 deletions api/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type HealthCheckDefinition struct {
TLSServerName string
TLSSkipVerify bool
TCP string
TCPUseTLS bool
UDP string
GRPC string
OSService string
Expand Down
4 changes: 4 additions & 0 deletions proto/private/pbservice/healthcheck.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading