Fix jwt-token
This commit is contained in:
@@ -154,42 +154,57 @@ function defineModels() {
|
||||
]
|
||||
});
|
||||
|
||||
// SecurityLog model (optional, might not exist in all installations)
|
||||
// SecurityLog model - IMPORTANT: Security logs have different retention policies (much longer)
|
||||
models.SecurityLog = sequelize.define('SecurityLog', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
type: DataTypes.UUID,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
defaultValue: DataTypes.UUIDV4
|
||||
},
|
||||
tenant_id: {
|
||||
type: DataTypes.UUID,
|
||||
allowNull: true
|
||||
},
|
||||
timestamp: {
|
||||
type: DataTypes.DATE,
|
||||
event_type: {
|
||||
type: DataTypes.STRING(50),
|
||||
allowNull: false
|
||||
},
|
||||
level: {
|
||||
severity: {
|
||||
type: DataTypes.STRING(20),
|
||||
allowNull: false
|
||||
},
|
||||
username: {
|
||||
type: DataTypes.STRING(100),
|
||||
allowNull: true
|
||||
},
|
||||
ip_address: {
|
||||
type: DataTypes.INET,
|
||||
allowNull: true
|
||||
},
|
||||
country_code: {
|
||||
type: DataTypes.STRING(2),
|
||||
allowNull: true
|
||||
},
|
||||
message: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: false
|
||||
},
|
||||
metadata: {
|
||||
type: DataTypes.JSONB,
|
||||
defaultValue: {}
|
||||
created_at: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
tableName: 'security_logs',
|
||||
timestamps: false,
|
||||
indexes: [
|
||||
{
|
||||
fields: ['tenant_id', 'timestamp']
|
||||
fields: ['tenant_id', 'created_at']
|
||||
},
|
||||
{
|
||||
fields: ['timestamp']
|
||||
fields: ['event_type', 'created_at']
|
||||
},
|
||||
{
|
||||
fields: ['ip_address', 'created_at']
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
@@ -165,11 +165,12 @@ class DataRetentionService {
|
||||
const cutoffDate = new Date();
|
||||
cutoffDate.setDate(cutoffDate.getDate() - effectiveRetentionDays);
|
||||
|
||||
console.log(`🧹 Cleaning tenant ${tenant.slug} - removing data older than ${effectiveRetentionDays} days (before ${cutoffDate.toISOString()})`);
|
||||
console.log(`🧹 Cleaning tenant ${tenant.slug} - removing operational data older than ${effectiveRetentionDays} days (before ${cutoffDate.toISOString()})`);
|
||||
console.log(`📋 Note: Security logs and audit trails are preserved and not subject to automatic cleanup`);
|
||||
|
||||
const { DroneDetection, Heartbeat, SecurityLog } = await getModels();
|
||||
const { DroneDetection, Heartbeat } = await getModels();
|
||||
|
||||
// Clean up drone detections
|
||||
// Clean up drone detections (operational data)
|
||||
const deletedDetections = await DroneDetection.destroy({
|
||||
where: {
|
||||
tenant_id: tenant.id,
|
||||
@@ -179,7 +180,7 @@ class DataRetentionService {
|
||||
}
|
||||
});
|
||||
|
||||
// Clean up heartbeats
|
||||
// Clean up heartbeats (operational data)
|
||||
const deletedHeartbeats = await Heartbeat.destroy({
|
||||
where: {
|
||||
tenant_id: tenant.id,
|
||||
@@ -189,23 +190,30 @@ class DataRetentionService {
|
||||
}
|
||||
});
|
||||
|
||||
// Clean up security logs (if they have tenant_id)
|
||||
// Clean up security logs - MUCH LONGER retention (7 years for compliance)
|
||||
// Security logs should only be cleaned up after 7 years, not the standard retention period
|
||||
let deletedLogs = 0;
|
||||
try {
|
||||
const securityLogCutoffDate = new Date();
|
||||
securityLogCutoffDate.setFullYear(securityLogCutoffDate.getFullYear() - 7); // 7 years retention
|
||||
|
||||
deletedLogs = await SecurityLog.destroy({
|
||||
where: {
|
||||
tenant_id: tenant.id,
|
||||
timestamp: {
|
||||
[Op.lt]: cutoffDate
|
||||
created_at: {
|
||||
[Op.lt]: securityLogCutoffDate
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (deletedLogs > 0) {
|
||||
console.log(`🔐 Cleaned ${deletedLogs} security logs older than 7 years for tenant ${tenant.slug}`);
|
||||
}
|
||||
} catch (error) {
|
||||
// SecurityLog might not have tenant_id field
|
||||
console.log(`⚠️ Skipping security logs for tenant ${tenant.slug}: ${error.message}`);
|
||||
console.log(`⚠️ Error cleaning security logs for tenant ${tenant.slug}: ${error.message}`);
|
||||
}
|
||||
|
||||
console.log(`✅ Tenant ${tenant.slug}: Deleted ${deletedDetections} detections, ${deletedHeartbeats} heartbeats, ${deletedLogs} logs`);
|
||||
console.log(`✅ Tenant ${tenant.slug}: Deleted ${deletedDetections} detections, ${deletedHeartbeats} heartbeats, ${deletedLogs} security logs (7yr retention)`);
|
||||
|
||||
return {
|
||||
detections: deletedDetections,
|
||||
|
||||
Reference in New Issue
Block a user