diff --git a/README.md b/README.md index 4161d62..9f662fd 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,9 @@ go get github.com/netboxlabs/diode-sdk-go ### Example -* `target` should be the address of the Diode service, e.g. `grpc://localhost:8080/diode` for insecure connection - or `grpcs://example.com` for secure connection. +* `target` should be the address of the Diode service. + * Insecure connections: `grpc://localhost:8080/diode` or `http://localhost:8080/diode` + * Secure connections: `grpcs://example.com` or `https://example.com` ```go package main diff --git a/diode/client.go b/diode/client.go index 4e4dd7c..db1a58c 100644 --- a/diode/client.go +++ b/diode/client.go @@ -54,7 +54,12 @@ const ( defaultStreamName = "latest" ) -var allowedSchemesRe = regexp.MustCompile(`grpc|grpcs`) +var ( + // ErrInvalidTargetScheme is returned when the target URL does not start with a valid scheme. + ErrInvalidTargetScheme = errors.New("target should start with grpc://, grpcs://, http:// or https://") + + allowedSchemesRe = regexp.MustCompile(`grpc|grpcs|http|https`) +) // loadCerts loads the system x509 cert pool func loadCerts() *x509.CertPool { @@ -70,12 +75,19 @@ func parseTarget(target string) (string, string, bool, error) { } if !allowedSchemesRe.MatchString(u.Scheme) { - return "", "", false, errors.New("target should start with grpc:// or grpcs://") + return "", "", false, ErrInvalidTargetScheme } authority := u.Host if u.Port() == "" { - authority += ":443" + switch u.Scheme { + case "grpc", "http": + authority += ":80" + case "grpcs", "https": + authority += ":443" + default: + return "", "", false, fmt.Errorf("missing port with unsupported scheme: %s: %w", u.Scheme, ErrInvalidTargetScheme) + } } path := u.Path @@ -83,7 +95,11 @@ func parseTarget(target string) (string, string, bool, error) { path = "" } - tlsVerify := u.Scheme == "grpcs" + tlsVerify := false + switch u.Scheme { + case "grpcs", "https": + tlsVerify = true + } return authority, path, tlsVerify, nil } diff --git a/diode/client_test.go b/diode/client_test.go index 5c9a607..b796a16 100644 --- a/diode/client_test.go +++ b/diode/client_test.go @@ -63,6 +63,20 @@ func TestParseTarget(t *testing.T) { tlsVerify: true, wantErr: nil, }, + { + desc: "valid HTTP target", + target: "http://localhost:8081", + authority: "localhost:8081", + tlsVerify: false, + wantErr: nil, + }, + { + desc: "valid HTTP target with tls", + target: "https://localhost:8081", + authority: "localhost:8081", + tlsVerify: true, + wantErr: nil, + }, { desc: "valid target empty path on grpc://localhost:8081/", target: "grpc://localhost:8081/", @@ -81,11 +95,11 @@ func TestParseTarget(t *testing.T) { }, { desc: "invalid scheme in target", - target: "http://localhost:8081", + target: "ftp://localhost:8081", authority: "", path: "", tlsVerify: false, - wantErr: errors.New("target should start with grpc:// or grpcs://"), + wantErr: ErrInvalidTargetScheme, }, { desc: "invalid target", @@ -317,14 +331,14 @@ func TestNewClient(t *testing.T) { }, { desc: "invalid target", - target: "http://localhost:8081", + target: "ftp://localhost:8081", appName: "my-producer", appVersion: "0.1.0", clientID: "client-id-123", clientSecret: "client-secret-456", clientIDEnvVarValue: "", clientSecretEnvVarValue: "", - wantErr: errors.New("target should start with grpc:// or grpcs://"), + wantErr: ErrInvalidTargetScheme, }, { desc: "missing clientID and clientSecret",