diff --git a/packages/backend-app-api/migrations/20240327104803_public_keys.js b/packages/backend-app-api/migrations/20240327104803_public_keys.js index b3ee96300c..d4292c775b 100644 --- a/packages/backend-app-api/migrations/20240327104803_public_keys.js +++ b/packages/backend-app-api/migrations/20240327104803_public_keys.js @@ -32,8 +32,9 @@ exports.up = async function up(knex) { table.text('key').notNullable().comment('JSON serialized public key'); + // Expiration is stored as a string for simplicity, all checks are done client-side table - .timestamp('expires_at') + .string('expires_at') .notNullable() .comment('The time that the key expires'); }, diff --git a/packages/backend-app-api/src/services/implementations/auth/DatabaseKeyStore.test.ts b/packages/backend-app-api/src/services/implementations/auth/DatabaseKeyStore.test.ts index f0c0d186c5..010cb68a11 100644 --- a/packages/backend-app-api/src/services/implementations/auth/DatabaseKeyStore.test.ts +++ b/packages/backend-app-api/src/services/implementations/auth/DatabaseKeyStore.test.ts @@ -118,7 +118,7 @@ describe('DatabaseKeyStore', () => { key: testKey, expiresAt: new Date(NaN), }), - ).rejects.toThrow('Failed to format public key expiration date'); + ).rejects.toThrow('Invalid time value'); }); }); }); diff --git a/packages/backend-app-api/src/services/implementations/auth/DatabaseKeyStore.ts b/packages/backend-app-api/src/services/implementations/auth/DatabaseKeyStore.ts index 62e11ad991..6a1536fd18 100644 --- a/packages/backend-app-api/src/services/implementations/auth/DatabaseKeyStore.ts +++ b/packages/backend-app-api/src/services/implementations/auth/DatabaseKeyStore.ts @@ -28,7 +28,7 @@ export const TABLE = 'backstage_backend_public_keys__keys'; type Row = { id: string; key: string; - expires_at: string | Date; // Needs parsing to handle different DB implementations + expires_at: string; }; export function applyDatabaseMigrations(knex: Knex): Promise { @@ -68,14 +68,10 @@ export class DatabaseKeyStore implements KeyStore { key: JsonObject & { kid: string }; expiresAt: Date; }) { - const expiresAt = DateTime.fromJSDate(options.expiresAt).toSQL(); - if (!expiresAt) { - throw new Error('Failed to format public key expiration date'); - } await this.client(TABLE).insert({ id: options.key.kid, key: JSON.stringify(options.key), - expires_at: expiresAt, + expires_at: options.expiresAt.toISOString(), }); } @@ -84,7 +80,7 @@ export class DatabaseKeyStore implements KeyStore { const keys = rows.map(row => ({ id: row.id, key: JSON.parse(row.key), - expiresAt: this.#parseDate(row.expires_at), + expiresAt: new Date(row.expires_at), })); const validKeys = []; @@ -120,19 +116,4 @@ export class DatabaseKeyStore implements KeyStore { return { keys: validKeys }; } - - #parseDate(date: string | Date) { - const parsedDate = - typeof date === 'string' - ? DateTime.fromSQL(date, { zone: 'UTC' }) - : DateTime.fromJSDate(date); - - if (!parsedDate.isValid) { - throw new Error( - `Failed to parse date, reason: ${parsedDate.invalidReason}, explanation: ${parsedDate.invalidExplanation}`, - ); - } - - return parsedDate.toJSDate(); - } }