Repository navigation
SHARD-657: shard setup sets up a local host or saves a remote connection #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
5fd8a8e
SHARD-657: save a connection under XDG_CONFIG_HOME, and use it after …
presmihaylov 80de9e5
SHARD-657: the setup skeleton: the verb and its help, the terminal pr…
presmihaylov b395b46
Merge branch 'shard-657-setup-wizard' of github.com:presmihaylov/shar…
presmihaylov 91b64e1
SHARD-657: reach a server without a request, and read SHARD_CA_FILE f…
presmihaylov 85587a8
Merge branch 'shard-657-setup-wizard' of github.com:presmihaylov/shar…
presmihaylov 26580ea
SHARD-657: setup exits 1 after a stopped step and 130 on Ctrl+C, take…
presmihaylov fe8accb
Merge branch 'shard-657-setup-wizard' of github.com:presmihaylov/shar…
presmihaylov 0d287c8
SHARD-657: the local setup steps: the provider tools, shard and shard…
presmihaylov 692d0e8
Merge remote-tracking branch 'origin/shard-657-setup-wizard' into sha…
presmihaylov f384abe
SHARD-657: the install manifest and the release fetch: the pick of th…
presmihaylov b09898c
SHARD-657: shard setup connects to a remote: verify in three steps, s…
presmihaylov 943580e
Merge remote-tracking branch 'origin/shard-657-setup-wizard' into sha…
presmihaylov 1821205
SHARD-657: an existing installation: check or repair, upgrade to the …
presmihaylov 36e5cea
Merge branch 'shard-657-setup-wizard' of github.com:presmihaylov/shar…
presmihaylov 779b426
SHARD-657: the user's shard stays executable after an upgrade, and go…
presmihaylov a12ee6a
SHARD-657: an edit after a failed check asks for the key, even with S…
presmihaylov 9efdb6b
Merge remote-tracking branch 'origin/shard-657-setup-wizard' into sha…
presmihaylov 6e02200
SHARD-657: a repair or an upgrade offers to drop a saved remote, as a…
presmihaylov beea7a8
SHARD-657: stage the service files at 0600, since install sets the mo…
presmihaylov e8d3ced
SHARD-657: an unreachable server says why and what to check, under th…
presmihaylov ff99236
Merge remote-tracking branch 'origin/shard-657-setup-wizard' into sha…
presmihaylov d1adf5d
SHARD-657: preflight, review and the local flow, with tests
presmihaylov 19e1bb4
Merge remote-tracking branch 'origin/shard-657-setup-wizard' into sha…
presmihaylov dfd3db6
SHARD-657: setup checks shard-init against SHA256SUMS; shard is the r…
presmihaylov 04e5f12
Merge remote-tracking branch 'origin/main' into shard-657-setup-wizard
presmihaylov 65da6fb
SHARD-657: preflight marks the block-size conversion gosec flags on L…
presmihaylov 08ab3f7
SHARD-657: a failed verify says what the daemon reported, not the sud…
presmihaylov 2c2eb66
SHARD-657: a table test over every row of the remote order: --remote,…
presmihaylov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/presmihaylov/shard/services/client" | ||
| ) | ||
|
|
||
| // leakKey is a synthetic key no real credential holds, so any text that carries it is a leak. | ||
| const leakKey = "shard657-synthetic-key-5a4b3c2d" | ||
|
|
||
| // isolateConfig points the saved connection at an empty directory of its own, so no test reads or writes the user's. | ||
| func isolateConfig() (func() error, error) { | ||
| dir, err := os.MkdirTemp("", "shard-config") | ||
| if err != nil { | ||
| return nil, fmt.Errorf("make a configuration directory: %w", err) | ||
| } | ||
| if err := os.Setenv(client.ConfigHomeEnv, dir); err != nil { | ||
| return nil, fmt.Errorf("set %s: %w", client.ConfigHomeEnv, err) | ||
| } | ||
|
|
||
| return func() error { return os.RemoveAll(dir) }, nil | ||
| } | ||
|
|
||
| // saveConnection saves remote and key as shard setup would, under a configuration directory of the test's own. | ||
| func saveConnection(t *testing.T, remote, key string) { | ||
| t.Helper() | ||
|
|
||
| t.Setenv(client.ConfigHomeEnv, t.TempDir()) | ||
| path, err := client.ConfigPath(os.Getenv) | ||
| if err != nil { | ||
| t.Fatalf("ConfigPath: %v", err) | ||
| } | ||
| if err := client.SaveConfig(path, client.Config{Remote: remote, APIKey: key}); err != nil { | ||
| t.Fatalf("SaveConfig: %v", err) | ||
| } | ||
| } | ||
|
|
||
| // A saved connection names the server with no flag and no SHARD_REMOTE or SHARD_API_KEY, and the verb goes there, not to the socket. (SHARD-657) | ||
| func TestASavedConnectionReachesTheFront(t *testing.T) { | ||
| var out bytes.Buffer | ||
|
|
||
| app, f, _ := newFrontApp(t, &out) | ||
| noRemoteEnv(t) | ||
| t.Setenv(client.CAFileEnv, f.ca) | ||
| saveConnection(t, f.url, f.key) | ||
| // The front still serves the daemon of the old root; the CLI gets a root with no socket at all. | ||
| app.Root = t.TempDir() | ||
|
|
||
| if err := app.Run(t.Context(), []string{"list"}); err != nil { | ||
| t.Fatalf("list through the saved connection: %v", err) | ||
| } | ||
| if !strings.Contains(out.String(), "up-1") { | ||
| t.Errorf("list through the saved connection printed %q, want the sandbox the daemon holds", out.String()) | ||
| } | ||
| } | ||
|
|
||
| // An explicit empty --remote asks for the socket, so the saved server sees no dial and the saved file is not read. (SHARD-657) | ||
| func TestAnEmptyRemoteFlagIgnoresTheSavedConnection(t *testing.T) { | ||
| var out bytes.Buffer | ||
| accepted := acceptCount(t) | ||
|
|
||
| app := newListApp(t, &out, listed(), nil) | ||
| noRemoteEnv(t) | ||
| saveConnection(t, "https://"+accepted.address, leakKey) | ||
|
|
||
| if err := app.Run(t.Context(), []string{"--remote", "", "list"}); err != nil { | ||
| t.Fatalf("list over the socket: %v", err) | ||
| } | ||
| if !strings.Contains(out.String(), "up-1") { | ||
| t.Errorf("list over the socket printed %q, want the sandbox the daemon holds", out.String()) | ||
| } | ||
| accepted.none(t) | ||
| } | ||
|
|
||
| // A host verb would report this host as the saved server, so it refuses before it dials or touches the root, and never prints the key. (SHARD-657) | ||
| func TestAHostVerbRefusesASavedConnection(t *testing.T) { | ||
| accepted := acceptCount(t) | ||
|
|
||
| noRemoteEnv(t) | ||
| saveConnection(t, "https://"+accepted.address, leakKey) | ||
| for verb, args := range map[string][]string{ | ||
| "pull": {"pull", "alpine:3.20"}, | ||
| "image list": {"image", "list"}, | ||
| "daemon status": {"daemon", "status"}, | ||
| "info": {"info"}, | ||
| "tokens mint": {"tokens", "mint", "--name", "ci"}, | ||
| "tokens list": {"tokens", "list"}, | ||
| } { | ||
| root := t.TempDir() | ||
| app := App{Version: "test", Root: root, Out: io.Discard} | ||
| ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) | ||
| err := app.Run(ctx, args) | ||
| cancel() | ||
| if want := "shard " + verb + " runs on the daemon host only"; err == nil || !strings.Contains(err.Error(), want) || !strings.Contains(err.Error(), "saved connection") { | ||
| t.Errorf("%s with a saved connection returned %v, want %q and the saved connection", verb, err, want) | ||
| } | ||
| if err != nil && strings.Contains(err.Error(), leakKey) { | ||
| t.Errorf("%s printed the saved key", verb) | ||
| } | ||
| entries, err := os.ReadDir(root) | ||
| if err != nil { | ||
| t.Fatalf("read the root: %v", err) | ||
| } | ||
| if len(entries) != 0 { | ||
| t.Errorf("%s with a saved connection left %d entries in the root, want none", verb, len(entries)) | ||
| } | ||
| } | ||
|
|
||
| accepted.none(t) | ||
| } | ||
|
|
||
| // The saved connection says where commands go, never where a front runs, so serve fails for its own reason alone. (SHARD-657) | ||
| func TestServeIgnoresTheSavedConnection(t *testing.T) { | ||
| accepted := acceptCount(t) | ||
|
|
||
| noRemoteEnv(t) | ||
| saveConnection(t, "https://"+accepted.address, leakKey) | ||
| missing := filepath.Join(t.TempDir(), "missing-signing-key") | ||
| app := App{Version: "test", Root: t.TempDir(), Out: io.Discard} | ||
|
|
||
| err := app.Run(t.Context(), []string{"serve", "--listen", "127.0.0.1:0", "--signing-key-file", missing}) | ||
| if err == nil || strings.Contains(err.Error(), "daemon host only") || !strings.Contains(err.Error(), missing) { | ||
| t.Errorf("serve with a saved connection returned %v, want the missing signing key", err) | ||
| } | ||
| accepted.none(t) | ||
| } | ||
|
|
||
| // A saved file no client can use stops every verb with its path, rather than a quiet fall back to the socket. (SHARD-657) | ||
| func TestABrokenSavedConnectionNamesTheFile(t *testing.T) { | ||
| var out bytes.Buffer | ||
|
|
||
| app := newListApp(t, &out, listed(), nil) | ||
| noRemoteEnv(t) | ||
| t.Setenv(client.ConfigHomeEnv, t.TempDir()) | ||
| path, err := client.ConfigPath(os.Getenv) | ||
| if err != nil { | ||
| t.Fatalf("ConfigPath: %v", err) | ||
| } | ||
| if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { | ||
| t.Fatalf("make the configuration directory: %v", err) | ||
| } | ||
| if err := os.WriteFile(path, []byte(`{"remote":"ftp://shard.example.com","api_key":"`+leakKey+`"}`), 0o600); err != nil { | ||
| t.Fatalf("write the configuration: %v", err) | ||
| } | ||
|
|
||
| err = app.Run(t.Context(), []string{"list"}) | ||
| if err == nil || !strings.Contains(err.Error(), path) || strings.Contains(err.Error(), leakKey) { | ||
| t.Errorf("list with a broken saved connection returned %v, want its path and never the key", err) | ||
| } | ||
| if strings.Contains(out.String(), "up-1") { | ||
| t.Error("list with a broken saved connection read the socket") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.