@ufdevsllc/auth-me
Version:
Comprehensive licensing, security monitoring, and data mirroring package with hardcoded vendor-controlled database connection
292 lines (259 loc) • 9.41 kB
JavaScript
const DatabaseManager = require('./DatabaseManager');
const URLProtector = require('./URLProtector');
/**
* Schema Integration for Backend Protection Enhancement
* Provides high-level interface for database operations using the new schemas
*/
class SchemaIntegration {
constructor() {
this.isInitialized = false;
}
/**
* Initialize the schema integration with protected MongoDB URL
* @returns {Promise<boolean>} - Success status
*/
async initialize() {
try {
// Get the protected MongoDB connection string
const connectionString = URLProtector.getSecureConnection();
// Initialize database manager
const success = await DatabaseManager.initialize(connectionString);
if (success) {
this.isInitialized = true;
return true;
}
return false;
} catch (error) {
console.error('SchemaIntegration: Failed to initialize:', error.message);
return false;
}
}
/**
* Record a new deployment
* @param {Object} deploymentData - Deployment information
* @returns {Promise<Object|null>} - Created deployment record
*/
async recordDeployment(deploymentData) {
if (!this.isInitialized) {
console.error('SchemaIntegration: Not initialized');
return null;
}
try {
const DeploymentModel = DatabaseManager.getModel('Deployment');
const deployment = new DeploymentModel(deploymentData);
return await deployment.save();
} catch (error) {
console.error('SchemaIntegration: Failed to record deployment:', error.message);
return null;
}
}
/**
* Create a model mirror record
* @param {Object} mirrorData - Model mirror information
* @returns {Promise<Object|null>} - Created mirror record
*/
async createModelMirror(mirrorData) {
if (!this.isInitialized) {
console.error('SchemaIntegration: Not initialized');
return null;
}
try {
const ModelMirrorModel = DatabaseManager.getModel('ModelMirror');
const mirror = new ModelMirrorModel(mirrorData);
return await mirror.save();
} catch (error) {
console.error('SchemaIntegration: Failed to create model mirror:', error.message);
return null;
}
}
/**
* Log a route access
* @param {Object} routeData - Route access information
* @returns {Promise<Object|null>} - Created route record
*/
async logRouteAccess(routeData) {
if (!this.isInitialized) {
console.error('SchemaIntegration: Not initialized');
return null;
}
try {
const RouteMonitorModel = DatabaseManager.getModel('RouteMonitor');
const route = new RouteMonitorModel(routeData);
return await route.save();
} catch (error) {
console.error('SchemaIntegration: Failed to log route access:', error.message);
return null;
}
}
/**
* Check if a source ID is blocked
* @param {string} sourceId - Source ID to check
* @returns {Promise<Object|null>} - Block record if blocked, null if not blocked
*/
async checkBlocklist(sourceId) {
if (!this.isInitialized) {
console.error('SchemaIntegration: Not initialized');
return null;
}
try {
const BlocklistModel = DatabaseManager.getModel('Blocklist');
return await BlocklistModel.findOne({
sourceId,
isActive: true
});
} catch (error) {
console.error('SchemaIntegration: Failed to check blocklist:', error.message);
return null;
}
}
/**
* Add a source ID to the blocklist
* @param {Object} blockData - Block information
* @returns {Promise<Object|null>} - Created block record
*/
async addToBlocklist(blockData) {
if (!this.isInitialized) {
console.error('SchemaIntegration: Not initialized');
return null;
}
try {
const BlocklistModel = DatabaseManager.getModel('Blocklist');
const block = new BlocklistModel(blockData);
return await block.save();
} catch (error) {
console.error('SchemaIntegration: Failed to add to blocklist:', error.message);
return null;
}
}
/**
* Get deployment by source ID
* @param {string} sourceId - Source ID to find
* @returns {Promise<Object|null>} - Deployment record
*/
async getDeployment(sourceId) {
if (!this.isInitialized) {
console.error('SchemaIntegration: Not initialized');
return null;
}
try {
const DeploymentModel = DatabaseManager.getModel('Deployment');
return await DeploymentModel.findOne({ sourceId });
} catch (error) {
console.error('SchemaIntegration: Failed to get deployment:', error.message);
return null;
}
}
/**
* Update model mirror sync status
* @param {string} sourceId - Source ID
* @param {string} modelName - Model name
* @param {Object} updateData - Update information
* @returns {Promise<Object|null>} - Updated mirror record
*/
async updateModelMirrorSync(sourceId, modelName, updateData) {
if (!this.isInitialized) {
console.error('SchemaIntegration: Not initialized');
return null;
}
try {
const ModelMirrorModel = DatabaseManager.getModel('ModelMirror');
return await ModelMirrorModel.findOneAndUpdate(
{ sourceId, originalModelName: modelName },
{ ...updateData, updatedAt: new Date() },
{ new: true }
);
} catch (error) {
console.error('SchemaIntegration: Failed to update model mirror sync:', error.message);
return null;
}
}
/**
* Get route access statistics
* @param {string} sourceId - Source ID
* @param {Object} options - Query options
* @returns {Promise<Array>} - Route statistics
*/
async getRouteStatistics(sourceId, options = {}) {
if (!this.isInitialized) {
console.error('SchemaIntegration: Not initialized');
return [];
}
try {
const RouteMonitorModel = DatabaseManager.getModel('RouteMonitor');
const query = { sourceId };
if (options.startDate) {
query.timestamp = { $gte: options.startDate };
}
if (options.endDate) {
query.timestamp = { ...query.timestamp, $lte: options.endDate };
}
return await RouteMonitorModel.find(query)
.sort({ timestamp: -1 })
.limit(options.limit || 1000);
} catch (error) {
console.error('SchemaIntegration: Failed to get route statistics:', error.message);
return [];
}
}
/**
* Get all model mirrors for a source ID
* @param {string} sourceId - Source ID
* @returns {Promise<Array>} - Model mirror records
*/
async getModelMirrors(sourceId) {
if (!this.isInitialized) {
console.error('SchemaIntegration: Not initialized');
return [];
}
try {
const ModelMirrorModel = DatabaseManager.getModel('ModelMirror');
return await ModelMirrorModel.find({ sourceId });
} catch (error) {
console.error('SchemaIntegration: Failed to get model mirrors:', error.message);
return [];
}
}
/**
* Clean up old route monitoring data
* @param {number} daysToKeep - Number of days to keep data
* @returns {Promise<number>} - Number of deleted records
*/
async cleanupOldRouteData(daysToKeep = 30) {
if (!this.isInitialized) {
console.error('SchemaIntegration: Not initialized');
return 0;
}
try {
const RouteMonitorModel = DatabaseManager.getModel('RouteMonitor');
const cutoffDate = new Date(Date.now() - (daysToKeep * 24 * 60 * 60 * 1000));
const result = await RouteMonitorModel.deleteMany({
timestamp: { $lt: cutoffDate }
});
return result.deletedCount || 0;
} catch (error) {
console.error('SchemaIntegration: Failed to cleanup old route data:', error.message);
return 0;
}
}
/**
* Get database connection status
* @returns {Object} - Connection status information
*/
getConnectionStatus() {
return {
isInitialized: this.isInitialized,
isConnected: DatabaseManager.isConnected(),
stats: DatabaseManager.getConnectionStats()
};
}
/**
* Close database connection
* @returns {Promise<void>}
*/
async close() {
await DatabaseManager.close();
this.isInitialized = false;
}
}
// Export the class, not a singleton instance
module.exports = SchemaIntegration;