diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d891cc..ffed934 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ All notable changes are recorded here. The format follows - A managed database or cache on Google Cloud plans and applies with the `gcloud` sign-in, as clusters do. `differently diff` used to stop with **Terraform has no Google Cloud sign-in** until `gcloud auth application-default login` was run as well. - An apply of a managed database or cache on Google Cloud turns on the APIs it uses first (Cloud SQL Admin and Secret Manager; Memorystore, Network Connectivity and Service Consumer Management), as cluster creation does. On a project where they were off, the first create failed with **accessNotConfigured**. - A failed `helm` or `kubectl` step during `differently apply` says why it failed, from the command's `Error:` line. It used to show the command's first line, so a failed OpenWork migration read **Release "openwork" does not exist. Installing it now.** +- A part added to an installed stack gets its own Secret made. `diff` and `apply` used to refuse it, reading the part's new Secret as one that was gone with a never-replace key in it, so OpenWork's runner (`HEADLESS_GRANT_KEY`) could not be added to a running OpenWork. A part's Secret the release already read and that is gone is still refused. - A cloud sign-in started from the local console is followed to its end. The page used to say "Finish signing in in the window that opened" until **Check again** was clicked; it now moves on by itself when `gcloud auth login` or `az login` finishes, says why when the CLI fails or times out, and offers the provider's sign-in page (`accounts.google.com`, `login.microsoftonline.com`) when the CLI printed one, for when no window opened. A second click while the window waits starts no second sign-in. - Summary reads a signed-out Azure or AWS CLI as **Sign-in needed**, from what the CLI said, where it said **Couldn’t verify access**. - Practice mode simulates the AWS sign-in too, from Summary and project setup. diff --git a/cmd/differently/stack_sandboxes_test.go b/cmd/differently/stack_sandboxes_test.go index befedba..2fbffd0 100644 --- a/cmd/differently/stack_sandboxes_test.go +++ b/cmd/differently/stack_sandboxes_test.go @@ -335,3 +335,46 @@ func TestANeverKeyInAnotherSecretIsNotMadeAgain(t *testing.T) { t.Error("a second token was made, or Helm ran") } } + +// A part added to an installed stack brings a Secret the release never +// read. Its never-replace keys are made, not refused as lost; once it has +// been written, the same Secret gone is a loss again. Found adding +// OpenWork's runner (HEADLESS_GRANT_KEY, rotate: never) to a deployed +// OpenWork in a live rehearsal. +func TestAPartAddedLaterGetsItsSecretMade(t *testing.T) { + needHelm(t) + realServer(t) + f := newFakeCluster(t, "lab") + dir := notesChart(t) + b, err := os.ReadFile(filepath.Join(dir, "stack.yaml")) + if err != nil { + t.Fatal(err) + } + never := strings.Replace(string(b), "RUNNER_TOKEN: {generate: 32-bytes, rotate: restart}", "RUNNER_TOKEN: {generate: 32-bytes, rotate: never}", 1) + if never == string(b) { + t.Fatal("the notes description has no RUNNER_TOKEN to change") + } + if err := os.WriteFile(filepath.Join(dir, "stack.yaml"), []byte(never), 0o644); err != nil { + t.Fatal(err) + } + file := setup(t, stackFile(dir, evaluation)) + mustCLI(t, "apply", "-f", file) + + if err := os.WriteFile(file, []byte(stackFile(dir, withRunner)), 0o600); err != nil { + t.Fatal(err) + } + out := mustCLI(t, "diff", "-f", file) + if strings.Contains(out, "! stack/notes") || strings.Contains(out, "is gone") { + t.Fatalf("adding the part was refused:\n%s", out) + } + mustCLI(t, "apply", "-f", file) + token := f.secrets["notes/notes-runner"]["RUNNER_TOKEN"] + if token == "" { + t.Fatal("the runner's Secret was not written") + } + + delete(f.secrets, "notes/notes-runner") + if out = mustCLI(t, "diff", "-f", file); !strings.Contains(out, "the Secret notes-runner is gone, and with it RUNNER_TOKEN") { + t.Errorf("diff with the runner's Secret gone:\n%s", out) + } +} diff --git a/cmd/differently/stacks.go b/cmd/differently/stacks.go index f10a694..c1e6e30 100644 --- a/cmd/differently/stacks.go +++ b/cmd/differently/stacks.go @@ -56,7 +56,10 @@ type stackWork struct { kube string files []string // installed is the chart version running now, "" when there is none. - installed string + installed string + // newSecrets are the parts' Secrets the installed release never read: + // their keys are made, not lost. + newSecrets map[string]bool sourceChanged bool // needs is the stack's managed needs, nil when it has none. needs *stackNeeds @@ -290,9 +293,22 @@ func readStack(ctx context.Context, m *manifest.Manifest, file, name string, srv // The first install failed: nothing of the application runs. return w, append(append(append(w.describe(where, want, pending), needLines...), w.sandboxes.changes(false)...), fmt.Sprintf("the last install, of %s, failed: applying tries again", release.attempt)), nil } + now, err := releaseValues(ctx, w.kube, w.dep.Namespace, name) + if err != nil { + return nil, nil, err + } // A key the chart says must never be replaced, and that is gone: a new // value would start the services on data they can no longer read. - for _, secret := range w.dep.AllSecrets() { + for i, secret := range w.dep.AllSecrets() { + if i > 0 && have[secret.Name] == nil && !namedBy(now, w.dep, secret.Name) { + // A part's Secret the installed release never read, because the + // part is new: its keys are made, not lost. + if w.newSecrets == nil { + w.newSecrets = map[string]bool{} + } + w.newSecrets[secret.Name] = true + continue + } lost := chart.Lost(secret.Keys, have[secret.Name], made[secret.Name]) if len(lost) == 0 { continue @@ -334,10 +350,6 @@ func readStack(ctx context.Context, m *manifest.Manifest, file, name string, srv } details = append(details, fmt.Sprintf("version: %s → %s", w.installed, w.chartVersion)) } - now, err := releaseValues(ctx, w.kube, w.dep.Namespace, name) - if err != nil { - return nil, nil, err - } for _, path := range sortedUnion(want, now) { a, inNow := now[path] b, inWant := want[path] @@ -858,6 +870,17 @@ func helmRelease(ctx context.Context, kube, namespace, name, chartName string) ( return r, nil } +// namedBy reports whether the installed release's values give a Secret's +// name to one of the chart values that take it. +func namedBy(installed map[string]string, dep *chart.Deployment, secret string) bool { + for _, v := range dep.Values { + if v.Need == "" && fmt.Sprint(v.Value) == secret && installed[v.Path] == secret { + return true + } + } + return false +} + // releaseValues is the values the release was last given, flattened. func releaseValues(ctx context.Context, kube, namespace, name string) (map[string]string, error) { out, err := toolRun(ctx, "helm", "get", "values", name, "--kube-context", kube, "--namespace", namespace, "-o", "json") @@ -1040,7 +1063,7 @@ func deployStack(ctx context.Context, w *stackWork) error { if err != nil { return err } - if lost := chart.Lost(secret.Keys, data, madeKeys(notes)); w.installed != "" && len(lost) > 0 { + if lost := chart.Lost(secret.Keys, data, madeKeys(notes)); w.installed != "" && len(lost) > 0 && !(data == nil && w.newSecrets[secret.Name]) { return fmt.Errorf("the Secret %s no longer holds %s, which the chart says must never be replaced (rotate: never). Nothing was changed", secret.Name, strings.Join(lost, ", ")) } now[secret.Name] = current{data, notes}