From a9bb3ff0ad7d67fe647fa6ca51fb1ad1dee5ef54 Mon Sep 17 00:00:00 2001 From: Daniel Czerwonk Date: Fri, 22 Oct 2021 09:49:21 +0200 Subject: [PATCH] allow requests with self signed certs --- cmd/http-check-server/main.go | 5 ++-- cmd/http-check/main.go | 2 ++ pb/service.pb.go | 53 ++++++++++++++++++++--------------- pb/service.proto | 1 + pkg/check/check.go | 7 +++++ pkg/server/server.go | 46 +++++++++++++++++------------- pkg/server/worker.go | 19 ++++++++++--- 7 files changed, 86 insertions(+), 47 deletions(-) diff --git a/cmd/http-check-server/main.go b/cmd/http-check-server/main.go index b609185..b8b3197 100644 --- a/cmd/http-check-server/main.go +++ b/cmd/http-check-server/main.go @@ -24,7 +24,8 @@ const ( var ( showVersion = kingpin.Flag("version", "Show version info").Bool() workerCount = kingpin.Flag("worker-count", "Number of workers processing http checks in parallel").Default("25").Uint32() - timeout = kingpin.Flag("timeout", "Timeout after a connection attempt will be cancelled").Default("10s").Duration() + timeout = kingpin.Flag("timeout", "Request timeout").Default("10s").Duration() + tlsTimeout = kingpin.Flag("tls-timeout", "TLS connect timeout").Default("1s").Duration() socketPath = kingpin.Flag("socket-path", "Socket to create to listen for check requests").Default("/tmp/http-check.sock").String() ) @@ -44,7 +45,7 @@ func main() { srv := grpc.NewServer() logrus.Infof("Starting %d workers", *workerCount) - s := server.New(*workerCount, server.WithTimeout(*timeout)) + s := server.New(*workerCount, *timeout, *tlsTimeout) pb.RegisterHttpCheckServiceServer(srv, s) logrus.Infof("Listen for connections on socket %s", *socketPath) diff --git a/cmd/http-check/main.go b/cmd/http-check/main.go index 4cc287b..20c7d1f 100644 --- a/cmd/http-check/main.go +++ b/cmd/http-check/main.go @@ -30,6 +30,7 @@ var ( expectedBodyRegex = kingpin.Flag("expect-body-regex", "Expected regex matching string in response body").Short('r').String() certExpireDays = kingpin.Flag("cert-min-expire-days", "Minimum number of days until certificate expiration").Uint32() socketPath = kingpin.Flag("socket-path", "Socket to use to communicate with the server performing the check").Default("/tmp/http-check.sock").String() + insecure = kingpin.Flag("insecure", "Allow invalid TLS certificaets (e.g. self signed)").Default("false").Bool() ) func main() { @@ -68,6 +69,7 @@ func runCheck() { ExpectedBodyRegex: *expectedBodyRegex, CertExpireDays: *certExpireDays, Debug: *verbose, + Insecure: *insecure, } resp, err := c.Check(context.Background(), req) if err != nil { diff --git a/pb/service.pb.go b/pb/service.pb.go index 399add8..7e22575 100644 --- a/pb/service.pb.go +++ b/pb/service.pb.go @@ -35,6 +35,7 @@ type Request struct { ExpectedBodyRegex string `protobuf:"bytes,8,opt,name=expected_body_regex,json=expectedBodyRegex,proto3" json:"expected_body_regex,omitempty"` CertExpireDays uint32 `protobuf:"varint,9,opt,name=cert_expire_days,json=certExpireDays,proto3" json:"cert_expire_days,omitempty"` Debug bool `protobuf:"varint,10,opt,name=debug,proto3" json:"debug,omitempty"` + Insecure bool `protobuf:"varint,11,opt,name=insecure,proto3" json:"insecure,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -135,6 +136,13 @@ func (m *Request) GetDebug() bool { return false } +func (m *Request) GetInsecure() bool { + if m != nil { + return m.Insecure + } + return false +} + type Response struct { Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"` Message string `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"` @@ -198,28 +206,29 @@ func init() { func init() { proto.RegisterFile("service.proto", fileDescriptor_a0b84a42fa06f626) } var fileDescriptor_a0b84a42fa06f626 = []byte{ - // 332 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x54, 0x91, 0xcf, 0x4e, 0xe3, 0x30, - 0x10, 0xc6, 0x37, 0xfd, 0x9b, 0xce, 0x36, 0xab, 0xae, 0xb7, 0x07, 0xab, 0xa7, 0xa8, 0xcb, 0x21, - 0xa7, 0x08, 0xc1, 0x85,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1, 0x59, 0x1a, 0x65, 0x89, 0xf8, 0xe3, - 0xfc, 0xa3, 0xd7, 0x0f, 0x65, 0x4b, 0x6c, 0x09, 0x63, 0x85, 0x55, 0xb3, 0xe3, 0x90, 0x46, 0x59, - 0x2c, 0x02, 0xac, 0x25, 0xc4, 0x02, 0xc9, 0xe8, 0x13, 0x21, 0xe3, 0x30, 0xa5, 0x46, 0x4a, 0x24, - 0xf2, 0xdb, 0x8c, 0x45, 0x8f, 0x2e, 0x39, 0x22, 0x51, 0xb9, 0xc3, 0x6e, 0x9f, 0x3d, 0xba, 0x8f, - 0xf2, 0x45, 0x45, 0x9f, 0x87, 0xdd, 0xce, 0xbd, 0x7c, 0x0e, 0xee, 0xe6, 0x0e, 0x16, 0x4f, 0xd6, - 0x9a, 0xcd, 0x1e, 0xe5, 0xcb, 0x36, 0x5c, 0x94, 0x5d, 0xc1, 0xd8, 0x33, 0xfb, 0x9d, 0x9b, 0x2a, - 0xef, 0x2e, 0xba, 0x9a, 0x07, 0x08, 0x7f, 0x68, 0xfd, 0xab, 0x9a, 0xf8, 0x7b, 0xde, 0x7e, 0x05, - 0x00, 0x00, 0xff, 0xff, 0x95, 0x30, 0x3b, 0xfe, 0x09, 0x02, 0x00, 0x00, + // 341 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x54, 0x91, 0xbf, 0x4e, 0xc3, 0x30, + 0x10, 0xc6, 0x49, 0xff, 0xa6, 0x6e, 0x83, 0x8a, 0xe9, 0x60, 0x75, 0x8a, 0x0a, 0x43, 0xa6, 0x08, + 0xc1, 0xc2, 0x4c, 0x41, 0x62, 0x61, 0x71, 0x1f, 0x20, 0x4a, 0xec, 0x53, 0x5b, 0x41, 0x6b, 0xe3, + 0x73, 0xa0, 0x79, 0x63, 0x1e, 0x03, 0xd9, 0x4e, 0x2a, 0xd8, 0xee, 0xf7, 0x7d, 0x97, 0x2f, 0xbe, + 0x3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 0x06, 0xd8, 0xd4, 0x1b, 0x67, 0x5e, 0x09, 0x12, 0x73, 0x40, + 0xad, 0x8e, 0x08, 0x94, 0x91, 0x31, 0xd6, 0x42, 0x00, 0xa2, 0xdf, 0x74, 0xcc, 0x3b, 0x74, 0xce, + 0x01, 0x10, 0xcb, 0x2d, 0xb4, 0xbb, 0xee, 0xd0, 0x0d, 0xec, 0x7f, 0x52, 0x74, 0x7e, 0xd8, 0xfb, + 0xcc, 0x8b, 0x6f, 0x41, 0xbb, 0x7f, 0x24, 0xf3, 0x57, 0x6b, 0xf5, 0x7a, 0x07, 0xe2, 0x7d, 0x13, + 0xae, 0x4d, 0x6f, 0xc9, 0xd0, 0x33, 0x9d, 0xe6, 0xba, 0xca, 0xdb, 0x6b, 0x2f, 0x67, 0x01, 0xc2, + 0x83, 0x56, 0x17, 0xd5, 0xc8, 0xdf, 0xfa, 0xe1, 0x37, 0x00, 0x00, 0xff, 0xff, 0x5a, 0xf5, 0xc0, + 0xb0, 0x25, 0x02, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. diff --git a/pb/service.proto b/pb/service.proto index 691ba49..f36bd8f 100644 --- a/pb/service.proto +++ b/pb/service.proto @@ -13,6 +13,7 @@ message Request { string expected_body_regex = 8; uint32 cert_expire_days = 9; bool debug = 10; + bool insecure = 11; } message Response { diff --git a/pkg/check/check.go b/pkg/check/check.go index bf321b0..6fb9094 100644 --- a/pkg/check/check.go +++ b/pkg/check/check.go @@ -31,6 +31,12 @@ func WithDebug(w io.Writer) Option { } } +func WithInsecure() Option { + return func(c *Check) { + c.insecure = true + } +} + // Check executes a web request and validates the response against a set of defined assertions type Check struct { client *http.Client @@ -39,6 +45,7 @@ type Check struct { password string assertions []assertion debug bool + insecure bool debugWriter io.Writer } diff --git a/pkg/server/server.go b/pkg/server/server.go index 310ad60..5a0ad8a 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -2,6 +2,8 @@ package server import ( "context" + "crypto/tls" + "net" "net/http" "time" @@ -10,32 +12,21 @@ import ( // HTTPCheckServer runs HTTP checks. It provides an gRPC interface to receive check tasks type HTTPCheckServer struct { - cl *http.Client workerCount uint32 + reqTimeout time.Duration + tlsTimeout time.Duration ch chan *task } -// Option specifies options for the server -type Option func(*HTTPCheckServer) - -// WithTimeout specifies the timeout for each HTTP request -func WithTimeout(t time.Duration) Option { - return func(s *HTTPCheckServer) { - s.cl.Timeout = t - } -} - // New creates a new server instance -func New(workerCount uint32, opts ...Option) *HTTPCheckServer { +func New(workerCount uint32, reqTimeout, tlsTimeout time.Duration) *HTTPCheckServer { s := &HTTPCheckServer{ - cl: &http.Client{}, workerCount: workerCount, + reqTimeout: reqTimeout, + tlsTimeout: tlsTimeout, ch: make(chan *task), } - for _, opt := range opts { - opt(s) - } s.startWorkers() return s @@ -44,14 +35,31 @@ func New(workerCount uint32, opts ...Option) *HTTPCheckServer { func (s *HTTPCheckServer) startWorkers() { for i := 0; i < int(s.workerCount); i++ { w := &worker{ - id: i + 1, - cl: s.cl, - ch: s.ch, + id: i + 1, + cl: s.newHttpClient(false), + insecureCl: s.newHttpClient(true), + ch: s.ch, } go w.run() } } +func (s *HTTPCheckServer) newHttpClient(insecure bool) *http.Client { + var tr = &http.Transport{ + Dial: (&net.Dialer{ + Timeout: s.reqTimeout, + }).Dial, + TLSHandshakeTimeout: s.tlsTimeout, + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: insecure, + }, + } + + return &http.Client{ + Transport: tr, + } +} + // Check performs a http check and returns the check result func (s *HTTPCheckServer) Check(ctx context.Context, in *pb.Request) (*pb.Response, error) { respCh := make(chan *pb.Response, 1) diff --git a/pkg/server/worker.go b/pkg/server/worker.go index 4c44bc1..01f81f3 100644 --- a/pkg/server/worker.go +++ b/pkg/server/worker.go @@ -18,9 +18,10 @@ type task struct { } type worker struct { - id int - cl *http.Client - ch chan *task + id int + cl *http.Client + insecureCl *http.Client + ch chan *task } func (w *worker) run() { @@ -64,8 +65,18 @@ func (w *worker) checkForRequest(req *pb.Request, out io.Writer) *check.Check { opts = append(opts, check.WithDebug(out)) } + if req.Insecure { + opts = append(opts, check.WithInsecure()) + } + url := fmt.Sprintf("%s://%s%s", req.Protocol, req.Host, req.Path) - c := check.NewCheck(w.cl, url, opts...) + + cl := w.cl + if req.Insecure { + cl = w.insecureCl + } + + c := check.NewCheck(cl, url, opts...) if len(req.ExpectedStatusCode) > 0 { c.AssertStatusCodeIn(req.ExpectedStatusCode)