Skip to content

Commit

Permalink
BYPASSRLS admin policy falllback (#4493)
Browse files Browse the repository at this point in the history
* BYPASSRLS admin policy falllback

* use fixed list instead

---------

Co-authored-by: Ruben Fiszel <[email protected]>
  • Loading branch information
HugoCasa and rubenfiszel authored Oct 8, 2024
1 parent 644dd87 commit e44decb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 78 deletions.

This file was deleted.

This file was deleted.

14 changes: 0 additions & 14 deletions backend/custom_migrations/bypassrls_1.sql

This file was deleted.

1 change: 1 addition & 0 deletions backend/migrations/20241006144414_admin_policy.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- Add down migration script here
24 changes: 24 additions & 0 deletions backend/migrations/20241006144414_admin_policy.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Add up migration script here
DO
$$
DECLARE
tbl_name text;
policy_exists boolean;
tbl_names text[] := ARRAY['account', 'app', 'audit', 'capture', 'completed_job', 'flow', 'folder', 'http_trigger', 'queue', 'raw_app', 'resource', 'schedule', 'script', 'usr_to_group', 'variable'];
BEGIN
FOR tbl_name IN SELECT unnest(tbl_names)
LOOP
SELECT EXISTS (
SELECT 1
FROM pg_policies
WHERE schemaname = 'public'
AND tablename = tbl_name
AND policyname = 'admin_policy'
) INTO policy_exists;

IF NOT policy_exists THEN
EXECUTE format('CREATE POLICY admin_policy ON %I TO windmill_admin USING (true);', tbl_name);
END IF;
END LOOP;
END;
$$;
32 changes: 0 additions & 32 deletions backend/windmill-api/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,6 @@ pub async fn migrate(db: &DB) -> Result<(), Error> {
Err(err) => Err(err),
}?;

#[cfg(feature = "enterprise")]
if let Err(e) = windmill_migrations(&mut custom_migrator, db).await {
tracing::error!("Could not apply windmill custom migrations: {e:#}")
}

Ok(())
}

Expand Down Expand Up @@ -497,33 +492,6 @@ async fn fix_job_completed_index(db: &DB) -> Result<(), Error> {
Ok(())
}

#[cfg(feature = "enterprise")]
async fn windmill_migrations(migrator: &mut CustomMigrator, db: &DB) -> Result<(), Error> {
if std::env::var("MIGRATION_NO_BYPASSRLS").is_ok() {
migrator.lock().await?;
let has_done_migration = sqlx::query_scalar!(
"SELECT EXISTS(SELECT name FROM windmill_migrations WHERE name = 'bypassrls_1-2')",
)
.fetch_one(db)
.await?
.unwrap_or(false);

if !has_done_migration {
let query = include_str!("../../custom_migrations/bypassrls_1.sql");
tracing::info!("Applying bypassrls_1.sql");
let mut tx: sqlx::Transaction<'_, Postgres> = db.begin().await?;
tx.execute(query).await?;
tracing::info!("Applied bypassrls_1.sql");
sqlx::query!("INSERT INTO windmill_migrations (name) VALUES ('bypassrls_1-2')")
.execute(&mut *tx)
.await?;
tx.commit().await?;
}
migrator.unlock().await?;
}
Ok(())
}

#[derive(Clone, Debug)]
pub struct ApiAuthed {
pub email: String,
Expand Down

0 comments on commit e44decb

Please sign in to comment.