Skip to content

Commit

Permalink
Reject invalid endpoint
Browse files Browse the repository at this point in the history
Prior, a non-empty endpoint of the incorrect format would cause an
embedded sqlite backend to be used. By passing a non-empty endpoint
the user is intending to use an external backend. Now, the endpoint
will be validated and passing a non-empty endpoint of an incorrect
format will result in an error.

Signed-off-by: Ricardo Weir <ricardo.weir@loft.sh>
  • Loading branch information
rmweir committed Sep 3, 2024
1 parent cb8c874 commit f683c2a
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions pkg/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ type ETCDConfig struct {
}

func Listen(ctx context.Context, config Config) (ETCDConfig, error) {
driver, dsn := ParseStorageEndpoint(config.Endpoint)
driver, dsn, err := ParseStorageEndpoint(config.Endpoint)
if err != nil {
return ETCDConfig{}, errors.Wrap(err, "parsing storage endpoint")
}

if driver == ETCDBackend {
return ETCDConfig{
Endpoints: strings.Split(config.Endpoint, ","),
Expand Down Expand Up @@ -233,19 +237,35 @@ func getKineStorageBackend(ctx context.Context, driver, dsn string, cfg Config)
}

// ParseStorageEndpoint returns the driver name and endpoint string from a datastore endpoint URL.
func ParseStorageEndpoint(storageEndpoint string) (string, string) {
func ParseStorageEndpoint(storageEndpoint string) (string, string, error) {
if storageEndpoint == "" {
return SQLiteBackend, "", nil
}

if err := validateDSNuri(storageEndpoint); err != nil {
return "", "", err
}

network, address := networkAndAddress(storageEndpoint)

switch network {
case "":
return SQLiteBackend, ""
case "nats":
return NATSBackend, storageEndpoint
return NATSBackend, storageEndpoint, nil
case "http":
fallthrough
case "https":
return ETCDBackend, address
return ETCDBackend, address, nil
}
return network, address, nil
}

// validateDSNuri ensure that the given string is of that format <scheme://<authority>
func validateDSNuri(str string) error {
parts := strings.SplitN(str, "://", 2)
if len(parts) > 1 {
return nil
}
return network, address
return errors.New("invalid datastore endpoint; endpoint should be a DSN URI in the format <scheme>://<authority>")
}

// networkAndAddress crudely splits a URL string into network (scheme) and address,
Expand Down

0 comments on commit f683c2a

Please sign in to comment.