Skip to content

Commit

Permalink
Instrument more audit log events
Browse files Browse the repository at this point in the history
  • Loading branch information
britmyerss committed Dec 18, 2024
1 parent 68eca14 commit c603164
Show file tree
Hide file tree
Showing 25 changed files with 1,339 additions and 80 deletions.
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) => {
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!("{0}-{1}", 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
14 changes: 13 additions & 1 deletion 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 @@ -32,7 +33,7 @@ pub async fn create_func_argument(
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: request.kind.to_string(),
element_kind: request.element_kind.map(|a| a.to_string()),
},
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?;
}
}
}

Expand Down
Loading

0 comments on commit c603164

Please sign in to comment.