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

Added functionality to import assets and collections from STIG Manage… #21

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
99 changes: 78 additions & 21 deletions Api/.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,81 @@
#!########################################################################
#*/

POAM_DB_HOST="localhost" #POAM database host, referenced in Api/utils/config.js
POAM_DB_USER="root" #POAM database user, referenced in Api/utils/config.js
POAM_DB_PASSWORD="root" #POAM database password, referenced in Api/utils/config.js
POAM_DB_SCHEMA="poamtracking" #POAM database name, referenced in Api/utils/config.js
POAM_DB_TYPE="mysql" #POAM database dialect, referenced in Api/utils/config.js
POAM_DB_PORT="3306" #POAM database port, referenced in Api/utils/config.js
POAM_DB_MAX_CONNECTIONS="25" #POAM database max connections, referenced in Api/utils/config.js

USERSERVICE_DB_HOST="localhost" #Userservice database host, referenced in Api/services/mysql/userservice.js
USERSERVICE_DB_PORT="3306" #Userservice database port, referenced in Api/services/mysql/userservice.js
USERSERVICE_DB_USER="root" #Userservice database user, referenced in Api/services/mysql/userservice.js
USERSERVICE_DB_PASSWORD="root" #Userservice database password, referenced in Api/services/mysql/userservice.js
USERSERVICE_DB_DATABASE="poamtracking" #Userservice database name, referenced in Api/services/mysql/userservice.js
USERSERVICE_DB_DIALECT="mysql" #Userservice database dialect, referenced in Api/services/mysql/userservice.js
USERSERVICE_DB_MAX_CONNECTIONS="25" #Userservice database max connections, referenced in Api/services/mysql/userservice.js
USERSERVICE_DB_MIN_CONNECTIONS="0" #Userservice database min connections, referenced in Api/services/mysql/userservice.js"
USERSERVICE_DB_ACQUIRE="30000" #Userservice database acquire, referenced in Api/services/mysql/userservice.js
USERSERVICE_DB_IDLE="10000" #Userservice database idle, referenced in Api/services/mysql/userservice.js


JWT_SECRET_KEY="wpm_token" #JWT secret key, referenced in Api/utils/token-generator.js
# referenced in Api/utils/config.js
COMMIT_BRANCH="na"
COMMIT_SHA="na"
COMMIT_TAG="na"
COMMIT_DESCRIBE="na"

#settings config
POAM_DEV_RESPONSE_VALIDATION="none"

#client config
POAM_CLIENT_ID="stig-manager"
POAM_CLIENT_API_BASE="api"
POAM_CLIENT_DISABLED="true"
POAM_CLIENT_DIRECTORY="../../client/dist"

#welcome congif
POAM_CLIENT_WELCOME_IMAGE=""
POAM_CLIENT_WELCOME_MESSAGE=""
POAM_CLIENT_WELCOME_TITLE=""
POAM_CLIENT_WELCOME_LINK=""

#docs config
POAM_DOCS_DISABLED="true"
POAM_DOCS_DIRECTORY="../../docs/_build/html"

#http config
POAM_API_ADDRESS="0.0.0.0"
POAM_API_PORT="54000"
POAM_API_MAX_JSON_BODY="5242880"
POAM_API_MAX_UPLOAD="1073741824"

#database config
POAM_DB_HOST="localhost"
POAM_DB_USER="root"
POAM_DB_PASSWORD="root"
POAM_DB_REVERT="true"
POAM_DB_SCHEMA="poamtracking"
POAM_DB_TYPE="mysql"
POAM_DB_PORT="3306"
POAM_DB_MAX_CONNECTIONS="25"

POAM_OIDC_PROVIDER="http://localhost:8080/realms/RMFTools"
POAM_API_AUTHORITY="http://localhost:8080/realms/RMFTools"

#init config
POAM_INIT_IMPORT_STIGS="true"
POAM_INIT_IMPORT_SCAP="true"

#swagger config
POAM_SWAGGER_OIDC_PROVIDER="http://localhost:8080/realms/RMFTools"
POAM_SWAGGER_ENABLED="true"
POAM_SWAGGER_SERVER="http://localhost:54000/api"
POAM_SWAGGER_REDIRECT="http://localhost:54000/api-docs/oauth2-redirect.html"

#oauth config
POAM_JWT_SCOPE_CLAIM="scope"
POAM_JWT_USERNAME_CLAIM="preferred_username"
POAM_JWT_NAME_CLAIM="name"
POAM_JWT_SERVICENAME_CLAIM="clientId"
POAM_JWT_EMAIL_CLAIM="email"

#log config
POAM_LOG_MODE="combined"

#userservice config
USERSERVICE_DB_HOST="localhost"
USERSERVICE_DB_PORT="3306"
USERSERVICE_DB_USER="root"
USERSERVICE_DB_PASSWORD="root"
USERSERVICE_DB_DATABASE="poamtracking"
USERSERVICE_DB_DIALECT="mysql"
USERSERVICE_DB_MAX_CONNECTIONS="25"
USERSERVICE_DB_MIN_CONNECTIONS="0"
USERSERVICE_DB_ACQUIRE="30000"
USERSERVICE_DB_IDLE="10000"

#JWT secret key, referenced in Api/utils/token-generator.js
JWT_SECRET_KEY="wpm_token"
44 changes: 44 additions & 0 deletions Api/Controllers/STIGMANAsset.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const express = require('express');
const db = require('../utils/sequelize');
const router = express.Router();

async function importAssets(req, res) {
try {
const { assets } = req.body;

// Handle Assets
for (const asset of assets) {
const collection = asset.collection || {};
const assetData = {
assetId: asset.assetId,
assetName: asset.name,
fullyQualifiedDomainName: asset.fqdn || '',
description: asset.description || '',
ipAddress: asset.ip || '',
macAddress: asset.mac || '',
nonComputing: asset.noncomputing ? 1 : 0,
collectionId: collection.collectionId || null,
metadata: asset.metadata ? JSON.stringify(asset.metadata) : '{}',
};

// Find or create the asset
const [assetRecord, assetCreated] = await db.Asset.findOrCreate({
where: { assetName: asset.name },
defaults: assetData
});

if (!assetCreated) {
await assetRecord.update(assetData);
}
}

res.status(200).json({ message: 'Assets Imported Successfully' });
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Internal Server Error' });
}
}

module.exports = {
importAssets
};
61 changes: 61 additions & 0 deletions Api/Controllers/STIGMANCollection.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const express = require('express');
const db = require('../utils/sequelize.js');
const router = express.Router();

async function importCollectionAndAssets(req, res) {
try {
const { collection, assets } = req.body;

// Handle Collection
const collectionData = {
collectionId: collection.collectionId,
collectionName: collection.name,
description: collection.description || '',
metadata: collection.metadata ? JSON.stringify(collection.metadata) : '{}',
settings: collection.settings ? JSON.stringify(collection.settings) : '{}'
};

const [collectionRecord, created] = await db.Collection.findOrCreate({
where: { collectionName: collection.name },
defaults: collectionData
});

if (!created) {
await collectionRecord.update(collectionData);
}

// Handle Assets
for (const asset of assets) {
const assetData = {
assetId: asset.assetId,
assetName: asset.name,
fullyQualifiedDomainName: asset.fqdn || '',
description: asset.description || '',
ipAddress: asset.ip || '',
macAddress: asset.mac || '',
nonComputing: asset.noncomputing ? 1 : 0,
collectionId: collectionRecord.collectionId, // Ensure this is correctly assigned
metadata: asset.metadata ? JSON.stringify(asset.metadata) : '{}',
};

const [assetRecord, assetCreated] = await db.Asset.findOrCreate({
where: { assetName: asset.name }, // Assuming assetName is unique
defaults: assetData
});

if (!assetCreated) {
await assetRecord.update(assetData);
}
}

res.status(200).json({ message: 'Collection and Assets Imported Successfully' });
} catch (error) {
// Log the error and send a server error response
console.error(error);
res.status(500).json({ message: 'Internal Server Error' });
}
};

module.exports = {
importCollectionAndAssets
};
58 changes: 58 additions & 0 deletions Api/Models/asset.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module.exports = (sequelize, DataTypes) => {
const Asset = sequelize.define('Asset', {
assetId: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
assetName: {
type: DataTypes.STRING(255),
allowNull: false,
unique: true,
},
fullyQualifiedDomainName: {
type: DataTypes.STRING(255),
},
collectionId: {
type: DataTypes.INTEGER,
allowNull: false,
},
description: {
type: DataTypes.STRING(75),
},
ipAddress: {
type: DataTypes.STRING(20),
},
macAddress: {
type: DataTypes.STRING(50),
},
nonComputing: {
type: DataTypes.TINYINT(1),
defaultValue: '0',
},
metadata: {
type: DataTypes.JSON,
},
state: {
type: DataTypes.ENUM('enabled', 'disabled'),
},
stateDate: {
type: DataTypes.DATE,
},
stateUserId: {
type: DataTypes.INTEGER,
},
isEnabled: {
type: DataTypes.VIRTUAL,
get() {
const state = this.getDataValue('state');
return state === 'enabled' ? 1 : 0;
}
},
}, {
tableName: 'asset',
timestamps: false,
});

return Asset;
};
67 changes: 67 additions & 0 deletions Api/Models/collection.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module.exports = (sequelize, DataTypes) => {
const Collection = sequelize.define('Collection', {
collectionId: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
collectionName: {
type: DataTypes.STRING(50),
allowNull: false,
},
description: {
type: DataTypes.STRING(255),
},
created: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
},
grantCount: {
type: DataTypes.INTEGER,
defaultValue: '0',
},
assetCount: {
type: DataTypes.INTEGER,
defaultValue: '0',
},
poamCount: {
type: DataTypes.INTEGER,
defaultValue: '0',
},
settings: {
type: DataTypes.JSON,
},
metadata: {
type: DataTypes.JSON,
},
state: {
type: DataTypes.ENUM('enabled', 'disabled'),
},
createdUserId: {
type: DataTypes.INTEGER,
},
stateDate: {
type: DataTypes.DATE,
},
stateUserId: {
type: DataTypes.INTEGER,
},
isEnabled: {
type: DataTypes.VIRTUAL,
get() {
return this.getDataValue('state') === 'enabled';
}
},
isNameUnavailable: {
type: DataTypes.VIRTUAL,
get() {
return this.getDataValue('state') === 'cloning' || this.getDataValue('state') === 'enabled';
}
},
}, {
tableName: 'collection',
timestamps: false,
});

return Collection;
};
Loading
Loading