mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-09-05 00:03:08 +00:00
* refactor: replace email with permissioned_as for triggers/schedules
Add a new `permissioned_as` column (format: `u/{username}`, `g/{group}`,
or raw email) to all trigger tables and schedule. This value is used
directly for job permission checks, removing the need for email lookups
when creating/updating triggers.
- Migration: add permissioned_as to all 9 trigger tables + schedule,
drop email from trigger tables (schedule keeps it for backwards compat)
- Backend: resolve_email() (async, DB) -> resolve_permissioned_as() (sync)
- Email cache: get_email_from_permissioned_as() with quick_cache for
places that still need email (fetch_api_authed, schedule backwards compat)
- Frontend: rename email/preserve_email -> permissioned_as/preserve_permissioned_as
in deploy data and OpenAPI schemas
- Tests updated for new field names and u/{username} format
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix sqlx/build
* update ee ref
* refactor: simplify resolve_edited_by to always use authed username
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix compile + migration
* update ref
* test: add trigger trait method tests for permissioned_as queries
Add tests that call TriggerCrud and Listener trait methods directly
to verify dynamic SQL correctly references the permissioned_as column.
Covers get_trigger_by_path, list_triggers, set_trigger_mode, and
fetch_enabled_unlistened_triggers for all trigger types.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* update sqlx
* fix: use permissioned_as directly for schedules and fix audit RLS for groups
- Schedule: permissioned_as only set on create, not on edit/set_enabled
- Schedule: stop reading email column, use get_email_from_permissioned_as
- Triggers: use fetch_api_authed_from_permissioned_as instead of edited_by
- Triggers: rename listener fields for clarity (username -> edited_by)
- Fix audit author username for group permissioned_as (g/test -> group-test)
to match session.user, preventing RLS policy violations on audit_partitioned
- OpenAPI: remove permissioned_as/preserve_permissioned_as from EditSchedule
- Add backwards-compat comments for schedule email writes
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* chore: regenerate system prompts for permissioned_as field
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix build
* refactor: generalize onBehalfOf naming, add permissioned_as to EditSchedule
- Frontend: rename onBehalfOfPermissionedAs -> onBehalfOf with comments
explaining it carries emails for flows/scripts and permissioned_as for
triggers/schedules
- Frontend: rename getOnBehalfOfEmail -> getOnBehalfOf,
getOnBehalfOfPermissionedAsForDeploy -> getOnBehalfOfForDeploy,
customOnBehalfOfEmails -> customOnBehalfOf
- Backend: add optional permissioned_as/preserve_permissioned_as to
EditSchedule with COALESCE (only updates when provided)
- Backend: add on_behalf_of audit log for schedule edit
- Backend: remove unused resolve_on_behalf_of_permissioned_as
- Tests: remove email assertions from schedule update test (email is
just backwards compat, only permissioned_as matters)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: preserve email column when permissioned_as is preserved on schedule edit
Derive email from the preserved permissioned_as via cache lookup instead
of always writing authed.email. This keeps the email column consistent
with the old behavior for backwards compat with old workers.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: update deploy UI labels from "edited by" to "run as" for triggers
Triggers now use permissioned_as (not edited_by) for permissions, so
update the deploy UI wording to reflect this. Also update wm_deployers
group description to mention schedules and permissioned_as.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: use u/username format for custom trigger/schedule deploy selection
When picking a custom user for trigger/schedule deployment, store
u/${username} (permissioned_as format) instead of the email. Flows/scripts
continue to use email format for on_behalf_of_email.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: show u/username format for "me" option in trigger deploy selector
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* refactor: simplify OnBehalfOfSelector to return the right format per kind
OnBehalfOfSelector now handles the email vs permissioned_as format
internally based on kind:
- triggers: returns u/username, displays u/username in all options
- flows/scripts/apps: returns email, displays username
The onSelect callback now takes (choice, value?) where value is already
in the correct format. Parent components just store it directly without
needing to know about the format difference.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: always show u/username format in OnBehalfOfSelector for all kinds
Display is now consistent: all kinds show u/username in the selector.
The returned value still differs (email for flows/scripts, u/username
for triggers) since the backend APIs expect different formats.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: replace email with permissioned_as in http_trigger test insert
The email column was dropped from trigger tables in the migration.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: review fixes — migration, app policy, capture cleanup, naming
- Migration: remove DEFAULT '', use nullable → populate → SET NOT NULL
- App policy: set both on_behalf_of and on_behalf_of_email for all choices
- OnBehalfOfSelector: return OnBehalfOfDetails {email, permissionedAs} instead of ambiguous value
- Remove unused email field from Capture struct and query
- Rename getSourceEmail/getTargetEmail → getSourceOnBehalfOf/getTargetOnBehalfOf
- Rename test functions from preserve_email to preserve_permissioned_as
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: add permissioned_as to all test schedule INSERTs
Since the migration no longer uses DEFAULT '', all INSERTs must
explicitly provide permissioned_as. Updated test fixtures and
schedule_push tests.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: strip permissioned_as from exports/sync, fix OpenAPI required field
- Add permissioned_as to workspace export strip list (like edited_by)
- Add permissioned_as to CLI TriggerFile Omit list
- Fix TriggerExtraProperty.required: email → permissioned_as
- Regenerate frontend and CLI types
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: remove accidentally committed generated files
These directories are gitignored and should not be tracked.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* chore: regenerate system prompts for permissioned_as schema changes
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: remove permissioned_as from CLI TriggerFile Omit list
Already stripped in workspace export, no need to also omit from the type.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: optimize email cache key and revert TriggerFile Omit change
- Use single concatenated string for cache key instead of (String, String) tuple
- Remove permissioned_as from CLI TriggerFile Omit (already stripped in export)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: zero-allocation email cache lookups using Equivalent trait
Use a borrowed EmailCacheKey(&str, &str) for cache lookups via
quick_cache's Equivalent support. Only allocates (String, String)
on cache miss for insert. This is called on every trigger fire
and schedule push.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: add permissioned_as to Schedule required fields in OpenAPI spec
The backend always returns permissioned_as (non-optional String),
so the schema should reflect that.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: handle group- prefix in migration UPDATE statements
edited_by can be 'group-{name}' for group-owned triggers/schedules.
The migration now correctly maps these to 'g/{name}' format instead
of incorrectly producing 'u/group-{name}'.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Revert "fix: handle group- prefix in migration UPDATE statements"
This reverts commit 0971392b38.
* fix: use superadmin email to resolve permissioned_as in schedule migration
For users upgrading from older versions where edited_by may not reflect
the actual schedule owner, check if the email belongs to a superadmin
and look up their username. Otherwise fall back to edited_by.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: fall back to superadmin email when not in workspace usr table
If the superadmin isn't a member of the workspace, use their email
as raw permissioned_as instead of falling back to edited_by.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: always update permissioned_as and email on schedule edit
Consistent with pre-refactor behavior where email and edited_by
were always updated on every edit. permissioned_as is now always
set (to editing user or preserved value), removing the COALESCE
that previously preserved it when not provided.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add schedule permission tests and centralize group prefix constants
Tests: schedule create/update for normal user, workspace admin, and
superadmin not in workspace. Verifies schedule fields (email,
permissioned_as, edited_by) and pushed job fields (permissioned_as,
permissioned_as_email).
Constants: centralize "u/", "g/", "group-" as PERMISSIONED_AS_USER_PREFIX,
PERMISSIONED_AS_GROUP_PREFIX, USERNAME_GROUP_PREFIX.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: use @unknown.windmill.dev for synthetic email fallback
Prevents privilege escalation: a user with username like
'superadmin_secret' would get superadmin via the synthetic
email matching SUPERADMIN_SECRET_EMAIL. Using a different
subdomain avoids any collision with hardcoded @windmill.dev emails.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* update ee ref
* sqlx
* chore: regenerate system prompts after main merge
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* chore: update ee-repo-ref to bda51bc33bcb573659e7ff07d0a23ff6e23b8148
This commit updates the EE repository reference after PR #468 was merged in windmill-ee-private.
Previous ee-repo-ref: 8cf1802f8fe183f430830590b4f3172a50207843
New ee-repo-ref: bda51bc33bcb573659e7ff07d0a23ff6e23b8148
Automated by sync-ee-ref workflow.
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: windmill-internal-app[bot] <windmill-internal-app[bot]@users.noreply.github.com>
644 lines
19 KiB
Rust
644 lines
19 KiB
Rust
use std::{
|
|
hash::DefaultHasher,
|
|
sync::atomic::{AtomicI64, Ordering},
|
|
};
|
|
|
|
use anyhow::Context;
|
|
use chrono::{DateTime, Duration, Utc};
|
|
use quick_cache::sync::Cache;
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
|
|
use crate::{
|
|
db::{Authed, AuthedRef},
|
|
error::{Error, Result},
|
|
jwt,
|
|
users::{SUPERADMIN_NOTIFICATION_EMAIL, SUPERADMIN_SECRET_EMAIL, SUPERADMIN_SYNC_EMAIL},
|
|
utils::WarnAfterExt,
|
|
DB,
|
|
};
|
|
|
|
/// Hash a raw token using SHA-256 (hex-encoded, 64 chars).
|
|
/// Used to store and look up tokens without keeping plaintext in the DB.
|
|
pub fn hash_token(token: &str) -> String {
|
|
crate::utils::calculate_hash(token)
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct IdToken {
|
|
token: String,
|
|
expiration: DateTime<Utc>,
|
|
}
|
|
|
|
pub const TOKEN_PREFIX_LEN: usize = 10;
|
|
|
|
/// Safely extract the token prefix (first TOKEN_PREFIX_LEN chars).
|
|
/// Returns the full token if it's shorter than TOKEN_PREFIX_LEN, preventing panics.
|
|
pub fn safe_token_prefix(token: &str) -> String {
|
|
token.get(..TOKEN_PREFIX_LEN).unwrap_or(token).to_string()
|
|
}
|
|
|
|
lazy_static::lazy_static! {
|
|
// Cache for script hash permissions - (ApiAuthed hash, script_hash) -> permission result
|
|
pub static ref HASH_PERMS_CACHE: PermsCache = PermsCache::new();
|
|
pub static ref FLOW_PERMS_CACHE: PermsCache = PermsCache::new();
|
|
}
|
|
|
|
pub struct PermsCache(Cache<(u64, u64), ()>, AtomicI64);
|
|
|
|
use std::hash::Hash;
|
|
use std::hash::Hasher;
|
|
|
|
impl PermsCache {
|
|
pub fn compute_hash(authed: &AuthedRef) -> u64 {
|
|
let mut hasher = DefaultHasher::new();
|
|
authed.username.hash(&mut hasher);
|
|
authed.folders.hash(&mut hasher);
|
|
authed.groups.hash(&mut hasher);
|
|
authed.is_admin.hash(&mut hasher);
|
|
hasher.finish()
|
|
}
|
|
}
|
|
|
|
pub const PERMS_CACHE_EXPIRATION_SECONDS: i64 = 60 * 60;
|
|
|
|
impl PermsCache {
|
|
pub fn new() -> Self {
|
|
PermsCache(
|
|
Cache::new(10000),
|
|
AtomicI64::new(chrono::Utc::now().timestamp() as i64),
|
|
)
|
|
}
|
|
|
|
pub fn check_perms_in_cache<'e, T: Into<u64>>(
|
|
&self,
|
|
authed: &'e AuthedRef<'e>,
|
|
key: T,
|
|
) -> (bool, u64) {
|
|
// Clear cache every hour
|
|
if self.1.load(Ordering::Relaxed)
|
|
< chrono::Utc::now().timestamp() - PERMS_CACHE_EXPIRATION_SECONDS
|
|
{
|
|
self.0.clear();
|
|
self.1
|
|
.store(chrono::Utc::now().timestamp() as i64, Ordering::Relaxed);
|
|
}
|
|
// Create hash of the ApiAuthed struct for caching
|
|
let authed_hash = Self::compute_hash(authed);
|
|
|
|
let key = key.into();
|
|
tracing::debug!(
|
|
"Checking cache for authed hash {authed_hash} and script hash {}",
|
|
key
|
|
);
|
|
// Check cache first
|
|
if let Some(_) = self.0.get(&(authed_hash, key)) {
|
|
tracing::debug!("Cached result for authed hash {authed_hash}",);
|
|
return (true, authed_hash);
|
|
}
|
|
|
|
return (false, authed_hash);
|
|
}
|
|
|
|
pub fn insert<'e, T: Into<u64>>(&self, authed_hash: u64, key: T) {
|
|
let key = key.into();
|
|
tracing::debug!("Inserting authed hash {authed_hash} and key {}", key);
|
|
self.0.insert((authed_hash, key), ());
|
|
}
|
|
}
|
|
|
|
/// Check a user's access level against an `extra_perms` JSONB object.
|
|
///
|
|
/// Returns `None` if the user has no matching entry (no access).
|
|
/// Returns `Some(true)` if the user (or any of their groups) has write access.
|
|
/// Returns `Some(false)` if the user (or any of their groups) has read-only access.
|
|
pub fn check_extra_perms(
|
|
extra_perms: &serde_json::Map<String, serde_json::Value>,
|
|
username: &str,
|
|
groups: &[String],
|
|
) -> Option<bool> {
|
|
// Check direct user permission
|
|
use crate::users::{PERMISSIONED_AS_GROUP_PREFIX, PERMISSIONED_AS_USER_PREFIX};
|
|
let user_key = if username.starts_with(PERMISSIONED_AS_USER_PREFIX) {
|
|
username.to_string()
|
|
} else {
|
|
format!("{PERMISSIONED_AS_USER_PREFIX}{username}")
|
|
};
|
|
if let Some(v) = extra_perms.get(&user_key) {
|
|
return Some(v.as_bool().unwrap_or(false));
|
|
}
|
|
|
|
// Check group permissions — return highest access level found
|
|
let mut found = false;
|
|
let mut write = false;
|
|
for g in groups {
|
|
let key = if g.starts_with(PERMISSIONED_AS_GROUP_PREFIX) {
|
|
g.to_string()
|
|
} else {
|
|
format!("{PERMISSIONED_AS_GROUP_PREFIX}{g}")
|
|
};
|
|
if let Some(v) = extra_perms.get(&key) {
|
|
found = true;
|
|
if v.as_bool().unwrap_or(false) {
|
|
write = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if found {
|
|
Some(write)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
pub fn has_expired(expiration_time: DateTime<Utc>, take: Option<Duration>) -> bool {
|
|
let now = Utc::now();
|
|
|
|
let expiration = match take {
|
|
Some(duration) => expiration_time - duration,
|
|
None => expiration_time,
|
|
};
|
|
|
|
now > expiration
|
|
}
|
|
|
|
impl From<IdToken> for String {
|
|
fn from(value: IdToken) -> Self {
|
|
value.token
|
|
}
|
|
}
|
|
|
|
impl ToString for IdToken {
|
|
fn to_string(&self) -> String {
|
|
self.token.clone()
|
|
}
|
|
}
|
|
|
|
impl IdToken {
|
|
pub fn new(token: String, expiration: DateTime<Utc>) -> Self {
|
|
Self { token, expiration }
|
|
}
|
|
|
|
pub fn token(&self) -> &str {
|
|
&self.token
|
|
}
|
|
pub fn expiration(&self) -> &DateTime<Utc> {
|
|
&self.expiration
|
|
}
|
|
}
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
pub struct JWTAuthClaims {
|
|
pub email: String,
|
|
pub username: String,
|
|
pub is_admin: bool,
|
|
pub is_operator: bool,
|
|
pub groups: Vec<String>,
|
|
pub folders: Vec<(String, bool, bool)>,
|
|
pub label: Option<String>,
|
|
pub workspace_id: Option<String>,
|
|
pub workspace_ids: Option<Vec<String>>,
|
|
pub exp: usize,
|
|
pub job_id: Option<String>,
|
|
pub scopes: Option<Vec<String>>,
|
|
pub audit_span: Option<String>,
|
|
}
|
|
|
|
impl JWTAuthClaims {
|
|
pub fn allowed_in_workspace(&self, w_id: &str) -> bool {
|
|
self.workspace_id
|
|
.as_ref()
|
|
.is_some_and(|token_w_id| w_id == token_w_id)
|
|
|| self
|
|
.workspace_ids
|
|
.as_ref()
|
|
.is_some_and(|token_w_ids| token_w_ids.iter().any(|token_w_id| w_id == token_w_id))
|
|
}
|
|
|
|
pub fn compute_ext_jwt_hash(&self) -> i64 {
|
|
let mut hasher = DefaultHasher::new();
|
|
self.email.hash(&mut hasher);
|
|
self.username.hash(&mut hasher);
|
|
self.is_admin.hash(&mut hasher);
|
|
self.is_operator.hash(&mut hasher);
|
|
self.groups.hash(&mut hasher);
|
|
self.folders.hash(&mut hasher);
|
|
self.workspace_id.hash(&mut hasher);
|
|
self.workspace_ids.hash(&mut hasher);
|
|
self.label.hash(&mut hasher);
|
|
self.scopes.hash(&mut hasher);
|
|
hasher.finish() as i64
|
|
}
|
|
}
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
pub struct JobPerms {
|
|
pub email: String,
|
|
pub username: String,
|
|
pub is_admin: bool,
|
|
pub is_operator: bool,
|
|
pub groups: Vec<String>,
|
|
pub folders: Vec<serde_json::Value>,
|
|
}
|
|
|
|
impl From<JobPerms> for Authed {
|
|
fn from(value: JobPerms) -> Self {
|
|
Self {
|
|
email: value.email,
|
|
username: value.username,
|
|
is_admin: value.is_admin,
|
|
is_operator: value.is_operator,
|
|
groups: value.groups,
|
|
folders: value
|
|
.folders
|
|
.into_iter()
|
|
.filter_map(|x| serde_json::from_value::<(String, bool, bool)>(x).ok())
|
|
.collect(),
|
|
scopes: None,
|
|
token_prefix: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub async fn is_super_admin_email(db: &DB, email: &str) -> Result<bool> {
|
|
if email == SUPERADMIN_SECRET_EMAIL || email == SUPERADMIN_NOTIFICATION_EMAIL {
|
|
return Ok(true);
|
|
}
|
|
|
|
let is_admin = sqlx::query_scalar!("SELECT super_admin FROM password WHERE email = $1", email)
|
|
.fetch_optional(db)
|
|
.await
|
|
.map_err(|e| Error::internal_err(format!("fetching super admin: {e:#}")))?
|
|
.unwrap_or(false);
|
|
|
|
Ok(is_admin)
|
|
}
|
|
|
|
pub async fn is_devops_email(db: &DB, email: &str) -> Result<bool> {
|
|
if is_super_admin_email(db, email).await? {
|
|
return Ok(true);
|
|
}
|
|
|
|
let is_devops = sqlx::query_scalar!("SELECT devops FROM password WHERE email = $1", email)
|
|
.fetch_optional(db)
|
|
.await
|
|
.map_err(|e| Error::internal_err(format!("fetching super admin: {e:#}")))?
|
|
.unwrap_or(false);
|
|
|
|
Ok(is_devops)
|
|
}
|
|
|
|
pub fn permissioned_as_to_username(permissioned_as: &str) -> String {
|
|
use crate::users::{PERMISSIONED_AS_USER_PREFIX, USERNAME_GROUP_PREFIX};
|
|
if let Some(name) = permissioned_as.strip_prefix(PERMISSIONED_AS_USER_PREFIX) {
|
|
name.to_string()
|
|
} else if let Some(name) =
|
|
permissioned_as.strip_prefix(crate::users::PERMISSIONED_AS_GROUP_PREFIX)
|
|
{
|
|
format!("{}{}", USERNAME_GROUP_PREFIX, name)
|
|
} else {
|
|
permissioned_as.to_string()
|
|
}
|
|
}
|
|
|
|
pub fn fetch_authed_from_permissioned_as<'a, A>(
|
|
permissioned_as: &'a str,
|
|
email: &'a str,
|
|
w_id: &'a str,
|
|
db: A,
|
|
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Authed>> + Send + 'a>>
|
|
where
|
|
A: sqlx::Acquire<'a, Database = sqlx::Postgres> + Send + 'a,
|
|
{
|
|
Box::pin(async move {
|
|
let mut conn = db
|
|
.acquire()
|
|
.await
|
|
.map_err(|e| Error::internal_err(format!("acquiring connection: {e:#}")))?;
|
|
|
|
fetch_authed_from_permissioned_as_inner(permissioned_as, email, w_id, &mut *conn).await
|
|
})
|
|
}
|
|
|
|
async fn fetch_authed_from_permissioned_as_inner(
|
|
permissioned_as: &str,
|
|
email: &str,
|
|
w_id: &str,
|
|
conn: &mut sqlx::PgConnection,
|
|
) -> Result<Authed> {
|
|
let is_super_admin = permissioned_as == SUPERADMIN_SYNC_EMAIL
|
|
|| email == SUPERADMIN_SECRET_EMAIL
|
|
|| email == SUPERADMIN_NOTIFICATION_EMAIL
|
|
|| sqlx::query_scalar!("SELECT super_admin FROM password WHERE email = $1", email)
|
|
.fetch_optional(&mut *conn)
|
|
.await
|
|
.map_err(|e| Error::internal_err(format!("fetching super admin: {e:#}")))?
|
|
.unwrap_or(false);
|
|
|
|
if let Some((prefix, name)) = permissioned_as.split_once('/') {
|
|
if prefix == "u" {
|
|
let (is_admin, is_operator) = if is_super_admin {
|
|
(true, false)
|
|
} else {
|
|
let r = sqlx::query!(
|
|
"SELECT is_admin, operator FROM usr where username = $1 AND \
|
|
workspace_id = $2 AND disabled = false",
|
|
name,
|
|
&w_id
|
|
)
|
|
.fetch_optional(&mut *conn)
|
|
.await?;
|
|
if let Some(r) = r {
|
|
(r.is_admin, r.operator)
|
|
} else {
|
|
return Err(Error::NotFound(format!(
|
|
"user {name} not found in workspace {w_id}"
|
|
)));
|
|
}
|
|
};
|
|
|
|
let groups = get_groups_for_user(w_id, &name, email, &mut *conn).await?;
|
|
|
|
let folders = get_folders_for_user(w_id, &name, &groups, &mut *conn).await?;
|
|
|
|
Ok(Authed {
|
|
email: email.to_string(),
|
|
username: name.to_string(),
|
|
is_admin,
|
|
is_operator,
|
|
groups,
|
|
folders,
|
|
scopes: None,
|
|
token_prefix: None,
|
|
})
|
|
} else {
|
|
let groups = vec![name.to_string()];
|
|
let folders = get_folders_for_user(&w_id, "", &groups, &mut *conn).await?;
|
|
Ok(Authed {
|
|
email: email.to_string(),
|
|
username: format!("{}{name}", crate::users::USERNAME_GROUP_PREFIX),
|
|
is_admin: false,
|
|
groups,
|
|
is_operator: false,
|
|
folders,
|
|
scopes: None,
|
|
token_prefix: None,
|
|
})
|
|
}
|
|
} else {
|
|
Ok(Authed {
|
|
email: email.to_string(),
|
|
username: permissioned_as.to_string(),
|
|
is_admin: is_super_admin,
|
|
is_operator: true,
|
|
groups: vec![],
|
|
folders: vec![],
|
|
scopes: None,
|
|
token_prefix: None,
|
|
})
|
|
}
|
|
}
|
|
|
|
pub async fn get_folders_for_user<'e, E: sqlx::PgExecutor<'e>>(
|
|
w_id: &str,
|
|
username: &str,
|
|
groups: &[String],
|
|
db: E,
|
|
) -> Result<Vec<(String, bool, bool)>> {
|
|
let mut perms = groups
|
|
.into_iter()
|
|
.map(|x| format!("g/{}", x))
|
|
.collect::<Vec<_>>();
|
|
perms.insert(0, format!("u/{}", username));
|
|
let folders = sqlx::query!(
|
|
"SELECT name, (EXISTS (SELECT 1 FROM (SELECT key, value FROM jsonb_each_text(extra_perms) WHERE key = ANY($1)) t WHERE value::boolean IS true)) as write, $1 && owners::text[] as owner FROM folder
|
|
WHERE extra_perms ?| $1 AND workspace_id = $2",
|
|
&perms[..],
|
|
w_id,
|
|
)
|
|
.fetch_all(db)
|
|
.await?
|
|
.into_iter()
|
|
.map(|x| (x.name, x.write.unwrap_or(false), x.owner.unwrap_or(false)))
|
|
.collect();
|
|
|
|
Ok(folders)
|
|
}
|
|
|
|
pub async fn get_groups_for_user<'e, E: sqlx::PgExecutor<'e>>(
|
|
w_id: &str,
|
|
username: &str,
|
|
email: &str,
|
|
db: E,
|
|
) -> Result<Vec<String>> {
|
|
let groups = sqlx::query_scalar!(
|
|
"SELECT group_ FROM usr_to_group where usr = $1 AND workspace_id = $2 UNION ALL SELECT igroup FROM email_to_igroup WHERE email = $3",
|
|
username,
|
|
w_id,
|
|
email
|
|
)
|
|
.fetch_all(db)
|
|
.await?
|
|
.into_iter().filter_map(|x| x)
|
|
.collect();
|
|
Ok(groups)
|
|
}
|
|
|
|
pub async fn get_job_perms<'a, E: sqlx::PgExecutor<'a>>(
|
|
db: E,
|
|
job_id: &Uuid,
|
|
w_id: &str,
|
|
) -> sqlx::Result<Option<JobPerms>> {
|
|
sqlx::query_as!(
|
|
JobPerms,
|
|
"SELECT email, username, is_admin, is_operator, groups, folders FROM job_perms WHERE job_id = $1 AND workspace_id = $2",
|
|
job_id,
|
|
w_id
|
|
)
|
|
.fetch_optional(db)
|
|
.warn_after_seconds(3)
|
|
.await
|
|
}
|
|
|
|
#[tracing::instrument(level = "trace", skip_all)]
|
|
pub async fn create_token_for_owner(
|
|
db: &DB,
|
|
w_id: &str,
|
|
owner: &str,
|
|
label: &str,
|
|
expires_in: u64,
|
|
email: &str,
|
|
job_id: &Uuid,
|
|
perms: Option<JobPerms>,
|
|
audit_span: Option<String>,
|
|
) -> crate::error::Result<String> {
|
|
let job_perms = if perms.is_some() {
|
|
Ok(perms)
|
|
} else {
|
|
get_job_perms(db, job_id, w_id).await
|
|
};
|
|
let job_authed = match job_perms {
|
|
Ok(Some(jp)) => jp.into(),
|
|
_ => {
|
|
tracing::warn!("Could not get permissions for job {job_id} from job_perms table, getting permissions directly...");
|
|
fetch_authed_from_permissioned_as(owner, email, w_id, db)
|
|
.await
|
|
.map_err(|e| {
|
|
Error::internal_err(format!(
|
|
"Could not get permissions directly for job {job_id}: {e:#}"
|
|
))
|
|
})?
|
|
}
|
|
};
|
|
|
|
create_jwt_token(
|
|
job_authed,
|
|
w_id,
|
|
expires_in,
|
|
Some(*job_id),
|
|
Some(label.to_string()),
|
|
audit_span,
|
|
None,
|
|
)
|
|
.await
|
|
}
|
|
|
|
pub async fn create_jwt_token(
|
|
authed: Authed,
|
|
workspace_id: &str,
|
|
expires_in_seconds: u64,
|
|
job_id: Option<Uuid>,
|
|
label: Option<String>,
|
|
audit_span: Option<String>,
|
|
scopes: Option<Vec<String>>,
|
|
) -> crate::error::Result<String> {
|
|
let payload = JWTAuthClaims {
|
|
email: authed.email.clone(),
|
|
username: authed.username.clone(),
|
|
is_admin: authed.is_admin,
|
|
is_operator: authed.is_operator,
|
|
groups: authed.groups.clone(),
|
|
folders: authed.folders.clone(),
|
|
label,
|
|
workspace_id: Some(workspace_id.to_string()),
|
|
workspace_ids: None,
|
|
exp: (chrono::Utc::now() + chrono::Duration::seconds(expires_in_seconds as i64)).timestamp()
|
|
as usize,
|
|
job_id: job_id.map(|id| id.to_string()),
|
|
scopes,
|
|
audit_span,
|
|
};
|
|
|
|
let token = jwt::encode_with_internal_secret(&payload)
|
|
.await
|
|
.with_context(|| match job_id {
|
|
Some(job_id) => format!("Could not encode JWT token for job {job_id}"),
|
|
None => "Could not encode JWT token".to_string(),
|
|
})?;
|
|
|
|
Ok(format!("jwt_{}", token))
|
|
}
|
|
|
|
#[cfg(feature = "aws_auth")]
|
|
pub mod aws {
|
|
|
|
use super::*;
|
|
use crate::utils::empty_as_none;
|
|
use aws_config::{BehaviorVersion, Region};
|
|
use aws_sdk_sts::{
|
|
config::Credentials as AwsCredentials,
|
|
operation::{
|
|
assume_role_with_saml::AssumeRoleWithSamlOutput,
|
|
assume_role_with_web_identity::{
|
|
builders::AssumeRoleWithWebIdentityFluentBuilder, AssumeRoleWithWebIdentityOutput,
|
|
},
|
|
},
|
|
types::Credentials,
|
|
Client,
|
|
};
|
|
|
|
pub const AWS_OIDC_AUDIENCE: &'static str = "sts.amazonaws.com";
|
|
|
|
pub trait GetAuthenticationOutput {
|
|
fn get_credentials(&self) -> Result<&Credentials>;
|
|
}
|
|
|
|
impl GetAuthenticationOutput for AssumeRoleWithSamlOutput {
|
|
fn get_credentials(&self) -> Result<&Credentials> {
|
|
let credentials = self.credentials.as_ref().ok_or(Error::BadGateway(
|
|
"Error fetching credentials from AWS STS".to_string(),
|
|
))?;
|
|
Ok(credentials)
|
|
}
|
|
}
|
|
|
|
impl GetAuthenticationOutput for AssumeRoleWithWebIdentityOutput {
|
|
fn get_credentials(&self) -> Result<&Credentials> {
|
|
let credentials = self.credentials.as_ref().ok_or(Error::BadGateway(
|
|
"Error fetching credentials from AWS STS".to_string(),
|
|
))?;
|
|
Ok(credentials)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::Type)]
|
|
#[sqlx(type_name = "AWS_AUTH_RESOURCE_TYPE", rename_all = "lowercase")]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum AwsAuthResourceType {
|
|
Credentials,
|
|
Oidc,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct CredentialsAuth {
|
|
#[serde(deserialize_with = "empty_as_none")]
|
|
pub region: Option<String>,
|
|
#[serde(rename = "awsAccessKeyId")]
|
|
pub aws_access_key_id: String,
|
|
#[serde(rename = "awsSecretAccessKey")]
|
|
pub aws_secret_access_key: String,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub struct OidcAuth {
|
|
#[serde(deserialize_with = "empty_as_none")]
|
|
pub region: Option<String>,
|
|
#[serde(rename = "roleArn")]
|
|
pub role_arn: String,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
#[serde(untagged)]
|
|
pub enum AWSAuthConfig {
|
|
Credentials(CredentialsAuth),
|
|
Oidc(OidcAuth),
|
|
}
|
|
|
|
pub async fn get_assume_role_with_web_identity_fluent_builder(
|
|
oidc_auth: &OidcAuth,
|
|
token: String,
|
|
role_session_name: Option<impl ToString>,
|
|
) -> Result<AssumeRoleWithWebIdentityFluentBuilder> {
|
|
let region = oidc_auth.region.as_deref().unwrap_or_else(|| "us-east-1");
|
|
|
|
let credentials = AwsCredentials::new("", "", None, None, "UserInput");
|
|
|
|
let config = aws_config::defaults(BehaviorVersion::latest())
|
|
.credentials_provider(credentials)
|
|
.region(Region::new(region.to_string()))
|
|
.load()
|
|
.await;
|
|
|
|
let assume_role_with_web_identity_fluent_builder = Client::new(&config)
|
|
.assume_role_with_web_identity()
|
|
.set_role_arn(Some(oidc_auth.role_arn.to_owned()))
|
|
.set_role_session_name(role_session_name.map(|str| str.to_string()))
|
|
.set_web_identity_token(Some(token));
|
|
|
|
Ok(assume_role_with_web_identity_fluent_builder)
|
|
}
|
|
}
|