Support removal of managed diagnostic views
This commit is contained in:
@@ -184,6 +184,66 @@ func ManagedAttemptTargets(sessionRoot, runID string, names []string) ([]string,
|
||||
return targets, nil
|
||||
}
|
||||
|
||||
// InspectManagedAttemptViews reports whether any recognized managed view exists.
|
||||
// Dangling links count as present; their targets are never followed.
|
||||
func InspectManagedAttemptViews(sessionRoot string, names []string) (bool, error) {
|
||||
return visitManagedAttemptViews(sessionRoot, names, false)
|
||||
}
|
||||
|
||||
// RemoveManagedAttemptViews removes recognized managed links without following them.
|
||||
func RemoveManagedAttemptViews(sessionRoot string, names []string) error {
|
||||
_, err := visitManagedAttemptViews(sessionRoot, names, true)
|
||||
return err
|
||||
}
|
||||
|
||||
func visitManagedAttemptViews(sessionRoot string, names []string, remove bool) (bool, error) {
|
||||
for _, name := range names {
|
||||
if err := pathsafe.ValidateOpaqueSegment(name); err != nil {
|
||||
return false, fmt.Errorf("invalid managed view %q: %w", name, err)
|
||||
}
|
||||
}
|
||||
root, err := os.OpenRoot(sessionRoot)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer root.Close()
|
||||
found := false
|
||||
for _, category := range []string{"logs", "debug"} {
|
||||
info, err := root.Lstat(category)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return false, fmt.Errorf("managed %s directory is unsafe", category)
|
||||
}
|
||||
parent, err := root.OpenRoot(category)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
for _, name := range names {
|
||||
target, err := existingManagedView(parent, name, category)
|
||||
if err == nil && target != nil {
|
||||
found = true
|
||||
if remove {
|
||||
err = parent.Remove(name)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
_ = parent.Close()
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
_ = parent.Close()
|
||||
}
|
||||
return found, nil
|
||||
}
|
||||
|
||||
func replaceManagedView(parent *os.Root, name, category, target string) error {
|
||||
if _, err := existingManagedView(parent, name, category); err != nil {
|
||||
return err
|
||||
|
||||
@@ -148,3 +148,74 @@ func TestAttemptViewsLeavePriorLinksOnDirectoryFailure(t *testing.T) {
|
||||
t.Fatalf("conflict changed: %q, %v", data, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedAttemptViewsInspectAndRemove(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
names := []string{"polish", "extract", "restore"}
|
||||
if found, err := InspectManagedAttemptViews(root, names); err != nil || found {
|
||||
t.Fatalf("empty views = %v, %v", found, err)
|
||||
}
|
||||
for _, pair := range [][2]string{{"first", "polish"}, {"second", "extract"}, {"third", "restore"}} {
|
||||
if _, err := BeginAttemptViews(root, pair[0], pair[1]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
unrelated := filepath.Join(root, "logs", "operator")
|
||||
if err := os.WriteFile(unrelated, []byte("keep"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.RemoveAll(filepath.Join(root, "runs", "second")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if found, err := InspectManagedAttemptViews(root, names); err != nil || !found {
|
||||
t.Fatalf("managed views = %v, %v", found, err)
|
||||
}
|
||||
if _, err := os.Lstat(filepath.Join(root, "logs", "extract")); err != nil {
|
||||
t.Fatalf("inspection changed dangling view: %v", err)
|
||||
}
|
||||
for range 2 {
|
||||
if err := RemoveManagedAttemptViews(root, names); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if found, err := InspectManagedAttemptViews(root, names); err != nil || found {
|
||||
t.Fatalf("removed views = %v, %v", found, err)
|
||||
}
|
||||
if data, err := os.ReadFile(unrelated); err != nil || string(data) != "keep" {
|
||||
t.Fatalf("operator content = %q, %v", data, err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(root, "runs", "first", "polish", "logs")); err != nil {
|
||||
t.Fatalf("view target removed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedAttemptViewsRejectConflicts(t *testing.T) {
|
||||
for _, kind := range []string{"file", "foreign link", "category link"} {
|
||||
t.Run(kind, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if kind == "category link" {
|
||||
if err := os.Symlink(t.TempDir(), filepath.Join(root, "logs")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
if err := os.Mkdir(filepath.Join(root, "logs"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(root, "logs", "polish")
|
||||
if kind == "file" {
|
||||
if err := os.WriteFile(path, []byte("keep"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else if err := os.Symlink("../../outside", path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := InspectManagedAttemptViews(root, []string{"polish"}); err == nil {
|
||||
t.Fatal("inspection accepted conflict")
|
||||
}
|
||||
if err := RemoveManagedAttemptViews(root, []string{"polish"}); err == nil {
|
||||
t.Fatal("removal accepted conflict")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user