Skip to content

Commit

Permalink
lxd/project: Simplify FilterUsedBy function.
Browse files Browse the repository at this point in the history
We no longer need to enforce that the project query parameter
is set in the URLs that are passed into Authorizer methods, nor
do we need to strip the project query parameter from the URL.

Signed-off-by: Mark Laing <[email protected]>
  • Loading branch information
markylaing committed Apr 24, 2024
1 parent d06573d commit a4fa4d3
Showing 1 changed file with 4 additions and 21 deletions.
25 changes: 4 additions & 21 deletions lxd/project/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1478,35 +1478,18 @@ func FilterUsedBy(authorizer auth.Authorizer, r *http.Request, entries []string)
continue
}

entityType, projectName, location, pathArguments, err := entity.ParseURL(*u)
entityType, _, _, _, err := entity.ParseURL(*u)
if err != nil {
logger.Warn("Failed to parse project used-by entity URL", logger.Ctx{"url": entry, "error": err})
continue
}

entityURL, err := entity.URL(entityType, projectName, location, pathArguments...)
if err != nil {
logger.Warn("Failed to create canonical entity URL for project used-by filtering", logger.Ctx{"url": entry, "error": err})
continue
}

urlsByEntityType[entityType] = append(urlsByEntityType[entityType], entityURL)
urlsByEntityType[entityType] = append(urlsByEntityType[entityType], &api.URL{URL: *u})
}

// Filter the entries.
usedBy := make([]string, 0, len(entries))

// Used-by lists do not include the project query parameter if it is the default project.
appendUsedBy := func(u *api.URL) {
if u.Query().Get("project") == api.ProjectDefaultName {
q := u.Query()
q.Del("project")
u.RawQuery = q.Encode()
}

usedBy = append(usedBy, u.String())
}

for entityType, urls := range urlsByEntityType {
// If only one entry of this type, check directly.
if len(urls) == 1 {
Expand All @@ -1515,7 +1498,7 @@ func FilterUsedBy(authorizer auth.Authorizer, r *http.Request, entries []string)
continue
}

appendUsedBy(urls[0])
usedBy = append(usedBy, urls[0].String())
continue
}

Expand All @@ -1528,7 +1511,7 @@ func FilterUsedBy(authorizer auth.Authorizer, r *http.Request, entries []string)
// Check each url and append.
for _, u := range urls {
if canViewEntity(u) {
appendUsedBy(u)
usedBy = append(usedBy, u.String())
}
}
}
Expand Down

0 comments on commit a4fa4d3

Please sign in to comment.