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

Instrument more audit log events #5151

Merged
merged 1 commit into from
Dec 18, 2024
Merged

Conversation

britmyerss
Copy link
Contributor

This PR adds coverage to the following areas:

  • Function Authoring (Test, Execute, Attach for each function kind, Detach, Delete, Create, Updating Metadata )
  • Schema Variant Authoring (Create, Regenerate, Delete, Unlock,)
  • Contributing a module
  • Components (Creating/Deleting Connections, Attaching/Orphaning)
  • Views (Create, Update, Delete)

To do still include:

  • Handling code changes
  • Handling View Diagram Objects
  • Updating Schema Variant Metadata
  • Installing a Module
  • More granular Func Binding updates (depending on how we want to approach this, for ex: updating the arguments for an attribute function)

@github-actions github-actions bot added A-sdf Area: Primary backend API service [Rust] A-dal labels Dec 18, 2024
Comment on lines +150 to +157
AttributeFuncDestination::Prop(prop_id) => Prop::get_by_id(ctx, *prop_id).await?.name,
AttributeFuncDestination::OutputSocket(output_socket_id) => {
OutputSocket::get_by_id(ctx, *output_socket_id)
.await?
.name()
.to_string()
}
AttributeFuncDestination::InputSocket(input_socket_id) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit for the future: I like to use Self when possible for readability and suppleness for refactoring. So the first one would be Self::Prop instead of AttributeFuncDestination::Prop.

to_socket_id: request.to_socket_id,
to_socket_name: to_socket_name.clone(),
},
format!("{0}-{1}", to_component_name, to_socket_name),
Copy link
Contributor

Choose a reason for hiding this comment

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

nit for the future: I'm a bit surprised the linter didn't catch this, but you can do the following:

format!("{to_component_name}-{to_socket_name}"),

Copy link
Contributor

Choose a reason for hiding this comment

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

You know, I didn't realize you could use positional numbers like 0 and 1. Before format! was able to use {variable} style syntax, the best you could do was format!("{}-{}", to_component_name, to_socket_name). Anyway, @nickgerace's idea is the most current Rustic way, agreed!

Copy link
Contributor

Choose a reason for hiding this comment

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

@fnichol year the positional numbers are interesting! I've used them in the past, moreso when I want to display something multiple times. They're cool!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will update this too!

Comment on lines +77 to +90
EventualParent::Component(component_id) => {
let component = Component::get_by_id(&ctx, component_id).await?;
ctx.write_audit_log(
AuditLogKind::DetachFunc {
func_id,
func_display_name: func.display_name.clone(),
schema_variant_id: None,
component_id: Some(component_id),
subject_name: component.name(&ctx).await?.to_owned(),
},
func.name.clone(),
)
.await?;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Great use of breaking out an if let into a match... sometimes you'll see uses of match when unnecessary to expose all the variants left unused to avoid bugs if more variants are added to the enum. Example:

enum MyEnum {
    One
    Two
    Three
}

impl MyEnum {
    pub fn name(&self) -> &'static str {
        match self {
            One => "one"
            Two => "one"
            Three => "one"
        }
    }
}

By using a match, we cover all the variants by default unless we use a wildcard, variable or match guard.

If that function instead used if let One = self { // code } else { // other code }, then adding new variants could cause undesired behavior in the else block.

I bring this all up since this is a great example of a sneaky if let that might not have been obvious from static code analysis, but you found it for this PR. Nice!

Comment on lines +367 to +369
let func = Func::get_by_id_or_error(&ctx, func_id).await?;
ctx.write_audit_log(
AuditLogKind::AttachManagementFunc {
Copy link
Contributor

Choose a reason for hiding this comment

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

Unrelated to your PR but this route is frightening with all the nested SHEESH! We should refactor this later...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, definitely worth a pass.

Comment on lines +260 to +282
ctx.write_audit_log(
AuditLogKind::CreateFunc {
func_display_name: func.display_name.clone(),
func_kind: func.kind.into(),
},
func.name.clone(),
)
.await?;
let schema_variant_name =
SchemaVariant::get_by_id_or_error(&ctx, schema_variant_id)
.await?
.display_name()
.to_string();
ctx.write_audit_log(
AuditLogKind::AttachCodeGenFunc {
func_id: func.id,
func_display_name: func.display_name.clone(),
schema_variant_id: Some(schema_variant_id),
component_id: None,
subject_name: schema_variant_name,
},
func.name.clone(),
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Right now we create audit logs outside of the dal, but I'm wondering if CreateFunc should just move to Func::new. Then again, some of the new methods are run by other copy or clone or duplicate methods... so the audit log doesn't always reveal the user intent...

Just rambling on your PR, but I'm thinking about making this less manual in some way. Feels like there's room for a crate in between the dal low level and the route authoring that could exist.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah I had similar thoughts - it's also notable we usually end up with 3 things per route, ws event(s), posthog events, and now audit events and usually the posthog and audit events are quite similar. Not sure what to do with it exactly, but it crossed my mind as I was in here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep... not this PR but it's starting to bubble over.

Comment on lines +58 to +62
let variant = if let Some(schema_variant_id) = request.schema_variant_id {
SchemaVariant::get_by_id(&ctx, schema_variant_id).await?
} else {
None
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Aren't all funcs attached to assets these days? How is this possible? (unrelated to your PR, just curious if you know)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Funcs can technically be attached to only components, it's in a bit of a half state though as we don't expose it in the UI today.

Comment on lines 121 to 122
// strings for now I suppose
kind: String,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh - this was from we discussed adding types to si-events that are only for audit events. I had this in there before that convo and never came back. I will update!

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah gotcha. I remember this. Sounds good!

new_color: String,
old_component_type: String,
new_component_type: String,
//todo: what to do about the code?
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this referring to?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just calling out that for updating the code for a schema variant definition, I wasn't sure how to handle that and punted :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Gotcha!

fnichol
fnichol previously approved these changes Dec 18, 2024
Copy link
Contributor

@fnichol fnichol left a comment

Choose a reason for hiding this comment

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

Not a small amount of change! Good news is that most of it is net-new.

Copy link
Contributor

@fnichol fnichol left a comment

Choose a reason for hiding this comment

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

All right!

@@ -83,6 +93,7 @@ pub async fn contribute(
"pkg_hash": response.latest_hash,
}),
);
ctx.commit().await?;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm guessing we want this now because ctx.write_audit_log().await? will flush its events on ctx.commit().await??

Copy link
Contributor

Choose a reason for hiding this comment

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

There was already a commit in the route! The bad GitHub diff got me too :/ took me a bit to find it

Copy link
Contributor

@nickgerace nickgerace Dec 18, 2024

Choose a reason for hiding this comment

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

Side note: there may also be an equivalent to "publish immediately" for audit logs to skip the rebaser I think. I had it at some point... can't remember if it's still there.

@britmyerss britmyerss added this pull request to the merge queue Dec 18, 2024
Merged via the queue into main with commit b59baf6 Dec 18, 2024
8 checks passed
@britmyerss britmyerss deleted the brit/add-more-audit-events branch December 18, 2024 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-dal A-sdf Area: Primary backend API service [Rust]
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants