Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log when User/SA/Roles is added to a group #1205

Open
wants to merge 38 commits into
base: master
Choose a base branch
from

Conversation

Ellen-Yi-Dong
Copy link
Contributor

@Ellen-Yi-Dong Ellen-Yi-Dong commented Sep 18, 2024

Link(s) to Jira

Description of Intent of Change(s)

The what, why and how.

Log when a user/principal, service account, or role is added to a group

Local Testing

How can the feature be exercised?
How can the bug be exploited and fix confirmed?
Is any special local setup required?

  • Added unit tests to test changes

Checklist

  • if API spec changes are required, is the spec updated?
  • are there any pre/post merge actions required? if so, document here.
  • are theses changes covered by unit tests?
  • if warranted, are documentation changes accounted for?
  • does this require migration changes?
    • if yes, are they backwards compatible?
  • is there known, direct impact to dependent teams/components?
    • if yes, how will this be handled?

Secure Coding Practices Checklist Link

Secure Coding Practices Checklist

  • Input Validation
  • Output Encoding
  • Authentication and Password Management
  • Session Management
  • Access Control
  • Cryptographic Practices
  • Error Handling and Logging
  • Data Protection
  • Communication Security
  • System Configuration
  • Database Security
  • File Management
  • Memory Management
  • General Coding Practices

@Ellen-Yi-Dong Ellen-Yi-Dong changed the title [WIP] Log when a user/sa/role is added to a group Log when User/SA/Roles is added to a group Sep 19, 2024
@Ellen-Yi-Dong Ellen-Yi-Dong requested review from petracihalova, astrozzc, vbelchio, lpichler and dagbay-rh and removed request for astrozzc September 19, 2024 04:52
Copy link
Contributor

@petracihalova petracihalova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice job 👍 I have few comments

rbac/management/audit_log/model.py Outdated Show resolved Hide resolved
rbac/management/group/view.py Outdated Show resolved Hide resolved
rbac/management/audit_log/view.py Outdated Show resolved Hide resolved
rbac/management/audit_log/view.py Outdated Show resolved Hide resolved
group=group, tenant=tenant, type="service-account", service_account_id__in=service_accounts
group=group,
tenant=tenant,
type="service-account",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use TYPE_SERVICE_ACCOUNT for this, I have a PR ongoing to replace these random type names with enums. So I can remember replacing this in my PR too

Copy link
Contributor

@astrozzc astrozzc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lpichler
Copy link
Contributor

@astrozzc we should discuss when this PR can be merged as it conflicts with some dual write PR as #1198,..

@@ -138,3 +153,17 @@ def log_edit(self, request, resource, object):
self.action = AuditLog.EDIT
self.tenant_id = self.get_tenant_id(request)
super(AuditLog, self).save()

def log_add(self, request, resource, object, type_dict, user_type):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe better name ? it is not visible difference between log_add and log_create.
something like "log_role_or_user_added_to_group" or log_group_membership or log_group_assigment.

Copy link
Contributor Author

@Ellen-Yi-Dong Ellen-Yi-Dong Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! There's another task to add permissions to a role, so I was thinking of having this log_add function do that as well! I just have it for now grouped this way to split the prs. Do you think that's alright...?

@astrozzc
Copy link
Contributor

@astrozzc we should discuss when this PR can be merged as it conflicts with some dual write PR as #1198,..

Oh, good point. That one is about to be merged, right? Let's merge that one first before we merge this one. That one is too big to do a rebase

@lpichler
Copy link
Contributor

@astrozzc we should discuss when this PR can be merged as it conflicts with some dual write PR as #1198,..

Oh, good point. That one is about to be merged, right? Let's merge that one first before we merge this one. That one is too big to do a rebase

yes it is merged now but into #1198, this PR also contains migration. I am not sure what it is the best approach here.

if we will merge this, we would need to solve conflicts in #1198 and also change names of migrations.

@Ellen-Yi-Dong Ellen-Yi-Dong force-pushed the log_add branch 2 times, most recently from f9465f2 to a2b27d7 Compare September 25, 2024 19:16
"""Create list of principals/roles/service accounts for description."""
names_list = []
if user_type == "user" or user_type == "User":
for i in type_dict:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can we use a more meaningful name to replace these "i" here?
Such as principal_data, role_data, etc.

Copy link
Contributor

@vbelchio vbelchio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

But we need to solve the related bug below to have the best behavior of the audit logs and the process to add a non-existent role to a group.

JIRA Bug: https://issues.redhat.com/browse/RHCLOUD-35765

@Ellen-Yi-Dong
Copy link
Contributor Author

/retest

@@ -771,6 +785,27 @@ def principals(self, request: Request, uuid: Optional[UUID] = None):
# Serialize the group...
output = GroupSerializer(group)
response = Response(status=status.HTTP_200_OK, data=output.data)

if status.is_success(response.status_code):
if len(principals) > 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be part of transaction block which start on 715.
you can put audit log creation under those related conditions (len(service_accounts|principals) > 0).

if operation fails, transaction is rolled back and record into audit log is not created.

@@ -138,3 +153,17 @@ def log_edit(self, request, resource, object):
self.action = AuditLog.EDIT
self.tenant_id = self.get_tenant_id(request)
super(AuditLog, self).save()

def log_group_assignment(self, request, resource, object, type_dict, user_type):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resource and object names are confusing

it looks that resource is rather resource_type and then object can be resource :)

Does it make sense ?

@@ -138,3 +153,17 @@ def log_edit(self, request, resource, object):
self.action = AuditLog.EDIT
self.tenant_id = self.get_tenant_id(request)
super(AuditLog, self).save()

def log_group_assignment(self, request, resource, object, type_dict, user_type):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user_type could be user, service or role. What about name target_resource_type or assigned_resource_type ? or something like that

request,
AuditLog.GROUP,
object=group,
type_dict=response_data.data,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about to pass just values for description ? Something like:
[response_data.data["uuid"] for role in response_data.data]

then type_dict could be renamed to something like values_for_description
and it will allow to remove method self.find_specific_list_of_users and it is possible to join(",") in log assignment method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants