From f683c2a543f4fe59973f144defd43b60bcf1368a Mon Sep 17 00:00:00 2001 From: Ricardo Weir Date: Tue, 3 Sep 2024 11:28:20 -0700 Subject: [PATCH 1/2] Reject invalid endpoint 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 --- pkg/endpoint/endpoint.go | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/pkg/endpoint/endpoint.go b/pkg/endpoint/endpoint.go index 81dedecd..ac9c9dba 100644 --- a/pkg/endpoint/endpoint.go +++ b/pkg/endpoint/endpoint.go @@ -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, ","), @@ -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 +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 ://") } // networkAndAddress crudely splits a URL string into network (scheme) and address, From 40b2102956596ac0d41020ad7f1ff5a630dab62c Mon Sep 17 00:00:00 2001 From: Ricardo Weir Date: Tue, 3 Sep 2024 14:15:58 -0700 Subject: [PATCH 2/2] Update sqlite test Signed-off-by: Ricardo Weir --- scripts/test-helpers | 2 +- scripts/test-run-sqlite | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/test-helpers b/scripts/test-helpers index 500e4260..7e293eb6 100755 --- a/scripts/test-helpers +++ b/scripts/test-helpers @@ -251,7 +251,7 @@ provision-kine() { $KINE_ENV \ $KINE_IMAGE \ --watch-progress-notify-interval=5s \ - --endpoint $KINE_ENDPOINT + --endpoint=$KINE_ENDPOINT local ip=$(docker container inspect --format '{{.NetworkSettings.IPAddress}}' $name | tee $TEST_DIR/kine/$count/metadata/ip) local port=$(docker container inspect --format '{{range $k, $v := .NetworkSettings.Ports}}{{printf "%s\n" $k}}{{end}}' $name | head -n 1 | cut -d/ -f1 | tee $TEST_DIR/kine/$count/metadata/port) diff --git a/scripts/test-run-sqlite b/scripts/test-run-sqlite index ead033fa..b8c4b6da 100755 --- a/scripts/test-run-sqlite +++ b/scripts/test-run-sqlite @@ -1,6 +1,6 @@ #!/bin/bash start-test() { - KINE_IMAGE=$IMAGE KINE_ENDPOINT="sqlite" provision-kine + KINE_IMAGE=$IMAGE KINE_ENDPOINT="" provision-kine local kine_url=$(cat $TEST_DIR/kine/*/metadata/url) K3S_DATASTORE_ENDPOINT=$kine_url provision-cluster }