Skip to content

Commit

Permalink
Merge pull request #5190 from systeminit/zack/audit-log-mgmt-ops
Browse files Browse the repository at this point in the history
feat: add audit trail and posthog tracking for templates
  • Loading branch information
zacharyhamm authored Dec 23, 2024
2 parents 2e02498 + 3c7b454 commit acb81b0
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 7 deletions.
20 changes: 19 additions & 1 deletion lib/sdf-server/src/service/v2/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use dal::{
SchemaVariantError, TransactionsError, WorkspacePk, WsEvent, WsEventError,
};
use serde::{Deserialize, Serialize};
use si_events::audit_log::AuditLogKind;
use si_layer_cache::LayerDbError;
use telemetry::prelude::*;
use thiserror::Error;
Expand Down Expand Up @@ -159,7 +160,7 @@ pub async fn run_prototype(
WsEvent::management_operations_complete(
&ctx,
request.request_ulid,
func.name,
func.name.clone(),
result.message.clone(),
result.status,
created_component_ids,
Expand All @@ -168,6 +169,23 @@ pub async fn run_prototype(
.publish_on_commit(&ctx)
.await?;

ctx.write_audit_log(
AuditLogKind::ManagementOperationsComplete {
component_id,
prototype_id,
func_id,
func_name: func.name.clone(),
status: match result.status {
ManagementFuncStatus::Ok => "ok",
ManagementFuncStatus::Error => "error",
}
.to_string(),
message: result.message.clone(),
},
func.name,
)
.await?;

ctx.commit().await?;

return Ok(ForceChangeSetResponse::new(
Expand Down
38 changes: 32 additions & 6 deletions lib/sdf-server/src/service/v2/management/generate_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use dal::{
ChangeSet, ChangeSetId, ComponentId, FuncId, SchemaVariantId, WorkspacePk, WsEvent,
};
use serde::{Deserialize, Serialize};
use si_events::audit_log::AuditLogKind;

use crate::extract::{AccessBuilder, HandlerContext, PosthogClient};

use super::{ManagementApiError, ManagementApiResult};
use super::{track, ManagementApiError, ManagementApiResult};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -34,9 +35,9 @@ pub struct GenerateTemplateResponse {
pub async fn generate_template(
HandlerContext(builder): HandlerContext,
AccessBuilder(access_builder): AccessBuilder,
PosthogClient(_posthog_client): PosthogClient,
OriginalUri(_original_uri): OriginalUri,
Host(_host_name): Host,
PosthogClient(posthog_client): PosthogClient,
OriginalUri(original_uri): OriginalUri,
Host(host_name): Host,
Path((_workspace_pk, change_set_id, view_id)): Path<(WorkspacePk, ChangeSetId, ViewId)>,
Json(request): Json<GenerateTemplateRequest>,
) -> ManagementApiResult<ForceChangeSetResponse<GenerateTemplateResponse>> {
Expand All @@ -60,7 +61,7 @@ pub async fn generate_template(

let func = FuncAuthoringClient::create_new_management_func(
&ctx,
Some(request.func_name),
Some(request.func_name.clone()),
new_variant.id(),
)
.await?;
Expand All @@ -76,7 +77,7 @@ pub async fn generate_template(

let return_value = serde_json::json!({
"status": "ok",
"message": format!("created {}", request.asset_name),
"message": format!("created {}", &request.asset_name),
"ops": {
"create": create_operations,
}
Expand Down Expand Up @@ -111,6 +112,31 @@ pub async fn generate_template(
.publish_on_commit(&ctx)
.await?;

track(
&posthog_client,
&ctx,
&original_uri,
&host_name,
"generate_template",
serde_json::json!({
"generated_schema_variant_id": new_variant.id,
"generated_prototype_id": prototype_id,
"generated_func_id": func.id,
}),
);

ctx.write_audit_log(
AuditLogKind::GenerateTemplate {
schema_variant_id: new_variant.id,
management_prototype_id: prototype_id,
func_id: func.id,
func_name: request.func_name,
asset_name: request.asset_name.to_owned(),
},
request.asset_name,
)
.await?;

ctx.commit().await?;

Ok(ForceChangeSetResponse::new(
Expand Down
74 changes: 74 additions & 0 deletions lib/si-events-rs/src/audit_log/v1.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use si_id::ManagementPrototypeId;
use strum::{Display, EnumDiscriminants};

use crate::{
Expand Down Expand Up @@ -187,12 +188,31 @@ pub enum AuditLogKindV1 {
name: String,
version: String,
},

GenerateTemplate {
schema_variant_id: SchemaVariantId,
management_prototype_id: ManagementPrototypeId,
func_id: FuncId,
func_name: String,
asset_name: String,
},

InstallWorkspace {
id: WorkspacePk,
name: String,
version: String,
},
Login,

ManagementOperationsComplete {
component_id: ComponentId,
prototype_id: ManagementPrototypeId,
func_id: FuncId,
func_name: String,
status: String,
message: Option<String>,
},

OrphanComponent {
component_id: ComponentId,
previous_parent_id: ComponentId,
Expand Down Expand Up @@ -559,14 +579,36 @@ pub enum AuditLogMetadataV1 {
name: String,
version: String,
},

#[serde(rename_all = "camelCase")]
GenerateTemplate {
schema_variant_id: SchemaVariantId,
management_prototype_id: ManagementPrototypeId,
func_id: FuncId,
func_name: String,
asset_name: String,
},

#[serde(rename_all = "camelCase")]
InstallWorkspace {
id: WorkspacePk,
name: String,
version: String,
},

#[serde(rename_all = "camelCase")]
Login,

#[serde(rename_all = "camelCase")]
ManagementOperationsComplete {
component_id: ComponentId,
prototype_id: ManagementPrototypeId,
func_id: FuncId,
func_name: String,
status: String,
message: Option<String>,
},

#[serde(rename_all = "camelCase")]
OrphanComponent {
component_id: ComponentId,
Expand Down Expand Up @@ -789,7 +831,11 @@ impl AuditLogMetadataV1 {
MetadataDiscrim::ExecuteFunc => ("Executed", Some("Function")),
MetadataDiscrim::ExportWorkspace => ("Exported", Some("Workspace")),
MetadataDiscrim::InstallWorkspace => ("Installed", Some("Workspace")),
MetadataDiscrim::GenerateTemplate => ("Generated", Some("Template")),
MetadataDiscrim::Login => ("Authenticated", None),
MetadataDiscrim::ManagementOperationsComplete => {
("Executed", Some("Management Operations"))
}
MetadataDiscrim::OrphanComponent => ("Orphaned", Some("Component")),
MetadataDiscrim::PutActionOnHold => ("Paused", Some("Action")),
MetadataDiscrim::RegenerateSchemaVariant => ("Regenerated", Some("Schema Variant")),
Expand Down Expand Up @@ -1094,10 +1140,38 @@ impl From<Kind> for Metadata {
Kind::ExportWorkspace { id, name, version } => {
Self::ExportWorkspace { id, name, version }
}
Kind::GenerateTemplate {
schema_variant_id,
management_prototype_id,
func_id,
func_name,
asset_name,
} => Self::GenerateTemplate {
schema_variant_id,
management_prototype_id,
func_id,
func_name,
asset_name,
},
Kind::InstallWorkspace { id, name, version } => {
Self::InstallWorkspace { id, name, version }
}
Kind::Login => Self::Login,
Kind::ManagementOperationsComplete {
component_id,
prototype_id,
func_id,
func_name,
status,
message,
} => Self::ManagementOperationsComplete {
component_id,
prototype_id,
func_id,
func_name,
status,
message,
},
Kind::OrphanComponent {
component_id,
previous_parent_id,
Expand Down

0 comments on commit acb81b0

Please sign in to comment.