Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External repository catalog and user access management #1104

Open
wants to merge 40 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f7ac33e
Enable routes for integration user
underscope Nov 28, 2023
c63995b
Disable UI options for external access management
underscope Nov 28, 2023
cc44a53
Expose new env variable
underscope Nov 28, 2023
ff36e2d
Enable integration user on user routes
underscope Nov 28, 2023
7b44f0b
Enable creation for integration user
underscope Nov 28, 2023
5021df8
💅
underscope Nov 28, 2023
ec695c2
Add missing role
underscope Nov 29, 2023
7abfd11
Export integration user API
underscope Nov 30, 2023
b39ec86
Update auth strategy
underscope Dec 1, 2023
2c69c16
Update postman collection
underscope Dec 1, 2023
f9153a8
Add readme
underscope Dec 1, 2023
4308616
💅
underscope Dec 1, 2023
5ad13a6
💄
underscope Dec 1, 2023
80a2693
Add UserTag model
underscope Dec 2, 2023
e3c74e8
Add UserTag model associations
underscope Dec 3, 2023
dd93d0c
Update association name
underscope Dec 3, 2023
6ad1783
Expose user tag API
underscope Dec 4, 2023
c17f5f0
Register UserTag model
underscope Dec 4, 2023
23b85c4
Add tag based access check
underscope Dec 4, 2023
df645aa
Add access tag flag
underscope Dec 5, 2023
4473109
Enable access tag creation only by the integration user
underscope Dec 5, 2023
1a0e88d
Disable access tag deletion
underscope Dec 5, 2023
2132c7a
Update tag based access check
underscope Dec 5, 2023
885b9a3
💅
underscope Dec 6, 2023
1cf83ce
throw error in case of missing resource
underscope Dec 6, 2023
6239a00
Improve error handling
underscope Dec 6, 2023
c60573c
Revert change
underscope Dec 6, 2023
6be83a0
💄 docs
underscope Dec 6, 2023
ff99b45
Lock user management page access
underscope Dec 7, 2023
04773c5
Add tag CRUD
underscope Dec 8, 2023
70dced5
💄
underscope Dec 11, 2023
0d48d48
Update postman collection
underscope Dec 11, 2023
2baf054
💅
underscope Dec 11, 2023
7cfb7f9
Fix issue with missing unlink callback
underscope Dec 11, 2023
efba78a
Update access rights
underscope Dec 11, 2023
4653a73
Resolve codacy issue
underscope Dec 11, 2023
e9f122c
Resolve codacy issues
underscope Dec 11, 2023
c35b614
Fix issue due to lack of env parsing
underscope Dec 12, 2023
7adfcf0
Restrict tag type update
underscope Dec 13, 2023
438e2b1
Lint 💄
underscope Dec 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ REDIS_PORT=6379
REDIS_HOST=localhost
REDIS_PASSWORD=

# External repository access management
EXTERNAL_ACCESS_MANAGEMENT=false

# Cypress
CYPRESS_USERNAME=admin1@example.com
CYPRESS_PASSWORD=admin123.
12 changes: 8 additions & 4 deletions client/components/catalog/Card/Tags/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@
<div class="tags-container">
<div class="tag-list d-flex align-center">
<v-chip
v-for="{ id, name, truncatedName } in tags"
v-for="{ id, name, isAccessTag, truncatedName } in tags"
:key="id"
@click:close="showDeleteConfirmation(id, name)"
color="primary darken-1"
:close="!isAccessTag"
:color="isAccessTag ? 'secondary darken-1' : 'primary darken-1'"
close-label="Remove tag"
label close small
label small
class="mr-2 mb-1">
<v-tooltip
:disabled="name.length === truncatedName.length"
open-delay="100"
bottom>
<template #activator="{ on }">
<span v-on="on">{{ truncatedName }}</span>
<span v-on="on" class="d-flex">
<v-icon size="18" class="pr-2">mdi-account-supervisor</v-icon>
{{ truncatedName }}
</span>
</template>
<span>{{ name }}</span>
</v-tooltip>
Expand Down
4 changes: 4 additions & 0 deletions client/components/catalog/Container.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default {
name: 'catalog-container',
data: () => ({ loading: true }),
computed: {
isExternalAccessManagement: () => process.env.EXTERNAL_ACCESS_MANAGEMENT,
...mapState('repositories', {
sortBy: state => state.$internals.sort,
repositoryFilter: 'repositoryFilter',
Expand Down Expand Up @@ -153,6 +154,9 @@ export default {
}
},
created() {
if (this.isExternalAccessManagement) {
this.$router.go(-1);
}
// repositories must be reloaded for publishing badge to work properly
// reset state manually to trigger "infinite" event in all cases
this.resetPagination();
Expand Down
2 changes: 2 additions & 0 deletions client/components/common/Navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ export default {
...mapGetters('repository', ['repository']),
title: () => BRAND_CONFIG.TITLE,
logo: () => BRAND_CONFIG.LOGO_FULL,
isExternalAccessManagement: () => process.env.EXTERNAL_ACCESS_MANAGEMENT,
routes() {
const items = [
{ name: 'Catalog', to: { name: 'catalog' } },
{ name: 'Admin', to: { name: 'system-user-management' } }
];
if (this.isExternalAccessManagement) items.shift();
if (!this.isAdmin) items.pop();
if (this.repository) {
items.unshift({
Expand Down
15 changes: 12 additions & 3 deletions client/components/repository/Settings/Sidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,29 @@
export default {
name: 'repository-settings-sidebar',
computed: {
isExternalAccessManagement: () => process.env.EXTERNAL_ACCESS_MANAGEMENT,
routes() {
const { query } = this.$route;
return [
const entries = [
{ label: 'General', name: 'repository-info', icon: 'wrench' },
{ label: 'People', name: 'user-management', icon: 'account' }
].map(route => ({ ...route, query }));
if (this.isExternalAccessManagement) entries.pop();
return entries;
},
actions() {
return [
{ label: 'Clone', icon: 'content-copy', name: 'clone' },
const defaultEntries = [
{ label: 'Publish', icon: 'upload', name: 'publish' },
{ label: 'Export', icon: 'export', name: 'export' },
{ label: 'Delete', icon: 'delete', name: 'delete', color: 'error' }
];
const conditionalEntries = this.isExternalAccessManagement
? []
: [{ label: 'Clone', icon: 'content-copy', name: 'clone' }];
return [
...defaultEntries,
...conditionalEntries
];
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ import UserList from './UserList.vue';
export default {
name: 'repository-user-management',
computed: {
isExternalAccessManagement: () => process.env.EXTERNAL_ACCESS_MANAGEMENT,
roles: () => map(role.repository, value => ({ text: titleCase(value), value }))
},
created() {
if (this.isExternalAccessManagement) this.$router.go(-1);
},
components: {
AddUserDialog,
UserList
Expand Down
5 changes: 4 additions & 1 deletion client/components/system-settings/Sidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
export default {
name: 'system-settings-sidebar',
computed: {
isExternalAccessManagement: () => process.env.EXTERNAL_ACCESS_MANAGEMENT,
routes() {
return [
const items = [
{ label: 'System Users', name: 'system-user-management', icon: 'account' },
{ label: 'Structure Types', name: 'installed-schemas', icon: 'file-tree' },
{ label: 'Installed Elements', name: 'installed-elements', icon: 'puzzle' }
];
if (this.isExternalAccessManagement) items.shift();
return items;
}
}
};
Expand Down
4 changes: 4 additions & 0 deletions client/components/system-settings/UserManagement/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export default {
},
computed: {
...mapState({ user: state => state.auth.user }),
isExternalAccessManagement: () => process.env.EXTERNAL_ACCESS_MANAGEMENT,
headers,
defaultPage
},
Expand Down Expand Up @@ -166,6 +167,9 @@ export default {
this.fetch();
}
},
created() {
if (this.isExternalAccessManagement) this.$router.go(-1);
},
components: { UserDialog }
};
</script>
Expand Down
8 changes: 6 additions & 2 deletions config/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import * as store from './store.js';
import * as tce from './tce.js';
import isLocalhost from 'is-localhost';
import parse from 'url-parse';
import yn from 'yn';

const hostname = resolveHostname();
const protocol = resolveProtocol(hostname);
const port = resolvePort();
const origin = resolveOrigin(hostname, protocol, port);
const previewUrl = process.env.PREVIEW_URL;
const isExternalAccessManagement = yn(process.env.EXTERNAL_ACCESS_MANAGEMENT);

// Legacy config support
function resolveHostname() {
Expand Down Expand Up @@ -54,7 +56,8 @@ export {
previewUrl,
consumer,
store,
tce
tce,
isExternalAccessManagement
};

export default {
Expand All @@ -68,5 +71,6 @@ export default {
previewUrl,
consumer,
store,
tce
tce,
isExternalAccessManagement
};
2 changes: 1 addition & 1 deletion config/shared/role.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import values from 'lodash/values.js';

const role = {
user: { USER: 'USER', ADMIN: 'ADMIN' },
user: { USER: 'USER', ADMIN: 'ADMIN', INTEGRATION: 'INTEGRATION' },
repository: { ADMIN: 'ADMIN', AUTHOR: 'AUTHOR' }
};

Expand Down
35 changes: 25 additions & 10 deletions server/repository/index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
import { authorize, authorizeIntegration } from '../shared/auth/mw.js';
import { NOT_FOUND, UNAUTHORIZED } from 'http-status-codes';
import { authorize } from '../shared/auth/mw.js';
import { createError } from '../shared/error/helpers.js';
import ctrl from './repository.controller.js';
import db from '../shared/database/index.js';
import express from 'express';
import feed from './feed/index.js';
import { isExternalAccessManagement } from '../../config/server/index.js';
import multer from 'multer';
import path from 'node:path';
import processQuery from '../shared/util/processListQuery.js';
import proxy from './proxy.js';
import proxyMw from '../shared/storage/proxy/mw.js';
import roleConfig from '../../config/shared/role.js';
import storage from './storage.js';

/* eslint-disable */
import activity from '../activity/index.js';
import comment from '../comment/index.js';
import revision from '../revision/index.js';
import contentElement from '../content-element/index.js';
const { user: role } = roleConfig;
import storageRouter from '../shared/storage/storage.router.js';
/* eslint-enable */

const { Repository } = db;
const { Repository, Tag } = db;
const router = express.Router();
const { setSignedCookies } = proxyMw(storage, proxy);

const authorizeUser = isExternalAccessManagement
? authorizeIntegration
: authorize();

// NOTE: disk storage engine expects an object to be passed as the first argument
// https://github.com/expressjs/multer/blob/6b5fff5/storage/disk.js#L17-L18
const upload = multer({ storage: multer.diskStorage({}) });

router
.post('/import', authorize(), upload.single('archive'), ctrl.import);
.post('/import', authorizeUser, upload.single('archive'), ctrl.import);

router
.param('repositoryId', getRepository)
.use('/:repositoryId', hasAccess, setSignedCookies);

router.route('/')
.get(processQuery({ limit: 100 }), ctrl.index)
.post(authorize(), ctrl.create);
.post(authorizeUser, ctrl.create);

router.route('/:repositoryId')
.get(ctrl.get)
Expand All @@ -46,15 +53,15 @@ router.route('/:repositoryId')

router
.post('/:repositoryId/pin', ctrl.pin)
.post('/:repositoryId/clone', authorize(), ctrl.clone)
.post('/:repositoryId/clone', authorizeUser, ctrl.clone)
.post('/:repositoryId/publish', ctrl.publishRepoInfo)
.get('/:repositoryId/users', ctrl.getUsers)
.get('/:repositoryId/export/setup', ctrl.initiateExportJob)
.post('/:repositoryId/export/:jobId', ctrl.export)
.post('/:repositoryId/users', ctrl.upsertUser)
.delete('/:repositoryId/users/:userId', ctrl.removeUser)
.post('/:repositoryId/tags', ctrl.addTag)
.delete('/:repositoryId/tags/:tagId', ctrl.removeTag);
.post('/:repositoryId/users', authorizeUser, ctrl.upsertUser)
.delete('/:repositoryId/users/:userId', authorizeUser, ctrl.removeUser)
.post('/:repositoryId/tags', authorizeUser, ctrl.addTag)
.delete('/:repositoryId/tags/:tagId', authorizeUser, ctrl.removeTag);

mount(router, '/:repositoryId', feed);
mount(router, '/:repositoryId', activity);
Expand All @@ -68,7 +75,8 @@ function mount(router, mountPath, subrouter) {
}

function getRepository(req, _res, next, repositoryId) {
return Repository.findByPk(repositoryId, { paranoid: false })
return Repository
.findByPk(repositoryId, { include: [{ model: Tag }], paranoid: false })
.then(repository => repository || createError(NOT_FOUND, 'Repository not found'))
.then(repository => {
req.repository = repository;
Expand All @@ -79,6 +87,13 @@ function getRepository(req, _res, next, repositoryId) {
function hasAccess(req, _res, next) {
const { user, repository } = req;
if (user.isAdmin()) return next();
const repositoryTagIds = repository.tags
?.filter(it => it.isAccessTag)
.map(it => it.id);
if (repositoryTagIds.length && user.isAssociatedWithSomeTag(repositoryTagIds)) {
req.repositoryRole = role.ADMIN;
return next();
}
return repository.getUser(user)
.then(user => user || createError(UNAUTHORIZED, 'Access restricted'))
.then(user => {
Expand Down
26 changes: 19 additions & 7 deletions server/repository/repository.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from 'node:fs';
import * as fsp from 'node:fs/promises';
import { NO_CONTENT, NOT_FOUND } from 'http-status-codes';
import { FORBIDDEN, NO_CONTENT, NOT_FOUND } from 'http-status-codes';
import { repository as role, user as userRole } from '../../config/shared/role.js';
import { createError } from '../shared/error/helpers.js';
import db from '../shared/database/index.js';
import getVal from 'lodash/get.js';
Expand All @@ -9,7 +10,6 @@ import { Op } from 'sequelize';
import pick from 'lodash/pick.js';
import Promise from 'bluebird';
import publishingService from '../shared/publishing/publishing.service.js';
import { repository as role } from '../../config/shared/role.js';
import sample from 'lodash/sample.js';
import { schema } from '../../config/shared/tailor.loader.js';
import { snakeCase } from 'change-case';
Expand Down Expand Up @@ -171,15 +171,27 @@ function findOrCreateRole(repository, user, role) {
.then(() => user);
}

function addTag({ body: { name }, repository }, res) {
function addTag(
{
body: { name, isAccessTag = false },
user,
repository
},
res
) {
return sequelize.transaction(async transaction => {
const [tag] = await Tag.findOrCreate({ where: { name }, transaction });
const tag = await Tag.fetchOrCreate({ user, name, isAccessTag, transaction });
await repository.addTags([tag], { transaction });
return res.json({ data: tag });
});
}

async function removeTag({ params: { tagId, repositoryId } }, res) {
async function removeTag({ user, params: { tagId, repositoryId } }, res) {
const tag = await Tag.findByPk(tagId);
if (!tag) return createError(NOT_FOUND, 'Tag not found');
if (tag.isAccessTag && user.role !== userRole.INTEGRATION) {
return createError(FORBIDDEN, 'Can be removed only by integration users');
}
const where = { tagId, repositoryId };
await RepositoryTag.destroy({ where });
return res.status(NO_CONTENT).send();
Expand Down Expand Up @@ -222,8 +234,8 @@ function importRepository({ body, file, user }, res) {
return TransferService
.createImportJob(path, options)
.toPromise()
.finally(() => {
fs.unlink(path);
.finally(async () => {
await fsp.unlink(path);
res.end();
});
}
Expand Down
9 changes: 6 additions & 3 deletions server/shared/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const options = {
};

auth.use(new LocalStrategy(options, (email, password, done) => {
return User.unscoped().findOne({ where: { email } })
return User
.unscoped()
.findOne({ where: { email } })
.then(user => user && user.authenticate(password))
.then(user => done(null, user || false))
.error(err => done(err, false));
Expand All @@ -27,7 +29,8 @@ auth.use(new JwtStrategy({
audience: Audience.Scope.Access,
jwtFromRequest: ExtractJwt.fromExtractors([
extractJwtFromCookie,
ExtractJwt.fromBodyField('token')
ExtractJwt.fromBodyField('token'),
ExtractJwt.fromHeader('access_token')
]),
secretOrKey: config.jwt.secret
}, verifyJWT));
Expand All @@ -50,7 +53,7 @@ auth.deserializeUser((user, done) => done(null, user));
export default auth;

function verifyJWT(payload, done) {
return User.unscoped().findByPk(payload.id)
return User.unscoped().findByPk(payload.id, { include: ['userTags'] })
.then(user => done(null, user || false))
.error(err => done(err, false));
}
Expand Down
Loading