Skip to content

Commit

Permalink
Merge pull request RedHatInsights#1448 from mjholder/correct-attribut…
Browse files Browse the repository at this point in the history
…e-filter-endpoint

[RHCLOUD-27273] Maintenance Endpoint to fix resourceDefinitions
  • Loading branch information
mjholder authored Jan 21, 2025
2 parents b15e66f + 92b448d commit 35d0da3
Show file tree
Hide file tree
Showing 3 changed files with 388 additions and 1 deletion.
1 change: 1 addition & 0 deletions rbac/internal/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
path("api/utils/bootstrap_tenant/", views.bootstrap_tenant),
path("api/utils/migration_resources/", views.migration_resources),
path("api/utils/reset_imported_tenants/", views.reset_imported_tenants),
path("api/utils/resource_definitions/", views.correct_resource_definitions),
]

urlpatterns.extend(integration_urlpatterns)
52 changes: 51 additions & 1 deletion rbac/internal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from django.utils.html import escape
from internal.utils import delete_bindings
from management.cache import TenantCache
from management.models import Group, Permission, Role
from management.models import Group, Permission, ResourceDefinition, Role
from management.principal.proxy import (
API_TOKEN_HEADER,
CLIENT_ID_HEADER,
Expand Down Expand Up @@ -781,3 +781,53 @@ def roles(request, uuid: str) -> HttpResponse:
def trigger_error(request):
"""Trigger an error to confirm Sentry is working."""
raise SentryDiagnosticError


def correct_resource_definitions(request):
"""Get/Fix resourceDefinitions with incorrect attributeFilters.
Attribute filters with lists must use 'in' operation. Those with a single string must use 'equal'
GET /_private/api/utils/resource_definitions
PATCH /_private/api/utils/resource_definitions
"""
list_query = """ FROM management_resourcedefinition
WHERE "attributeFilter"->>'operation' = 'equal'
AND jsonb_typeof("attributeFilter"->'value') = 'array';"""

string_query = """ from management_resourcedefinition WHERE "attributeFilter"->>'operation' = 'in'
AND jsonb_typeof("attributeFilter"->'value') = 'string';"""

if request.method == "GET":
with connection.cursor() as cursor:

cursor.execute("SELECT COUNT(*)" + list_query)
count = cursor.fetchone()[0]

cursor.execute("SELECT COUNT(*)" + string_query)
count += cursor.fetchone()[0]

return HttpResponse(f"{count} resource definitions would be corrected", status=200)
elif request.method == "PATCH":
count = 0
with connection.cursor() as cursor:

cursor.execute("SELECT id " + list_query)
result = cursor.fetchall()
for id in result:
resource_definition = ResourceDefinition.objects.get(id=id[0])
resource_definition.attributeFilter["operation"] = "in"
resource_definition.save()
count += 1

cursor.execute("SELECT id " + string_query)
result = cursor.fetchall()
for id in result:
resource_definition = ResourceDefinition.objects.get(id=id[0])
resource_definition.attributeFilter["operation"] = "equal"
resource_definition.save()
count += 1

return HttpResponse(f"Updated {count} bad resource definitions", status=200)

return HttpResponse('Invalid method, only "GET" or "PATCH" are allowed.', status=405)
Loading

0 comments on commit 35d0da3

Please sign in to comment.