Skip to content

Commit 276366a

Browse files
committed
controllers/token: Add client IP address and headers to the log message
1 parent a913075 commit 276366a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/controllers/token.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use anyhow::Context;
66

77
use crate::app::AppState;
88
use crate::auth::AuthCheck;
9+
use crate::middleware::real_ip::RealIp;
910
use crate::models::token::{CrateScope, EndpointScope};
1011
use crate::util::errors::{AppResult, bad_request, custom};
1112
use crate::util::token::PlainToken;
@@ -20,8 +21,8 @@ use diesel::dsl::{IntervalDsl, now};
2021
use diesel::prelude::*;
2122
use diesel::sql_types::Timestamptz;
2223
use diesel_async::RunQueryDsl;
23-
use http::StatusCode;
2424
use http::request::Parts;
25+
use http::{StatusCode, header};
2526
use minijinja::context;
2627
use secrecy::ExposeSecret;
2728
use serde::{Deserialize, Serialize};
@@ -129,7 +130,16 @@ pub async fn create_api_token(
129130

130131
// Check if token creation is disabled
131132
if let Some(disable_message) = &app.config.disable_token_creation {
133+
let client_ip = parts.extensions.get::<RealIp>().map(|ip| ip.to_string());
134+
let client_ip = client_ip.as_deref().unwrap_or("unknown");
135+
136+
let mut headers = parts.headers.clone();
137+
headers.remove(header::AUTHORIZATION);
138+
headers.remove(header::COOKIE);
139+
132140
warn!(
141+
network.client.ip = client_ip,
142+
http.headers = ?headers,
133143
"Blocked token creation for user `{}` (id: {}) due to disabled flag (token name: `{}`)",
134144
user.gh_login, user.id, new.api_token.name
135145
);

0 commit comments

Comments
 (0)