Icu 5578 - introduce injected application credential on the api (#1269)

* chore: 🤖 include injected cred sources to the target model
This commit is contained in:
Dharini Jeeva authored and GitHub committed 2022-09-01 10:07:36 -07:00
1 parent bd0854b0f6
commit 2614b311a1
11 files changed
+395 -171

No files matched your search

+108 -10
View File
@@ -20,7 +20,7 @@ export default class TargetModel extends GeneratedTargetModel {
host_sources;
/**
* Credential source ids are read only and can be
* Brokered Credential source ids are read only and can be
* persisted via a dedicated call to `addBrokeredCredentialSources()`.
*/
@attr('string-array', {
@@ -29,6 +29,16 @@ export default class TargetModel extends GeneratedTargetModel {
})
brokered_credential_source_ids;
/**
* Injected Application Credential source ids are read only and can be
* persisted via a dedicated call to `addInjectedApplicationCredentialSources()`.
*/
@attr('string-array', {
readOnly: true,
emptyArrayIfMissing: true,
})
injected_application_credential_source_ids;
/**
* An array of resolved host set and host catalog instances. Model instances
* must already be loaded into the store (this method will not load unloaded
@@ -62,6 +72,24 @@ export default class TargetModel extends GeneratedTargetModel {
.filter(Boolean);
}
/**
* An array of resolved credential library and credential instances. Model instances
* must already be loaded into the store (this method will not load unloaded
* instances). Unresolvable instances are excluded from the array.
* @type {[CredentialLibraryModel, CredentialModel]}
*/
get injectedApplicationCredentialSources() {
return this.injected_application_credential_source_ids
.map((source) => {
if (source.value.startsWith('cred')) {
return this.store.peekRecord('credential', source.value);
} else {
return this.store.peekRecord('credential-library', source.value);
}
})
.filter(Boolean);
}
/**
* Sessions associated with this target (but only already loaded sessions).
* @type {SessionModel[]}
@@ -152,18 +180,44 @@ export default class TargetModel extends GeneratedTargetModel {
/**
* Adds credential sources via the `add-credential-sources` method.
* See serializer and adapter for more information.
* @param {[string]} credentialSourceIDs
* @param {[string]} brokeredCredentialSourceIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
addBrokeredCredentialSources(
credentialSourceIDs,
brokeredCredentialSourceIDs,
options = { adapterOptions: {} }
) {
const defaultAdapterOptions = {
method: 'add-credential-sources',
credentialSourceIDs,
brokeredCredentialSourceIDs,
};
// There is no "deep merge" in ES.
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
...options.adapterOptions,
},
});
}
/**
* Adds credential sources via the `add-credential-sources` method.
* See serializer and adapter for more information.
* @param {[string]} injectedApplicationCredentialSourceIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
addInjectedApplicationCredentialSources(
injectedApplicationCredentialSourceIDs,
options = { adapterOptions: {} }
) {
const defaultAdapterOptions = {
method: 'add-credential-sources',
injectedApplicationCredentialSourceIDs,
};
// There is no "deep merge" in ES.
return this.save({
@@ -178,18 +232,18 @@ export default class TargetModel extends GeneratedTargetModel {
/**
* Delete credential libraries and credentials via the `remove-credential-sources` method.
* See serializer and adapter for more information.
* @param {[string]} credentialSourceIDs
* @param {[string]} brokeredCredentialSourceIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
removeBrokeredCredentialSources(
credentialSourceIDs,
brokeredCredentialSourceIDs,
options = { adapterOptions: {} }
) {
const defaultAdapterOptions = {
method: 'remove-credential-sources',
credentialSourceIDs,
brokeredCredentialSourceIDs,
};
// There is no "deep merge" in ES.
return this.save({
@@ -203,14 +257,58 @@ export default class TargetModel extends GeneratedTargetModel {
/**
* Delete a single credential library/credential set via the `remove-credential-sources` method.
* @param {number} credentialSourceID
* @param {number} brokeredCredentialSourceID
* @param {object} options
* @return {Promise}
*/
removeBrokeredCredentialSource(credentialSourceID, options) {
return this.removeBrokeredCredentialSources([credentialSourceID], options);
removeBrokeredCredentialSource(brokeredCredentialSourceID, options) {
return this.removeBrokeredCredentialSources(
[brokeredCredentialSourceID],
options
);
}
/**
* Delete credential libraries and credentials via the `remove-credential-sources` method.
* See serializer and adapter for more information.
* @param {[string]} injectedApplicationCredentialSourceIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
removeInjectedApplicationCredentialSources(
injectedApplicationCredentialSourceIDs,
options = { adapterOptions: {} }
) {
const defaultAdapterOptions = {
method: 'remove-credential-sources',
injectedApplicationCredentialSourceIDs,
};
// There is no "deep merge" in ES.
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
...options.adapterOptions,
},
});
}
/**
* Delete a single credential library/credential set via the `remove-credential-sources` method.
* @param {number} injectedApplicationCredentialSourceID
* @param {object} options
* @return {Promise}
*/
removeInjectedApplicationCredentialSource(
injectedApplicationCredentialSourceID,
options
) {
return this.removeInjectedApplicationCredentialSources(
[injectedApplicationCredentialSourceID],
options
);
}
/**
* True if the target type is tcp.
* @type {boolean}
+36 -7
View File
@@ -21,11 +21,20 @@ export default class TargetSerializer extends ApplicationSerializer {
if (hostSourceIDs) {
serialized = this.serializeWithHostSources(snapshot, hostSourceIDs);
}
const credentialSourceIDs = snapshot?.adapterOptions?.credentialSourceIDs;
if (credentialSourceIDs)
serialized = this.serializeWithCredentialSources(
const brokeredCredentialSourceIDs =
snapshot?.adapterOptions?.brokeredCredentialSourceIDs;
const injectedApplicationCredentialSourceIDs =
snapshot?.adapterOptions?.injectedApplicationCredentialSourceIDs;
if (brokeredCredentialSourceIDs)
serialized = this.serializeWithBrokeredCredentialSources(
snapshot,
credentialSourceIDs
brokeredCredentialSourceIDs
);
if (injectedApplicationCredentialSourceIDs)
serialized = this.serializeWithInjectedApplicationCredentialSources(
snapshot,
injectedApplicationCredentialSourceIDs
);
return serialized;
}
@@ -48,13 +57,33 @@ export default class TargetSerializer extends ApplicationSerializer {
* Returns a payload containing only version and an array of passed IDs,
* rather than existing instances on the model.
* @param {Snapshot} snapshot
* @param {[string]} credentialSourceIDs
* @param {[string]} brokered_credential_source_ids
* @return {object}
*/
serializeWithCredentialSources(snapshot, credentialSourceIDs) {
serializeWithBrokeredCredentialSources(
snapshot,
brokered_credential_source_ids
) {
return {
version: snapshot.attr('version'),
brokered_credential_source_ids: credentialSourceIDs,
brokered_credential_source_ids,
};
}
/**
* Returns a payload containing only version and an array of passed IDs,
* rather than existing instances on the model.
* @param {Snapshot} snapshot
* @param {[string]} injected_application_credential_source_ids
* @return {object}
*/
serializeWithInjectedApplicationCredentialSources(
snapshot,
injected_application_credential_source_ids
) {
return {
version: snapshot.attr('version'),
injected_application_credential_source_ids,
};
}
}
+2 -58
View File
@@ -1,5 +1,6 @@
import config from '../config/environment';
import { authHandler, deauthHandler } from './route-handlers/auth';
import { targetHandler } from './route-handlers/target';
import { pickRandomStatusString } from './factories/session';
import { Response } from 'miragejs';
import initializeMockIPC from './scenarios/ipc';
@@ -435,64 +436,7 @@ export default function () {
this.get('/targets/:id');
this.patch('/targets/:id');
this.del('/targets/:id');
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,
};
// If adding host sources, push them into the array
if (method === 'add-host-sources') {
updatedAttrs.hostSetIds = target.hostSetIds;
attrs.hostSourceIds.forEach((id) => {
if (!updatedAttrs.hostSetIds.includes(id)) {
updatedAttrs.hostSetIds.push(id);
}
});
}
// If deleting host sources, filter them out of the array
if (method === 'remove-host-sources') {
updatedAttrs.hostSetIds = target.hostSetIds;
updatedAttrs.hostSetIds = updatedAttrs.hostSetIds.filter((id) => {
return !attrs.hostSourceIds.includes(id);
});
}
// If adding brokered credential sources, push them into the array
if (method === 'add-credential-sources') {
updatedAttrs.credentialLibraryIds = target.credentialLibraryIds;
updatedAttrs.credentialIds = target.credentialIds;
attrs.brokeredCredentialSourceIds.forEach((id) => {
if (
!updatedAttrs.credentialLibraryIds.includes(id) ||
!updatedAttrs.credentialIds.includes(id)
) {
if (id.startsWith('cred')) {
updatedAttrs.credentialIds.push(id);
} else {
updatedAttrs.credentialLibraryIds.push(id);
}
}
});
}
// If deleting brokered credential sources, filter them out of the array
if (method === 'remove-credential-sources') {
updatedAttrs.credentialLibraryIds = target.credentialLibraryIds;
updatedAttrs.credentialIds = target.credentialIds;
updatedAttrs.credentialLibraryIds =
updatedAttrs.credentialLibraryIds.filter((id) => {
return !attrs.brokeredCredentialSourceIds.includes(id);
});
updatedAttrs.credentialIds = updatedAttrs.credentialIds.filter((id) => {
return !attrs.brokeredCredentialSourceIds.includes(id);
});
}
return target.update(updatedAttrs);
}
);
this.post('/targets/:idMethod', targetHandler);
// session
+12 -4
View File
@@ -65,14 +65,22 @@ export default factory.extend({
.where(
(credentialLibrary) => credentialLibrary.scopeId === target.scope.id
)
.models.filter(() => randomBoolean());
.models.filter(() => randomBoolean())
.map((cred) => cred.id);
randomlySelectedCredentials = server.schema.credentials
.where((credential) => credential.scopeId === target.scope.id)
.models.filter(() => randomBoolean());
.models.filter(() => randomBoolean())
.map((cred) => cred.id);
target.update({
hostSets: randomlySelectedHostSets,
credentialLibraries: randomlySelectedCredentialLibraries,
credentials: randomlySelectedCredentials,
brokeredCredentialSourceIds: [
...randomlySelectedCredentialLibraries,
...randomlySelectedCredentials,
],
injectedApplicationCredentialSourceIds: [
...randomlySelectedCredentialLibraries,
...randomlySelectedCredentials,
],
});
},
}),
-2
View File
@@ -4,6 +4,4 @@ import { belongsTo, hasMany } from 'ember-cli-mirage';
export default Model.extend({
scope: belongsTo(),
hostSets: hasMany(),
credentialLibraries: hasMany(),
credentials: hasMany(),
});
@@ -0,0 +1,99 @@
export function targetHandler({ targets }, { params: { idMethod } }) {
const attrs = this.normalizedRequestAttrs();
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];
const target = targets.find(id);
const {
brokeredCredentialSourceIds: originalBrokeredCredentials,
injectedApplicationCredentialSourceIds: originalInjectedCredentials,
} = target;
const selectedBrokeredCredentials = attrs.brokeredCredentialSourceIds;
const selectedInjectedCredentials =
attrs.injectedApplicationCredentialSourceIds;
const selectedHostSourceIds = attrs.hostSourceIds;
const originHostSetIds = target.hostSetIds;
const updatedAttrs = {
version: attrs.version,
};
const addToSourcesList = (originalSourceIds, selectedSourceIds) => {
const sourceIds = new Set([...originalSourceIds, ...selectedSourceIds]);
return [...sourceIds];
};
const removeFromSourcesList = (originalSourceIds, selectedSourceIds) => {
const sourceIds = new Set(originalSourceIds);
for (const elem of selectedSourceIds) {
sourceIds.delete(elem);
}
return [...sourceIds];
};
// If adding host sources, push them into the array
if (method === 'add-host-sources') {
if (selectedHostSourceIds) {
const listOfHostSources = addToSourcesList(
originHostSetIds,
selectedHostSourceIds
);
if (listOfHostSources?.length) {
updatedAttrs.hostSetIds = listOfHostSources;
}
}
}
// If deleting host sources, filter them out of the array
if (method === 'remove-host-sources') {
if (selectedHostSourceIds) {
const removedHostSources = removeFromSourcesList(
originHostSetIds,
selectedHostSourceIds
);
updatedAttrs.hostSetIds = removedHostSources;
}
}
// If adding brokered or injected application cred sources, push them into the array
if (method === 'add-credential-sources') {
if (selectedBrokeredCredentials) {
const listOfBrokeredCredentialSources = addToSourcesList(
originalBrokeredCredentials,
selectedBrokeredCredentials
);
if (listOfBrokeredCredentialSources?.length) {
updatedAttrs.brokeredCredentialSourceIds =
listOfBrokeredCredentialSources;
}
}
if (selectedInjectedCredentials) {
const listOfInjectedCredentialSources = addToSourcesList(
originalInjectedCredentials,
selectedInjectedCredentials
);
if (listOfInjectedCredentialSources?.length) {
updatedAttrs.injectedApplicationCredentialSourceIds =
listOfInjectedCredentialSources;
}
}
}
// If deleting brokered or injected application cred sources, filter them out of the array
if (method === 'remove-credential-sources') {
if (selectedBrokeredCredentials) {
const removedCredentialSources = removeFromSourcesList(
originalBrokeredCredentials,
selectedBrokeredCredentials
);
updatedAttrs.brokeredCredentialSourceIds = removedCredentialSources;
}
if (selectedInjectedCredentials) {
const removedCredentialSources = removeFromSourcesList(
originalInjectedCredentials,
selectedInjectedCredentials
);
updatedAttrs.injectedApplicationCredentialSourceIds =
removedCredentialSources;
}
}
return target.update(updatedAttrs);
}
-6
View File
@@ -23,12 +23,6 @@ export default ApplicationSerializer.extend({
return { id: host_set_id, host_catalog_id };
});
}
if (model.credentialLibraryIds?.length || model.credentialIds?.length) {
json.brokered_credential_source_ids = [
...model.credentialLibraryIds,
...model.credentialIds,
];
}
return json;
},
});
@@ -215,6 +215,7 @@ module('Unit | Serializer | application', function (hooks) {
scope: { id: 'o_123', scope_id: 'o_123' },
host_sources: [],
brokered_credential_source_ids: [],
injected_application_credential_source_ids: [],
},
relationships: {},
},
@@ -16,6 +16,10 @@ module('Unit | Serializer | target', function (hooks) {
{ host_source_id: '3', host_catalog_id: '4' },
],
brokered_credential_source_ids: [{ value: '1' }, { value: '2' }],
injected_application_credential_source_ids: [
{ value: '4' },
{ value: '5' },
],
scope: {
scope_id: 'org_1',
type: 'org',
@@ -66,7 +70,7 @@ module('Unit | Serializer | target', function (hooks) {
});
});
test('it serializes only credential sources and version when an `adapterOptions.credentialSourceIDs` array is passed', function (assert) {
test('it serializes only credential sources and version when an `adapterOptions.brokeredCredentialSourceIDs` array is passed', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('target');
@@ -78,15 +82,40 @@ module('Unit | Serializer | target', function (hooks) {
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {
credentialSourceIDs: ['4', '5'],
brokeredCredentialSourceIDs: ['4', '5'],
};
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
brokered_credential_source_ids: ['4', '5'],
version: 1,
});
});
test('it serializes only credential sources and version when an `adapterOptions.injectedApplicationCredentialSourceIDs` array is passed', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('target');
const record = store.createRecord('target', {
name: 'User',
description: 'Description',
injected_application_credential_source_ids: [
{ value: '1' },
{ value: '2' },
],
version: 1,
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {
injectedApplicationCredentialSourceIDs: ['4', '5'],
};
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
injected_application_credential_source_ids: ['4', '5'],
version: 1,
});
});
test('it serializes the worker_filter attribute if present', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
@@ -132,6 +161,7 @@ module('Unit | Serializer | target', function (hooks) {
{ host_source_id: '3', host_catalog_id: '4' },
],
brokered_credential_source_ids: ['1'],
injected_application_credential_source_ids: ['3'],
};
const normalized = serializer.normalizeSingleResponse(
store,
@@ -151,6 +181,7 @@ module('Unit | Serializer | target', function (hooks) {
{ host_source_id: '3', host_catalog_id: '4' },
],
brokered_credential_source_ids: [{ value: '1' }],
injected_application_credential_source_ids: [{ value: '3' }],
},
relationships: {},
},
@@ -181,6 +212,7 @@ module('Unit | Serializer | target', function (hooks) {
type: 'target',
attributes: {
brokered_credential_source_ids: [],
injected_application_credential_source_ids: [],
authorized_actions: [],
host_sources: [],
name: 'Target 1',
@@ -217,6 +249,7 @@ module('Unit | Serializer | target', function (hooks) {
scope: { id: 'o_123', scope_id: 'o_123' },
host_sources: [],
brokered_credential_source_ids: [],
injected_application_credential_source_ids: [],
},
relationships: {},
},
@@ -248,6 +281,7 @@ module('Unit | Serializer | target', function (hooks) {
name: 'Target 1',
host_sources: [],
brokered_credential_source_ids: [],
injected_application_credential_source_ids: [],
worker_filter: 'worker',
},
relationships: {},