merge: integrate proxyctl ticket 07
# Conflicts: # cmd/proxyctl/main.go # internal/securefs/securefs.go
This commit is contained in:
+172
-25
@@ -1,15 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"proxyctl/internal/app"
|
||||
"proxyctl/internal/dockerdaemon"
|
||||
"proxyctl/internal/operationlock"
|
||||
"proxyctl/internal/oplog"
|
||||
"proxyctl/internal/profile"
|
||||
"proxyctl/internal/shell"
|
||||
"proxyctl/internal/target"
|
||||
@@ -54,42 +62,72 @@ func runWithEnvironment(args []string, input io.Reader, output io.Writer, enviro
|
||||
func runWithEnvironmentAndTargets(args []string, input io.Reader, output io.Writer, environment profile.IdentityEnvironment, files shell.FileSystem, executable string, systemTargets []target.Target) error {
|
||||
profileConfiguration, err := profile.ResolveConfiguration(environment)
|
||||
if err != nil {
|
||||
if len(args) == 1 && args[0] == "doctor" {
|
||||
fmt.Fprintf(output, "[FAIL] identity resolution — %s\n", profile.RedactText(err.Error()))
|
||||
}
|
||||
return err
|
||||
}
|
||||
shellConfiguration, err := shell.ResolveConfiguration(environment)
|
||||
if err != nil {
|
||||
if len(args) == 1 && args[0] == "doctor" {
|
||||
fmt.Fprintf(output, "[FAIL] Shell Target configuration — %s\n", profile.RedactText(err.Error()))
|
||||
}
|
||||
return err
|
||||
}
|
||||
shellTarget := shell.New(shellConfiguration, files, environment)
|
||||
|
||||
if len(args) != 0 {
|
||||
if len(args) != 2 || args[0] != "shell" || (args[1] != "install" && args[1] != "uninstall") {
|
||||
return fmt.Errorf("usage: proxyctl [shell install|shell uninstall]")
|
||||
}
|
||||
availability := shellTarget.Availability(context.Background())
|
||||
if !availability.Available {
|
||||
return fmt.Errorf("Shell Target unavailable: %s", availability.Evidence)
|
||||
}
|
||||
if args[1] == "install" {
|
||||
if err := shellTarget.Install(context.Background(), executable); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(output, "Shell integration installed; restart or source your Shell startup file once.")
|
||||
return nil
|
||||
}
|
||||
if err := shellTarget.Uninstall(context.Background()); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(output, "Shell integration uninstalled.")
|
||||
return nil
|
||||
}
|
||||
|
||||
store := profile.NewOwnedFileStoreAt(profileConfiguration.Path, profileConfiguration.Owner, profileConfiguration.TrustedRoot)
|
||||
if systemTargets == nil {
|
||||
systemTargets = []target.Target{dockerdaemon.New(dockerdaemon.DefaultConfiguration(), nil, nil)}
|
||||
}
|
||||
targets := append([]target.Target{shellTarget}, systemTargets...)
|
||||
application, err := app.NewWithTargets(store, targets)
|
||||
stateDirectory := filepath.Dir(shellConfiguration.RecoveryFile)
|
||||
mutationLock := operationlock.NewOwned(filepath.Join(stateDirectory, "operation.lock"), profileConfiguration.Owner.UID, profileConfiguration.Owner.GID)
|
||||
logger := oplog.NewOwned(filepath.Join(stateDirectory, "operations.log"), 64*1024, 3, profileConfiguration.Owner.UID, profileConfiguration.Owner.GID)
|
||||
store := profile.NewOwnedFileStoreAt(profileConfiguration.Path, profileConfiguration.Owner, profileConfiguration.TrustedRoot)
|
||||
|
||||
if len(args) != 0 {
|
||||
switch {
|
||||
case len(args) == 2 && args[0] == "shell" && (args[1] == "install" || args[1] == "uninstall"):
|
||||
availability := shellTarget.Availability(context.Background())
|
||||
if !availability.Available {
|
||||
return fmt.Errorf("Shell Target unavailable: %s", availability.Evidence)
|
||||
}
|
||||
unlock, err := mutationLock.Lock(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("lock proxyctl mutation: %w", err)
|
||||
}
|
||||
defer unlock()
|
||||
operation := "shell-" + args[1]
|
||||
var operationErr error
|
||||
if args[1] == "install" {
|
||||
operationErr = shellTarget.Install(context.Background(), executable)
|
||||
} else {
|
||||
operationErr = shellTarget.Uninstall(context.Background())
|
||||
}
|
||||
result, detail := "success", "Shell integration changed"
|
||||
if operationErr != nil {
|
||||
result, detail = "failure", operationErr.Error()
|
||||
}
|
||||
_ = logger.Write(app.LogEntry{Operation: operation, Target: shellTarget.ID(), Result: result, Detail: detail})
|
||||
if operationErr != nil {
|
||||
return operationErr
|
||||
}
|
||||
if args[1] == "install" {
|
||||
fmt.Fprintln(output, "Shell integration installed; restart or source your Shell startup file once.")
|
||||
} else {
|
||||
fmt.Fprintln(output, "Shell integration uninstalled.")
|
||||
}
|
||||
return nil
|
||||
case len(args) == 1 && args[0] == "doctor":
|
||||
checks := diagnosticChecks(profileConfiguration)
|
||||
return runMaintenanceCommand(args, input, output, store, targets, mutationLock, logger, checks)
|
||||
case len(args) == 1 && args[0] == "reset":
|
||||
return runMaintenanceCommand(args, input, output, store, targets, mutationLock, logger, nil)
|
||||
default:
|
||||
return fmt.Errorf("usage: proxyctl [doctor|reset|shell install|shell uninstall]")
|
||||
}
|
||||
}
|
||||
|
||||
application, err := app.NewWithTargetsAndRuntime(store, targets, mutationLock, logger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -97,3 +135,112 @@ func runWithEnvironmentAndTargets(args []string, input io.Reader, output io.Writ
|
||||
_, err = program.Run()
|
||||
return err
|
||||
}
|
||||
|
||||
func runMaintenanceCommand(args []string, input io.Reader, output io.Writer, store *profile.FileStore, targets []target.Target, mutationLock app.MutationLock, logger app.OperationLogger, checks []target.Check) error {
|
||||
if len(args) != 1 {
|
||||
return errors.New("invalid maintenance command")
|
||||
}
|
||||
if args[0] == "doctor" {
|
||||
state, err := store.ReadOnlyLoad()
|
||||
profileCheck := target.Check{Name: "Profile validity", OK: err == nil, Evidence: "Profile configuration is valid"}
|
||||
if err != nil {
|
||||
profileCheck.Evidence = err.Error()
|
||||
}
|
||||
checks = append([]target.Check{profileCheck}, checks...)
|
||||
report := app.NewMaintenance(state, targets, nil, nil).Doctor(context.Background(), checks)
|
||||
fmt.Fprint(output, report.String())
|
||||
if !report.Healthy() {
|
||||
return errors.New("doctor found configuration problems")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if args[0] != "reset" {
|
||||
return errors.New("invalid maintenance command")
|
||||
}
|
||||
fmt.Fprint(output, "Reset every managed Target and restore recorded original state? [y/N] ")
|
||||
answer, _ := bufio.NewReader(input).ReadString('\n')
|
||||
answer = strings.ToLower(strings.TrimSpace(answer))
|
||||
if answer != "y" && answer != "yes" {
|
||||
fmt.Fprintln(output, "Reset cancelled; no Targets changed.")
|
||||
return nil
|
||||
}
|
||||
if mutationLock == nil {
|
||||
return errors.New("mutation lock is not configured")
|
||||
}
|
||||
unlock, err := mutationLock.Lock(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("lock proxyctl mutation: %w", err)
|
||||
}
|
||||
defer unlock()
|
||||
state, err := store.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result := app.NewMaintenance(state, targets, heldMutationLock{}, logger).Reset(context.Background(), true)
|
||||
for _, item := range result.Targets {
|
||||
if item.Skipped {
|
||||
fmt.Fprintf(output, "%s: skipped (%s) — %s\n", item.TargetID, item.Inspection.State, profile.RedactText(item.Inspection.Evidence))
|
||||
continue
|
||||
}
|
||||
if item.Err != nil {
|
||||
fmt.Fprintf(output, "%s: failed (%s) — %s\n", item.TargetID, item.Inspection.State, profile.RedactText(item.Err.Error()))
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(output, "%s: reset (%s) — %s\n", item.TargetID, item.Inspection.State, profile.RedactText(item.Inspection.Evidence))
|
||||
}
|
||||
return result.Err
|
||||
}
|
||||
|
||||
type heldMutationLock struct{}
|
||||
|
||||
func (heldMutationLock) Lock(context.Context) (func(), error) { return func() {}, nil }
|
||||
|
||||
func diagnosticChecks(configuration profile.Configuration) []target.Check {
|
||||
identity := target.Check{Name: "identity resolution", OK: true, Evidence: fmt.Sprintf("configuration owner %s (uid %d, gid %d)", configuration.Owner.Username, configuration.Owner.UID, configuration.Owner.GID)}
|
||||
ok, evidence := storagePermissions(configuration)
|
||||
storage := target.Check{Name: "storage permissions", OK: ok, Evidence: evidence}
|
||||
return []target.Check{identity, storage}
|
||||
}
|
||||
|
||||
func storagePermissions(configuration profile.Configuration) (bool, string) {
|
||||
ok, evidence := writableLocation(configuration.Path)
|
||||
info, err := os.Stat(configuration.Path)
|
||||
if os.IsNotExist(err) {
|
||||
return ok, evidence + "; Profile configuration does not exist yet"
|
||||
}
|
||||
if err != nil {
|
||||
return false, evidence + "; Profile configuration cannot be inspected: " + err.Error()
|
||||
}
|
||||
if info.Mode().Perm() != 0o600 {
|
||||
return false, fmt.Sprintf("%s; Profile configuration mode is %o, want 600", evidence, info.Mode().Perm())
|
||||
}
|
||||
if stat, present := info.Sys().(*syscall.Stat_t); present && int(stat.Uid) != configuration.Owner.UID {
|
||||
return false, fmt.Sprintf("%s; Profile configuration owner uid is %d, want %d", evidence, stat.Uid, configuration.Owner.UID)
|
||||
}
|
||||
return ok, evidence + "; Profile configuration is owner-only"
|
||||
}
|
||||
|
||||
// writableLocation observes current authority without creating or changing files.
|
||||
func writableLocation(path string) (bool, string) {
|
||||
candidate := path
|
||||
for {
|
||||
info, err := os.Stat(candidate)
|
||||
if err == nil {
|
||||
if !info.IsDir() {
|
||||
candidate = filepath.Dir(candidate)
|
||||
}
|
||||
if err := unix.Access(candidate, unix.W_OK); err != nil {
|
||||
return false, candidate + " is not writable: " + err.Error()
|
||||
}
|
||||
return true, candidate + " is writable"
|
||||
}
|
||||
if !os.IsNotExist(err) {
|
||||
return false, candidate + " cannot be inspected: " + err.Error()
|
||||
}
|
||||
parent := filepath.Dir(candidate)
|
||||
if parent == candidate {
|
||||
return false, path + " has no writable existing ancestor"
|
||||
}
|
||||
candidate = parent
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user