From ffc52dfbaeb6924e19bc47b696496c8bd608624b Mon Sep 17 00:00:00 2001 From: Bruce MacDonald Date: Fri, 22 Oct 2021 11:04:33 -0400 Subject: [PATCH] Wait to add engine until it resolves in an external DNS lookup (#371) * Check DNS resolution in engine before creating destination * Update docker-desktop.yaml.in to log at the debug level * update docs --- .gitignore | 2 +- docker-desktop.yaml.in | 2 ++ docs/cli.md | 55 +++++++++++++++++++++++---------------- internal/cmd/list.go | 4 +-- internal/cmd/login.go | 6 ++--- internal/engine/engine.go | 21 +++++++++++++++ 6 files changed, 62 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index ec7bc18a5c..948de5a60a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ release .vscode # configuration file used during development -infra.yaml +docker-desktop.yaml diff --git a/docker-desktop.yaml.in b/docker-desktop.yaml.in index 33aa15dc66..222fe3fd02 100644 --- a/docker-desktop.yaml.in +++ b/docker-desktop.yaml.in @@ -1,3 +1,4 @@ +logLevel: debug image: tag: $IMAGE_TAG pullPolicy: Never @@ -20,3 +21,4 @@ engine: - name: https port: 8443 targetPort: 443 + logLevel: debug diff --git a/docs/cli.md b/docs/cli.md index bd70b95976..65380d163a 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -5,7 +5,7 @@ * [infra login](#infra-login) * [infra logout](#infra-logout) * [infra list](#infra-list) -* [infra token](#infra-token) +* [infra tokens create](#infra-tokens-create) * [infra version](#infra-version) * [infra registry](#infra-registry) * [infra engine](#infra-engine) @@ -28,7 +28,8 @@ $ infra login infra.example.com ### Options ``` - -h, --help help for login + -h, --help help for login + -t, --timeout duration login timeout (default 5m0s) ``` ## `infra logout` @@ -39,6 +40,12 @@ Logout of an Infra Registry infra logout [flags] ``` +### Examples + +``` +$ infra logout +``` + ### Options ``` @@ -59,18 +66,18 @@ infra list [flags] -h, --help help for list ``` -## `infra token` +## `infra tokens create` -Generate a JWT token for connecting to a destination, e.g. Kubernetes +Create a JWT token for connecting to a destination, e.g. Kubernetes ``` -infra token DESTINATION [flags] +infra tokens create DESTINATION [flags] ``` ### Options ``` - -h, --help help for token + -h, --help help for create ``` ## `infra version` @@ -84,7 +91,9 @@ infra version [flags] ### Options ``` - -h, --help help for version + -c, --client Display client version only + -h, --help help for version + -r, --registry Display registry version only ``` ## `infra registry` @@ -98,15 +107,17 @@ infra registry [flags] ### Options ``` - -c, --config string config file - --db string path to database file (default "~/.infra/infra.db") - -h, --help help for registry - --engine-api-key string initial api key for adding destinations - --root-api-key string the root api key for privileged actions - --sync-interval int the interval (in seconds) at which Infra will poll sources for users and groups (default 30) - --tls-cache string path to directory to cache tls self-signed and Let's Encrypt certificates (default "~/.infra/cache") - --ui enable ui - --ui-proxy string proxy ui requests to this host + -c, --config string config file + --db string path to database file (default "~/.infra/infra.db") + --enable-crash-reporting enable crash reporting (default true) + --enable-telemetry enable telemetry (default true) + --engine-api-key string engine registration API key + -h, --help help for registry + --root-api-key string root API key + --sync-interval int the interval (in seconds) at which Infra will poll sources for users and groups (default 30) + --tls-cache string path to directory to cache tls self-signed and Let's Encrypt certificates (default "~/.infra/cache") + --ui enable ui + --ui-proxy string proxy ui requests to this host ``` ## `infra engine` @@ -120,11 +131,11 @@ infra engine [flags] ### Options ``` - --api-key string api key - --force-tls-verify force TLS verification - -h, --help help for engine - -n, --name string cluster name - -r, --registry string registry hostname - --tls-cache string path to directory to cache tls self-signed and Let's Encrypt certificates (default "~/.infra/cache") + --engine-api-key string engine registration API key + --force-tls-verify force TLS verification + -h, --help help for engine + -n, --name string cluster name + -r, --registry string registry hostname + --tls-cache string path to directory to cache tls self-signed and Let's Encrypt certificates (default "~/.infra/cache") ``` diff --git a/internal/cmd/list.go b/internal/cmd/list.go index 2cb2227f8b..c6a2890f52 100644 --- a/internal/cmd/list.go +++ b/internal/cmd/list.go @@ -58,9 +58,9 @@ func list() error { // This shouldn't be possible but check nonetheless switch { case len(users) < 1: - return fmt.Errorf("User \"%s\" not found", config.Name) + return fmt.Errorf("user \"%s\" not found", config.Name) case len(users) > 1: - return fmt.Errorf("Found multiple users \"%s\"", config.Name) + return fmt.Errorf("found multiple users \"%s\"", config.Name) } user := users[0] diff --git a/internal/cmd/login.go b/internal/cmd/login.go index 78cf6bdc97..6714b924bf 100644 --- a/internal/cmd/login.go +++ b/internal/cmd/login.go @@ -111,7 +111,7 @@ registry: } if len(sources) == 0 { - return errors.New("Zero sources have been configured.") + return errors.New("zero sources have been configured") } var selectedSource *api.Source @@ -224,11 +224,11 @@ source: } if len(users) < 1 { - return fmt.Errorf("User \"%s\" not found", loginRes.Name) + return fmt.Errorf("user \"%s\" not found", loginRes.Name) } if len(users) > 1 { - return fmt.Errorf("Found multiple users \"%s\"", loginRes.Name) + return fmt.Errorf("found multiple users \"%s\"", loginRes.Name) } err = updateKubeconfig(users[0]) diff --git a/internal/engine/engine.go b/internal/engine/engine.go index 44f882f940..2b432ca641 100644 --- a/internal/engine/engine.go +++ b/internal/engine/engine.go @@ -8,10 +8,12 @@ import ( "errors" "fmt" "io/ioutil" + "net" "net/http" "net/http/httputil" "net/url" "os" + "regexp" "strings" "sync" "time" @@ -335,6 +337,25 @@ func Run(options Options) error { return } + logging.L.Sugar().Debugf("endpoint is: %s", endpoint) + + // check if the endpoint is localhost, if it is DNS lookup won't resolve + local, err := regexp.MatchString("(http://|https://)?localhost:?[0-9]{0,5}", endpoint) + if err != nil { + logging.L.Sugar().Errorf("endpoint match failed: %w", err) + // continue as a non-local endpoint + } + + if local { + logging.L.Sugar().Debug("not testing DNS lookup for localhost") + } else { + _, err = net.LookupIP(endpoint) + if err != nil { + logging.L.Sugar().Errorf("endpoint DNS does not yet resolve, waiting to register") + return + } + } + url, err := urlx.Parse(endpoint) if err != nil { logging.L.Error("url parse: " + err.Error())