Skip to content

migrate permissions to Cedar during application bootstrap#1653

Merged
Artuomka merged 1 commit intomainfrom
backend_extend_cedar_permissions
Mar 9, 2026
Merged

migrate permissions to Cedar during application bootstrap#1653
Artuomka merged 1 commit intomainfrom
backend_extend_cedar_permissions

Conversation

@Artuomka
Copy link
Collaborator

@Artuomka Artuomka commented Mar 9, 2026

No description provided.

Copilot AI review requested due to automatic review settings March 9, 2026 13:50
@Artuomka Artuomka merged commit 414714d into main Mar 9, 2026
16 of 19 checks passed
@Artuomka Artuomka deleted the backend_extend_cedar_permissions branch March 9, 2026 13:52
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR migrates existing group permission records into stored Cedar policies automatically during NestJS application startup, so Cedar authorization has policies available without running a separate migration step.

Changes:

  • Invoke migratePermissionsToCedar() during application bootstrap using the TypeORM DataSource.
  • Make the Cedar migration idempotent by skipping groups that already have a cedarPolicy.
  • Improve migration completion logging to clarify skipped groups.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
backend/src/main.ts Runs the Cedar permission migration during application startup before listening.
backend/src/entities/cedar-authorization/scripts/migrate-permissions-to-cedar.ts Skips already-migrated groups and updates the migration log message.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}

console.log(`Migrated Cedar policies for ${migratedCount} groups`);
console.log(`Migrated Cedar policies for ${migratedCount} groups (skipped groups with existing policies)`);
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

This migration now runs as part of API startup, so logging via console.log will bypass the configured Winston/Nest logger (and any structured logging / log routing). Prefer using the existing application logger (e.g., Nest Logger or WinstonLogger) so the message is consistent with the rest of the service logs.

Copilot uses AI. Check for mistakes.
);

const dataSource = app.get(DataSource);
await migratePermissionsToCedar(dataSource);
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

Running a full permissions→Cedar migration as part of every application bootstrap can significantly delay startup and can prevent the API from coming up if the DB is temporarily unavailable or the migration errors. Consider gating this behind an explicit env flag (and/or CEDAR_AUTHORIZATION_ENABLED), and/or taking a DB advisory lock / single-run mechanism so multiple instances don’t race and repeat the work on deploy; alternatively run it as a separate one-off job and only log/report failures without crashing the server (depending on desired fail-fast behavior).

Suggested change
await migratePermissionsToCedar(dataSource);
if (process.env.CEDAR_MIGRATION_ON_BOOT !== 'false') {
await migratePermissionsToCedar(dataSource);
}

Copilot uses AI. Check for mistakes.
Comment on lines 20 to 24
.leftJoinAndSelect('group.connection', 'connection')
.leftJoinAndSelect('group.permissions', 'permission')
.where('connection.id = :connectionId', { connectionId: connection.id })
.andWhere('(group.cedarPolicy IS NULL OR group.cedarPolicy = :empty)', { empty: '' })
.getMany();
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

Now that this script runs during normal bootstrap, the migration approach can become a startup bottleneck: it loads groups per connection and then persists each group one-by-one. Consider reducing round-trips (e.g., query all groups needing migration in one pass, batch updates, and prefer update on cedarPolicy rather than save on fully loaded entities) to keep startup time predictable for large datasets.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants