|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/5.0/. |
| 4 | + |
| 5 | +// Copyright 2025 Oxide Computer Company |
| 6 | + |
| 7 | +use crate::schema::audit_log; |
| 8 | +use chrono::{DateTime, TimeDelta, Utc}; |
| 9 | +use diesel::prelude::*; |
| 10 | +use nexus_types::external_api::views; |
| 11 | +use uuid::Uuid; |
| 12 | + |
| 13 | +#[derive(Queryable, Insertable, Selectable, Clone, Debug)] |
| 14 | +#[diesel(table_name = audit_log)] |
| 15 | +pub struct AuditLogEntry { |
| 16 | + pub id: Uuid, |
| 17 | + pub timestamp: DateTime<Utc>, |
| 18 | + pub request_id: String, |
| 19 | + // TODO: this isn't in the RFD but it seems nice to have |
| 20 | + pub request_uri: String, |
| 21 | + pub operation_id: String, |
| 22 | + pub source_ip: String, |
| 23 | + pub resource_type: String, |
| 24 | + |
| 25 | + // TODO: we probably want a dedicated enum for these columns and for that |
| 26 | + // we need a fancier set of columns. For example, we may want to initialize |
| 27 | + // the row with a _potential_ actor (probably a different field), like the |
| 28 | + // username or whatever is being used for login. This should probably be |
| 29 | + // preserved even after authentication determines an actual actor ID. See |
| 30 | + // the Actor struct in nexus/auth/src/authn/mod.ts |
| 31 | + |
| 32 | + // these are optional because of requests like login attempts, where there |
| 33 | + // is no actor until after the operation. |
| 34 | + pub actor_id: Option<Uuid>, |
| 35 | + pub actor_silo_id: Option<Uuid>, |
| 36 | + |
| 37 | + /// The specific action that was attempted (create, delete, update, etc) |
| 38 | + pub action: String, // TODO: enum type? |
| 39 | + |
| 40 | + // TODO: we will need to add headers in the client to get this info |
| 41 | + // How the actor authenticated (api_key, console, etc) |
| 42 | + // pub access_method: String, |
| 43 | + |
| 44 | + // TODO: RFD 523 says: "Additionally, the response (or error) data should be |
| 45 | + // included in the same log entry as the original request data. Separating |
| 46 | + // the response from the request into two different log entries is extremely |
| 47 | + // expensive for customers to identify which requests correspond to which |
| 48 | + // responses." I guess the typical thing is to include a duration of the |
| 49 | + // request rather than a second timestamp. |
| 50 | + |
| 51 | + // Seems like it has to be optional because at the beginning of the |
| 52 | + // operation, we have not yet resolved the resource selector to an ID |
| 53 | + pub resource_id: Option<Uuid>, |
| 54 | + |
| 55 | + // Fields that are optional because they get filled in after the action completes |
| 56 | + /// Time in milliseconds between receiving request and responding |
| 57 | + pub duration: Option<TimeDelta>, |
| 58 | + |
| 59 | + // Error information if the action failed |
| 60 | + pub error_code: Option<String>, |
| 61 | + pub error_message: Option<String>, |
| 62 | + // TODO: including a real response complicates things |
| 63 | + // Response data on success (if applicable) |
| 64 | + // pub success_response: Option<Value>, |
| 65 | +} |
| 66 | + |
| 67 | +impl AuditLogEntry { |
| 68 | + pub fn new( |
| 69 | + request_id: String, |
| 70 | + operation_id: String, |
| 71 | + request_uri: String, |
| 72 | + actor_id: Option<Uuid>, |
| 73 | + actor_silo_id: Option<Uuid>, |
| 74 | + ) -> Self { |
| 75 | + Self { |
| 76 | + id: Uuid::new_v4(), |
| 77 | + timestamp: Utc::now(), |
| 78 | + request_id, |
| 79 | + request_uri, |
| 80 | + operation_id, |
| 81 | + actor_id, |
| 82 | + actor_silo_id, |
| 83 | + |
| 84 | + // TODO: actually get all these values |
| 85 | + source_ip: String::new(), |
| 86 | + resource_type: String::new(), |
| 87 | + action: String::new(), |
| 88 | + |
| 89 | + // fields that can only be filled in after the operation |
| 90 | + resource_id: None, |
| 91 | + duration: None, |
| 92 | + error_code: None, |
| 93 | + error_message: None, |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// TODO: Add a struct representing only the fields set at log entry init time, |
| 99 | +// use as an arg to the datastore init function to make misuse harder |
| 100 | + |
| 101 | +// TODO: AuditLogActor |
| 102 | +// pub enum AuditLogActor { |
| 103 | +// UserBuiltin { user_builtin_id: Uuid }, |
| 104 | +// TODO: include info about computed roles at runtime? |
| 105 | +// SiloUser { silo_user_id: Uuid, silo_id: Uuid }, |
| 106 | +// Unauthenticated, |
| 107 | +// } |
| 108 | + |
| 109 | +impl From<AuditLogEntry> for views::AuditLogEntry { |
| 110 | + fn from(entry: AuditLogEntry) -> Self { |
| 111 | + Self { |
| 112 | + id: entry.id, |
| 113 | + timestamp: entry.timestamp, |
| 114 | + request_id: entry.request_id, |
| 115 | + request_uri: entry.request_uri, |
| 116 | + operation_id: entry.operation_id, |
| 117 | + source_ip: entry.source_ip, |
| 118 | + resource_type: entry.resource_type, |
| 119 | + resource_id: entry.resource_id, |
| 120 | + actor_id: entry.actor_id, |
| 121 | + actor_silo_id: entry.actor_silo_id, |
| 122 | + action: entry.action, |
| 123 | + duration_ms: entry.duration.map(|d| d.num_milliseconds()), |
| 124 | + error_code: entry.error_code, |
| 125 | + error_message: entry.error_message, |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments