|
| 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 std::net::IpAddr; |
| 8 | + |
| 9 | +use crate::SqlU16; |
| 10 | +use crate::schema::{audit_log, audit_log_complete}; |
| 11 | +use chrono::{DateTime, Utc}; |
| 12 | +use diesel::prelude::*; |
| 13 | +use ipnetwork::IpNetwork; |
| 14 | +use nexus_types::external_api::views; |
| 15 | +use uuid::Uuid; |
| 16 | + |
| 17 | +#[derive(Queryable, Insertable, Selectable, Clone, Debug)] |
| 18 | +#[diesel(table_name = audit_log)] |
| 19 | +pub struct AuditLogEntryInit { |
| 20 | + pub id: Uuid, |
| 21 | + pub timestamp: DateTime<Utc>, |
| 22 | + pub request_id: String, |
| 23 | + /// The API endpoint being logged, e.g., `project_create` |
| 24 | + pub request_uri: String, |
| 25 | + pub operation_id: String, |
| 26 | + pub source_ip: IpNetwork, |
| 27 | + // TODO: we probably want a dedicated enum for these columns and for that |
| 28 | + // we need a fancier set of columns. For example, we may want to initialize |
| 29 | + // the row with a _potential_ actor (probably a different field), like the |
| 30 | + // username or whatever is being used for login. This should probably be |
| 31 | + // preserved even after authentication determines an actual actor ID. See |
| 32 | + // the Actor struct in nexus/auth/src/authn/mod.ts |
| 33 | + |
| 34 | + // these are optional because of requests like login attempts, where there |
| 35 | + // is no actor until after the operation. |
| 36 | + pub actor_id: Option<Uuid>, |
| 37 | + pub actor_silo_id: Option<Uuid>, |
| 38 | + |
| 39 | + // TODO: fancier type for access method capturing possibility of login |
| 40 | + // attempts. might make sense to roll this all into the actor enum because |
| 41 | + // we have an access method if and only if we have an actor (I think) |
| 42 | + /// API token or session cookie. Optional because it will not be defined |
| 43 | + /// on unauthenticated requests like login attempts. |
| 44 | + pub access_method: Option<String>, |
| 45 | +} |
| 46 | + |
| 47 | +// TODO: doc comments |
| 48 | +// TODO: figure out how this relates to the other struct. currently we're not |
| 49 | +// retrieving partial entries at all, but I think we will probably want to have |
| 50 | +// that capability |
| 51 | +#[derive(Queryable, Selectable, Clone, Debug)] |
| 52 | +#[diesel(table_name = audit_log_complete)] |
| 53 | +pub struct AuditLogEntry { |
| 54 | + pub id: Uuid, |
| 55 | + pub timestamp: DateTime<Utc>, |
| 56 | + pub request_id: String, |
| 57 | + pub request_uri: String, |
| 58 | + pub operation_id: String, |
| 59 | + pub source_ip: IpNetwork, |
| 60 | + pub actor_id: Option<Uuid>, |
| 61 | + pub actor_silo_id: Option<Uuid>, |
| 62 | + pub access_method: Option<String>, |
| 63 | + |
| 64 | + // TODO: RFD 523 says: "Additionally, the response (or error) data should be |
| 65 | + // included in the same log entry as the original request data. Separating |
| 66 | + // the response from the request into two different log entries is extremely |
| 67 | + // expensive for customers to identify which requests correspond to which |
| 68 | + // responses." I guess the typical thing is to include a duration of the |
| 69 | + // request rather than a second timestamp. |
| 70 | + |
| 71 | + // Seems like it has to be optional because at the beginning of the |
| 72 | + // operation, we have not yet resolved the resource selector to an ID |
| 73 | + pub resource_id: Option<Uuid>, |
| 74 | + |
| 75 | + // Fields that are not present on init |
| 76 | + /// Time log entry was completed with info about result of operation |
| 77 | + pub time_completed: DateTime<Utc>, |
| 78 | + pub http_status_code: SqlU16, |
| 79 | + |
| 80 | + // Error information if the action failed |
| 81 | + pub error_code: Option<String>, |
| 82 | + pub error_message: Option<String>, |
| 83 | + // TODO: including a real response complicates things |
| 84 | + // Response data on success (if applicable) |
| 85 | + // pub success_response: Option<Value>, |
| 86 | +} |
| 87 | + |
| 88 | +impl AuditLogEntryInit { |
| 89 | + pub fn new( |
| 90 | + request_id: String, |
| 91 | + operation_id: String, |
| 92 | + request_uri: String, |
| 93 | + source_ip: IpAddr, |
| 94 | + actor_id: Option<Uuid>, |
| 95 | + actor_silo_id: Option<Uuid>, |
| 96 | + access_method: Option<String>, |
| 97 | + ) -> Self { |
| 98 | + Self { |
| 99 | + id: Uuid::new_v4(), |
| 100 | + timestamp: Utc::now(), |
| 101 | + request_id, |
| 102 | + request_uri, |
| 103 | + operation_id, |
| 104 | + actor_id, |
| 105 | + actor_silo_id, |
| 106 | + source_ip: source_ip.into(), |
| 107 | + access_method, |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +#[derive(AsChangeset, Clone)] |
| 113 | +#[diesel(table_name = audit_log)] |
| 114 | +pub struct AuditLogCompletion { |
| 115 | + pub time_completed: DateTime<Utc>, |
| 116 | + pub http_status_code: SqlU16, |
| 117 | +} |
| 118 | + |
| 119 | +impl AuditLogCompletion { |
| 120 | + pub fn new(http_status_code: u16) -> Self { |
| 121 | + Self { |
| 122 | + time_completed: Utc::now(), |
| 123 | + http_status_code: SqlU16(http_status_code), |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// TODO: AuditLogActor |
| 129 | +// pub enum AuditLogActor { |
| 130 | +// UserBuiltin { user_builtin_id: Uuid }, |
| 131 | +// TODO: include info about computed roles at runtime? |
| 132 | +// SiloUser { silo_user_id: Uuid, silo_id: Uuid }, |
| 133 | +// Unauthenticated, |
| 134 | +// } |
| 135 | + |
| 136 | +impl From<AuditLogEntry> for views::AuditLogEntry { |
| 137 | + fn from(entry: AuditLogEntry) -> Self { |
| 138 | + Self { |
| 139 | + id: entry.id, |
| 140 | + timestamp: entry.timestamp, |
| 141 | + request_id: entry.request_id, |
| 142 | + request_uri: entry.request_uri, |
| 143 | + operation_id: entry.operation_id, |
| 144 | + source_ip: entry.source_ip.ip(), |
| 145 | + resource_id: entry.resource_id, |
| 146 | + actor_id: entry.actor_id, |
| 147 | + actor_silo_id: entry.actor_silo_id, |
| 148 | + access_method: entry.access_method, |
| 149 | + time_completed: entry.time_completed, |
| 150 | + http_status_code: entry.http_status_code.0, |
| 151 | + error_code: entry.error_code, |
| 152 | + error_message: entry.error_message, |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments