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

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


The following commit(s) were added to refs/heads/master by this push:
     new 2d24d087a Remove deleted account's Slurm user (#687)
2d24d087a is described below

commit 2d24d087a67dc3b612a126593c9b8b707994dcbe
Author: Yasith Jayawardana <[email protected]>
AuthorDate: Thu Nov 5 15:58:46 2026 -0500

    Remove deleted account's Slurm user (#687)
---
 .../internal/subscribers/account.go                | 20 +++++++
 .../internal/subscribers/account_delete_test.go    | 62 ++++++++++++++++++++++
 .../internal/subscribers/deprovision.go            | 26 +++++++++
 .../internal/subscribers/members_test.go           | 18 +++++--
 .../internal/subscribers/subscriber.go             |  2 +
 .../SLURM/Rest-Client/pkg/client/accounts.go       |  6 +++
 6 files changed, 129 insertions(+), 5 deletions(-)

diff --git 
a/connectors/SLURM/Association-Mapper/internal/subscribers/account.go 
b/connectors/SLURM/Association-Mapper/internal/subscribers/account.go
index 8912e2c90..e01507d10 100644
--- a/connectors/SLURM/Association-Mapper/internal/subscribers/account.go
+++ b/connectors/SLURM/Association-Mapper/internal/subscribers/account.go
@@ -248,3 +248,23 @@ func (a *AssociationSubscriber) 
handleComputeAllocationResourceMappingCreated(ct
        }
        return nil
 }
+
+func (a *AssociationSubscriber) handleComputeClusterUserDeleted(ctx 
context.Context, csu models.ComputeClusterUser) error {
+       ctx = audit.WithSource(ctx, "slurm")
+       ctx, span := tracing.Start(ctx, "slurm.compute_cluster_user_delete")
+       defer span.End()
+       span.SetAttributes(attribute.String("slurm.user_id", csu.UserID))
+
+       fetchCtx, cancel := context.WithTimeout(ctx, coreCallTimeout)
+       defer cancel()
+
+       if err := a.removeUserFromCluster(fetchCtx, csu); err != nil {
+               slog.ErrorContext(ctx, "Failed to remove Slurm user for deleted 
cluster account", "error", err)
+               span.RecordError(err)
+               span.SetStatus(codes.Error, err.Error())
+               a.recordAuditEvent(ctx, "ComputeClusterUserDeletionFailed", 
"compute_cluster_user", csu.ID, "Failed to remove Slurm user. Error: 
"+err.Error())
+               return err
+       }
+       a.recordAuditEvent(ctx, "ComputeClusterUserDeprovisioned", 
"compute_cluster_user", csu.ID, "Removed the Slurm user for the deleted cluster 
account.")
+       return nil
+}
diff --git 
a/connectors/SLURM/Association-Mapper/internal/subscribers/account_delete_test.go
 
b/connectors/SLURM/Association-Mapper/internal/subscribers/account_delete_test.go
new file mode 100644
index 000000000..40ec249c7
--- /dev/null
+++ 
b/connectors/SLURM/Association-Mapper/internal/subscribers/account_delete_test.go
@@ -0,0 +1,62 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package subscribers
+
+import (
+       "context"
+       "slices"
+       "testing"
+
+       
"github.com/apache/airavata-custos/connectors/SLURM/Rest-Client/pkg/client"
+       "github.com/apache/airavata-custos/pkg/models"
+)
+
+var deletedAccount = models.ComputeClusterUser{ID: "csu-1", ComputeClusterID: 
"cluster-1", UserID: "user-1", LocalUsername: "testuser"}
+
+func TestAccountDeletionRemovesTheSlurmUser(t *testing.T) {
+       slurm := &fakeSlurmClient{existing: []client.Association{
+               {Cluster: "testcluster", Account: noAllocation, User: 
"testuser"},
+               {Cluster: "testcluster", Account: "test-alloc", User: 
"testuser"},
+       }}
+       if err := NewAssociationSubscriber(slurm, nil, coreMock(mockOpts{}), 0, 
0).
+               handleComputeClusterUserDeleted(context.Background(), 
deletedAccount); err != nil {
+               t.Fatalf("delete: %v", err)
+       }
+       if !slices.Equal(slurm.deletedUsers, []string{"testuser"}) || 
len(slurm.allDeletes()) != 0 {
+               t.Fatalf("deleted users %v, association deletes %+v; want the 
user only", slurm.deletedUsers, slurm.allDeletes())
+       }
+}
+
+func TestAccountDeletionKeepsAUserOnAnotherCluster(t *testing.T) {
+       slurm := &fakeSlurmClient{existing: []client.Association{
+               {Cluster: "testcluster", Account: noAllocation, User: 
"testuser"},
+               {Cluster: "testcluster", Account: "test-alloc", User: 
"testuser"},
+               {Cluster: "othercluster", Account: noAllocation, User: 
"testuser"},
+       }}
+       if err := NewAssociationSubscriber(slurm, nil, coreMock(mockOpts{}), 0, 
0).
+               handleComputeClusterUserDeleted(context.Background(), 
deletedAccount); err != nil {
+               t.Fatalf("delete: %v", err)
+       }
+       want := []client.AssocFilter{
+               {Cluster: "testcluster", Account: "test-alloc", User: 
"testuser"},
+               {Cluster: "testcluster", Account: noAllocation, User: 
"testuser"},
+       }
+       if len(slurm.deletedUsers) != 0 || !slices.Equal(slurm.allDeletes(), 
want) {
+               t.Fatalf("deleted users %v, association deletes %+v; want %+v", 
slurm.deletedUsers, slurm.allDeletes(), want)
+       }
+}
diff --git 
a/connectors/SLURM/Association-Mapper/internal/subscribers/deprovision.go 
b/connectors/SLURM/Association-Mapper/internal/subscribers/deprovision.go
index 2a3bbdf24..74547d60e 100644
--- a/connectors/SLURM/Association-Mapper/internal/subscribers/deprovision.go
+++ b/connectors/SLURM/Association-Mapper/internal/subscribers/deprovision.go
@@ -22,6 +22,7 @@ import (
        "errors"
        "fmt"
        "log/slog"
+       "slices"
 
        
"github.com/apache/airavata-custos/connectors/SLURM/Rest-Client/pkg/client"
        "github.com/apache/airavata-custos/pkg/models"
@@ -93,6 +94,31 @@ func (a *AssociationSubscriber) 
removeAssociationsForAllocation(ctx context.Cont
        return nil
 }
 
+// removeUserFromCluster deletes a deleted account's Slurm user, or only this 
cluster's associations if the user is on others.
+func (a *AssociationSubscriber) removeUserFromCluster(ctx context.Context, csu 
models.ComputeClusterUser) error {
+       cluster, err := a.coreService.GetComputeCluster(ctx, 
csu.ComputeClusterID)
+       if err != nil {
+               return fmt.Errorf("get compute cluster: %w", err)
+       }
+       user := csu.LocalUsername
+       assocs, err := a.slurmClient.ListAssociations(client.AssocFilter{User: 
user})
+       if err != nil || len(assocs) == 0 {
+               return err
+       }
+       if !slices.ContainsFunc(assocs, func(x client.Association) bool { 
return x.Cluster != cluster.Name }) {
+               return a.slurmClient.DeleteUser(user)
+       }
+       // Slurm refuses the default account while others remain, so it goes 
last.
+       for _, x := range assocs {
+               if x.Cluster == cluster.Name && x.Account != noAllocation {
+                       if err := 
a.slurmClient.DeleteAssociation(client.AssocFilter{Cluster: cluster.Name, 
Account: x.Account, User: user}); err != nil {
+                               return err
+                       }
+               }
+       }
+       return a.slurmClient.DeleteAssociation(client.AssocFilter{Cluster: 
cluster.Name, Account: noAllocation, User: user})
+}
+
 // activeAllocation reports whether an allocation should still grant access.
 func activeAllocation(allocation models.ComputeAllocation) bool {
        return allocation.Status == models.ACTIVE
diff --git 
a/connectors/SLURM/Association-Mapper/internal/subscribers/members_test.go 
b/connectors/SLURM/Association-Mapper/internal/subscribers/members_test.go
index 03bfc191d..0db792566 100644
--- a/connectors/SLURM/Association-Mapper/internal/subscribers/members_test.go
+++ b/connectors/SLURM/Association-Mapper/internal/subscribers/members_test.go
@@ -32,11 +32,12 @@ import (
 )
 
 type fakeSlurmClient struct {
-       mu       sync.Mutex
-       upserts  []client.Association
-       defaults []string
-       deletes  []client.AssocFilter
-       existing []client.Association
+       mu           sync.Mutex
+       upserts      []client.Association
+       defaults     []string
+       deletes      []client.AssocFilter
+       existing     []client.Association
+       deletedUsers []string
 }
 
 func (f *fakeSlurmClient) DeleteAssociation(filter client.AssocFilter) error {
@@ -46,6 +47,13 @@ func (f *fakeSlurmClient) DeleteAssociation(filter 
client.AssocFilter) error {
        return nil
 }
 
+func (f *fakeSlurmClient) DeleteUser(name string) error {
+       f.mu.Lock()
+       defer f.mu.Unlock()
+       f.deletedUsers = append(f.deletedUsers, name)
+       return nil
+}
+
 func (f *fakeSlurmClient) allDeletes() []client.AssocFilter {
        f.mu.Lock()
        defer f.mu.Unlock()
diff --git 
a/connectors/SLURM/Association-Mapper/internal/subscribers/subscriber.go 
b/connectors/SLURM/Association-Mapper/internal/subscribers/subscriber.go
index ab5a266ad..944e271c6 100644
--- a/connectors/SLURM/Association-Mapper/internal/subscribers/subscriber.go
+++ b/connectors/SLURM/Association-Mapper/internal/subscribers/subscriber.go
@@ -40,6 +40,7 @@ type SlurmClient interface {
        SetDefaultAccount(cluster, user, account string) error
        ListAssociations(f client.AssocFilter) ([]client.Association, error)
        DeleteAssociation(f client.AssocFilter) error
+       DeleteUser(name string) error
 }
 
 type AssociationSubscriber struct {
@@ -72,6 +73,7 @@ func (a *AssociationSubscriber) 
RegisterSubscribers(subscriber string) {
        a.eventBus.SubscribeComputeAllocationMembershipDeleted(subscriber, 
a.handleComputeAllocationMembershipDeleted)
        
a.eventBus.SubscribeComputeAllocationMembershipResourceOverrideCreated(subscriber,
 a.handleComputeAllocationMembershipResourceOverrideCreated)
        a.eventBus.SubscribeComputeAllocationResourceMappingCreated(subscriber, 
a.handleComputeAllocationResourceMappingCreated)
+       a.eventBus.SubscribeComputeClusterUserDeleted(subscriber, 
a.handleComputeClusterUserDeleted)
 }
 
 func (a *AssociationSubscriber) recordAuditEvent(ctx context.Context, 
eventType, entityType, entityId, message string) {
diff --git a/connectors/SLURM/Rest-Client/pkg/client/accounts.go 
b/connectors/SLURM/Rest-Client/pkg/client/accounts.go
index 41f241a55..9b281e659 100644
--- a/connectors/SLURM/Rest-Client/pkg/client/accounts.go
+++ b/connectors/SLURM/Rest-Client/pkg/client/accounts.go
@@ -72,3 +72,9 @@ func (c *Client) DeleteAccount(name string) error {
        _, err := c.do("DELETE", 
"/slurmdb/v0.0."+c.apiVersion+"/account/"+name, nil, nil)
        return err
 }
+
+// DeleteUser deletes a user with all its associations.
+func (c *Client) DeleteUser(name string) error {
+       _, err := c.do("DELETE", "/slurmdb/v0.0."+c.apiVersion+"/user/"+name, 
nil, nil)
+       return err
+}

Reply via email to