You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by ya...@apache.org on 2026/10/20 23:41:23 UTC

(airavata-custos) 02/02: remove views, keep files, when an account or allocation is deleted

This is an automated email from the ASF dual-hosted git repository.

yasithdev pushed a commit to branch vast-integrity
in repository https://gitbox.apache.org/repos/asf/airavata-custos.git

commit 31ec77ae3195bf037866702145b3b6b21b0f2182
Author: Yasith Jayawardana <ya...@icloud.com>
AuthorDate: Tue Oct 20 19:41:15 2026 -0400

    remove views, keep files, when an account or allocation is deleted
    
    Claude-Session: https://claude.ai/code/session_012X8JL6k2JbwLqzzKgscTyA
---
 connectors/Storage/VAST-Provisioner/README.md      |  8 ++-
 .../VAST-Provisioner/internal/client/client.go     | 25 ++++++++
 .../internal/client/client_test.go                 | 35 ++++++++++++
 .../internal/subscribers/subscriber.go             | 59 ++++++++++++++++++-
 .../internal/subscribers/subscriber_test.go        | 66 ++++++++++++++++++++--
 5 files changed, 184 insertions(+), 9 deletions(-)

diff --git a/connectors/Storage/VAST-Provisioner/README.md b/connectors/Storage/VAST-Provisioner/README.md
index b1741328b..b7a81e186 100644
--- a/connectors/Storage/VAST-Provisioner/README.md
+++ b/connectors/Storage/VAST-Provisioner/README.md
@@ -20,8 +20,8 @@
 # VAST-Provisioner
 
 Creates directories, views and quotas on each configured cluster's VAST through the VMS REST API, once the COmanage
-Identity-Provisioner has provisioned the account or the allocation's group. Nothing is changed once it exists, and nothing is deleted,
-except a quota with other limits, which is recreated.
+Identity-Provisioner has provisioned the account or the allocation's group. Nothing is changed once it exists, except a quota with
+other limits, which is recreated; deleting either removes its views and keeps its files.
 
 The directories come from each cluster's `targets` list in the config. Each entry's `path`, `owner` and `group` are
 templates over `{user}`, the account's username, and `{allocation}`, the allocation's group; the ones it uses decide
@@ -37,6 +37,8 @@ fields on a view. The policy is looked up once at startup, and Custos does not s
 | `compute_cluster_user::provision` | The account's directories, then its directory in each of its projects |
 | `compute_allocation::group_provision` | The project's directories and each provisioned member's, once the group's first member puts it on the cluster |
 | `compute_allocation_membership::create` and `::update` | That member's project directory, once their account is provisioned |
+| `compute_cluster_user::delete` | Removal of the views on the account's directories, including its directory in each project |
+| `compute_allocation::delete` | Removal of the views on the project's directories and its members' directories inside them |
 | Startup, then every 24 hours | Everything, for every account and allocation on each cluster |
 
 Each event creates only what it is about, so a failed delivery names the user it is for and can be retried alone.
@@ -52,7 +54,7 @@ VAST admins make these changes before the connector is enabled.
 | The COmanage LDAP is a VMS user-directory provider | VMS resolves owners only from its providers; Custos users and `proj-*` groups are otherwise unknown and every create retries |
 | The service account may create folders, views and quotas | The account the connector logs in with |
 | The view policy in `vast.view_policy` exists | Used for every view the connector creates |
-| The service account may delete quotas | A quota with other limits is recreated |
+| The service account may delete quotas and views | A quota with other limits is recreated; removals |
 
 VMS drops setgid from `create_dir_mode` (a `2770` request yields `0770`), so folders configured with setgid, such as
 `/project/{allocation}`, `shared` and `/project/{allocation}/{user}`, come out without it and files in them do not
diff --git a/connectors/Storage/VAST-Provisioner/internal/client/client.go b/connectors/Storage/VAST-Provisioner/internal/client/client.go
index 51faee46f..2ae854392 100644
--- a/connectors/Storage/VAST-Provisioner/internal/client/client.go
+++ b/connectors/Storage/VAST-Provisioner/internal/client/client.go
@@ -30,6 +30,7 @@ import (
 	"io"
 	"net/http"
 	"net/url"
+	"strconv"
 	"strings"
 	"time"
 )
@@ -117,6 +118,30 @@ func (c *Client) ViewPolicyID(ctx context.Context, name string) (int, error) {
 	return found[0].ID, nil
 }
 
+// RemoveViews deletes the views on path and under it; files and quotas stay.
+func (c *Client) RemoveViews(ctx context.Context, path string) error {
+	token, err := c.login(ctx)
+	if err != nil {
+		return err
+	}
+	var views []struct {
+		ID   int
+		Path string
+	}
+	if err := c.do(ctx, token, http.MethodGet, "views/?tenant_id="+strconv.Itoa(c.tenantID), nil, &views); err != nil {
+		return err
+	}
+	for _, v := range views {
+		if v.Path != path && !strings.HasPrefix(v.Path, path+"/") {
+			continue
+		}
+		if err := c.do(ctx, token, http.MethodDelete, fmt.Sprintf("views/%d/", v.ID), nil, nil); err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
 func (c *Client) create(ctx context.Context, path string, body map[string]any, exists func(token string) bool) error {
 	token, err := c.login(ctx)
 	if err != nil {
diff --git a/connectors/Storage/VAST-Provisioner/internal/client/client_test.go b/connectors/Storage/VAST-Provisioner/internal/client/client_test.go
index 94b7af224..57530056d 100644
--- a/connectors/Storage/VAST-Provisioner/internal/client/client_test.go
+++ b/connectors/Storage/VAST-Provisioner/internal/client/client_test.go
@@ -156,3 +156,38 @@ func TestCreateQuota(t *testing.T) {
 		})
 	}
 }
+
+// Make sure only the views on and under the path go, not a prefix sibling's, and a refused delete is an error.
+func TestRemoveViews(t *testing.T) {
+	tests := []struct {
+		name    string
+		status  int
+		want    []string
+		wantErr bool
+	}{
+		{name: "removed", status: 200, want: []string{"/api/views/1/", "/api/views/2/"}},
+		{name: "refused", status: 503, want: []string{"/api/views/1/"}, wantErr: true},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			var deleted []string
+			srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+				switch r.Method {
+				case http.MethodPost:
+					_, _ = w.Write([]byte(`{"access":"fresh"}`))
+				case http.MethodGet:
+					_, _ = w.Write([]byte(`[{"id":1,"path":"/project/proj-a","tenant_id":1},{"id":2,"path":"/project/proj-a/custos-jdoe","tenant_id":1},
+						{"id":3,"path":"/project/proj-ab","tenant_id":1}]`))
+				case http.MethodDelete:
+					deleted = append(deleted, r.URL.Path)
+					w.WriteHeader(tt.status)
+				}
+			}))
+			defer srv.Close()
+			err := New(srv.URL, "u", "p", 1).RemoveViews(context.Background(), "/project/proj-a")
+			if (err != nil) != tt.wantErr || !slices.Equal(deleted, tt.want) {
+				t.Fatalf("err=%v deleted=%v, want %v", err, deleted, tt.want)
+			}
+		})
+	}
+}
diff --git a/connectors/Storage/VAST-Provisioner/internal/subscribers/subscriber.go b/connectors/Storage/VAST-Provisioner/internal/subscribers/subscriber.go
index b78efc87e..51f96864f 100644
--- a/connectors/Storage/VAST-Provisioner/internal/subscribers/subscriber.go
+++ b/connectors/Storage/VAST-Provisioner/internal/subscribers/subscriber.go
@@ -18,7 +18,7 @@
 // Package subscribers creates the configured directories and quotas on VAST
 // once COmanage has provisioned the account, the allocation's group, or both, since VAST resolves owners by name
 // from the LDAP that COmanage feeds. Mounts are created in config order, so
-// parents come first.
+// parents come first. Deleting an account or allocation removes its views and keeps its files.
 package subscribers
 
 import (
@@ -76,6 +76,63 @@ func (s *StorageSubscriber) RegisterSubscribers(subscriber string) {
 	s.bus.SubscribeComputeAllocationGroupProvisioned(subscriber, s.ensureAllocation)
 	s.bus.SubscribeComputeAllocationMembershipCreated(subscriber, s.ensureMember)
 	s.bus.SubscribeComputeAllocationMembershipUpdated(subscriber, s.ensureMember)
+	s.bus.SubscribeComputeClusterUserDeleted(subscriber, s.removeUser)
+	s.bus.SubscribeComputeAllocationDeleted(subscriber, s.removeAllocation)
+}
+
+// removeUser removes the views on the account's mounts, including its mount in each allocation.
+func (s *StorageSubscriber) removeUser(ctx context.Context, cu models.ComputeClusterUser) error {
+	c, ok := s.clusters[cu.ComputeClusterID]
+	if !ok || cu.LocalUsername == "" {
+		return nil
+	}
+	if err := remove(ctx, c, cu.LocalUsername, ""); err != nil {
+		return err
+	}
+	memberships, err := s.core.ListAllocationsForUser(ctx, cu.UserID)
+	if err != nil {
+		return err
+	}
+	for _, m := range memberships {
+		a, err := s.core.GetComputeAllocation(ctx, m.ComputeAllocationID)
+		if errors.Is(err, service.ErrNotFound) {
+			continue
+		}
+		if err != nil {
+			return err
+		}
+		if a.ComputeClusterID != cu.ComputeClusterID || a.PosixGroup == nil || *a.PosixGroup == "" {
+			continue
+		}
+		if err := remove(ctx, c, cu.LocalUsername, *a.PosixGroup); err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
+// removeAllocation removes the views on the allocation's mounts and its members' mounts inside them.
+func (s *StorageSubscriber) removeAllocation(ctx context.Context, a models.ComputeAllocation) error {
+	c, ok := s.clusters[a.ComputeClusterID]
+	if !ok || a.PosixGroup == nil || *a.PosixGroup == "" {
+		return nil
+	}
+	return remove(ctx, c, "", *a.PosixGroup)
+}
+
+// remove removes the views on the mounts that use exactly the placeholders given; an empty name would match the
+// shared mounts, so callers skip it.
+func remove(ctx context.Context, c Cluster, user, group string) error {
+	r := strings.NewReplacer("{user}", user, "{allocation}", group)
+	for _, m := range c.Mounts {
+		if m.uses("{user}") != (user != "") || m.uses("{allocation}") != (group != "") {
+			continue
+		}
+		if err := c.VMS.RemoveViews(ctx, r.Replace(m.Path)); err != nil {
+			return err
+		}
+	}
+	return nil
 }
 
 // ensureUser needs no approval check: COmanage provisions approved accounts only.
diff --git a/connectors/Storage/VAST-Provisioner/internal/subscribers/subscriber_test.go b/connectors/Storage/VAST-Provisioner/internal/subscribers/subscriber_test.go
index 78ed2c265..f5e2d5333 100644
--- a/connectors/Storage/VAST-Provisioner/internal/subscribers/subscriber_test.go
+++ b/connectors/Storage/VAST-Provisioner/internal/subscribers/subscriber_test.go
@@ -23,6 +23,8 @@ import (
 	"net/http"
 	"net/http/httptest"
 	"slices"
+	"strconv"
+	"strings"
 	"testing"
 	"time"
 
@@ -69,14 +71,15 @@ func allocationWithMembers() *service.CoreServiceMock {
 	}
 }
 
-// recordingVMS stands in for VMS and records each create as "<kind> <path>".
-func recordingVMS(t *testing.T) (Cluster, *[]string) {
+// recordingVMS stands in for VMS and records each create as "<kind> <path>", starting from held.
+// A deleted view becomes "removed".
+func recordingVMS(t *testing.T, held ...string) (Cluster, *[]string) {
 	t.Helper()
-	var log []string
+	log := slices.Clone(held)
 	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 		var body struct{ Path string }
 		_ = json.NewDecoder(r.Body).Decode(&body)
-		if r.Method == http.MethodGet {
+		if r.Method == http.MethodGet && r.URL.Path == "/api/quotas/" {
 			_, _ = w.Write([]byte(`[]`))
 			return
 		}
@@ -88,7 +91,21 @@ func recordingVMS(t *testing.T) (Cluster, *[]string) {
 		case "/api/quotas/":
 			log = append(log, "quota "+body.Path)
 		case "/api/views/":
-			log = append(log, "view "+body.Path)
+			if r.Method != http.MethodGet {
+				log = append(log, "view "+body.Path)
+				return
+			}
+			var views []map[string]any
+			for i, e := range log {
+				if p, ok := strings.CutPrefix(e, "view "); ok {
+					views = append(views, map[string]any{"id": i, "path": p})
+				}
+			}
+			_ = json.NewEncoder(w).Encode(views)
+		default:
+			if id, err := strconv.Atoi(strings.Trim(strings.TrimPrefix(r.URL.Path, "/api/views/"), "/")); err == nil && r.Method == http.MethodDelete {
+				log[id] = "removed"
+			}
 		}
 	}))
 	t.Cleanup(srv.Close)
@@ -172,3 +189,42 @@ func TestEnsureAllocation(t *testing.T) {
 		})
 	}
 }
+
+// Make sure a deletion removes only its own views, keeps folders and quotas, and repeats harmlessly.
+func TestRemove(t *testing.T) {
+	group := "proj-a"
+	provisioned := join(jdoeOwn, groupMounts, jdoeMember, asmithMember)
+	withoutViews := func(of string) []string {
+		return slices.DeleteFunc(slices.Clone(provisioned), func(e string) bool { return strings.HasPrefix(e, "view "+of) })
+	}
+	tests := []struct {
+		name   string
+		remove func(*StorageSubscriber) error
+		want   []string
+	}{
+		{name: "account deleted, allocation kept", remove: func(s *StorageSubscriber) error {
+			return s.removeUser(context.Background(), models.ComputeClusterUser{ComputeClusterID: "c1", UserID: "u1", LocalUsername: "custos-jdoe"})
+		}, want: slices.DeleteFunc(withoutViews("/scratch/custos-jdoe"), func(e string) bool { return e == "view /project/proj-a/custos-jdoe" })},
+		{name: "allocation deleted, accounts kept", remove: func(s *StorageSubscriber) error {
+			return s.removeAllocation(context.Background(), models.ComputeAllocation{ID: "a1", ComputeClusterID: "c1", PosixGroup: &group})
+		}, want: withoutViews("/project/")},
+		{name: "nothing for an empty name", remove: func(s *StorageSubscriber) error {
+			return s.removeUser(context.Background(), models.ComputeClusterUser{ComputeClusterID: "c1", UserID: "u1", LocalUsername: ""})
+		}, want: provisioned},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			c, log := recordingVMS(t, provisioned...)
+			s := NewStorageSubscriber(nil, allocationWithMembers(), map[string]Cluster{"c1": c})
+			for run := range 2 {
+				if err := tt.remove(s); err != nil {
+					t.Fatalf("run %d: %v", run+1, err)
+				}
+				left := slices.DeleteFunc(slices.Clone(*log), func(e string) bool { return e == "removed" })
+				if !slices.Equal(left, tt.want) {
+					t.Fatalf("run %d: left %v, want %v", run+1, left, tt.want)
+				}
+			}
+		})
+	}
+}