-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat(talos): detect and repair malformed CA in saved talosconfig #4645
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
Open
devantler
wants to merge
5
commits into
main
Choose a base branch
from
devantler/talos-ca-cert-update-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7155676
feat(talos): detect and repair malformed CA in saved talosconfig
Copilot 51c10e3
fix(repairer): address PR review feedback
Copilot 630d9c3
fix(repairer): satisfy lint and dead-code checks at root cause
Copilot aef26d6
fix(repairer): remove unused package-level Register/All helpers
Copilot 227bcc2
fix(repairer): dedupe Register by Name and canonicalize talosconfig path
Copilot 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
29 changes: 29 additions & 0 deletions
29
docs/src/content/docs/cli-flags/cluster/cluster-repair.mdx
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,29 @@ | ||
| --- | ||
| title: "ksail cluster repair" | ||
| description: "Repair local KSail/Talos state files" | ||
| --- | ||
|
|
||
| {/* This page is auto-generated by go generate ./docs/... — DO NOT EDIT */} | ||
|
|
||
| ```text | ||
| Detect and repair known corruption patterns in local state files. | ||
|
|
||
| Currently supported repairs: | ||
| - talosconfig-ca: fixes a single-byte BasicConstraints corruption in | ||
| the Talos talosconfig CA that prevents 'cluster update' from | ||
| establishing a Talos client. | ||
|
|
||
| Each repair is idempotent and writes a timestamped backup of any file | ||
| it modifies. | ||
|
|
||
| Usage: | ||
| ksail cluster repair [flags] | ||
|
|
||
| Flags: | ||
| --talosconfig string path to talosconfig (default: ~/.talos/config) | ||
|
|
||
| Global Flags: | ||
| --benchmark Show per-activity benchmark output | ||
| --config string Path to config file (default: ksail.yaml found via directory traversal) | ||
|
|
||
| ``` |
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,139 @@ | ||
| package cluster | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
|
|
||
| "github.com/devantler-tech/ksail/v7/pkg/di" | ||
| "github.com/devantler-tech/ksail/v7/pkg/notify" | ||
| "github.com/devantler-tech/ksail/v7/pkg/svc/repairer" | ||
| talosconfigrepair "github.com/devantler-tech/ksail/v7/pkg/svc/repairer/talosconfig" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // NewRepairCmd creates the `ksail cluster repair` command, backed by | ||
| // the supplied [repairer.Registry]. Pass [repairer.Default] for normal | ||
| // operation; tests can pass an isolated registry from | ||
| // [repairer.NewRegistry] to avoid cross-package contention. | ||
| // | ||
| // The command runs every [repairer.Repair] registered with the | ||
| // supplied registry, printing one status line per repair. It is | ||
| // idempotent and safe to run repeatedly. The first registered repair | ||
| // fixes a known single-byte corruption in Talos talosconfig CA bytes | ||
| // that produces: | ||
| // | ||
| // failed to append CA certificate to RootCAs pool | ||
| // | ||
| // during `ksail cluster update`. | ||
| func NewRepairCmd(_ *di.Runtime, registry *repairer.Registry) *cobra.Command { | ||
| if registry == nil { | ||
| registry = repairer.Default() | ||
| } | ||
|
|
||
| var talosconfigPath string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "repair", | ||
| Short: "Repair local KSail/Talos state files", | ||
| Long: `Detect and repair known corruption patterns in local state files. | ||
|
|
||
| Currently supported repairs: | ||
| - talosconfig-ca: fixes a single-byte BasicConstraints corruption in | ||
| the Talos talosconfig CA that prevents 'cluster update' from | ||
| establishing a Talos client. | ||
|
|
||
| Each repair is idempotent and writes a timestamped backup of any file | ||
| it modifies.`, | ||
| SilenceUsage: true, | ||
| RunE: func(cmd *cobra.Command, _ []string) error { | ||
| return runRepair(cmd.Context(), cmd, registry, talosconfigPath) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar( | ||
| &talosconfigPath, | ||
| "talosconfig", | ||
| "", | ||
| "path to talosconfig (default: ~/.talos/config)", | ||
| ) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runRepair( | ||
| ctx context.Context, | ||
| cmd *cobra.Command, | ||
| registry *repairer.Registry, | ||
| talosconfigPath string, | ||
| ) error { | ||
| out := cmd.OutOrStdout() | ||
|
|
||
| repairs := registry.All() | ||
| configurePerRepairOptions(repairs, talosconfigPath) | ||
|
|
||
| if len(repairs) == 0 { | ||
| notify.Activityf(out, "no repairs registered") | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| var hadError bool | ||
|
|
||
| for _, r := range repairs { | ||
| notify.Activityf(out, "running repair %q...", r.Name()) | ||
|
|
||
| result := r.Run(ctx, out) | ||
| printRepairResult(cmd, result) | ||
|
|
||
| if result.Err != nil || result.Status == repairer.StatusUnrepairable { | ||
| hadError = true | ||
| } | ||
| } | ||
|
|
||
| if hadError { | ||
| return errRepairsFailed | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // errRepairsFailed signals that at least one repair returned an error | ||
| // or [repairer.StatusUnrepairable]. Cobra picks this up via RunE and | ||
| // surfaces it as a non-zero exit. | ||
| var errRepairsFailed = errors.New("one or more repairs reported failures") | ||
|
|
||
| // configurePerRepairOptions threads CLI flags into individual repair | ||
| // implementations that need them. Today only the talosconfig repair | ||
| // reads --talosconfig. | ||
| func configurePerRepairOptions(repairs []repairer.Repair, talosconfigPath string) { | ||
| if talosconfigPath == "" { | ||
| return | ||
| } | ||
|
|
||
| for _, r := range repairs { | ||
| if tc, ok := r.(*talosconfigrepair.Repair); ok { | ||
| tc.Path = talosconfigPath | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func printRepairResult(cmd *cobra.Command, result repairer.Result) { | ||
| out := cmd.OutOrStdout() | ||
|
|
||
| switch result.Status { | ||
| case repairer.StatusOK: | ||
| notify.Successf(out, "[%s] %s", result.Name, result.Detail) | ||
| case repairer.StatusRepaired: | ||
| notify.Successf(out, "[%s] %s (backup: %s)", result.Name, result.Detail, result.BackupPath) | ||
| case repairer.StatusUnrepairable: | ||
| notify.Warningf(out, "[%s] %s", result.Name, result.Detail) | ||
| case repairer.StatusSkipped: | ||
| notify.Activityf(out, "[%s] %s", result.Name, result.Detail) | ||
| default: | ||
| notify.Activityf(out, "[%s] %s (status=%s)", result.Name, result.Detail, result.Status) | ||
| } | ||
|
|
||
| if result.Err != nil { | ||
| notify.Errorf(out, "[%s] error: %v", result.Name, result.Err) | ||
| } | ||
| } |
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,101 @@ | ||
| package cluster_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "io" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| clustercmd "github.com/devantler-tech/ksail/v7/pkg/cli/cmd/cluster" | ||
| "github.com/devantler-tech/ksail/v7/pkg/svc/repairer" | ||
| ) | ||
|
|
||
| type stubRepair struct { | ||
| name string | ||
| result repairer.Result | ||
| } | ||
|
|
||
| func (s *stubRepair) Name() string { return s.name } | ||
|
|
||
| func (s *stubRepair) Run(_ context.Context, _ io.Writer) repairer.Result { | ||
| return s.result | ||
| } | ||
|
|
||
| func TestRepairCmd_RunsRegisteredRepairs(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| reg := repairer.NewRegistry() | ||
| reg.Register(&stubRepair{ | ||
| name: "fake-ok", | ||
| result: repairer.Result{Name: "fake-ok", Status: repairer.StatusOK, Detail: "all good"}, | ||
| }) | ||
|
|
||
| cmd := clustercmd.NewRepairCmd(nil, reg) | ||
| cmd.SetContext(context.Background()) | ||
|
|
||
|
devantler marked this conversation as resolved.
|
||
| var out bytes.Buffer | ||
| cmd.SetOut(&out) | ||
| cmd.SetErr(&out) | ||
| cmd.SetArgs([]string{}) | ||
|
|
||
| err := cmd.Execute() | ||
| if err != nil { | ||
| t.Fatalf("execute: %v\nout: %s", err, out.String()) | ||
| } | ||
|
|
||
| if !strings.Contains(out.String(), "fake-ok") { | ||
| t.Fatalf("expected fake-ok in output: %s", out.String()) | ||
| } | ||
| } | ||
|
|
||
| // errStubFailure is a sentinel used by stub repairs in failure-path tests. | ||
| var errStubFailure = errors.New("stub repair failed") | ||
|
|
||
| func TestRepairCmd_FailsOnUnrepairable(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| reg := repairer.NewRegistry() | ||
| reg.Register(&stubRepair{name: "broken", result: repairer.Result{ | ||
| Name: "broken", | ||
| Status: repairer.StatusUnrepairable, | ||
| Detail: "cannot fix", | ||
| Err: errStubFailure, | ||
| }}) | ||
|
|
||
| cmd := clustercmd.NewRepairCmd(nil, reg) | ||
| cmd.SetContext(context.Background()) | ||
|
|
||
| var out bytes.Buffer | ||
| cmd.SetOut(&out) | ||
| cmd.SetErr(&out) | ||
|
|
||
| err := cmd.Execute() | ||
| if err == nil { | ||
| t.Fatalf("expected non-nil error, got nil; out: %s", out.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestRepairCmd_NoRepairsRegistered(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| reg := repairer.NewRegistry() | ||
|
|
||
| cmd := clustercmd.NewRepairCmd(nil, reg) | ||
| cmd.SetContext(context.Background()) | ||
|
|
||
| var out bytes.Buffer | ||
|
|
||
| cmd.SetOut(&out) | ||
| cmd.SetErr(&out) | ||
|
|
||
| err := cmd.Execute() | ||
| if err != nil { | ||
| t.Fatalf("expected nil err, got %v", err) | ||
| } | ||
|
|
||
| if !strings.Contains(out.String(), "no repairs registered") { | ||
| t.Fatalf("expected 'no repairs registered' in output: %s", out.String()) | ||
| } | ||
| } | ||
Large diffs are not rendered by default.
Oops, something went wrong.
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.