Repository navigation
feat: accept user-provided API key #1815
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
Open
pratik50
wants to merge
7
commits into
parseablehq:main
Choose a base branch
from
pratik50:feature/provided-api-key
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+211
−11
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8f64c36
feat: accept caller-provided API keys
pratik50 d4d06e0
feat: add ingest-only mode for caller-provided API keys
pratik50 41ec78e
feat: support typed API keys with global reader access
pratik50 4d92af0
chore: remove API key tests
pratik50 7f9be95
fix: reject non-RFC UUID variants in provided API keys
pratik50 47162e9
feat: support API key name and value updates
pratik50 d248171
fix: use clone_from for API key metadata update
pratik50 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
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 |
|---|---|---|
|
|
@@ -6,14 +6,17 @@ use serde::{Deserialize, Deserializer, Serialize, de}; | |
| use ulid::Ulid; | ||
|
|
||
| use crate::{ | ||
| apikeys::{ApiKeyError, CreateApiKeyRequest}, | ||
| apikeys::{ | ||
| ApiKeyError, ApiKeyType, CreateApiKeyRequest, PatchApiKeyRequest, | ||
| validate_api_key as validate_api_key_value, | ||
| }, | ||
| handlers::http::{ | ||
| modal::utils::rbac_utils::{get_metadata, put_metadata}, | ||
| rbac::{RBACError, UPDATE_LOCK}, | ||
| }, | ||
| parseable::DEFAULT_TENANT, | ||
| rbac::{ | ||
| Users, | ||
| GLOBAL_INGESTOR_ROLE, GLOBAL_QUERIER_ROLE, Users, | ||
| map::{roles, users}, | ||
| role::model::{DefaultPrivilege, Role}, | ||
| user::{User, UserType}, | ||
|
|
@@ -206,9 +209,25 @@ pub async fn create_api_key( | |
|
|
||
| let guard = UPDATE_LOCK.lock().await; | ||
|
|
||
| // Reject any role names that don't exist in this tenant while holding the | ||
| // update lock so a concurrent role update cannot invalidate the check. | ||
| validate_roles(&body.roles, &tenant_id)?; | ||
| // Typed keys get server-managed roles; the existing UI/API flow keeps | ||
| // assigning explicit roles when apiKeyType is omitted. | ||
| let assigned_roles = match body.api_key_type { | ||
| Some(api_key_type) => { | ||
| if !body.roles.is_empty() { | ||
| return Err(ApiKeyError::ApiKeyTypeWithRoles); | ||
| } | ||
| let role = match api_key_type { | ||
| ApiKeyType::Ingestor => GLOBAL_INGESTOR_ROLE, | ||
| ApiKeyType::Query => GLOBAL_QUERIER_ROLE, | ||
| }; | ||
| HashSet::from([role.to_string()]) | ||
| } | ||
| None => { | ||
| // Hold the update lock while checking user-defined roles. | ||
| validate_roles(&body.roles, &tenant_id)?; | ||
| body.roles | ||
| } | ||
| }; | ||
|
|
||
| // Duplicate key-name detection inside the same tenant. | ||
| let existing = collect_tenant_api_keys(&tenant_id); | ||
|
|
@@ -221,12 +240,24 @@ pub async fn create_api_key( | |
| } | ||
|
|
||
| let key_id = Ulid::new(); | ||
| let api_key_value = generate_api_key_value(); | ||
| let api_key_value = if let Some(api_key) = body.api_key { | ||
| validate_api_key_value(&api_key)?; | ||
| api_key | ||
| } else { | ||
| generate_api_key_value() | ||
| }; | ||
| if existing | ||
| .iter() | ||
| .filter_map(|user: &User| user.as_api_key()) | ||
| .any(|key| key.api_key == api_key_value) | ||
| { | ||
| return Err(ApiKeyError::DuplicateApiKey); | ||
| } | ||
| let user = User::new_api_key( | ||
| key_id, | ||
| api_key_value, | ||
| body.key_name, | ||
| body.roles, | ||
| assigned_roles, | ||
| created_by, | ||
| tenant_id.clone(), | ||
| ); | ||
|
|
@@ -262,6 +293,91 @@ pub async fn create_api_key( | |
| Ok(HttpResponse::Ok().json(response)) | ||
| } | ||
|
|
||
| /// PATCH /api/prism/v1/apikeys/{key_id} | ||
| /// | ||
| /// Update a key's name or value without changing its identity or roles. | ||
| pub async fn patch_api_key( | ||
| req: HttpRequest, | ||
| path: web::Path<String>, | ||
| web::Json(body): web::Json<PatchApiKeyRequest>, | ||
| ) -> Result<impl Responder, ApiKeyError> { | ||
| let (caller_userid, tenant_id) = verify_admin(&req)?; | ||
| let key_id_str = path.into_inner(); | ||
| let key_id = | ||
| Ulid::from_string(&key_id_str).map_err(|_| ApiKeyError::KeyNotFound(key_id_str.clone()))?; | ||
| if body.key_name.is_none() && body.api_key.is_none() { | ||
| return Err(ApiKeyError::EmptyApiKeyUpdate); | ||
| } | ||
|
|
||
| let guard = UPDATE_LOCK.lock().await; | ||
| let existing = collect_tenant_api_keys(&tenant_id); | ||
| let mut user = existing | ||
| .iter() | ||
| .find(|user| user.as_api_key().is_some_and(|key| key.key_id == key_id)) | ||
| .cloned() | ||
| .ok_or_else(|| ApiKeyError::KeyNotFound(key_id_str))?; | ||
| let current = user.as_api_key().expect("selected API key user"); | ||
| let key_name = body.key_name.unwrap_or_else(|| current.key_name.clone()); | ||
| let api_key_value = body.api_key.unwrap_or_else(|| current.api_key.clone()); | ||
|
|
||
| if key_name.trim().is_empty() { | ||
| return Err(ApiKeyError::InvalidApiKeyName); | ||
| } | ||
| if api_key_value != current.api_key { | ||
| validate_api_key_value(&api_key_value)?; | ||
| } | ||
| if existing.iter().any(|other| { | ||
| other | ||
| .as_api_key() | ||
| .is_some_and(|key| key.key_id != key_id && key.key_name == key_name) | ||
| }) { | ||
| return Err(ApiKeyError::DuplicateKeyName(key_name.clone())); | ||
| } | ||
| if existing.iter().any(|other| { | ||
| other | ||
| .as_api_key() | ||
| .is_some_and(|key| key.key_id != key_id && key.api_key == api_key_value) | ||
| }) { | ||
| return Err(ApiKeyError::DuplicateApiKey); | ||
| } | ||
|
|
||
| let changed = key_name != current.key_name || api_key_value != current.api_key; | ||
| if changed { | ||
| let key = match &mut user.ty { | ||
| UserType::ApiKey(key) => key, | ||
| _ => unreachable!("selected API key user"), | ||
| }; | ||
| key.key_name.clone_from(&key_name); | ||
| key.api_key.clone_from(&api_key_value); | ||
| key.modified_at = Utc::now(); | ||
|
|
||
| let mut metadata = get_metadata(&tenant_id).await?; | ||
| let stored = metadata | ||
| .users | ||
| .iter_mut() | ||
| .find(|stored| stored.userid() == user.userid()) | ||
| .ok_or_else(|| ApiKeyError::KeyNotFound(key_id.to_string()))?; | ||
| stored.clone_from(&user); | ||
| put_metadata(&metadata, &tenant_id).await?; | ||
| Users.put_user(user.clone()); | ||
| } | ||
| drop(guard); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we release the update lock before cluster sync, we should hold the lock until sync is completed |
||
|
|
||
| // A retry also re-sends an already applied update to any lagging nodes. | ||
| crate::handlers::http::cluster::sync_user_creation( | ||
| &req, | ||
| user.clone(), | ||
| &None, | ||
| &tenant_id, | ||
| &caller_userid, | ||
| ) | ||
| .await | ||
| .map_err(|error: RBACError| ApiKeyError::UpdateSyncFailed(error.to_string()))?; | ||
|
|
||
| let response = ApiKeyResponse::try_from(&user)?; | ||
| Ok(HttpResponse::Ok().json(response)) | ||
| } | ||
|
|
||
| /// DELETE /api/prism/v1/apikeys/{key_id} | ||
| /// | ||
| /// Delete an API key by key_id. Only admins can delete keys. | ||
|
|
||
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
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.