Skip to content

Commit e38c03c

Browse files
authored
fix(cubesql): Hide security context from logs (#9761)
1 parent 2db54ba commit e38c03c

File tree

6 files changed

+60
-11
lines changed

6 files changed

+60
-11
lines changed

packages/cubejs-backend-native/src/auth.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::gateway::{
1818
GatewayAuthContext, GatewayAuthContextRef, GatewayAuthService, GatewayAuthenticateResponse,
1919
GatewayCheckAuthRequest,
2020
};
21+
use crate::utils::NonDebugInRelease;
2122

2223
#[derive(Debug)]
2324
pub struct NodeBridgeAuthService {
@@ -90,7 +91,7 @@ struct CheckSQLAuthTransportResponse {
9091
pub struct NativeSQLAuthContext {
9192
pub user: Option<String>,
9293
pub superuser: bool,
93-
pub security_context: Option<serde_json::Value>,
94+
pub security_context: NonDebugInRelease<Option<serde_json::Value>>,
9495
}
9596

9697
impl AuthContext for NativeSQLAuthContext {
@@ -141,7 +142,7 @@ impl SqlAuthService for NodeBridgeAuthService {
141142
context: Arc::new(NativeSQLAuthContext {
142143
user,
143144
superuser: response.superuser,
144-
security_context: response.security_context,
145+
security_context: NonDebugInRelease::from(response.security_context),
145146
}),
146147
password: response.password,
147148
skip_password_check: response.skip_password_check.unwrap_or(false),

packages/cubejs-backend-native/src/node_export.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::sql4sql::sql4sql;
1818
use crate::stream::OnDrainHandler;
1919
use crate::tokio_runtime_node;
2020
use crate::transport::NodeBridgeTransport;
21-
use crate::utils::batch_to_rows;
21+
use crate::utils::{batch_to_rows, NonDebugInRelease};
2222
use cubenativeutils::wrappers::neon::context::neon_run_with_guarded_lifetime;
2323
use cubenativeutils::wrappers::neon::inner_types::NeonInnerTypes;
2424
use cubenativeutils::wrappers::neon::object::NeonObject;
@@ -429,7 +429,7 @@ fn exec_sql(mut cx: FunctionContext) -> JsResult<JsValue> {
429429
let native_auth_ctx = Arc::new(NativeSQLAuthContext {
430430
user: Some(String::from("unknown")),
431431
superuser: false,
432-
security_context,
432+
security_context: NonDebugInRelease::from(security_context),
433433
});
434434

435435
let (deferred, promise) = cx.promise();

packages/cubejs-backend-native/src/sql4sql.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::auth::NativeSQLAuthContext;
1616
use crate::config::NodeCubeServices;
1717
use crate::cubesql_utils::with_session;
1818
use crate::tokio_runtime_node;
19+
use crate::utils::NonDebugInRelease;
1920

2021
enum Sql4SqlQueryType {
2122
Regular,
@@ -208,7 +209,7 @@ pub fn sql4sql(mut cx: FunctionContext) -> JsResult<JsValue> {
208209
let native_auth_ctx = Arc::new(NativeSQLAuthContext {
209210
user: Some(String::from("unknown")),
210211
superuser: false,
211-
security_context,
212+
security_context: NonDebugInRelease::from(security_context),
212213
});
213214

214215
let (deferred, promise) = cx.promise();

packages/cubejs-backend-native/src/utils.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use cubesql::{compile::engine::df::scan::RecordBatch, sql::dataframe, CubeError};
22
use neon::prelude::*;
33
use serde_json::Value;
4+
use std::fmt::Debug;
5+
use std::ops::{Deref, DerefMut};
46

57
#[inline(always)]
68
pub fn call_method<'a, AS>(
@@ -38,3 +40,45 @@ pub fn batch_to_rows(batch: RecordBatch) -> Result<(Value, Vec<Value>), CubeErro
3840

3941
Ok((columns, rows))
4042
}
43+
44+
/// Allow skipping Debug output in release builds for specific field or type.
45+
pub struct NonDebugInRelease<T: Debug>(T);
46+
47+
impl<T: Debug> Debug for NonDebugInRelease<T> {
48+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49+
if cfg!(debug_assertions) {
50+
self.0.fmt(f)
51+
} else {
52+
f.debug_struct("skipped in release build").finish()
53+
}
54+
}
55+
}
56+
57+
impl<T: Debug> From<T> for NonDebugInRelease<T> {
58+
fn from(value: T) -> Self {
59+
Self(value)
60+
}
61+
}
62+
63+
impl<T: Debug> Deref for NonDebugInRelease<T> {
64+
type Target = T;
65+
66+
fn deref(&self) -> &Self::Target {
67+
&self.0
68+
}
69+
}
70+
71+
impl<T: Debug> DerefMut for NonDebugInRelease<T> {
72+
fn deref_mut(&mut self) -> &mut Self::Target {
73+
&mut self.0
74+
}
75+
}
76+
77+
impl<T: Debug> Default for NonDebugInRelease<T>
78+
where
79+
T: Default,
80+
{
81+
fn default() -> Self {
82+
Self(T::default())
83+
}
84+
}

rust/cubesql/cubesql/src/compile/router.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,6 @@ impl QueryRouter {
455455
.session_manager
456456
.server
457457
.auth
458-
// TODO do we want to send actual password here?
459458
.authenticate(sql_auth_request, Some(to_user.clone()), None)
460459
.await
461460
.map_err(|e| {
@@ -465,8 +464,13 @@ impl QueryRouter {
465464
.set_auth_context(Some(authenticate_response.context));
466465
} else {
467466
return Err(CompilationError::user(format!(
468-
"{:?} is not allowed to switch to '{}'",
469-
auth_context, to_user
467+
"user '{}' is not allowed to switch to '{}'",
468+
auth_context
469+
.user()
470+
.as_ref()
471+
.map(|v| v.as_str())
472+
.unwrap_or("not specified"),
473+
to_user
470474
)));
471475
}
472476
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
source: cubesql/src/compile/mod.rs
3-
assertion_line: 7213
4-
expression: "execute_queries_with_flags(vec![\"SET user = 'bad_user'\".to_string()],\n DatabaseProtocol::PostgreSQL).await.err().unwrap().to_string()"
3+
expression: "execute_queries_with_flags(vec![\"SET user = 'bad_user'\".to_string()],\nDatabaseProtocol::PostgreSQL).await.err().unwrap().to_string()"
54
---
6-
Error during planning: SQLCompilationError: User: HttpAuthContext { access_token: "access_token", base_path: "base_path" } is not allowed to switch to 'bad_user'
5+
Error during planning: SQLCompilationError: User: user 'not specified' is not allowed to switch to 'bad_user'

0 commit comments

Comments
 (0)