Skip to content

Commit 4e4bd5c

Browse files
authored
fix: login_history missing username when password incorrect (#18587)
* fix: login_history missing username when password incorrect * fixup * chmod+x * fixup
1 parent 77e0832 commit 4e4bd5c

File tree

11 files changed

+317
-210
lines changed

11 files changed

+317
-210
lines changed

src/query/service/src/auth.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ impl Credential {
7070
Credential::NoNeed => CredentialType::NoNeed,
7171
}
7272
}
73+
74+
pub fn user_name(&self) -> Option<String> {
75+
match self {
76+
Credential::DatabendToken { .. } => None,
77+
Credential::Jwt { .. } => None,
78+
Credential::Password { name, .. } => Some(name.clone()),
79+
Credential::NoNeed => None,
80+
}
81+
}
7382
}
7483

7584
impl AuthMgr {

src/query/service/src/servers/http/middleware/session.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ impl<E> HTTPSessionEndpoint<E> {
395395

396396
let credential = get_credential(req, self.kind, self.endpoint_kind)?;
397397
login_history.auth_type = credential.type_name();
398+
399+
// Extract and record username from credential before authenticate
400+
// This ensures we log the username even if authentication failed
401+
if let Some(user_name) = credential.user_name() {
402+
login_history.user_name = user_name.clone();
403+
}
398404
let session_manager = SessionManager::instance();
399405

400406
let mut session = session_manager.create_session(SessionType::Dummy).await?;

tests/logging/check_logs_table.sh

Lines changed: 0 additions & 205 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Source query IDs from setup script
6+
if [ -z "$QUERY_ID" ]; then
7+
echo "Error: Query IDs not set. Run setup_test_data.sh first."
8+
echo "Usage: source setup_test_data.sh && ./check_logs_table.sh"
9+
exit 1
10+
fi
11+
12+
execute_query() {
13+
local sql="$1"
14+
curl -s -u root: -XPOST "http://localhost:8000/v1/query" -H 'Content-Type: application/json' -d "{\"sql\": \"$sql\", \"pagination\": {\"wait_time_secs\": 10}}"
15+
}
16+
17+
check_query_log() {
18+
local test_name=$1
19+
local query_id=$2
20+
local check_query=$3
21+
local expected_result=$4
22+
23+
local full_sql_query="$check_query"
24+
25+
if [ -n "$query_id" ] && [ "$query_id" != "null" ]; then
26+
full_sql_query+=" query_id = '$query_id'"
27+
fi
28+
29+
echo "$full_sql_query"
30+
response=$(execute_query "$full_sql_query")
31+
32+
result=$(echo "$response" | jq -r '.data[0][0]' | tr -d '"')
33+
if [ "$result" != "$expected_result" ]; then
34+
echo "Log table test #$test_name failed, Result: $result, Expected: $expected_result"
35+
exit 1
36+
else
37+
echo "Log table test #$test_name passed, Result: $result, Expected: $expected_result"
38+
fi
39+
}
40+
41+
42+
# Basic log table tests
43+
check_query_log "basic-1" "$QUERY_ID" "SELECT count(*) FROM system_history.log_history WHERE target = 'databend::log::profile' and" "1"
44+
45+
check_query_log "basic-2" "$QUERY_ID" "SELECT count(*) FROM system_history.profile_history WHERE" "1"
46+
47+
check_query_log "basic-3" "$QUERY_ID" "SELECT count(*) FROM system_history.log_history WHERE target = 'databend::log::query' and" "2"
48+
49+
check_query_log "basic-4" "$QUERY_ID" "SELECT count(*) FROM system_history.query_history WHERE" "1"
50+
51+
check_query_log "basic-6" "$SELECT_QUERY_ID" "SELECT count(*) FROM system_history.access_history WHERE" "1"
52+
53+
# Access history tests
54+
check_query_log "basic-7" "$CREATE_QUERY_ID" "SELECT object_modified_by_ddl[0]['object_name'] FROM system_history.access_history WHERE" "default.default.t"
55+
56+
check_query_log "basic-8" "$INSERT_QUERY_ID" "SELECT objects_modified[0]['object_name'] FROM system_history.access_history WHERE" "default.default.t"
57+
58+
check_query_log "basic-9" "$SELECT_QUERY_ID" "SELECT base_objects_accessed[0]['object_name'] FROM system_history.access_history WHERE" "default.default.t"
59+
60+
check_query_log "basic-10" "$CREATE_VIEW_QUERY_ID" "select query_text from system_history.query_history where" "CREATE VIEW v AS SELECT a FROM t"
61+
62+
check_query_log "basic-11" null "SELECT count(*) FROM system_history.login_history WHERE session_id = '$SELECT_SESSION_ID' " "1"
63+
64+
# Timezone tests - regression test for https://github.com/databendlabs/databend/pull/18059
65+
check_query_log "timezone-1" "$SELECT_QUERY_ID" "settings (timezone='Asia/Shanghai') SELECT DATE_DIFF(hour, timestamp, now()) FROM system_history.log_history WHERE target = 'databend::log::profile' and" "0"
66+
67+
check_query_log "timezone-2" "$SELECT_QUERY_ID" "settings (timezone='Asia/Shanghai') SELECT DATE_DIFF(hour, event_time, now()) FROM system_history.query_history WHERE" "0"
68+
69+
check_query_log "timezone-3" "$SELECT_QUERY_ID" "settings (timezone='Asia/Shanghai') SELECT DATE_DIFF(hour, timestamp, now()) FROM system_history.profile_history WHERE" "0"
70+
71+
check_query_log "timezone-4" null "settings (timezone='Asia/Shanghai') SELECT sum(DATE_DIFF(hour, event_time, now())) FROM system_history.login_history" "0"
72+
73+
check_query_log "timezone-5" "$SELECT_QUERY_ID" "settings (timezone='Asia/Shanghai') SELECT DATE_DIFF(hour, query_start, now()) FROM system_history.access_history WHERE" "0"
74+
75+
# Login history tests
76+
check_query_log "login-1" null "SELECT CASE WHEN count(*) >= 1 THEN 1 ELSE 0 END FROM system_history.login_history WHERE user_name = 'root'" "1"
77+
78+
check_query_log "login-2" null "SELECT CASE WHEN count(*) >= 1 THEN 1 ELSE 0 END FROM system_history.login_history WHERE error_message LIKE 'AuthenticateFailure.%' AND user_name = 'wrong_pass_user'" "1"
79+
80+
81+
echo "All log table tests passed successfully!"
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
SCRIPT_DIR="$(dirname "$0")"
6+
cd "$SCRIPT_DIR"
7+
8+
echo "Running Databend logging tests..."
9+
10+
echo "1. Setting up test data..."
11+
source setup_test_data.sh
12+
13+
echo "2. Running log table checks..."
14+
./check_logs_table.sh
15+
16+
echo "3. Running permissions tests..."
17+
./test_permissions.sh
18+
19+
echo "All tests completed successfully!"

0 commit comments

Comments
 (0)