Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 20 additions & 4 deletions diode/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -70,20 +75,31 @@ 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
if path == "/" {
path = ""
}

tlsVerify := u.Scheme == "grpcs"
tlsVerify := false
switch u.Scheme {
case "grpcs", "https":
tlsVerify = true
}

return authority, path, tlsVerify, nil
}
Expand Down
22 changes: 18 additions & 4 deletions diode/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Loading