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

2718 audit project provisioning 2 #2818

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

rorymckinley
Copy link
Collaborator

Description

Audit when a project is inserted, updated or deleted via the provisioning api.

This PR includes changes to the tests to in preparation for the new code and this makes the PR a bit noisy. Working though it commit-by-commit will make it easier to follow. The actual code change is in this commit.

Closes #2718

Validation steps

  • Copy the attached files to /tmp on your workstation.

delete.json
insert.json
update.json

  • In an IEx session run the following and keep the session open:
import Lightning.Repo
import Ecto.Query
latest_provisioner_event_query = from a in Lightning.Auditing.Audit, where: like(a.event, "%by_provisioner"), order_by: [desc: :inserted_at], limit: 1
  • In your terminal, set the API_TOKEN var to your users api token for your local Lightning:
API_TOKEN='...'

From the terminal execute an insertion:

curl -X POST -d @/tmp/insert.json -H "Authorization: Bearer $API_TOKEN" -H "Content-Type: application/json" http://localhost:4000/api/provision

Then, in your IEx session:

latest_event = Repo.one!(latest_provisioner_event_query)

The event attribute of latest_event should be set to inserted_by_provisioner.

From the terminal execute an update:

curl -X POST -d @/tmp/update.json -H "Authorization: Bearer $API_TOKEN" -H "Content-Type: application/json" http://localhost:4000/api/provision

Then, in your IEx session:

latest_event = Repo.one!(latest_provisioner_event_query)

The event attribute of latest_event should be set to updated_by_provisioner.

From the terminal execute a deletion:

curl -X POST -d @/tmp/delete.json -H "Authorization: Bearer $API_TOKEN" -H "Content-Type: application/json" http://localhost:4000/api/provision

Then, in your IEx session:

latest_event = Repo.one!(latest_provisioner_event_query)

The event attribute of latest_event should be set to deleted_by_provisioner.

AI Usage

Please disclose how you've used AI in this work (it's cool, we just want to know!):

  • Code generation (copilot but not intellisense)
  • Learning or fact checking
  • Strategy / design
  • Optimisation / refactoring
  • Translation / spellchecking / doc gen
  • Other
  • I have not used AI

You can read more details in our Responsible AI Policy

Pre-submission checklist

  • I have performed a self-review of my code.
  • I have implemented and tested all related authorization policies. (e.g., :owner, :admin, :editor, :viewer)
  • I have updated the changelog.
  • I have ticked a box in "AI usage" in this PR

Copy link

codecov bot commented Jan 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.40%. Comparing base (abad0f3) to head (e496b47).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2818      +/-   ##
==========================================
+ Coverage   91.37%   91.40%   +0.03%     
==========================================
  Files         338      338              
  Lines       12052    12067      +15     
==========================================
+ Hits        11012    11030      +18     
+ Misses       1040     1037       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rorymckinley rorymckinley marked this pull request as ready for review January 14, 2025 14:24
@rorymckinley rorymckinley requested a review from jyeshe January 14, 2025 14:24
Copy link
Member

@jyeshe jyeshe left a comment

Choose a reason for hiding this comment

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

Thanks for the detailed explanation on the PR Rory. The shortcut link to the commit was helpful.
Left only one request for a higher level function on audit_workflows 🙏

Comment on lines +84 to +87
project_changeset
|> get_assoc(:workflows)
|> Enum.reduce(
Multi.new(),
Copy link
Member

Choose a reason for hiding this comment

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

Being already part of a transaction it wasn't necessary to be on a Multi. Nonetheless, I see little benefit from refactoring it to a reduce_while for now.

Comment on lines +90 to +99
classify_audit(workflow_changeset)
|> case do
{:no_action, _nil} ->
Multi.new()

{action, workflow_id} ->
audit_workflow_multi(action, workflow_id, user_or_repo_connection)
end

Multi.append(multi, additional_multi)
Copy link
Member

Choose a reason for hiding this comment

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

I imagine it would be more readable replacing the case assignment with a function append_multi or a smarter audit_workflow_multi that does all the logic. Here on the high level would contain only the iteration.

data: %{id: workflow_id},
changes: changes
})
when changes != %{} do
Copy link
Member

Choose a reason for hiding this comment

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

Got it, that's why you haven't used the Changeset.get_field


def provisioner_event(action, workflow_id, actor) do
event(
"#{past_tense(action)}_by_provisioner",
Copy link
Member

Choose a reason for hiding this comment

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

Neat!

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

Successfully merging this pull request may close these issues.

Audit Provisioning Events
2 participants