Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create user if not exists on JWT authenticate #4924

Merged
merged 3 commits into from
Apr 19, 2022

Conversation

junnplus
Copy link
Contributor

@junnplus junnplus commented Apr 18, 2022

I hereby agree to the terms of the CLA available at: https://databend.rs/dev/policies/cla/

Summary

Create user if not exists on JWT authenticate.

Specify the create_user struct in custom claims to create a user.

#[derive(Default, Deserialize, Serialize)]
pub struct CreateUser {
    pub tenant_id: Option<String>,
    pub roles: Vec<String>,
}

#[derive(Default, Deserialize, Serialize)]
pub struct CustomClaims {
    pub create_user: Option<CreateUser>,
}

Changelog

  • New Feature

Related Issues

Fixes #4875

@vercel
Copy link

vercel bot commented Apr 18, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated
databend ⬜️ Ignored (Inspect) Apr 19, 2022 at 7:25AM (UTC)

@mergify
Copy link
Contributor

mergify bot commented Apr 18, 2022

Thanks for the contribution!
I have applied any labels matching special text in your PR Changelog.

Please review the labels and make any necessary changes.

@mergify mergify bot added the pr-feature this PR introduces a new feature to the codebase label Apr 18, 2022
None => return Err(ErrorCode::AuthenticateFailure("jwt auth not configured.")),
};
self.users
let user_name = jwt.subject.unwrap();
if let Some(create_user) = jwt.custom.create_user {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. add_user return error if user exists. does the cloud platform when it should carry the create_user field?

  2. for an existed user, what if the existed user info diff with info in create_user?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add_user return error if user exists. does the cloud platform when it should carry the create_user field?

User will be created only if user does not exist. is_not_exists is true for add_user .

for an existed user, what if the existed user info diff with info in create_user?

create_user just used to create users, not update.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User will be created only if user does not exist

do you mean cloud platform do this check to determine whether or not to addcreate_user in jwt? cc @flaneur2020

add_user may ErrorCode::UserAlreadyExists

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean cloud platform do this check to determine whether or not to addcreate_user in jwt?

i guess the cloud platform could ensure user exists by an jwt with create_user enabled when the user successfully logged in, and then pass normal jwt for the normal operations to interact with databend-query.

a jwt is used to authenticate an user, if the jwt verified successfully and the user already existed, it can pass the authentication happily, it might a bit weird to get a userAlreadyExists in this case. 🤔

Copy link
Member

@flaneur2020 flaneur2020 Apr 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, how about making the create_user claim into a bool?

the infomation about tenant and role may already existed some another claims 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, how about making the create_user claim into a bool?

like this?

{
    "create_user": true,
    "tenant_id": "",
    "roles": []
}

I'm not sure if the same fields would be in custom claims, it's better together.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@junnplus sorry, what I saw is UserApi::add_user.

Copy link
Member

@youngsofun youngsofun Apr 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess the cloud platform could ensure user exists by an jwt with create_user enabled when the user successfully logged in, and then pass normal jwt for the normal operations to interact with databend-query.

maybe we can call it ensure_user? I saw it a lot in puppet. or "create_user_if_not_exist".

btw, how about making the create_user claim into a bool?

seems currently this create_user provides only a basic template for a newly-seen user, so the value in it take effective only when user dose not exist. and it will not override behavior specified by an existing userInfo.

So, I prefer to separate it from other fields,which while be regard,naturally, being able to override the behavior of the session

@flaneur2020

Copy link
Contributor Author

@junnplus junnplus Apr 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ensure_user SGTM. cc @flaneur2020

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, ensure_user is much better than create_user_if_not_exist.

I guess jwt in some manner favors shorter names, as it'd like to avoids getting too big in the transport, likewise "aud" over "audience", "sub" over "subject" etc.

}
self.user_mgr.add_user(&tenant, user_info, true).await?;
}
self.user_mgr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if add_user does return ok, no need to get_user here?

Copy link
Contributor Author

@junnplus junnplus Apr 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if add_user does return ok, no need to get_user here?

If the user already exists, we still need to get the user.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not return the UserInfo just created?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not return the UserInfo just created?

User will not be created if user already exists, so we need to get_user.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, put it together, Jwt only create a user if not exist.
the user may be altered later as normal.

lgtm

Signed-off-by: Ye Sijun <junnplus@gmail.com>
@BohuTANG BohuTANG merged commit d411978 into databendlabs:main Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need-review pr-feature this PR introduces a new feature to the codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: create user if not exists on JWT authenticate
5 participants