-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
257 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
server/migrations/v2.17.3-use-subfolder-for-oidc-redirect-uris.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* @typedef MigrationContext | ||
* @property {import('sequelize').QueryInterface} queryInterface - a suquelize QueryInterface object. | ||
* @property {import('../Logger')} logger - a Logger object. | ||
* | ||
* @typedef MigrationOptions | ||
* @property {MigrationContext} context - an object containing the migration context. | ||
*/ | ||
|
||
/** | ||
* This upward migration adds an subfolder setting for OIDC redirect URIs. | ||
* It updates existing OIDC setups to set this option to None (empty subfolder), so they continue to work as before. | ||
* IF OIDC is not enabled, no action is taken (i.e. the subfolder is left undefined), | ||
* so that future OIDC setups will use the default subfolder. | ||
* | ||
* @param {MigrationOptions} options - an object containing the migration context. | ||
* @returns {Promise<void>} - A promise that resolves when the migration is complete. | ||
*/ | ||
async function up({ context: { queryInterface, logger } }) { | ||
// Upwards migration script | ||
logger.info('[2.17.3 migration] UPGRADE BEGIN: 2.17.3-use-subfolder-for-oidc-redirect-uris') | ||
|
||
const serverSettings = await getServerSettings(queryInterface, logger) | ||
if (serverSettings.authActiveAuthMethods?.includes('openid')) { | ||
logger.info('[2.17.3 migration] OIDC is enabled, adding authOpenIDSubfolderForRedirectURLs to server settings') | ||
serverSettings.authOpenIDSubfolderForRedirectURLs = '' | ||
await updateServerSettings(queryInterface, logger, serverSettings) | ||
} else { | ||
logger.info('[2.17.3 migration] OIDC is not enabled, no action required') | ||
} | ||
|
||
logger.info('[2.17.3 migration] UPGRADE END: 2.17.3-use-subfolder-for-oidc-redirect-uris') | ||
} | ||
|
||
/** | ||
* This downward migration script removes the subfolder setting for OIDC redirect URIs. | ||
* | ||
* @param {MigrationOptions} options - an object containing the migration context. | ||
* @returns {Promise<void>} - A promise that resolves when the migration is complete. | ||
*/ | ||
async function down({ context: { queryInterface, logger } }) { | ||
// Downward migration script | ||
logger.info('[2.17.3 migration] DOWNGRADE BEGIN: 2.17.3-use-subfolder-for-oidc-redirect-uris ') | ||
|
||
// Remove the OIDC subfolder option from the server settings | ||
const serverSettings = await getServerSettings(queryInterface, logger) | ||
if (serverSettings.authOpenIDSubfolderForRedirectURLs !== undefined) { | ||
logger.info('[2.17.3 migration] Removing authOpenIDSubfolderForRedirectURLs from server settings') | ||
delete serverSettings.authOpenIDSubfolderForRedirectURLs | ||
await updateServerSettings(queryInterface, logger, serverSettings) | ||
} else { | ||
logger.info('[2.17.3 migration] authOpenIDSubfolderForRedirectURLs not found in server settings, no action required') | ||
} | ||
|
||
logger.info('[2.17.3 migration] DOWNGRADE END: 2.17.3-use-subfolder-for-oidc-redirect-uris ') | ||
} | ||
|
||
async function getServerSettings(queryInterface, logger) { | ||
const result = await queryInterface.sequelize.query('SELECT value FROM settings WHERE key = "server-settings";') | ||
if (!result[0].length) { | ||
logger.error('[2.17.3 migration] Server settings not found') | ||
throw new Error('Server settings not found') | ||
} | ||
|
||
let serverSettings = null | ||
try { | ||
serverSettings = JSON.parse(result[0][0].value) | ||
} catch (error) { | ||
logger.error('[2.17.3 migration] Error parsing server settings:', error) | ||
throw error | ||
} | ||
|
||
return serverSettings | ||
} | ||
|
||
async function updateServerSettings(queryInterface, logger, serverSettings) { | ||
await queryInterface.sequelize.query('UPDATE settings SET value = :value WHERE key = "server-settings";', { | ||
replacements: { | ||
value: JSON.stringify(serverSettings) | ||
} | ||
}) | ||
} | ||
|
||
module.exports = { up, down } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
test/server/migrations/v2.17.3-use-subfolder-for-oidc-redirect-uris.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
const { expect } = require('chai') | ||
const sinon = require('sinon') | ||
const { up, down } = require('../../../server/migrations/v2.17.3-use-subfolder-for-oidc-redirect-uris') | ||
const { Sequelize } = require('sequelize') | ||
const Logger = require('../../../server/Logger') | ||
|
||
describe('Migration v2.17.3-use-subfolder-for-oidc-redirect-uris', () => { | ||
let queryInterface, logger, context | ||
|
||
beforeEach(() => { | ||
queryInterface = { | ||
sequelize: { | ||
query: sinon.stub() | ||
} | ||
} | ||
logger = { | ||
info: sinon.stub(), | ||
error: sinon.stub() | ||
} | ||
context = { queryInterface, logger } | ||
}) | ||
|
||
describe('up', () => { | ||
it('should add authOpenIDSubfolderForRedirectURLs if OIDC is enabled', async () => { | ||
queryInterface.sequelize.query.onFirstCall().resolves([[{ value: JSON.stringify({ authActiveAuthMethods: ['openid'] }) }]]) | ||
queryInterface.sequelize.query.onSecondCall().resolves() | ||
|
||
await up({ context }) | ||
|
||
expect(logger.info.calledWith('[2.17.3 migration] UPGRADE BEGIN: 2.17.3-use-subfolder-for-oidc-redirect-uris')).to.be.true | ||
expect(logger.info.calledWith('[2.17.3 migration] OIDC is enabled, adding authOpenIDSubfolderForRedirectURLs to server settings')).to.be.true | ||
expect(queryInterface.sequelize.query.calledTwice).to.be.true | ||
expect(queryInterface.sequelize.query.calledWith('SELECT value FROM settings WHERE key = "server-settings";')).to.be.true | ||
expect( | ||
queryInterface.sequelize.query.calledWith('UPDATE settings SET value = :value WHERE key = "server-settings";', { | ||
replacements: { | ||
value: JSON.stringify({ authActiveAuthMethods: ['openid'], authOpenIDSubfolderForRedirectURLs: '' }) | ||
} | ||
}) | ||
).to.be.true | ||
expect(logger.info.calledWith('[2.17.3 migration] UPGRADE END: 2.17.3-use-subfolder-for-oidc-redirect-uris')).to.be.true | ||
}) | ||
|
||
it('should not add authOpenIDSubfolderForRedirectURLs if OIDC is not enabled', async () => { | ||
queryInterface.sequelize.query.onFirstCall().resolves([[{ value: JSON.stringify({ authActiveAuthMethods: [] }) }]]) | ||
|
||
await up({ context }) | ||
|
||
expect(logger.info.calledWith('[2.17.3 migration] UPGRADE BEGIN: 2.17.3-use-subfolder-for-oidc-redirect-uris')).to.be.true | ||
expect(logger.info.calledWith('[2.17.3 migration] OIDC is not enabled, no action required')).to.be.true | ||
expect(queryInterface.sequelize.query.calledOnce).to.be.true | ||
expect(queryInterface.sequelize.query.calledWith('SELECT value FROM settings WHERE key = "server-settings";')).to.be.true | ||
expect(logger.info.calledWith('[2.17.3 migration] UPGRADE END: 2.17.3-use-subfolder-for-oidc-redirect-uris')).to.be.true | ||
}) | ||
|
||
it('should throw an error if server settings cannot be parsed', async () => { | ||
queryInterface.sequelize.query.onFirstCall().resolves([[{ value: 'invalid json' }]]) | ||
|
||
try { | ||
await up({ context }) | ||
} catch (error) { | ||
expect(queryInterface.sequelize.query.calledOnce).to.be.true | ||
expect(queryInterface.sequelize.query.calledWith('SELECT value FROM settings WHERE key = "server-settings";')).to.be.true | ||
expect(logger.error.calledWith('[2.17.3 migration] Error parsing server settings:')).to.be.true | ||
expect(error).to.be.instanceOf(Error) | ||
} | ||
}) | ||
|
||
it('should throw an error if server settings are not found', async () => { | ||
queryInterface.sequelize.query.onFirstCall().resolves([[]]) | ||
|
||
try { | ||
await up({ context }) | ||
} catch (error) { | ||
expect(queryInterface.sequelize.query.calledOnce).to.be.true | ||
expect(queryInterface.sequelize.query.calledWith('SELECT value FROM settings WHERE key = "server-settings";')).to.be.true | ||
expect(logger.error.calledWith('[2.17.3 migration] Server settings not found')).to.be.true | ||
expect(error).to.be.instanceOf(Error) | ||
} | ||
}) | ||
}) | ||
|
||
describe('down', () => { | ||
it('should remove authOpenIDSubfolderForRedirectURLs if it exists', async () => { | ||
queryInterface.sequelize.query.onFirstCall().resolves([[{ value: JSON.stringify({ authOpenIDSubfolderForRedirectURLs: '' }) }]]) | ||
queryInterface.sequelize.query.onSecondCall().resolves() | ||
|
||
await down({ context }) | ||
|
||
expect(logger.info.calledWith('[2.17.3 migration] DOWNGRADE BEGIN: 2.17.3-use-subfolder-for-oidc-redirect-uris ')).to.be.true | ||
expect(logger.info.calledWith('[2.17.3 migration] Removing authOpenIDSubfolderForRedirectURLs from server settings')).to.be.true | ||
expect(queryInterface.sequelize.query.calledTwice).to.be.true | ||
expect(queryInterface.sequelize.query.calledWith('SELECT value FROM settings WHERE key = "server-settings";')).to.be.true | ||
expect( | ||
queryInterface.sequelize.query.calledWith('UPDATE settings SET value = :value WHERE key = "server-settings";', { | ||
replacements: { | ||
value: JSON.stringify({}) | ||
} | ||
}) | ||
).to.be.true | ||
expect(logger.info.calledWith('[2.17.3 migration] DOWNGRADE END: 2.17.3-use-subfolder-for-oidc-redirect-uris ')).to.be.true | ||
}) | ||
|
||
it('should not remove authOpenIDSubfolderForRedirectURLs if it does not exist', async () => { | ||
queryInterface.sequelize.query.onFirstCall().resolves([[{ value: JSON.stringify({}) }]]) | ||
|
||
await down({ context }) | ||
|
||
expect(logger.info.calledWith('[2.17.3 migration] DOWNGRADE BEGIN: 2.17.3-use-subfolder-for-oidc-redirect-uris ')).to.be.true | ||
expect(logger.info.calledWith('[2.17.3 migration] authOpenIDSubfolderForRedirectURLs not found in server settings, no action required')).to.be.true | ||
expect(queryInterface.sequelize.query.calledOnce).to.be.true | ||
expect(queryInterface.sequelize.query.calledWith('SELECT value FROM settings WHERE key = "server-settings";')).to.be.true | ||
expect(logger.info.calledWith('[2.17.3 migration] DOWNGRADE END: 2.17.3-use-subfolder-for-oidc-redirect-uris ')).to.be.true | ||
}) | ||
}) | ||
}) |