Skip to content

Commit bf82c95

Browse files
authored
fix(query): create a virtual JWT user instead of ensuring on metasrv (#10760)
* fix(query): create a virtual JWT user instead of from metasrv * fix: remove hostname for JWT auth & deny root user * fix: tests for auth
1 parent 6b028ef commit bf82c95

File tree

8 files changed

+81
-206
lines changed

8 files changed

+81
-206
lines changed

src/query/service/src/auth.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ pub struct AuthMgr {
3131
pub enum Credential {
3232
Jwt {
3333
token: String,
34-
hostname: Option<String>,
3534
},
3635
Password {
3736
name: String,
@@ -52,10 +51,7 @@ impl AuthMgr {
5251

5352
pub async fn auth(&self, session: Arc<Session>, credential: &Credential) -> Result<()> {
5453
match credential {
55-
Credential::Jwt {
56-
token: t,
57-
hostname: h,
58-
} => {
54+
Credential::Jwt { token: t } => {
5955
let jwt_auth = self
6056
.jwt_auth
6157
.as_ref()
@@ -71,32 +67,26 @@ impl AuthMgr {
7167
if let Some(tenant) = jwt.custom.tenant_id {
7268
session.set_current_tenant(tenant);
7369
};
74-
let tenant = session.get_current_tenant();
7570

76-
// create user if not exists when the JWT claims contains ensure_user
71+
// create a virtual JWT user only available in current session
72+
let auth_role = jwt.custom.role.clone();
73+
let mut user_info = UserInfo::new(&user_name, "%", AuthInfo::JWT);
74+
if user_info.identity().is_root() {
75+
return Err(ErrorCode::AuthenticateFailure(
76+
"root user is not allowed in jwt auth.",
77+
));
78+
}
79+
if let Some(ref role) = auth_role {
80+
user_info.grants.grant_role(role.clone());
81+
}
7782
if let Some(ref ensure_user) = jwt.custom.ensure_user {
78-
let mut user_info = UserInfo::new(&user_name, "%", AuthInfo::JWT);
7983
if let Some(ref roles) = ensure_user.roles {
8084
for role in roles.clone().into_iter() {
8185
user_info.grants.grant_role(role);
8286
}
8387
}
84-
UserApiProvider::instance()
85-
.ensure_builtin_roles(&tenant)
86-
.await?;
87-
UserApiProvider::instance()
88-
.add_user(&tenant, user_info.clone(), true)
89-
.await?;
9088
}
9189

92-
let auth_role = jwt.custom.role.clone();
93-
let user_info = UserApiProvider::instance()
94-
.get_user_with_client_ip(
95-
&tenant,
96-
&user_name,
97-
h.as_ref().unwrap_or(&"%".to_string()),
98-
)
99-
.await?;
10090
session.set_authed_user(user_info, auth_role).await?;
10191
}
10292
Credential::Password {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ fn auth_by_header(
100100
match Bearer::decode(value) {
101101
Some(bearer) => Ok(Credential::Jwt {
102102
token: bearer.token().to_string(),
103-
hostname: client_ip,
104103
}),
105104
None => Err(ErrorCode::AuthenticateFailure("bad Bearer auth header")),
106105
}

0 commit comments

Comments
 (0)