1
0
Fork 0
mirror of https://github.com/TwiN/gatus.git synced 2024-12-14 11:58:04 +00:00

Close #14: Support skipping certificate verification (services[].insecure)

This commit is contained in:
TwinProduction 2020-10-04 17:01:10 -04:00
parent ed490669b1
commit 6a3f65db7f
5 changed files with 46 additions and 13 deletions

View file

@ -84,6 +84,7 @@ Note that you can also add environment variables in the configuration file (i.e.
| `services[].name` | Name of the service. Can be anything. | Required `""` | | `services[].name` | Name of the service. Can be anything. | Required `""` |
| `services[].url` | URL to send the request to | Required `""` | | `services[].url` | URL to send the request to | Required `""` |
| `services[].conditions` | Conditions used to determine the health of the service | `[]` | | `services[].conditions` | Conditions used to determine the health of the service | `[]` |
| `services[].insecure` | Whether to skip verifying the server's certificate chain and host name | `false` |
| `services[].interval` | Duration to wait between every status check | `60s` | | `services[].interval` | Duration to wait between every status check | `60s` |
| `services[].method` | Request method | `GET` | | `services[].method` | Request method | `GET` |
| `services[].graphql` | Whether to wrap the body in a query param (`{"query":"$body"}`) | `false` | | `services[].graphql` | Whether to wrap the body in a query param (`{"query":"$body"}`) | `false` |

View file

@ -73,7 +73,7 @@ func (provider *AlertProvider) buildRequest(serviceName, alertDescription string
// Send a request to the alert provider and return the body // Send a request to the alert provider and return the body
func (provider *AlertProvider) Send(serviceName, alertDescription string, resolved bool) ([]byte, error) { func (provider *AlertProvider) Send(serviceName, alertDescription string, resolved bool) ([]byte, error) {
request := provider.buildRequest(serviceName, alertDescription, resolved) request := provider.buildRequest(serviceName, alertDescription, resolved)
response, err := client.GetHttpClient().Do(request) response, err := client.GetHttpClient(false).Do(request)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -1,19 +1,35 @@
package client package client
import ( import (
"crypto/tls"
"net/http" "net/http"
"time" "time"
) )
var ( var (
client *http.Client secureHttpClient *http.Client
insecureHttpClient *http.Client
) )
func GetHttpClient() *http.Client { func GetHttpClient(insecure bool) *http.Client {
if client == nil { if insecure {
client = &http.Client{ if insecureHttpClient == nil {
Timeout: time.Second * 10, insecureHttpClient = &http.Client{
Timeout: time.Second * 10,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
} }
return insecureHttpClient
} else {
if secureHttpClient == nil {
secureHttpClient = &http.Client{
Timeout: time.Second * 10,
}
}
return secureHttpClient
} }
return client
} }

View file

@ -3,11 +3,24 @@ package client
import "testing" import "testing"
func TestGetHttpClient(t *testing.T) { func TestGetHttpClient(t *testing.T) {
if client != nil { if secureHttpClient != nil {
t.Error("client should've been nil since it hasn't been called a single time yet") t.Error("secureHttpClient should've been nil since it hasn't been called a single time yet")
} }
_ = GetHttpClient() if insecureHttpClient != nil {
if client == nil { t.Error("insecureHttpClient should've been nil since it hasn't been called a single time yet")
t.Error("client shouldn't have been nil, since it has been called once") }
_ = GetHttpClient(false)
if secureHttpClient == nil {
t.Error("secureHttpClient shouldn't have been nil, since it has been called once")
}
if insecureHttpClient != nil {
t.Error("insecureHttpClient should've been nil since it hasn't been called a single time yet")
}
_ = GetHttpClient(true)
if secureHttpClient == nil {
t.Error("secureHttpClient shouldn't have been nil, since it has been called once")
}
if insecureHttpClient == nil {
t.Error("insecureHttpClient shouldn't have been nil, since it has been called once")
} }
} }

View file

@ -46,6 +46,9 @@ type Service struct {
// Alerts is the alerting configuration for the service in case of failure // Alerts is the alerting configuration for the service in case of failure
Alerts []*Alert `yaml:"alerts"` Alerts []*Alert `yaml:"alerts"`
// Insecure is whether to skip verifying the server's certificate chain and host name
Insecure bool `yaml:"insecure,omitempty"`
NumberOfFailuresInARow int NumberOfFailuresInARow int
NumberOfSuccessesInARow int NumberOfSuccessesInARow int
} }
@ -135,7 +138,7 @@ func (service *Service) getIp(result *Result) {
func (service *Service) call(result *Result) { func (service *Service) call(result *Result) {
request := service.buildRequest() request := service.buildRequest()
startTime := time.Now() startTime := time.Now()
response, err := client.GetHttpClient().Do(request) response, err := client.GetHttpClient(service.Insecure).Do(request)
if err != nil { if err != nil {
result.Duration = time.Since(startTime) result.Duration = time.Since(startTime)
result.Errors = append(result.Errors, err.Error()) result.Errors = append(result.Errors, err.Error())