diff --git a/.changeset/fuzzy-months-study.md b/.changeset/fuzzy-months-study.md new file mode 100644 index 0000000000..024349f90b --- /dev/null +++ b/.changeset/fuzzy-months-study.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-auth-backend': patch +--- + +Fixed a bug in auth plugin on the backend where it ignores the skip migration database options when using the database provider. diff --git a/plugins/auth-backend/src/identity/DatabaseKeyStore.test.ts b/plugins/auth-backend/src/identity/DatabaseKeyStore.test.ts index fa7b251780..da67a2f88f 100644 --- a/plugins/auth-backend/src/identity/DatabaseKeyStore.test.ts +++ b/plugins/auth-backend/src/identity/DatabaseKeyStore.test.ts @@ -14,10 +14,22 @@ * limitations under the License. */ -import Knex from 'knex'; +import Knex, { Knex as KnexType } from 'knex'; import { DatabaseKeyStore } from './DatabaseKeyStore'; import { DateTime } from 'luxon'; +function createDatabaseManager( + client: KnexType, + skipMigrations: boolean = false, +) { + return { + getClient: async () => client, + migrations: { + skip: skipMigrations, + }, + }; +} + function createDB() { const knex = Knex({ client: 'better-sqlite3', @@ -38,8 +50,10 @@ const keyBase = { describe('DatabaseKeyStore', () => { it('should store a key', async () => { - const database = createDB(); - const store = await DatabaseKeyStore.create({ database }); + const client = createDB(); + const store = await DatabaseKeyStore.create({ + database: createDatabaseManager(client), + }); const key = { kid: '123', @@ -59,8 +73,10 @@ describe('DatabaseKeyStore', () => { }); it('should remove stored keys', async () => { - const database = createDB(); - const store = await DatabaseKeyStore.create({ database }); + const client = createDB(); + const store = await DatabaseKeyStore.create({ + database: createDatabaseManager(client), + }); const key1 = { kid: '1', ...keyBase }; const key2 = { kid: '2', ...keyBase }; diff --git a/plugins/auth-backend/src/identity/DatabaseKeyStore.ts b/plugins/auth-backend/src/identity/DatabaseKeyStore.ts index 6bae1f4412..8039837469 100644 --- a/plugins/auth-backend/src/identity/DatabaseKeyStore.ts +++ b/plugins/auth-backend/src/identity/DatabaseKeyStore.ts @@ -14,7 +14,10 @@ * limitations under the License. */ -import { resolvePackagePath } from '@backstage/backend-common'; +import { + PluginDatabaseManager, + resolvePackagePath, +} from '@backstage/backend-common'; import { Knex } from 'knex'; import { DateTime } from 'luxon'; import { AnyJWK, KeyStore, StoredKey } from './types'; @@ -33,7 +36,7 @@ type Row = { }; type Options = { - database: Knex; + database: PluginDatabaseManager; }; const parseDate = (date: string | Date) => { @@ -54,29 +57,32 @@ const parseDate = (date: string | Date) => { export class DatabaseKeyStore implements KeyStore { static async create(options: Options): Promise { const { database } = options; + const client = await database.getClient(); - await database.migrate.latest({ - directory: migrationsDir, - }); + if (!database.migrations?.skip) { + await client.migrate.latest({ + directory: migrationsDir, + }); + } - return new DatabaseKeyStore(options); + return new DatabaseKeyStore(client); } - private readonly database: Knex; + private readonly client: Knex; - private constructor(options: Options) { - this.database = options.database; + private constructor(client: Knex) { + this.client = client; } async addKey(key: AnyJWK): Promise { - await this.database(TABLE).insert({ + await this.client(TABLE).insert({ kid: key.kid, key: JSON.stringify(key), }); } async listKeys(): Promise<{ items: StoredKey[] }> { - const rows = await this.database(TABLE).select(); + const rows = await this.client(TABLE).select(); return { items: rows.map(row => ({ @@ -87,6 +93,6 @@ export class DatabaseKeyStore implements KeyStore { } async removeKeys(kids: string[]): Promise { - await this.database(TABLE).delete().whereIn('kid', kids); + await this.client(TABLE).delete().whereIn('kid', kids); } } diff --git a/plugins/auth-backend/src/identity/KeyStores.ts b/plugins/auth-backend/src/identity/KeyStores.ts index 3d238adba9..e29b4b6fc4 100644 --- a/plugins/auth-backend/src/identity/KeyStores.ts +++ b/plugins/auth-backend/src/identity/KeyStores.ts @@ -53,9 +53,7 @@ export class KeyStores { throw new Error('This KeyStore provider requires a database'); } - return await DatabaseKeyStore.create({ - database: await database.getClient(), - }); + return await DatabaseKeyStore.create({ database }); } if (provider === 'memory') {