Skip to content

Commit

Permalink
Wait to add engine until it resolves in an external DNS lookup (#371)
Browse files Browse the repository at this point in the history
* Check DNS resolution in engine before creating destination
* Update docker-desktop.yaml.in to log at the debug level
* update docs
  • Loading branch information
BruceMacD authored Oct 22, 2021
1 parent 64c1f60 commit ffc52df
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ release
.vscode

# configuration file used during development
infra.yaml
docker-desktop.yaml
2 changes: 2 additions & 0 deletions docker-desktop.yaml.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
logLevel: debug
image:
tag: $IMAGE_TAG
pullPolicy: Never
Expand All @@ -20,3 +21,4 @@ engine:
- name: https
port: 8443
targetPort: 443
logLevel: debug
55 changes: 33 additions & 22 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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`
Expand All @@ -39,6 +40,12 @@ Logout of an Infra Registry
infra logout [flags]
```

### Examples

```
$ infra logout
```

### Options

```
Expand All @@ -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`
Expand All @@ -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`
Expand All @@ -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`
Expand All @@ -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")
```

4 changes: 2 additions & 2 deletions internal/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down
21 changes: 21 additions & 0 deletions internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"regexp"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit ffc52df

Please sign in to comment.