-
-
Notifications
You must be signed in to change notification settings - Fork 147
Add multi user support #409
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0c222d7
Add multi user support
trueleo 68f4ac6
Update metadata on storage
trueleo 4c52e64
Set Users on startup
trueleo 4623b84
Refactor metadata check
trueleo 8461f56
Fix validator
trueleo beabeee
Add migration
trueleo 73eb409
Add banner
trueleo ec55772
Remove comments
trueleo eb76066
Fix
trueleo 011125a
Add comment
trueleo 0bca6af
Fix
trueleo 2056b35
Change user api
trueleo 868d1c7
Fix middleware
trueleo b528b89
Change validation
trueleo bfe58b4
Fix
trueleo 8d750ba
Change to roles
trueleo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Parseable Server (C) 2022 - 2023 Parseable, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
use crate::{ | ||
option::CONFIG, | ||
rbac::{ | ||
get_user_map, | ||
user::{PassCode, User}, | ||
}, | ||
storage::{self, ObjectStorageError, StorageMetadata}, | ||
validator::{self, error::UsernameValidationError}, | ||
}; | ||
use actix_web::{http::header::ContentType, web, Responder}; | ||
use http::StatusCode; | ||
use tokio::sync::Mutex; | ||
|
||
// async aware lock for updating storage metadata and user map atomicically | ||
static UPDATE_LOCK: Mutex<()> = Mutex::const_new(()); | ||
|
||
// Handler for PUT /api/v1/user/{username} | ||
// Creates a new user by username if it does not exists | ||
// Otherwise make a call to reset password | ||
// returns password generated for this user | ||
pub async fn put_user(username: web::Path<String>) -> Result<impl Responder, RBACError> { | ||
trueleo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let username = username.into_inner(); | ||
validator::verify_username(&username)?; | ||
let _ = UPDATE_LOCK.lock().await; | ||
nitisht marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let user_exists = get_user_map().read().unwrap().contains_key(&username); | ||
if user_exists { | ||
reset_password(username).await | ||
} else { | ||
let mut metadata = get_metadata().await?; | ||
if metadata.users.iter().any(|user| user.username == username) { | ||
// should be unreachable given state is always consistent | ||
return Err(RBACError::UserExists); | ||
} | ||
|
||
let (user, password) = User::create_new(username); | ||
metadata.users.push(user.clone()); | ||
put_metadata(&metadata).await?; | ||
// set this user to user map | ||
get_user_map().write().unwrap().insert(user); | ||
|
||
Ok(password) | ||
} | ||
} | ||
|
||
// Handler for DELETE /api/v1/user/delete/{username} | ||
pub async fn delete_user(username: web::Path<String>) -> Result<impl Responder, RBACError> { | ||
let username = username.into_inner(); | ||
let _ = UPDATE_LOCK.lock().await; | ||
// fail this request if the user does not exists | ||
if !get_user_map().read().unwrap().contains_key(&username) { | ||
return Err(RBACError::UserDoesNotExist); | ||
}; | ||
// delete from parseable.json first | ||
let mut metadata = get_metadata().await?; | ||
metadata.users.retain(|user| user.username != username); | ||
put_metadata(&metadata).await?; | ||
// update in mem table | ||
get_user_map().write().unwrap().remove(&username); | ||
Ok(format!("deleted user: {}", username)) | ||
} | ||
|
||
// Reset password for given username | ||
// returns new password generated for this user | ||
pub async fn reset_password(username: String) -> Result<String, RBACError> { | ||
// get new password for this user | ||
let PassCode { password, hash } = User::gen_new_password(); | ||
// update parseable.json first | ||
let mut metadata = get_metadata().await?; | ||
if let Some(user) = metadata | ||
.users | ||
.iter_mut() | ||
.find(|user| user.username == username) | ||
{ | ||
user.password_hash.clone_from(&hash); | ||
} else { | ||
// should be unreachable given state is always consistent | ||
return Err(RBACError::UserDoesNotExist); | ||
} | ||
put_metadata(&metadata).await?; | ||
|
||
// update in mem table | ||
get_user_map() | ||
.write() | ||
.unwrap() | ||
.get_mut(&username) | ||
.expect("checked that user exists in map") | ||
.password_hash = hash; | ||
|
||
Ok(password) | ||
} | ||
|
||
async fn get_metadata() -> Result<crate::storage::StorageMetadata, ObjectStorageError> { | ||
let metadata = CONFIG | ||
.storage() | ||
.get_object_store() | ||
.get_metadata() | ||
.await? | ||
.expect("metadata is initialized"); | ||
Ok(metadata) | ||
} | ||
|
||
async fn put_metadata(metadata: &StorageMetadata) -> Result<(), ObjectStorageError> { | ||
storage::put_remote_metadata(metadata).await?; | ||
storage::put_staging_metadata(metadata)?; | ||
Ok(()) | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum RBACError { | ||
#[error("User exists already")] | ||
UserExists, | ||
#[error("User does not exist")] | ||
UserDoesNotExist, | ||
#[error("Failed to connect to storage: {0}")] | ||
ObjectStorageError(#[from] ObjectStorageError), | ||
#[error("invalid Username: {0}")] | ||
ValidationError(#[from] UsernameValidationError), | ||
} | ||
|
||
impl actix_web::ResponseError for RBACError { | ||
fn status_code(&self) -> http::StatusCode { | ||
match self { | ||
Self::UserExists => StatusCode::BAD_REQUEST, | ||
Self::UserDoesNotExist => StatusCode::NOT_FOUND, | ||
Self::ValidationError(_) => StatusCode::BAD_REQUEST, | ||
Self::ObjectStorageError(_) => StatusCode::INTERNAL_SERVER_ERROR, | ||
} | ||
} | ||
|
||
fn error_response(&self) -> actix_web::HttpResponse<actix_web::body::BoxBody> { | ||
actix_web::HttpResponse::build(self.status_code()) | ||
.insert_header(ContentType::plaintext()) | ||
.body(self.to_string()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.