fix(auth-plugin-backend): Allow it to skip migrations

When auth plugin is using database provider it verify if needs to skip migrations

Signed-off-by: Daniel Dias Branco Arthaud <arthaud@gmail.com>
This commit is contained in:
Daniel Dias Branco Arthaud
2022-08-08 18:13:44 -03:00
committed by Daniel Dias Branco Arthaud
parent 0f8d5ff557
commit c676a9e07b
4 changed files with 45 additions and 20 deletions
+5
View File
@@ -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.
@@ -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 };
@@ -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<DatabaseKeyStore> {
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<void> {
await this.database<Row>(TABLE).insert({
await this.client<Row>(TABLE).insert({
kid: key.kid,
key: JSON.stringify(key),
});
}
async listKeys(): Promise<{ items: StoredKey[] }> {
const rows = await this.database<Row>(TABLE).select();
const rows = await this.client<Row>(TABLE).select();
return {
items: rows.map(row => ({
@@ -87,6 +93,6 @@ export class DatabaseKeyStore implements KeyStore {
}
async removeKeys(kids: string[]): Promise<void> {
await this.database(TABLE).delete().whereIn('kid', kids);
await this.client(TABLE).delete().whereIn('kid', kids);
}
}
@@ -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') {