Disable catalog permission middleware unless permission.enabled = true

Signed-off-by: Joe Porpeglia <josephp@spotify.com>
This commit is contained in:
Joe Porpeglia
2022-01-11 18:51:07 -05:00
committed by Joe Porpeglia
parent 17ebcb09cd
commit 63e8b8605e
2 changed files with 50 additions and 12 deletions
@@ -413,6 +413,20 @@ data: {"updated":true}
});
describe('GET /static/docs', () => {
it('should delegate to the publisher handler', async () => {
const docsRouter = jest.fn((_req, res) => res.sendStatus(200));
publisher.docsRouter.mockReturnValue(docsRouter);
const app = await createApp(outOfTheBoxOptions);
const response = await request(app)
.get('/static/docs/default/component/test')
.send();
expect(response.status).toBe(200);
expect(docsRouter).toBeCalled();
});
it('should return assets from cache', async () => {
const app = await createApp(outOfTheBoxOptions);
@@ -428,6 +442,25 @@ data: {"updated":true}
expect(MockTechDocsCache.get).toBeCalled();
});
it('should check entity access when permissions are enabled', async () => {
MockedConfigReader.prototype.getOptionalBoolean.mockImplementation(key =>
key === 'permission.enabled' ? true : undefined,
);
const docsRouter = jest.fn((_req, res) => res.sendStatus(200));
publisher.docsRouter.mockReturnValue(docsRouter);
const app = await createApp(outOfTheBoxOptions);
MockCachedEntityLoader.prototype.load.mockResolvedValue(entity);
const response = await request(app)
.get('/static/docs/default/component/test')
.send();
expect(response.status).toBe(200);
expect(MockCachedEntityLoader.prototype.load).toBeCalled();
});
it('should not return assets without corresponding entity access', async () => {
MockedConfigReader.prototype.getOptionalBoolean.mockImplementation(key =>
key === 'permission.enabled' ? true : undefined,
+17 -12
View File
@@ -237,21 +237,26 @@ export async function createRouter(
});
// Ensures that the related entity exists and the current user has permission to view it.
router.use('/static/docs/:namespace/:kind/:name', async (req, _res, next) => {
const { kind, namespace, name } = req.params;
const entityName = { kind, namespace, name };
const token = getBearerToken(req.headers.authorization);
if (config.getOptionalBoolean('permission.enabled')) {
router.use(
'/static/docs/:namespace/:kind/:name',
async (req, _res, next) => {
const { kind, namespace, name } = req.params;
const entityName = { kind, namespace, name };
const token = getBearerToken(req.headers.authorization);
const entity = await entityLoader.load(entityName, token);
const entity = await entityLoader.load(entityName, token);
if (!entity) {
throw new NotFoundError(
`Entity not found for ${stringifyEntityRef(entityName)}`,
);
}
if (!entity) {
throw new NotFoundError(
`Entity not found for ${stringifyEntityRef(entityName)}`,
);
}
next();
});
next();
},
);
}
// If a cache manager was provided, attach the cache middleware.
if (cache) {