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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/dal/src/func/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ pub enum FuncArgumentKind {
String,
}

impl From<FuncArgumentKind> for si_events::FuncArgumentKind {
fn from(func_argument_kind: FuncArgumentKind) -> Self {
match func_argument_kind {
FuncArgumentKind::Any => si_events::FuncArgumentKind::Any,
FuncArgumentKind::Array => si_events::FuncArgumentKind::Array,
FuncArgumentKind::Boolean => si_events::FuncArgumentKind::Boolean,
FuncArgumentKind::Integer => si_events::FuncArgumentKind::Integer,
FuncArgumentKind::Json => si_events::FuncArgumentKind::Json,
FuncArgumentKind::Map => si_events::FuncArgumentKind::Map,
FuncArgumentKind::Object => si_events::FuncArgumentKind::Object,
FuncArgumentKind::String => si_events::FuncArgumentKind::String,
}
}
}

impl From<PropKind> for FuncArgumentKind {
fn from(prop_kind: PropKind) -> Self {
match prop_kind {
Expand Down
6 changes: 3 additions & 3 deletions lib/dal/src/func/authoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl FuncAuthoringClient {
name: impl Into<String>,
kind: FuncArgumentKind,
element_kind: Option<FuncArgumentKind>,
) -> FuncAuthoringResult<()> {
) -> FuncAuthoringResult<FuncArgument> {
let func = Func::get_by_id_or_error(ctx, id).await?;
// don't create a func argument if the function is locked
func.error_if_locked()?;
Expand All @@ -354,9 +354,9 @@ impl FuncAuthoringClient {
));
}

let _func_argument = FuncArgument::new(ctx, name, kind, element_kind, id).await?;
let func_argument = FuncArgument::new(ctx, name, kind, element_kind, id).await?;

Ok(())
Ok(func_argument)
}

/// Deletes a [`FuncArgument`].
Expand Down
25 changes: 22 additions & 3 deletions lib/dal/src/func/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use crate::{
FuncError, FuncId, PropId, SchemaVariantError, SchemaVariantId,
};
use crate::{
ComponentId, InputSocketId, OutputSocketId, SchemaError, SchemaId, SchemaVariant,
WorkspaceSnapshotError, WsEventError,
ComponentId, InputSocket, InputSocketId, OutputSocket, OutputSocketId, Prop, SchemaError,
SchemaId, SchemaVariant, WorkspaceSnapshotError, WsEventError,
};

pub use attribute_argument::AttributeArgumentBinding;
Expand Down Expand Up @@ -127,7 +127,7 @@ pub enum AttributeFuncDestination {
}

impl AttributeFuncDestination {
pub(crate) async fn find_schema_variant(
pub async fn find_schema_variant(
&self,
ctx: &DalContext,
) -> FuncBindingResult<SchemaVariantId> {
Expand All @@ -144,6 +144,25 @@ impl AttributeFuncDestination {
};
Ok(schema_variant_id)
}

pub async fn get_name_of_destination(&self, ctx: &DalContext) -> FuncBindingResult<String> {
let name = match self {
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) => {
Comment on lines +150 to +157
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.

InputSocket::get_by_id(ctx, *input_socket_id)
.await?
.name()
.to_string()
}
};
Ok(name)
}
}

/// Represents at what level a given Prototype is attached to
Expand Down
27 changes: 25 additions & 2 deletions lib/sdf-server/src/service/diagram/create_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ use axum::{
extract::{Host, OriginalUri},
Json,
};
use dal::diagram::SummaryDiagramInferredEdge;
use dal::{
change_status::ChangeStatus, diagram::SummaryDiagramEdge, ChangeSet, Component, ComponentId,
InputSocketId, OutputSocketId, Visibility, WsEvent,
InputSocket, InputSocketId, OutputSocketId, Visibility, WsEvent,
};
use dal::{diagram::SummaryDiagramInferredEdge, OutputSocket};
use serde::{Deserialize, Serialize};
use si_events::audit_log::AuditLogKind;

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -112,6 +113,28 @@ pub async fn create_connection(
.await?;
}
}
let to_component_name = to_component.name(&ctx).await?;
let to_socket_name = InputSocket::get_by_id(&ctx, request.to_socket_id)
.await?
.name()
.to_string();
ctx.write_audit_log(
AuditLogKind::CreateConnection {
from_component_id: request.from_component_id,
from_component_name: from_component.name(&ctx).await?,
from_socket_id: request.from_socket_id,
from_socket_name: OutputSocket::get_by_id(&ctx, request.from_socket_id)
.await?
.name()
.to_string(),
to_component_id: request.to_component_id,
to_component_name: to_component_name.clone(),
to_socket_id: request.to_socket_id,
to_socket_name: to_socket_name.clone(),
},
format!("{to_component_name} --- {to_socket_name}"),
)
.await?;

ctx.commit().await?;

Expand Down
23 changes: 23 additions & 0 deletions lib/sdf-server/src/service/diagram/delete_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use dal::{
InputSocket, InputSocketId, OutputSocket, OutputSocketId, Visibility, WsEvent,
};
use serde::{Deserialize, Serialize};
use si_events::audit_log::AuditLogKind;

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -134,6 +135,28 @@ pub async fn delete_connection(

let to_component_schema =
Component::schema_for_component_id(&ctx, request.to_component_id).await?;
let to_component_name = to_component.name(&ctx).await?;
let to_socket_name = InputSocket::get_by_id(&ctx, request.to_socket_id)
.await?
.name()
.to_string();
ctx.write_audit_log(
AuditLogKind::DeleteConnection {
from_component_id: request.from_component_id,
from_component_name: from_component.name(&ctx).await?,
from_socket_id: request.from_socket_id,
from_socket_name: OutputSocket::get_by_id(&ctx, request.from_socket_id)
.await?
.name()
.to_string(),
to_component_id: request.to_component_id,
to_component_name: to_component_name.clone(),
to_socket_id: request.to_socket_id,
to_socket_name: to_socket_name.clone(),
},
format!("{0}-{1}", to_component_name, to_socket_name),
)
.await?;
track(
&posthog_client,
&ctx,
Expand Down
4 changes: 3 additions & 1 deletion lib/sdf-server/src/service/v2/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use dal::{
runner::FuncRunnerError,
},
workspace_snapshot::graph::WorkspaceSnapshotGraphError,
ChangeSetError, DalContext, Func, FuncError, FuncId, SchemaVariantError,
ChangeSetError, ComponentError, DalContext, Func, FuncError, FuncId, SchemaVariantError,
WorkspaceSnapshotError, WsEventError,
};
use si_frontend_types::FuncCode;
Expand Down Expand Up @@ -51,6 +51,8 @@ pub enum FuncAPIError {
CannotDeleteLockedFunc(FuncId),
#[error("change set error: {0}")]
ChangeSet(#[from] ChangeSetError),
#[error("component error: {0}")]
Component(#[from] ComponentError),
#[error("func error: {0}")]
Func(#[from] FuncError),
#[error("func already unlocked: {0}")]
Expand Down
16 changes: 14 additions & 2 deletions lib/sdf-server/src/service/v2/func/argument/create_argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use dal::{
WsEvent,
};
use frontend_types::FuncSummary;
use si_events::audit_log::AuditLogKind;
use si_frontend_types as frontend_types;

use crate::{
Expand All @@ -29,10 +30,10 @@ pub async fn create_func_argument(
.await?;
let force_change_set_id = ChangeSet::force_new(&mut ctx).await?;

FuncAuthoringClient::create_func_argument(
let new_arg = FuncAuthoringClient::create_func_argument(
&ctx,
func_id,
request.name,
request.name.clone(),
request.kind.into(),
request.element_kind.map(Into::into),
)
Expand All @@ -42,6 +43,17 @@ pub async fn create_func_argument(
.await?
.into_frontend_type(&ctx)
.await?;
ctx.write_audit_log(
AuditLogKind::CreateFuncArgument {
func_id: func_summary.func_id,
func_display_name: func_summary.display_name.clone(),
func_name: func_summary.name.clone(),
kind: new_arg.kind.into(),
element_kind: new_arg.element_kind.map(|a| a.into()),
},
request.name,
)
.await?;
WsEvent::func_updated(&ctx, func_summary.clone(), None)
.await?
.publish_on_commit(&ctx)
Expand Down
18 changes: 16 additions & 2 deletions lib/sdf-server/src/service/v2/func/argument/delete_argument.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use axum::extract::{Host, OriginalUri, Path};
use dal::{
func::{argument::FuncArgumentId, authoring::FuncAuthoringClient},
func::{
argument::{FuncArgument, FuncArgumentId},
authoring::FuncAuthoringClient,
},
ChangeSet, ChangeSetId, Func, FuncId, WorkspacePk, WsEvent,
};
use si_events::audit_log::AuditLogKind;
use si_frontend_types::FuncSummary;

use crate::{
Expand All @@ -28,6 +32,7 @@ pub async fn delete_func_argument(
.build(access_builder.build(change_set_id.into()))
.await?;
let force_change_set_id = ChangeSet::force_new(&mut ctx).await?;
let func_arg = FuncArgument::get_by_id_or_error(&ctx, func_argument_id).await?;
FuncAuthoringClient::delete_func_argument(&ctx, func_argument_id).await?;

let func_summary = Func::get_by_id_or_error(&ctx, func_id)
Expand All @@ -52,7 +57,16 @@ pub async fn delete_func_argument(
"func_kind": func_summary.kind.clone(),
}),
);

ctx.write_audit_log(
AuditLogKind::DeleteFuncArgument {
func_id,
func_display_name: func_summary.display_name.clone(),
func_name: func_summary.name.clone(),
func_argument_id,
},
func_arg.name,
)
.await?;
ctx.commit().await?;

Ok(ForceChangeSetResponse::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use axum::{
};
use dal::{
func::binding::{attribute::AttributeBinding, EventualParent},
ChangeSet, ChangeSetId, Func, FuncId, SchemaVariant, WorkspacePk, WsEvent,
ChangeSet, ChangeSetId, Component, Func, FuncId, SchemaVariant, WorkspacePk, WsEvent,
};
use si_events::audit_log::AuditLogKind;
use si_frontend_types as frontend_types;

use crate::{
Expand Down Expand Up @@ -51,15 +52,42 @@ pub async fn reset_attribute_binding(
)
.await?;

if let EventualParent::SchemaVariant(schema_variant_id) = eventual_parent {
let schema =
SchemaVariant::schema_id_for_schema_variant_id(&ctx, schema_variant_id).await?;
let schema_variant = SchemaVariant::get_by_id_or_error(&ctx, schema_variant_id).await?;

WsEvent::schema_variant_updated(&ctx, schema, schema_variant)
.await?
.publish_on_commit(&ctx)
match eventual_parent {
EventualParent::SchemaVariant(schema_variant_id) => {
let schema =
SchemaVariant::schema_id_for_schema_variant_id(&ctx, schema_variant_id).await?;
let schema_variant =
SchemaVariant::get_by_id_or_error(&ctx, schema_variant_id).await?;
ctx.write_audit_log(
AuditLogKind::DetachFunc {
func_id,
func_display_name: func.display_name.clone(),
schema_variant_id: Some(schema_variant_id),
component_id: None,
subject_name: schema_variant.display_name().to_owned(),
},
func.name.clone(),
)
.await?;
WsEvent::schema_variant_updated(&ctx, schema, schema_variant)
.await?
.publish_on_commit(&ctx)
.await?;
}
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?;
}
Comment on lines +77 to +90
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!

}
}

Expand Down
Loading
Loading