Files
threshold-ui/addons/api/mirage/config.js
T

557 lines
17 KiB
JavaScript
Raw Normal View History

2020-05-19 18:10:39 -04:00
import config from '../config/environment';
import { authHandler, deauthHandler } from './route-handlers/auth';
import { pickRandomStatusString } from './factories/session';
2020-09-22 11:03:30 -04:00
import { Response } from 'miragejs';
import initializeMockIPC from './scenarios/ipc';
import makeBooleanFilter from './helpers/bexpr-filter';
2020-05-19 18:10:39 -04:00
2021-10-23 12:55:23 -04:00
const isTesting = config.environment === 'test';
2021-03-04 15:47:38 -05:00
export default function () {
2021-11-05 11:52:12 -04:00
initializeMockIPC(this, config);
2020-05-19 18:10:39 -04:00
// make this `http://localhost:8080`, for example, if your API is on a different server
// this.urlPrefix = '';
// make this `/api`, for example, if your API is namespaced
this.namespace = config.api.namespace;
// delay for each request, automatically set to 0 during testing
this.timing = 1;
2020-05-19 18:10:39 -04:00
2020-06-04 16:53:37 -04:00
// Allow any configured API host to passthrough, which is useful in
// development for testing against a locally running backend.
if (config.api.host) this.passthrough(`${config.api.host}/**`);
this.passthrough();
// Scope resources
2021-03-04 15:47:38 -05:00
this.get(
'/scopes',
2021-12-13 13:34:28 -05:00
({ scopes }, { queryParams: { scope_id, recursive, filter } }) => {
let resultSet;
2021-03-04 15:47:38 -05:00
// Default parent scope is global
if (!scope_id) scope_id = 'global';
if (recursive && scope_id === 'global') {
2021-12-13 13:34:28 -05:00
resultSet = scopes.all();
2021-03-04 15:47:38 -05:00
} else {
2021-12-13 13:34:28 -05:00
resultSet = scopes.where((scope) => {
2021-03-04 15:47:38 -05:00
return scope.scope ? scope.scope.id === scope_id : false;
});
}
2021-12-13 13:34:28 -05:00
return resultSet.filter(makeBooleanFilter(filter));
}
2021-03-04 15:47:38 -05:00
);
this.post('/scopes', function ({ scopes }) {
2020-07-23 11:31:46 -04:00
// Parent scope comes through the payload via `scope_id`, but this needs
// to be expanded to a scope object `scope: {id:..., type:...}` before
// saving, which is the gist of this function.
const attrs = this.normalizedRequestAttrs();
const parentScopeAttrs = this.serialize(scopes.find(attrs.scopeId));
2020-07-23 11:31:46 -04:00
attrs.scope = parentScopeAttrs;
return scopes.create(attrs);
});
2020-07-24 19:12:20 -04:00
this.get('/scopes/:id');
this.patch('/scopes/:id');
this.del('/scopes/:id');
2020-06-04 16:53:37 -04:00
2020-06-18 15:51:40 -04:00
// Auth & IAM resources
2021-03-04 15:47:38 -05:00
this.get(
'/auth-methods',
({ authMethods }, { queryParams: { scope_id: scopeId, filter } }) => {
const resultSet = authMethods.where({ scopeId });
return resultSet.filter(makeBooleanFilter(filter));
2021-03-04 15:47:38 -05:00
}
);
2020-09-09 12:24:01 -04:00
this.post('/auth-methods');
this.get('/auth-methods/:id');
this.patch('/auth-methods/:id');
this.del('/auth-methods/:id', ({ authMethods }, { params: { id } }) => {
const authMethod = authMethods.find(id);
const scope = authMethod.scope;
authMethod.destroy();
// If the primary auth method is deleted, the associated scope's
// primary_auth_method_id field should be set to null.
2021-03-26 12:47:49 -04:00
if (scope.primaryAuthMethodId === id) {
scope.update({ primaryAuthMethodId: null });
}
2021-04-12 13:47:58 -04:00
return new Response(202);
});
2020-06-18 15:51:40 -04:00
// Auth Method Accounts
2021-03-04 15:47:38 -05:00
this.get(
'/accounts',
2021-11-12 11:22:16 -05:00
(
{ accounts },
{ queryParams: { auth_method_id: authMethodId, filter } }
) => {
let resultSet;
if (authMethodId) {
resultSet = accounts.where({ authMethodId });
} else {
resultSet = accounts.all();
}
return resultSet.filter(makeBooleanFilter(filter));
2021-03-04 15:47:38 -05:00
}
);
this.post('/accounts');
this.get('/accounts/:id');
this.patch('/accounts/:id');
this.del('/accounts/:id');
2021-03-04 15:47:38 -05:00
this.post(
'/accounts/:idMethod',
function ({ accounts }, { params: { idMethod } }) {
const attrs = this.normalizedRequestAttrs();
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];
const account = accounts.find(id);
const updatedAttrs = {
version: attrs.version,
attributes: account.attributes,
};
// Set new password
if (method === 'set-password') {
updatedAttrs.attributes.password = attrs.password;
}
// Set new password
if (method === 'change-password') {
updatedAttrs.attributes.password = attrs.new_password;
}
return account.update(updatedAttrs);
2020-09-17 10:24:22 -04:00
}
2021-03-04 15:47:38 -05:00
);
// Authenticate + auth methods custom routes
this.post('/auth-methods/:id_method', authHandler);
// Deauthenticate route
2020-07-24 17:49:42 -04:00
this.post('/scopes/:id_method', deauthHandler);
2020-06-23 15:06:30 -04:00
// IAM : Users
2020-09-09 16:53:41 -04:00
this.get('/users', ({ users }, { queryParams: { scope_id: scopeId } }) => {
return users.where({ scopeId });
});
this.post('/users');
this.get('/users/:id');
this.patch('/users/:id');
this.del('/users/:id');
2020-09-30 10:45:39 -04:00
this.post('/users/:idMethod', function ({ users }, { params: { idMethod } }) {
const attrs = this.normalizedRequestAttrs();
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];
const user = users.find(id);
const updatedAttrs = {
version: attrs.version,
2021-03-04 15:47:38 -05:00
accountIds: user.accountIds,
2020-09-30 10:45:39 -04:00
};
// If adding accounts, push them into the array
if (method === 'add-accounts') {
2021-03-04 15:47:38 -05:00
attrs.accountIds.forEach((id) => {
2020-09-30 10:45:39 -04:00
if (!updatedAttrs.accountIds.includes(id)) {
updatedAttrs.accountIds.push(id);
}
});
}
// If deleting accounts, filter them out of the array
if (method === 'remove-accounts') {
2021-03-04 15:47:38 -05:00
updatedAttrs.accountIds = updatedAttrs.accountIds.filter((id) => {
2020-09-30 10:45:39 -04:00
return !attrs.accountIds.includes(id);
});
}
return user.update(updatedAttrs);
});
2020-09-09 16:53:41 -04:00
// IAM: Groups
this.get('/groups', ({ groups }, { queryParams: { scope_id: scopeId } }) => {
return groups.where({ scopeId });
});
this.post('/groups');
this.get('/groups/:id');
this.patch('/groups/:id');
this.del('/groups/:id');
2021-03-04 15:47:38 -05:00
this.post(
'/groups/:idMethod',
function ({ groups }, { params: { idMethod } }) {
const attrs = this.normalizedRequestAttrs();
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];
const group = groups.find(id);
const updatedAttrs = {
version: attrs.version,
memberIds: group.memberIds,
};
// If adding members, push them into the array
if (method === 'add-members') {
attrs.memberIds.forEach((id) => {
if (!updatedAttrs.memberIds.includes(id)) {
updatedAttrs.memberIds.push(id);
}
});
}
// If deleting members, filter them out of the array
if (method === 'remove-members') {
updatedAttrs.memberIds = updatedAttrs.memberIds.filter((id) => {
return !attrs.memberIds.includes(id);
});
}
return group.update(updatedAttrs);
2020-09-09 16:53:41 -04:00
}
2021-03-04 15:47:38 -05:00
);
// IAM: Roles
2020-09-10 11:03:44 -04:00
this.get('/roles', ({ roles }, { queryParams: { scope_id: scopeId } }) => {
return roles.where({ scopeId });
});
this.post('/roles');
this.get('/roles/:id');
this.patch('/roles/:id');
this.del('/roles/:id');
2021-03-04 15:47:38 -05:00
this.post(
'/roles/:idMethod',
function ({ roles, users, groups }, { params: { idMethod } }) {
const attrs = this.normalizedRequestAttrs();
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];
const role = roles.find(id);
let updatedAttrs = {};
// Principals is a combined list of users and groups, but in Mirage we've
// internally modelled them as separate lists. Therefore we must check
// the type by looking up the user or group first, then determine which
// list to add the principal to.
if (method === 'add-principals') {
updatedAttrs = {
version: attrs.version,
userIds: role.userIds,
groupIds: role.groupIds,
};
attrs.principalIds.forEach((id) => {
const isUser = users.find(id);
const isGroup = groups.find(id);
if (isUser && !updatedAttrs.userIds.includes(id)) {
updatedAttrs.userIds.push(id);
}
if (isGroup && !updatedAttrs.groupIds.includes(id)) {
updatedAttrs.groupIds.push(id);
}
});
}
// If deleting principals, filter them out of both users and groups,
// and in this case don't care about the type
if (method === 'remove-principals') {
updatedAttrs = {
version: attrs.version,
userIds: role.userIds,
groupIds: role.groupIds,
};
updatedAttrs.userIds = updatedAttrs.userIds.filter((id) => {
return !attrs.principalIds.includes(id);
});
updatedAttrs.groupIds = updatedAttrs.groupIds.filter((id) => {
return !attrs.principalIds.includes(id);
});
}
2021-03-04 15:47:38 -05:00
if (method === 'set-grants') {
updatedAttrs = {
version: attrs.version,
grantStrings: attrs.grantStrings,
};
}
2020-09-12 12:03:52 -04:00
2021-03-04 15:47:38 -05:00
return role.update(updatedAttrs);
2020-09-12 12:03:52 -04:00
}
2021-03-04 15:47:38 -05:00
);
2020-06-22 21:39:38 -04:00
// Other resources
2020-06-04 16:53:37 -04:00
// host-catalog
2021-03-04 15:47:38 -05:00
this.get(
'/host-catalogs',
({ hostCatalogs }, { queryParams: { scope_id: scopeId } }) => {
return hostCatalogs.where({ scopeId });
}
);
this.post('/host-catalogs');
this.get('/host-catalogs/:id');
this.patch('/host-catalogs/:id');
this.del('/host-catalogs/:id');
2020-06-17 13:20:57 -04:00
2020-08-14 15:16:35 -04:00
// host
2021-03-04 15:47:38 -05:00
this.get(
'/hosts',
function ({ hosts }, { queryParams: { host_catalog_id: hostCatalogId } }) {
return hosts.where({ hostCatalogId });
}
);
2020-09-10 17:59:31 -04:00
this.post('/hosts', function ({ hostCatalogs, hosts }) {
const attrs = this.normalizedRequestAttrs();
2020-09-10 17:59:31 -04:00
const hostCatalog = hostCatalogs.find(attrs.hostCatalogId);
attrs.scopeId = hostCatalog.scope.id;
return hosts.create(attrs);
});
2020-09-10 17:59:31 -04:00
this.get('/hosts/:id');
this.patch('/hosts/:id');
this.del('/hosts/:id');
2020-08-14 15:16:35 -04:00
2020-08-14 10:46:17 -04:00
// host-set
2021-03-04 15:47:38 -05:00
this.get(
'/host-sets',
function (
{ hostSets },
{ queryParams: { host_catalog_id: hostCatalogId } }
) {
return hostSets.where({ hostCatalogId });
}
);
2020-09-10 17:59:31 -04:00
this.post('/host-sets', function ({ hostCatalogs, hostSets }) {
const attrs = this.normalizedRequestAttrs();
const hostCatalog = hostCatalogs.find(attrs.hostCatalogId);
attrs.scopeId = hostCatalog.scope.id;
return hostSets.create(attrs);
});
2020-09-10 16:57:11 -04:00
this.get('/host-sets/:id');
this.patch('/host-sets/:id');
this.del('/host-sets/:id');
2021-03-04 15:47:38 -05:00
this.post(
'/host-sets/:idMethod',
function ({ hostSets }, { params: { idMethod } }) {
const attrs = this.normalizedRequestAttrs();
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];
const hostSet = hostSets.find(id);
const updatedAttrs = {
version: attrs.version,
hostIds: hostSet.hostIds,
};
2020-09-10 12:59:20 -04:00
2021-03-04 15:47:38 -05:00
// If adding hosts, push them into the array
if (method === 'add-hosts') {
attrs.hostIds.forEach((id) => {
if (!updatedAttrs.hostIds.includes(id)) {
updatedAttrs.hostIds.push(id);
}
});
}
2020-09-10 12:59:20 -04:00
2021-03-04 15:47:38 -05:00
// If deleting hosts, filter them out of the array
if (method === 'remove-hosts') {
updatedAttrs.hostIds = updatedAttrs.hostIds.filter((id) => {
return !attrs.hostIds.includes(id);
});
}
2020-09-10 12:59:20 -04:00
2021-03-04 15:47:38 -05:00
return hostSet.update(updatedAttrs);
}
);
2020-08-14 10:46:17 -04:00
2020-08-21 11:53:53 -04:00
// target
2021-03-04 15:47:38 -05:00
this.get(
'/targets',
function ({ targets }, { queryParams: { scope_id, recursive, filter } }) {
let resultSet;
2021-03-04 15:47:38 -05:00
if (recursive && scope_id === 'global') {
resultSet = targets.all();
2021-03-04 15:47:38 -05:00
} else if (recursive) {
resultSet = targets.where((target) => {
2021-03-04 15:47:38 -05:00
const targetModel = targets.find(target.id);
return (
target.scopeId === scope_id ||
targetModel?.scope?.scope?.id === scope_id
);
});
} else {
2021-12-16 16:56:35 -05:00
resultSet = targets.where((target) => target.scopeId === scope_id);
2021-03-04 15:47:38 -05:00
}
return resultSet.filter(makeBooleanFilter(filter));
}
2021-03-04 15:47:38 -05:00
);
2020-09-04 08:41:28 -04:00
this.post('/targets', function ({ targets }) {
2020-09-03 14:35:05 -04:00
const attrs = this.normalizedRequestAttrs();
return targets.create(attrs);
});
this.get('/targets/:id');
this.patch('/targets/:id');
this.del('/targets/:id');
2021-03-04 15:47:38 -05:00
this.post(
'/targets/:idMethod',
function ({ targets }, { params: { idMethod } }) {
const attrs = this.normalizedRequestAttrs();
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];
const target = targets.find(id);
const updatedAttrs = {
version: attrs.version,
};
2021-09-23 10:04:23 -04:00
// If adding host sources, push them into the array
2021-09-22 18:52:38 -04:00
if (method === 'add-host-sources') {
updatedAttrs.hostSetIds = target.hostSetIds;
2021-09-22 19:15:09 -04:00
attrs.hostSourceIds.forEach((id) => {
2021-03-04 15:47:38 -05:00
if (!updatedAttrs.hostSetIds.includes(id)) {
updatedAttrs.hostSetIds.push(id);
}
});
}
2021-09-23 10:04:23 -04:00
// If deleting host sources, filter them out of the array
2021-09-22 18:52:38 -04:00
if (method === 'remove-host-sources') {
updatedAttrs.hostSetIds = target.hostSetIds;
2021-03-04 15:47:38 -05:00
updatedAttrs.hostSetIds = updatedAttrs.hostSetIds.filter((id) => {
2021-09-22 19:15:09 -04:00
return !attrs.hostSourceIds.includes(id);
2021-03-04 15:47:38 -05:00
});
}
2021-09-23 10:04:23 -04:00
// If adding credential sources, push them into the array
2021-09-22 18:34:35 -04:00
if (method === 'add-credential-sources') {
updatedAttrs.credentialLibraryIds = target.credentialLibraryIds;
attrs.applicationCredentialSourceIds.forEach((id) => {
if (!updatedAttrs.credentialLibraryIds.includes(id)) {
updatedAttrs.credentialLibraryIds.push(id);
}
});
}
2021-09-23 10:04:23 -04:00
// If deleting credential sources, filter them out of the array
2021-09-22 18:34:35 -04:00
if (method === 'remove-credential-sources') {
updatedAttrs.credentialLibraryIds = target.credentialLibraryIds;
updatedAttrs.credentialLibraryIds =
updatedAttrs.credentialLibraryIds.filter((id) => {
return !attrs.applicationCredentialSourceIds.includes(id);
});
}
2021-03-04 15:47:38 -05:00
return target.update(updatedAttrs);
}
2021-03-04 15:47:38 -05:00
);
2020-09-18 12:54:31 -04:00
// session
2021-03-04 15:47:38 -05:00
this.get(
'/sessions',
function ({ sessions }, { queryParams: { scope_id, recursive, filter } }) {
let resultSet;
2021-03-04 15:47:38 -05:00
// To simulate changes to `session.status` that may occur in the backend,
// we quietly randomize the value of the field on GET.
// To populate sessions for logged in user,
// update alternate sessions to auth user.
// But only if not in testing mode.
// In tests, we need deterministic statuses.
2021-10-23 12:55:23 -04:00
if (!isTesting) {
2021-03-04 15:47:38 -05:00
sessions.all().models.forEach((session) => {
session.update({
2021-03-04 15:47:38 -05:00
status: pickRandomStatusString(),
});
2021-03-04 15:47:38 -05:00
if (session.id.split('-').pop() % 2)
session.update({
userId: 'authenticateduser',
});
});
2021-03-04 15:47:38 -05:00
}
if (recursive && scope_id === 'global') {
resultSet = sessions.all();
2021-03-04 15:47:38 -05:00
} else if (recursive) {
resultSet = sessions.where((session) => {
2021-03-04 15:47:38 -05:00
const sessionModel = sessions.find(session.id);
return (
session.scopeId === scope_id ||
sessionModel?.scope?.scope?.id === scope_id
);
});
} else {
resultSet = sessions.where((session) => session.scopeId === scope_id);
2021-03-04 15:47:38 -05:00
}
return resultSet.filter(makeBooleanFilter(filter));
2020-10-06 11:07:29 -04:00
}
2021-03-04 15:47:38 -05:00
);
this.get('/sessions/:id', function ({ sessions }, { params: { id } }) {
const session = sessions.find(id);
2021-10-23 12:55:23 -04:00
if (!isTesting) {
session.update({
status: pickRandomStatusString(),
});
}
return session;
2020-09-18 12:54:31 -04:00
});
2021-03-04 15:47:38 -05:00
this.post(
'/sessions/:idMethod',
function ({ sessions }, { params: { idMethod } }) {
const attrs = this.normalizedRequestAttrs();
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];
const session = sessions.find(id);
const updatedAttrs = {
version: attrs.version,
};
if (method === 'cancel') {
updatedAttrs.status = 'canceling';
}
return session.update(updatedAttrs);
2020-09-18 15:38:32 -04:00
}
2021-03-04 15:47:38 -05:00
);
2020-09-18 15:38:32 -04:00
2020-09-22 11:03:30 -04:00
// auth-tokens
this.get('/auth-tokens/:id', function () {
// Set to 401 or 404 to simulate token invalidation, which will
// cause the session to fail restoration and logout the user.
return new Response(200);
});
// credential-stores
this.get(
'/credential-stores',
({ credentialStores }, { queryParams: { scope_id: scopeId } }) =>
credentialStores.where({ scopeId })
);
2021-07-14 07:59:15 -07:00
this.get('/credential-stores/:id');
2021-07-16 10:41:04 -07:00
this.post('/credential-stores');
2021-07-14 07:59:15 -07:00
this.patch('/credential-stores/:id');
2021-07-20 14:55:00 -07:00
this.del('/credential-stores/:id');
2021-07-14 07:59:15 -07:00
2021-07-12 16:03:06 -04:00
// credential-libraries
this.get(
'/credential-libraries',
(
{ credentialLibraries },
{ queryParams: { credential_store_id: credentialStoreId } }
) => credentialLibraries.where({ credentialStoreId })
);
this.get('/credential-libraries/:id');
2021-07-18 14:31:45 -04:00
this.post(
'/credential-libraries',
function ({ credentialStores, credentialLibraries }) {
const attrs = this.normalizedRequestAttrs();
const credentialStore = credentialStores.find(attrs.credentialStoreId);
attrs.scopeId = credentialStore.scope.id;
return credentialLibraries.create(attrs);
}
);
2021-07-12 16:03:06 -04:00
this.del('/credential-libraries/:id');
this.patch('/credential-libraries/:id');
2021-10-29 13:23:41 -07:00
// managed-groups
this.get(
'/managed-groups',
({ managedGroups }, { queryParams: { auth_method_id: authMethodId } }) => {
return managedGroups.where({ authMethodId });
}
);
2022-01-14 16:56:59 -05:00
this.post('/managed-groups');
2021-11-09 10:29:49 -08:00
this.get('/managed-groups/:id');
2022-01-14 16:56:59 -05:00
this.patch('/managed-groups/:id');
2021-11-09 12:01:30 -08:00
this.del('/managed-groups/:id');
2021-10-29 13:23:41 -07:00
2020-09-14 10:31:43 -04:00
/* Uncomment the following line and the Response import above
* Then change the response code to simulate error responses.
* this.get('/scopes', () => new Response(505));
*
* Update error payload to simulate specific error responses.
*/
// this.get('/host-catalogs', () => new Response(404));
// this.get('/scopes/:id', () => new Response(505, {}, {
// errors: [{
// status: 505,
// message: 'HTTP version not supported.',
// }]
// }));
2020-05-19 18:10:39 -04:00
}