|
| 1 | +use axum::{ |
| 2 | + extract::{Path, State}, |
| 3 | + http::StatusCode, |
| 4 | + response::IntoResponse, |
| 5 | + routing::get, |
| 6 | + Extension, Json, Router, |
| 7 | +}; |
| 8 | +use serde::{Deserialize, Serialize}; |
| 9 | +use std::sync::Arc; |
| 10 | +use utoipa::ToSchema; |
| 11 | + |
| 12 | +use temps_auth::{permission_guard, RequireAuth}; |
| 13 | +use temps_core::audit::{AuditContext, AuditOperation}; |
| 14 | +use temps_core::problemdetails::Problem; |
| 15 | +use temps_core::RequestMetadata; |
| 16 | + |
| 17 | +use crate::handlers::AppState; |
| 18 | +use crate::services::secret_service::SecretType; |
| 19 | + |
| 20 | +// ── Request / Response DTOs ────────────────────────────────────────────────── |
| 21 | + |
| 22 | +#[derive(Debug, Deserialize, ToSchema)] |
| 23 | +pub struct UpsertSecretRequest { |
| 24 | + pub name: String, |
| 25 | + /// "env" (environment variable) or "file" (written to mount_path) |
| 26 | + #[serde(default = "default_env")] |
| 27 | + pub secret_type: String, |
| 28 | + pub value: String, |
| 29 | + /// Required for "file" type secrets — absolute path inside the sandbox |
| 30 | + pub mount_path: Option<String>, |
| 31 | + pub description: Option<String>, |
| 32 | +} |
| 33 | + |
| 34 | +fn default_env() -> String { |
| 35 | + "env".to_string() |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Debug, Serialize, ToSchema)] |
| 39 | +pub struct SecretResponse { |
| 40 | + pub id: i32, |
| 41 | + pub name: String, |
| 42 | + pub secret_type: String, |
| 43 | + /// Always masked in responses |
| 44 | + pub value: String, |
| 45 | + pub mount_path: Option<String>, |
| 46 | + pub description: Option<String>, |
| 47 | + pub created_at: String, |
| 48 | + pub updated_at: String, |
| 49 | +} |
| 50 | + |
| 51 | +impl From<temps_entities::agent_secrets::Model> for SecretResponse { |
| 52 | + fn from(model: temps_entities::agent_secrets::Model) -> Self { |
| 53 | + Self { |
| 54 | + id: model.id, |
| 55 | + name: model.name, |
| 56 | + secret_type: model.secret_type, |
| 57 | + value: "***".to_string(), |
| 58 | + mount_path: model.mount_path, |
| 59 | + description: model.description, |
| 60 | + created_at: model.created_at.to_rfc3339(), |
| 61 | + updated_at: model.updated_at.to_rfc3339(), |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[derive(Debug, Serialize, ToSchema)] |
| 67 | +pub struct ListSecretsResponse { |
| 68 | + pub items: Vec<SecretResponse>, |
| 69 | + pub total: usize, |
| 70 | +} |
| 71 | + |
| 72 | +// ── Audit structs ──────────────────────────────────────────────────────────── |
| 73 | + |
| 74 | +#[derive(Debug, Clone, Serialize)] |
| 75 | +struct SecretUpsertedAudit { |
| 76 | + context: AuditContext, |
| 77 | + secret_name: String, |
| 78 | +} |
| 79 | + |
| 80 | +#[derive(Debug, Clone, Serialize)] |
| 81 | +struct SecretDeletedAudit { |
| 82 | + context: AuditContext, |
| 83 | + secret_name: String, |
| 84 | +} |
| 85 | + |
| 86 | +impl AuditOperation for SecretUpsertedAudit { |
| 87 | + fn operation_type(&self) -> String { |
| 88 | + "SECRET_UPSERTED".to_string() |
| 89 | + } |
| 90 | + fn user_id(&self) -> i32 { |
| 91 | + self.context.user_id |
| 92 | + } |
| 93 | + fn ip_address(&self) -> Option<String> { |
| 94 | + self.context.ip_address.clone() |
| 95 | + } |
| 96 | + fn user_agent(&self) -> &str { |
| 97 | + &self.context.user_agent |
| 98 | + } |
| 99 | + fn serialize(&self) -> temps_core::anyhow::Result<String> { |
| 100 | + serde_json::to_string(self) |
| 101 | + .map_err(|e| temps_core::anyhow::anyhow!("Failed to serialize audit: {}", e)) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl AuditOperation for SecretDeletedAudit { |
| 106 | + fn operation_type(&self) -> String { |
| 107 | + "SECRET_DELETED".to_string() |
| 108 | + } |
| 109 | + fn user_id(&self) -> i32 { |
| 110 | + self.context.user_id |
| 111 | + } |
| 112 | + fn ip_address(&self) -> Option<String> { |
| 113 | + self.context.ip_address.clone() |
| 114 | + } |
| 115 | + fn user_agent(&self) -> &str { |
| 116 | + &self.context.user_agent |
| 117 | + } |
| 118 | + fn serialize(&self) -> temps_core::anyhow::Result<String> { |
| 119 | + serde_json::to_string(self) |
| 120 | + .map_err(|e| temps_core::anyhow::anyhow!("Failed to serialize audit: {}", e)) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// ── Routes ─────────────────────────────────────────────────────────────────── |
| 125 | + |
| 126 | +pub fn routes() -> Router<Arc<AppState>> { |
| 127 | + Router::new() |
| 128 | + .route("/settings/secrets", get(list_secrets).post(upsert_secret)) |
| 129 | + .route( |
| 130 | + "/settings/secrets/{name}", |
| 131 | + axum::routing::delete(delete_secret), |
| 132 | + ) |
| 133 | +} |
| 134 | + |
| 135 | +// ── Handlers ───────────────────────────────────────────────────────────────── |
| 136 | + |
| 137 | +#[utoipa::path( |
| 138 | + tag = "Secrets", |
| 139 | + get, |
| 140 | + path = "/settings/secrets", |
| 141 | + responses( |
| 142 | + (status = 200, description = "List of global agent secrets", body = ListSecretsResponse), |
| 143 | + (status = 401, description = "Unauthorized"), |
| 144 | + (status = 403, description = "Insufficient permissions"), |
| 145 | + ), |
| 146 | + security(("bearer_auth" = [])) |
| 147 | +)] |
| 148 | +async fn list_secrets( |
| 149 | + RequireAuth(auth): RequireAuth, |
| 150 | + State(app_state): State<Arc<AppState>>, |
| 151 | +) -> Result<impl IntoResponse, Problem> { |
| 152 | + permission_guard!(auth, SettingsRead); |
| 153 | + |
| 154 | + let secrets = app_state |
| 155 | + .secret_service |
| 156 | + .list_secrets() |
| 157 | + .await |
| 158 | + .map_err(Problem::from)?; |
| 159 | + |
| 160 | + let total = secrets.len(); |
| 161 | + Ok(Json(ListSecretsResponse { |
| 162 | + items: secrets.into_iter().map(SecretResponse::from).collect(), |
| 163 | + total, |
| 164 | + })) |
| 165 | +} |
| 166 | + |
| 167 | +#[utoipa::path( |
| 168 | + tag = "Secrets", |
| 169 | + post, |
| 170 | + path = "/settings/secrets", |
| 171 | + request_body = UpsertSecretRequest, |
| 172 | + responses( |
| 173 | + (status = 201, description = "Secret created/updated", body = SecretResponse), |
| 174 | + (status = 400, description = "Validation error"), |
| 175 | + (status = 401, description = "Unauthorized"), |
| 176 | + (status = 403, description = "Insufficient permissions"), |
| 177 | + ), |
| 178 | + security(("bearer_auth" = [])) |
| 179 | +)] |
| 180 | +async fn upsert_secret( |
| 181 | + RequireAuth(auth): RequireAuth, |
| 182 | + State(app_state): State<Arc<AppState>>, |
| 183 | + Extension(metadata): Extension<RequestMetadata>, |
| 184 | + Json(request): Json<UpsertSecretRequest>, |
| 185 | +) -> Result<impl IntoResponse, Problem> { |
| 186 | + permission_guard!(auth, SettingsWrite); |
| 187 | + |
| 188 | + let secret_type = match request.secret_type.as_str() { |
| 189 | + "file" => SecretType::File, |
| 190 | + _ => SecretType::Env, |
| 191 | + }; |
| 192 | + |
| 193 | + let secret = app_state |
| 194 | + .secret_service |
| 195 | + .upsert_secret( |
| 196 | + &request.name, |
| 197 | + secret_type, |
| 198 | + &request.value, |
| 199 | + request.mount_path.as_deref(), |
| 200 | + request.description.as_deref(), |
| 201 | + ) |
| 202 | + .await |
| 203 | + .map_err(Problem::from)?; |
| 204 | + |
| 205 | + let audit = SecretUpsertedAudit { |
| 206 | + context: AuditContext { |
| 207 | + user_id: auth.user_id(), |
| 208 | + ip_address: Some(metadata.ip_address.clone()), |
| 209 | + user_agent: metadata.user_agent.clone(), |
| 210 | + }, |
| 211 | + secret_name: secret.name.clone(), |
| 212 | + }; |
| 213 | + if let Err(e) = app_state.audit_service.create_audit_log(&audit).await { |
| 214 | + tracing::error!( |
| 215 | + "Failed to create audit log for secret upsert (name {}): {}", |
| 216 | + secret.name, |
| 217 | + e |
| 218 | + ); |
| 219 | + } |
| 220 | + |
| 221 | + Ok((StatusCode::CREATED, Json(SecretResponse::from(secret)))) |
| 222 | +} |
| 223 | + |
| 224 | +#[utoipa::path( |
| 225 | + tag = "Secrets", |
| 226 | + delete, |
| 227 | + path = "/settings/secrets/{name}", |
| 228 | + params( |
| 229 | + ("name" = String, Path, description = "Secret name"), |
| 230 | + ), |
| 231 | + responses( |
| 232 | + (status = 204, description = "Secret deleted"), |
| 233 | + (status = 404, description = "Secret not found"), |
| 234 | + (status = 401, description = "Unauthorized"), |
| 235 | + (status = 403, description = "Insufficient permissions"), |
| 236 | + ), |
| 237 | + security(("bearer_auth" = [])) |
| 238 | +)] |
| 239 | +async fn delete_secret( |
| 240 | + RequireAuth(auth): RequireAuth, |
| 241 | + State(app_state): State<Arc<AppState>>, |
| 242 | + Extension(metadata): Extension<RequestMetadata>, |
| 243 | + Path(name): Path<String>, |
| 244 | +) -> Result<impl IntoResponse, Problem> { |
| 245 | + permission_guard!(auth, SettingsWrite); |
| 246 | + |
| 247 | + app_state |
| 248 | + .secret_service |
| 249 | + .delete_secret(&name) |
| 250 | + .await |
| 251 | + .map_err(Problem::from)?; |
| 252 | + |
| 253 | + let audit = SecretDeletedAudit { |
| 254 | + context: AuditContext { |
| 255 | + user_id: auth.user_id(), |
| 256 | + ip_address: Some(metadata.ip_address.clone()), |
| 257 | + user_agent: metadata.user_agent.clone(), |
| 258 | + }, |
| 259 | + secret_name: name.clone(), |
| 260 | + }; |
| 261 | + if let Err(e) = app_state.audit_service.create_audit_log(&audit).await { |
| 262 | + tracing::error!( |
| 263 | + "Failed to create audit log for secret delete (name {}): {}", |
| 264 | + &name, |
| 265 | + e |
| 266 | + ); |
| 267 | + } |
| 268 | + |
| 269 | + Ok(StatusCode::NO_CONTENT) |
| 270 | +} |
0 commit comments