1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

Maintenance mode middleware (#2707)

This commit is contained in:
sjaanus 2022-12-19 09:01:04 +02:00 committed by GitHub
parent ff9bc1d10c
commit 2d5455d203
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 51 additions and 22 deletions

View File

@ -25,8 +25,9 @@ const Maintenance = () => {
<StyledErrorRoundedIcon />
<b>Maintenance Mode! </b>
<p>
Any changes you make during maintenance mode will not be saved.
We apologize for any inconvenience this may cause.
During maintenance mode, any changes made will not be saved and
you may receive an error. We apologize for any inconvenience
this may cause.
</p>
</StyledDiv>
);

View File

@ -15,9 +15,14 @@ const getPermissions = (
uiConfig: IUiConfig
): IPermission[] | undefined => {
let permissions =
auth.data && 'permissions' in auth.data && !uiConfig?.flags?.maintenance
auth.data && 'permissions' in auth.data
? auth.data.permissions
: undefined;
if (permissions && uiConfig?.flags?.maintenance) {
permissions = permissions.filter(
permission => permission.permission === 'ADMIN'
);
}
return permissions;
};

View File

@ -25,6 +25,7 @@ import { findPublicFolder } from './util/findPublicFolder';
import { conditionalMiddleware } from './middleware/conditional-middleware';
import patMiddleware from './middleware/pat-middleware';
import { Knex } from 'knex';
import maintenanceMiddleware from './middleware/maintenance-middleware';
export default async function getApp(
config: IUnleashConfig,
@ -138,6 +139,8 @@ export default async function getApp(
rbacMiddleware(config, stores, services.accessService),
);
app.use('/api/admin', maintenanceMiddleware(config));
if (typeof config.preRouterHook === 'function') {
config.preRouterHook(app, config, services, stores, db);
}

View File

@ -0,0 +1,21 @@
import { IUnleashConfig } from '../types';
import { Request } from 'express';
const maintenanceMiddleware = ({
getLogger,
flagResolver,
}: Pick<IUnleashConfig, 'getLogger' | 'flagResolver'>): any => {
const logger = getLogger('/middleware/maintenance-middleware.ts');
logger.debug('Enabling Maintenance middleware');
return async (req: Request, res, next) => {
const writeMethod = ['POST', 'PUT', 'DELETE'].includes(req.method);
if (writeMethod && flagResolver.isEnabled('maintenance')) {
res.status(503).send({});
} else {
next();
}
};
};
export default maintenanceMiddleware;

View File

@ -14,11 +14,6 @@ describe('Public Signup API', () => {
preRouterHook: perms.hook,
});
config.flagResolver = {
isEnabled: jest.fn().mockResolvedValue(true),
getAll: jest.fn(),
};
stores.accessStore = {
...stores.accessStore,
addUserToRole: jest.fn(),

View File

@ -14,11 +14,6 @@ describe('Public Signup API', () => {
preRouterHook: perms.hook,
});
config.flagResolver = {
isEnabled: jest.fn().mockResolvedValue(true),
getAll: jest.fn(),
};
stores.accessStore = {
...stores.accessStore,
addUserToRole: jest.fn(),

View File

@ -837,3 +837,21 @@ test('should have access to the get all features endpoint even if api is disable
.get('/api/admin/features')
.expect(200);
});
test('should not allow creation of feature toggle in maintenance mode', async () => {
const appWithMaintenanceMode = await setupAppWithCustomConfig(db.stores, {
experimental: {
flags: {
maintenance: true,
},
},
});
return appWithMaintenanceMode.request
.post('/api/admin/features')
.send({
name: 'maintenance-feature',
})
.set('Content-Type', 'application/json')
.expect(503);
});

View File

@ -7,15 +7,6 @@ import { PublicSignupTokenCreateSchema } from '../../../../lib/openapi/spec/publ
let stores;
let db;
jest.mock('../../../../lib/util/flag-resolver', () => {
return jest.fn().mockImplementation(() => {
return {
getAll: jest.fn(),
isEnabled: jest.fn().mockResolvedValue(true),
};
});
});
beforeEach(async () => {
db = await dbInit('test', getLogger);
stores = db.stores;