From 8a37147b9815900b2aa057c5f2d395f053a8918a Mon Sep 17 00:00:00 2001 From: Sun Date: Tue, 20 Feb 2024 15:00:24 -0500 Subject: [PATCH 1/6] created repository and migrating db calls --- .../system-configurations-repository.js | 20 +++++++++++++++++++ app/services/system-configuration-service.js | 13 ++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 app/repository/system-configurations-repository.js diff --git a/app/repository/system-configurations-repository.js b/app/repository/system-configurations-repository.js new file mode 100644 index 00000000..d51b34e9 --- /dev/null +++ b/app/repository/system-configurations-repository.js @@ -0,0 +1,20 @@ +const { DatabaseError, DuplicateIdError, BadlyFormattedParameterError } = require('../exceptions'); + +class SystemConfigurationsRepository { + + async saveDocument(document) { + try { + return await document.save(); + } + catch(err) { + if (err.name === 'MongoServerError' && err.code === 11000) { + throw new DuplicateIdError({ + details: `Document with id '${ document.stix.id }' already exists.` + }); + } + throw new DatabaseError(err); + } + } +} + +module.exports = new SystemConfigurationsRepository(); \ No newline at end of file diff --git a/app/services/system-configuration-service.js b/app/services/system-configuration-service.js index f99f1f4c..b24d8b68 100644 --- a/app/services/system-configuration-service.js +++ b/app/services/system-configuration-service.js @@ -7,6 +7,7 @@ const SystemConfiguration = require('../models/system-configuration-model'); const Identity = require('../models/identity-model'); const UserAccount = require('../models/user-account-model'); const MarkingDefinition = require('../models/marking-definition-model'); +const SystemConfigurationRepository = require('../repository/system-configurations-repository'); let allowedValues; @@ -107,7 +108,7 @@ exports.setOrganizationIdentity = async function(stixId) { if (systemConfig) { // The document exists already. Set the identity reference. systemConfig.organization_identity_ref = stixId; - await systemConfig.save(); + await SystemConfigurationRepository.saveDocument(systemConfig); } else { // The document doesn't exist yet. Create a new one. @@ -115,7 +116,7 @@ exports.setOrganizationIdentity = async function(stixId) { organization_identity_ref: stixId }; const systemConfig = new SystemConfiguration(systemConfigData); - await systemConfig.save(); + await SystemConfigurationRepository.saveDocument(systemConfig); } } @@ -165,7 +166,7 @@ exports.setDefaultMarkingDefinitions = async function(stixIds) { if (systemConfig) { // The document exists already. Set the default marking definitions. systemConfig.default_marking_definitions = stixIds; - await systemConfig.save(); + await SystemConfigurationRepository.saveDocument(systemConfig); } else { // The document doesn't exist yet. Create a new one. @@ -173,7 +174,7 @@ exports.setDefaultMarkingDefinitions = async function(stixIds) { default_marking_definitions: stixIds }; const systemConfig = new SystemConfiguration(systemConfigData); - await systemConfig.save(); + await SystemConfigurationRepository.saveDocument(systemConfig); } } @@ -204,7 +205,7 @@ exports.setAnonymousUserAccountId = async function(userAccountId) { if (systemConfig) { // The document exists already. Set the anonymous user account id. systemConfig.anonymous_user_account_id = userAccountId; - await systemConfig.save(); + await SystemConfigurationRepository.saveDocument(systemConfig); } else { throw new Error(errors.systemConfigurationDocumentNotFound); @@ -240,7 +241,7 @@ exports.setOrganizationNamespace = async function(namespace) { if (systemConfig) { systemConfig.organization_namespace = namespace; - await systemConfig.save(); + await SystemConfigurationRepository.saveDocument(systemConfig); } else { throw new Error(errors.systemConfigurationDocumentNotFound); From c92eddfef7f0684ed4bda7b37d77f4f15d3b3d04 Mon Sep 17 00:00:00 2001 From: Sun Date: Tue, 20 Feb 2024 15:04:07 -0500 Subject: [PATCH 2/6] adding more db calls --- .../system-configurations-repository.js | 10 ++++++++++ app/services/system-configuration-service.js | 18 +++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/app/repository/system-configurations-repository.js b/app/repository/system-configurations-repository.js index d51b34e9..b3f37574 100644 --- a/app/repository/system-configurations-repository.js +++ b/app/repository/system-configurations-repository.js @@ -15,6 +15,16 @@ class SystemConfigurationsRepository { throw new DatabaseError(err); } } + + async retrieveOneById(model) { + const res = await model.findOne(); + return res; + } + + async retrieveOneByIdLean(model) { + const res = await model.findOne().lean(); + return res; + } } module.exports = new SystemConfigurationsRepository(); \ No newline at end of file diff --git a/app/services/system-configuration-service.js b/app/services/system-configuration-service.js index b24d8b68..13f38b0c 100644 --- a/app/services/system-configuration-service.js +++ b/app/services/system-configuration-service.js @@ -71,7 +71,7 @@ exports.retrieveAllowedValuesForTypePropertyDomain = retrieveAllowedValuesForTyp exports.retrieveOrganizationIdentityRef = async function() { // There should be exactly one system configuration document - const systemConfig = await SystemConfiguration.findOne(); + const systemConfig = await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); if (systemConfig && systemConfig.organization_identity_ref) { return systemConfig.organization_identity_ref; @@ -83,7 +83,7 @@ exports.retrieveOrganizationIdentityRef = async function() { exports.retrieveOrganizationIdentity = async function() { // There should be exactly one system configuration document - const systemConfig = await SystemConfiguration.findOne(); + const systemConfig = await await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); if (systemConfig && systemConfig.organization_identity_ref) { const identity = await Identity.findOne({ 'stix.id': systemConfig.organization_identity_ref }).lean(); @@ -103,7 +103,7 @@ exports.retrieveOrganizationIdentity = async function() { exports.setOrganizationIdentity = async function(stixId) { // There should be exactly one system configuration document - const systemConfig = await SystemConfiguration.findOne(); + const systemConfig = await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); if (systemConfig) { // The document exists already. Set the identity reference. @@ -124,7 +124,7 @@ exports.retrieveDefaultMarkingDefinitions = async function(options) { options = options ?? {}; // There should be exactly one system configuration document - const systemConfig = await SystemConfiguration.findOne().lean(); + const systemConfig = await SystemConfigurationRepository.retrieveOneByIdLean(SystemConfiguration); if (systemConfig) { if (systemConfig.default_marking_definitions) { @@ -161,7 +161,7 @@ exports.retrieveDefaultMarkingDefinitions = async function(options) { exports.setDefaultMarkingDefinitions = async function(stixIds) { // There should be exactly one system configuration document - const systemConfig = await SystemConfiguration.findOne(); + const systemConfig = await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); if (systemConfig) { // The document exists already. Set the default marking definitions. @@ -180,7 +180,7 @@ exports.setDefaultMarkingDefinitions = async function(stixIds) { exports.retrieveAnonymousUserAccount = async function() { // There should be exactly one system configuration document - const systemConfig = await SystemConfiguration.findOne(); + const systemConfig = await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); if (systemConfig && systemConfig.anonymous_user_account_id) { const userAccount = await UserAccount.findOne({ 'id': systemConfig.anonymous_user_account_id }); @@ -200,7 +200,7 @@ exports.retrieveAnonymousUserAccount = async function() { exports.setAnonymousUserAccountId = async function(userAccountId) { // There should be exactly one system configuration document - const systemConfig = await SystemConfiguration.findOne(); + const systemConfig = await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); if (systemConfig) { // The document exists already. Set the anonymous user account id. @@ -225,7 +225,7 @@ exports.retrieveAuthenticationConfig = function() { exports.retrieveOrganizationNamespace = async function() { // There should be exactly one system configuration document - const systemConfig = await SystemConfiguration.findOne(); + const systemConfig = await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); if (systemConfig) { return systemConfig.organization_namespace; @@ -237,7 +237,7 @@ exports.retrieveOrganizationNamespace = async function() { exports.setOrganizationNamespace = async function(namespace) { // There should be exactly one system configuration document - const systemConfig = await SystemConfiguration.findOne(); + const systemConfig = await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); if (systemConfig) { systemConfig.organization_namespace = namespace; From 5ccc500e0004fc69cec7ab8675027e019727bfc9 Mon Sep 17 00:00:00 2001 From: Sun Date: Tue, 20 Feb 2024 15:07:58 -0500 Subject: [PATCH 3/6] fixing linter issues --- .../system-configurations-repository.js | 13 +++++++++---- app/services/system-configuration-service.js | 18 +++++++++--------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/app/repository/system-configurations-repository.js b/app/repository/system-configurations-repository.js index b3f37574..84516008 100644 --- a/app/repository/system-configurations-repository.js +++ b/app/repository/system-configurations-repository.js @@ -1,7 +1,12 @@ -const { DatabaseError, DuplicateIdError, BadlyFormattedParameterError } = require('../exceptions'); +const { DatabaseError, DuplicateIdError } = require('../exceptions'); +const SystemConfiguration = require('../models/system-configuration-model'); class SystemConfigurationsRepository { + constructor(model) { + this.model = model; + } + async saveDocument(document) { try { return await document.save(); @@ -17,14 +22,14 @@ class SystemConfigurationsRepository { } async retrieveOneById(model) { - const res = await model.findOne(); + const res = await this.model.findOne(); return res; } async retrieveOneByIdLean(model) { - const res = await model.findOne().lean(); + const res = await this.model.findOne().lean(); return res; } } -module.exports = new SystemConfigurationsRepository(); \ No newline at end of file +module.exports = new SystemConfigurationsRepository(SystemConfiguration); \ No newline at end of file diff --git a/app/services/system-configuration-service.js b/app/services/system-configuration-service.js index 13f38b0c..71e108b1 100644 --- a/app/services/system-configuration-service.js +++ b/app/services/system-configuration-service.js @@ -71,7 +71,7 @@ exports.retrieveAllowedValuesForTypePropertyDomain = retrieveAllowedValuesForTyp exports.retrieveOrganizationIdentityRef = async function() { // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); + const systemConfig = await SystemConfigurationRepository.retrieveOneById(); if (systemConfig && systemConfig.organization_identity_ref) { return systemConfig.organization_identity_ref; @@ -83,7 +83,7 @@ exports.retrieveOrganizationIdentityRef = async function() { exports.retrieveOrganizationIdentity = async function() { // There should be exactly one system configuration document - const systemConfig = await await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); + const systemConfig = await await SystemConfigurationRepository.retrieveOneById(); if (systemConfig && systemConfig.organization_identity_ref) { const identity = await Identity.findOne({ 'stix.id': systemConfig.organization_identity_ref }).lean(); @@ -103,7 +103,7 @@ exports.retrieveOrganizationIdentity = async function() { exports.setOrganizationIdentity = async function(stixId) { // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); + const systemConfig = await SystemConfigurationRepository.retrieveOneById(); if (systemConfig) { // The document exists already. Set the identity reference. @@ -124,7 +124,7 @@ exports.retrieveDefaultMarkingDefinitions = async function(options) { options = options ?? {}; // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneByIdLean(SystemConfiguration); + const systemConfig = await SystemConfigurationRepository.retrieveOneByIdLean(); if (systemConfig) { if (systemConfig.default_marking_definitions) { @@ -161,7 +161,7 @@ exports.retrieveDefaultMarkingDefinitions = async function(options) { exports.setDefaultMarkingDefinitions = async function(stixIds) { // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); + const systemConfig = await SystemConfigurationRepository.retrieveOneById(); if (systemConfig) { // The document exists already. Set the default marking definitions. @@ -180,7 +180,7 @@ exports.setDefaultMarkingDefinitions = async function(stixIds) { exports.retrieveAnonymousUserAccount = async function() { // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); + const systemConfig = await SystemConfigurationRepository.retrieveOneById(); if (systemConfig && systemConfig.anonymous_user_account_id) { const userAccount = await UserAccount.findOne({ 'id': systemConfig.anonymous_user_account_id }); @@ -200,7 +200,7 @@ exports.retrieveAnonymousUserAccount = async function() { exports.setAnonymousUserAccountId = async function(userAccountId) { // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); + const systemConfig = await SystemConfigurationRepository.retrieveOneById(); if (systemConfig) { // The document exists already. Set the anonymous user account id. @@ -225,7 +225,7 @@ exports.retrieveAuthenticationConfig = function() { exports.retrieveOrganizationNamespace = async function() { // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); + const systemConfig = await SystemConfigurationRepository.retrieveOneById(); if (systemConfig) { return systemConfig.organization_namespace; @@ -237,7 +237,7 @@ exports.retrieveOrganizationNamespace = async function() { exports.setOrganizationNamespace = async function(namespace) { // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneById(SystemConfiguration); + const systemConfig = await SystemConfigurationRepository.retrieveOneById(); if (systemConfig) { systemConfig.organization_namespace = namespace; From 55e4b056af9f7c5c14abd9ead1d778bd5f210060 Mon Sep 17 00:00:00 2001 From: Sun Date: Tue, 20 Feb 2024 15:09:02 -0500 Subject: [PATCH 4/6] fixed all linter issues --- app/repository/system-configurations-repository.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/repository/system-configurations-repository.js b/app/repository/system-configurations-repository.js index 84516008..644e6c6b 100644 --- a/app/repository/system-configurations-repository.js +++ b/app/repository/system-configurations-repository.js @@ -1,8 +1,11 @@ -const { DatabaseError, DuplicateIdError } = require('../exceptions'); + const SystemConfiguration = require('../models/system-configuration-model'); class SystemConfigurationsRepository { + DatabaseError = require('../exceptions'); + DuplicateIdError = require('../exceptions'); + constructor(model) { this.model = model; } @@ -13,11 +16,11 @@ class SystemConfigurationsRepository { } catch(err) { if (err.name === 'MongoServerError' && err.code === 11000) { - throw new DuplicateIdError({ + throw new this.DuplicateIdError({ details: `Document with id '${ document.stix.id }' already exists.` }); } - throw new DatabaseError(err); + throw new this.DatabaseError(err); } } From aacf8c17276e1583834dcc91ab2d598b8cea4c6a Mon Sep 17 00:00:00 2001 From: Sun Date: Tue, 20 Feb 2024 15:09:27 -0500 Subject: [PATCH 5/6] all linter issues resolved --- app/repository/system-configurations-repository.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app/repository/system-configurations-repository.js b/app/repository/system-configurations-repository.js index 644e6c6b..5be6d570 100644 --- a/app/repository/system-configurations-repository.js +++ b/app/repository/system-configurations-repository.js @@ -4,6 +4,7 @@ const SystemConfiguration = require('../models/system-configuration-model'); class SystemConfigurationsRepository { DatabaseError = require('../exceptions'); + DuplicateIdError = require('../exceptions'); constructor(model) { From 53ee9c6e8e8c2728604b951aa70099e5e65f0afc Mon Sep 17 00:00:00 2001 From: Jack Sheriff <> Date: Fri, 8 Mar 2024 15:33:13 -0500 Subject: [PATCH 6/6] Cleanup system configuration refactor. --- app/exceptions/index.js | 44 ++++++- app/lib/database-configuration.js | 11 +- .../system-configurations-repository.js | 37 +++--- app/services/system-configuration-service.js | 110 +++++++++--------- 4 files changed, 117 insertions(+), 85 deletions(-) diff --git a/app/exceptions/index.js b/app/exceptions/index.js index de4f7643..dc144f33 100644 --- a/app/exceptions/index.js +++ b/app/exceptions/index.js @@ -104,7 +104,43 @@ class PropertyNotAllowedError extends CustomError { super(`Unable to create software, property ${propertyName} is not allowed`, options); } } - + +class SystemConfigurationNotFound extends CustomError { + constructor(options) { + super(`System configuration not found`, options); + } +} + +class OrganizationIdentityNotSetError extends CustomError { + constructor(options) { + super(`Organization identity not set`, options); + } +} + +class DefaultMarkingDefinitionsNotFoundError extends CustomError { + constructor(options) { + super(`Default marking definitions not found`, options); + } +} + +class OrganizationIdentityNotFoundError extends CustomError { + constructor(identityRef, options) { + super(`Identity with id ${ identityRef } not found`, options); + } +} + +class AnonymousUserAccountNotSetError extends CustomError { + constructor(options) { + super(`Anonymous user account not set`, options); + } +} + +class AnonymousUserAccountNotFoundError extends CustomError { + constructor(userAccountid, options) { + super(`Anonymous user account ${ userAccountid } not found`, options); + } +} + class InvalidTypeError extends CustomError { constructor(options) { super('Invalid stix.type', options); @@ -135,6 +171,12 @@ module.exports = { TechniquesServiceError, TacticsServiceError, PropertyNotAllowedError, + SystemConfigurationNotFound, + DefaultMarkingDefinitionsNotFoundError, + OrganizationIdentityNotSetError, + OrganizationIdentityNotFoundError, + AnonymousUserAccountNotSetError, + AnonymousUserAccountNotFoundError, InvalidTypeError, }; diff --git a/app/lib/database-configuration.js b/app/lib/database-configuration.js index 33539196..f38c6746 100644 --- a/app/lib/database-configuration.js +++ b/app/lib/database-configuration.js @@ -11,6 +11,7 @@ const logger = require('../lib/logger'); const AttackObject = require('../models/attack-object-model'); const CollectionIndex = require('../models/collection-index-model'); const MarkingDefinition = require('../models/marking-definition-model'); +const { OrganizationIdentityNotFoundError, OrganizationIdentityNotSetError, AnonymousUserAccountNotFoundError, AnonymousUserAccountNotSetError } = require('../exceptions'); async function createPlaceholderOrganizationIdentity() { // Create placeholder identity object @@ -74,11 +75,11 @@ async function checkForOrganizationIdentity() { logger.info(`Success: Organization identity is set to ${ identity.stix.name }`); } catch(err) { - if (err.message === systemConfigurationService.errors.organizationIdentityNotFound) { - logger.warn(`Organization identity with id ${ err.organizationIdentityRef } not found, setting to placeholder identity`); + if (err instanceof OrganizationIdentityNotFoundError) { + logger.warn(`Organization identity with id ${ err } not found, setting to placeholder identity`); await createPlaceholderOrganizationIdentity(); } - else if (err.message === systemConfigurationService.errors.organizationIdentityNotSet) { + else if (err instanceof OrganizationIdentityNotSetError) { logger.warn(`Organization identity not set, setting to placeholder identity`); await createPlaceholderOrganizationIdentity(); } @@ -100,11 +101,11 @@ async function checkForAnonymousUserAccount() { logger.info(`Success: Anonymous user account is set to ${ anonymousUserAccount.id }`); } catch (err) { - if (err.message === systemConfigurationService.errors.anonymousUserAccountNotFound) { + if (err instanceof AnonymousUserAccountNotFoundError) { logger.warn(`Anonymous user account with id ${ err.anonymousUserAccountId } not found, creating new anonymous user account`); await createAnonymousUserAccount(); } - else if (err.message === systemConfigurationService.errors.anonymousUserAccountNotSet) { + else if (err instanceof AnonymousUserAccountNotSetError) { logger.warn(`Anonymous user account not set, creating new anonymous user account`); await createAnonymousUserAccount(); } diff --git a/app/repository/system-configurations-repository.js b/app/repository/system-configurations-repository.js index 5be6d570..3ee017b7 100644 --- a/app/repository/system-configurations-repository.js +++ b/app/repository/system-configurations-repository.js @@ -1,38 +1,33 @@ const SystemConfiguration = require('../models/system-configuration-model'); +const { DatabaseError } = require('../exceptions'); -class SystemConfigurationsRepository { - - DatabaseError = require('../exceptions'); - - DuplicateIdError = require('../exceptions'); - +class SystemConfigurationsRepository { constructor(model) { this.model = model; } - async saveDocument(document) { + createNewDocument(data) { + return new this.model(data); + } + + static async saveDocument(document) { try { return await document.save(); } catch(err) { - if (err.name === 'MongoServerError' && err.code === 11000) { - throw new this.DuplicateIdError({ - details: `Document with id '${ document.stix.id }' already exists.` - }); - } - throw new this.DatabaseError(err); + throw new DatabaseError(err); } } - async retrieveOneById(model) { - const res = await this.model.findOne(); - return res; - } - - async retrieveOneByIdLean(model) { - const res = await this.model.findOne().lean(); - return res; + async retrieveOne(options) { + options = options ?? {}; + if (options.lean) { + return await this.model.findOne().lean(); + } + else { + return await this.model.findOne(); + } } } diff --git a/app/services/system-configuration-service.js b/app/services/system-configuration-service.js index 71e108b1..94760a43 100644 --- a/app/services/system-configuration-service.js +++ b/app/services/system-configuration-service.js @@ -2,25 +2,19 @@ const fs = require('fs'); const config = require('../config/config'); - -const SystemConfiguration = require('../models/system-configuration-model'); -const Identity = require('../models/identity-model'); -const UserAccount = require('../models/user-account-model'); -const MarkingDefinition = require('../models/marking-definition-model'); -const SystemConfigurationRepository = require('../repository/system-configurations-repository'); +const systemConfigurationRepository = require('../repository/system-configurations-repository'); +const userAccountsService = require('./user-accounts-service'); +const { + SystemConfigurationNotFound, + OrganizationIdentityNotSetError, + OrganizationIdentityNotFoundError, + DefaultMarkingDefinitionsNotFoundError, + AnonymousUserAccountNotSetError, + AnonymousUserAccountNotFoundError } = require('../exceptions'); let allowedValues; - -const errors = { - systemConfigurationDocumentNotFound: 'System configuration document not found', - organizationIdentityNotFound: 'Organization identity not found', - organizationIdentityNotSet: 'Organization identity not set', - anonymousUserAccountNotFound: 'Anonymous user account not found', - anonymousUserAccountNotSet: 'Anonymous user account not set', - defaultMarkingDefinitionNotFound: 'Default marking definition not found', - systemConfigurationNotFound: 'System configuration not found' -}; -exports.errors = errors; +let markingDefinitionsService; +let identitiesService; // NOTE: Some parts of the system configuration are stored in the systemconfiguration collection in the database // (the systemconfiguration collection should have exactly one document) @@ -71,60 +65,64 @@ exports.retrieveAllowedValuesForTypePropertyDomain = retrieveAllowedValuesForTyp exports.retrieveOrganizationIdentityRef = async function() { // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneById(); + const systemConfig = await systemConfigurationRepository.retrieveOne(); if (systemConfig && systemConfig.organization_identity_ref) { return systemConfig.organization_identity_ref; } else { - throw new Error(errors.organizationIdentityNotSet); + throw new OrganizationIdentityNotSetError; } } exports.retrieveOrganizationIdentity = async function() { + if (!identitiesService) { + identitiesService = require('./identities-service'); + } // There should be exactly one system configuration document - const systemConfig = await await SystemConfigurationRepository.retrieveOneById(); + const systemConfig = await systemConfigurationRepository.retrieveOne({ lean: true }); if (systemConfig && systemConfig.organization_identity_ref) { - const identity = await Identity.findOne({ 'stix.id': systemConfig.organization_identity_ref }).lean(); - if (identity) { - return identity; + const identities = await identitiesService.retrieveById(systemConfig.organization_identity_ref, { versions: 'latest' }); + if (identities.length === 1) { + return identities[0]; } else { - const error = new Error(errors.organizationIdentityNotFound) - error.organizationIdentityRef = systemConfig.organization_identity_ref; - throw error; + throw new OrganizationIdentityNotFoundError(systemConfig.organization_identity_ref); } } else { - throw new Error(errors.organizationIdentityNotSet); + throw new OrganizationIdentityNotSetError; } } exports.setOrganizationIdentity = async function(stixId) { // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneById(); + const systemConfig = await systemConfigurationRepository.retrieveOne(); if (systemConfig) { // The document exists already. Set the identity reference. systemConfig.organization_identity_ref = stixId; - await SystemConfigurationRepository.saveDocument(systemConfig); + await systemConfigurationRepository.constructor.saveDocument(systemConfig); } else { // The document doesn't exist yet. Create a new one. const systemConfigData = { organization_identity_ref: stixId }; - const systemConfig = new SystemConfiguration(systemConfigData); - await SystemConfigurationRepository.saveDocument(systemConfig); + const systemConfig = systemConfigurationRepository.createNewDocument(systemConfigData); + await systemConfigurationRepository.constructor.saveDocument(systemConfig); } } exports.retrieveDefaultMarkingDefinitions = async function(options) { + if (!markingDefinitionsService) { + markingDefinitionsService = require('./marking-definitions-service'); + } options = options ?? {}; // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneByIdLean(); + const systemConfig = await systemConfigurationRepository.retrieveOne({ lean: true }); if (systemConfig) { if (systemConfig.default_marking_definitions) { @@ -135,16 +133,14 @@ exports.retrieveDefaultMarkingDefinitions = async function(options) { const defaultMarkingDefinitions = []; for (const stixId of systemConfig.default_marking_definitions) { // eslint-disable-next-line no-await-in-loop - const markingDefinition = await MarkingDefinition.findOne({ 'stix.id': stixId }).lean(); - if (markingDefinition) { - defaultMarkingDefinitions.push(markingDefinition); - } else { - const error = new Error(errors.defaultMarkingDefinitionNotFound); - error.markingDefinitionRef = stixId; - throw error; + const markingDefinition = await markingDefinitionsService.retrieveById(stixId); + if (markingDefinition.length === 1) { + defaultMarkingDefinitions.push(markingDefinition[0]); + } + else { + throw new DefaultMarkingDefinitionsNotFoundError; } } - return defaultMarkingDefinitions; } } @@ -161,54 +157,52 @@ exports.retrieveDefaultMarkingDefinitions = async function(options) { exports.setDefaultMarkingDefinitions = async function(stixIds) { // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneById(); + const systemConfig = await systemConfigurationRepository.retrieveOne(); if (systemConfig) { // The document exists already. Set the default marking definitions. systemConfig.default_marking_definitions = stixIds; - await SystemConfigurationRepository.saveDocument(systemConfig); + await systemConfigurationRepository.constructor.saveDocument(systemConfig); } else { // The document doesn't exist yet. Create a new one. const systemConfigData = { default_marking_definitions: stixIds }; - const systemConfig = new SystemConfiguration(systemConfigData); - await SystemConfigurationRepository.saveDocument(systemConfig); + const systemConfig = systemConfigurationRepository.createNewDocument(systemConfigData); + await systemConfigurationRepository.constructor.saveDocument(systemConfig); } } exports.retrieveAnonymousUserAccount = async function() { // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneById(); + const systemConfig = await systemConfigurationRepository.retrieveOne({ lean: true }); if (systemConfig && systemConfig.anonymous_user_account_id) { - const userAccount = await UserAccount.findOne({ 'id': systemConfig.anonymous_user_account_id }); + const userAccount = await userAccountsService.retrieveById(systemConfig.anonymous_user_account_id, {}); if (userAccount) { return userAccount; } else { - const error = new Error(errors.anonymousUserAccountNotFound) - error.anonymousUserAccountId = systemConfig.anonymous_user_account_id; - throw error; + throw new AnonymousUserAccountNotFoundError(systemConfig.anonymous_user_account_id); } } else { - throw new Error(errors.anonymousUserAccountNotSet); + throw new AnonymousUserAccountNotSetError; } } exports.setAnonymousUserAccountId = async function(userAccountId) { // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneById(); + const systemConfig = await systemConfigurationRepository.retrieveOne(); if (systemConfig) { // The document exists already. Set the anonymous user account id. systemConfig.anonymous_user_account_id = userAccountId; - await SystemConfigurationRepository.saveDocument(systemConfig); + await systemConfigurationRepository.constructor.saveDocument(systemConfig); } else { - throw new Error(errors.systemConfigurationDocumentNotFound); + throw new SystemConfigurationNotFound; } } @@ -225,25 +219,25 @@ exports.retrieveAuthenticationConfig = function() { exports.retrieveOrganizationNamespace = async function() { // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneById(); + const systemConfig = await systemConfigurationRepository.retrieveOne({ lean: true }); if (systemConfig) { return systemConfig.organization_namespace; } else { - throw new Error(errors.systemConfigurationDocumentNotFound); + throw new SystemConfigurationNotFound; } } exports.setOrganizationNamespace = async function(namespace) { // There should be exactly one system configuration document - const systemConfig = await SystemConfigurationRepository.retrieveOneById(); + const systemConfig = await systemConfigurationRepository.retrieveOne(); if (systemConfig) { systemConfig.organization_namespace = namespace; - await SystemConfigurationRepository.saveDocument(systemConfig); + await systemConfigurationRepository.constructor.saveDocument(systemConfig); } else { - throw new Error(errors.systemConfigurationDocumentNotFound); + throw new SystemConfigurationNotFound; } }