Skip to content

Commit

Permalink
lxd: Update GET /1.0/auth/permissions handler.
Browse files Browse the repository at this point in the history
This updates the handler to ensure we only display valid permissions.
All dangling permissions are deleted.

Signed-off-by: Mark Laing <mark.laing@canonical.com>
  • Loading branch information
markylaing committed Feb 28, 2024
1 parent dc5dcd7 commit 6812751
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lxd/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/canonical/lxd/lxd/response"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/entity"
"github.com/canonical/lxd/shared/logger"
)

var permissionsCmd = APIEndpoint{
Expand Down Expand Up @@ -153,6 +154,19 @@ func getPermissions(d *Daemon, r *http.Request) response.Response {
return fmt.Errorf("Failed to get currently assigned permissions: %w", err)
}

// Call GetPermissionEntityURLs to ensure only valid permissions are returned.
var danglingPermissions []cluster.Permission
permissions, danglingPermissions, _, err = cluster.GetPermissionEntityURLs(ctx, tx.Tx(), permissions)
if err != nil {
return fmt.Errorf("Failed to get entity URLs for permissions: %w", err)
}

// Clean up dangling permissions if present.
err = cluster.DeletePermissions(ctx, tx.Tx(), danglingPermissions)
if err != nil {
logger.Warn("Failed to clean up dangling permissions", logger.Ctx{"error": err})
}

groupsByPermissionID, err = cluster.GetAllAuthGroupsByPermissionID(ctx, tx.Tx())
if err != nil {
return fmt.Errorf("Failed to get groups by permission mapping: %w", err)
Expand Down

0 comments on commit 6812751

Please sign in to comment.