Skip to content

Commit

Permalink
Merge branch 'main' into fix/resource-initialization
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
  • Loading branch information
byewokko committed Apr 18, 2024
2 parents eb8b88c + 18ce59d commit fc1a9e4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v24.06

### Pre-releases
- `v24.06-alpha15`
- `v24.06-alpha14`
- `v24.06-alpha13`
- `v24.06-alpha12`
Expand All @@ -26,7 +27,8 @@
- Disable special characters in tenant ID (#349, `v24.06-alpha6`)

### Fix
- Fix the initialization and updating of built-in resources (#363, `v24.06-alpha14`)
- Fix the initialization and updating of built-in resources (#363, `v24.06-alpha15`)
- Fix searching credentials with multiple filters (#362, `v24.06-alpha14`)
- Better TOTP error responses (#352, `v24.06-alpha10`)
- Fix resource editability (#355, `v24.06-alpha9`)
- Make FIDO MDS request non-blocking using TaskService (#354, `v24.06-alpha8`)
Expand Down
3 changes: 3 additions & 0 deletions seacatauth/credentials/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,10 @@ async def list_credentials(self, request):
mode = request.query.get("m", "default")
if mode == "role":
search.AdvancedFilter["role"] = request.query.get("f")
search.SimpleFilter = None
elif mode == "tenant":
search.AdvancedFilter["tenant"] = request.query.get("f")
search.SimpleFilter = None
elif mode == "default":
search.SimpleFilter = request.query.get("f")

Expand All @@ -215,6 +217,7 @@ async def list_credentials(self, request):
return asab.web.rest.json_response(request, {
"data": result["data"],
"count": result["count"],
"result": "OK",
})


Expand Down
27 changes: 20 additions & 7 deletions seacatauth/credentials/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ async def list(self, session: SessionAdapter, search_params: generic.SearchParam
List credentials that are members of currently authorized tenants.
Global_search lists all credentials, regardless of tenants, but this requires superuser authorization.
"""
# TODO: Searching with filters is very inefficient and needs serious optimization
if len(search_params.AdvancedFilter) > 1:
raise asab.exceptions.ValidationError("No more than one advanced filter at a time is supported.")

Expand Down Expand Up @@ -260,8 +261,7 @@ async def list(self, session: SessionAdapter, search_params: generic.SearchParam
estimated_count = None
if searched_roles:
role_svc = self.App.get_service("seacatauth.RoleService")
assignments = await role_svc.list_role_assignments(
role_id=searched_roles, page=search_params.Page, limit=search_params.ItemsPerPage)
assignments = await role_svc.list_role_assignments(role_id=searched_roles)
if filtered_cids is None:
filtered_cids = set(a["c"] for a in assignments["data"])
estimated_count = assignments["count"]
Expand All @@ -271,8 +271,7 @@ async def list(self, session: SessionAdapter, search_params: generic.SearchParam
if searched_tenants:
tenant_svc = self.App.get_service("seacatauth.TenantService")
provider = tenant_svc.get_provider()
assignments = await provider.list_tenant_assignments(
searched_tenants, page=search_params.Page, limit=search_params.ItemsPerPage)
assignments = await provider.list_tenant_assignments(searched_tenants)
if filtered_cids is None:
filtered_cids = set(a["c"] for a in assignments["data"])
estimated_count = assignments["count"]
Expand All @@ -285,20 +284,34 @@ async def list(self, session: SessionAdapter, search_params: generic.SearchParam
return {"count": 0, "data": []}

credentials = []
total_count = estimated_count
filtered_cids = sorted(filtered_cids)

offset = search_params.Page * search_params.ItemsPerPage
for cid in filtered_cids:
_, provider_id, _ = cid.split(":", 2)
try:
provider = self.CredentialProviders[provider_id]
credentials.append(await provider.get(cid))
cred_data = await provider.get(cid)
except KeyError:
L.info("Found an assignment of nonexisting credentials", struct_data={
"cid": cid, "role_ids": searched_roles, "tenant_ids": searched_tenants})
continue
if not search_params.SimpleFilter or (
cred_data.get("username", "").startswith(search_params.SimpleFilter)
or cred_data.get("email", "").startswith(search_params.SimpleFilter)
):
if offset > 0:
# Skip until offset is reached
offset -= 1
continue
credentials.append(cred_data)
if len(credentials) >= search_params.ItemsPerPage:
# Page is full
break

return {
"data": credentials,
"count": total_count,
"count": estimated_count,
}

# Search without external filters
Expand Down

0 comments on commit fc1a9e4

Please sign in to comment.