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

[http] Add unix socket option #2764

Merged
42 changes: 25 additions & 17 deletions cmd/notification-http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
"crypto/x509"
"fmt"
"io"
"net"
"net/http"
"os"
"strings"

"github.com/crowdsecurity/crowdsec/pkg/protobufs"
"github.com/hashicorp/go-hclog"
Expand All @@ -19,6 +21,7 @@
type PluginConfig struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
UnixSocket string `yaml:"unix_socket"`
Headers map[string]string `yaml:"headers"`
SkipTLSVerification bool `yaml:"skip_tls_verification"`
Method string `yaml:"method"`
Expand Down Expand Up @@ -66,36 +69,40 @@
return cp, nil
}

func getTLSClient(tlsVerify bool, caPath, certPath, keyPath string) (*http.Client, error) {
var client *http.Client

caCertPool, err := getCertPool(caPath)
func getTLSClient(c *PluginConfig) error {
caCertPool, err := getCertPool(c.CAPath)

Check warning on line 73 in cmd/notification-http/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/notification-http/main.go#L72-L73

Added lines #L72 - L73 were not covered by tests
if err != nil {
return nil, err
return err

Check warning on line 75 in cmd/notification-http/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/notification-http/main.go#L75

Added line #L75 was not covered by tests
}

tlsConfig := &tls.Config{
RootCAs: caCertPool,
InsecureSkipVerify: tlsVerify,
InsecureSkipVerify: c.SkipTLSVerification,

Check warning on line 80 in cmd/notification-http/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/notification-http/main.go#L80

Added line #L80 was not covered by tests
}

if certPath != "" && keyPath != "" {
logger.Info(fmt.Sprintf("Using client certificate '%s' and key '%s'", certPath, keyPath))
if c.CertPath != "" && c.KeyPath != "" {
logger.Info(fmt.Sprintf("Using client certificate '%s' and key '%s'", c.CertPath, c.KeyPath))

Check warning on line 84 in cmd/notification-http/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/notification-http/main.go#L83-L84

Added lines #L83 - L84 were not covered by tests

cert, err := tls.LoadX509KeyPair(certPath, keyPath)
cert, err := tls.LoadX509KeyPair(c.CertPath, c.KeyPath)

Check warning on line 86 in cmd/notification-http/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/notification-http/main.go#L86

Added line #L86 was not covered by tests
if err != nil {
return nil, fmt.Errorf("unable to load client certificate '%s' and key '%s': %w", certPath, keyPath, err)
return fmt.Errorf("unable to load client certificate '%s' and key '%s': %w", c.CertPath, c.KeyPath, err)

Check warning on line 88 in cmd/notification-http/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/notification-http/main.go#L88

Added line #L88 was not covered by tests
}

tlsConfig.Certificates = []tls.Certificate{cert}
}

client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
transport := &http.Transport{
TLSClientConfig: tlsConfig,
}
if c.UnixSocket != "" {
logger.Info(fmt.Sprintf("Using socket '%s'", c.UnixSocket))
transport.DialContext = func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", strings.TrimSuffix(c.UnixSocket, "/"))
}

Check warning on line 100 in cmd/notification-http/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/notification-http/main.go#L93-L100

Added lines #L93 - L100 were not covered by tests
}
c.Client = &http.Client{
Transport: transport,

Check warning on line 103 in cmd/notification-http/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/notification-http/main.go#L102-L103

Added lines #L102 - L103 were not covered by tests
}
return client, err
return nil

Check warning on line 105 in cmd/notification-http/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/notification-http/main.go#L105

Added line #L105 was not covered by tests
}

func (s *HTTPPlugin) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
Expand Down Expand Up @@ -135,6 +142,7 @@

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
logger.Warn(fmt.Sprintf("HTTP server returned non 200 status code: %d", resp.StatusCode))
logger.Debug(fmt.Sprintf("HTTP server returned body: %s", string(respData)))

Check warning on line 145 in cmd/notification-http/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/notification-http/main.go#L145

Added line #L145 was not covered by tests
return &protobufs.Empty{}, nil
}

Expand All @@ -147,7 +155,7 @@
if err != nil {
return nil, err
}
d.Client, err = getTLSClient(d.SkipTLSVerification, d.CAPath, d.CertPath, d.KeyPath)
err = getTLSClient(&d)

Check warning on line 158 in cmd/notification-http/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/notification-http/main.go#L158

Added line #L158 was not covered by tests
if err != nil {
return nil, err
}
Expand Down
Loading