Skip to main content
Glama
by Coder-RL
security-orchestrator.ts15.8 kB
import { EventEmitter } from 'events'; import { ZeroTrustFramework } from './zero-trust-framework.js'; import { AuthenticationService, AuthConfig } from './auth-service.js'; import { AuthorizationService } from './authorization-service.js'; export interface SecurityConfig { auth: AuthConfig; zeroTrust: { enabled: boolean; strictMode: boolean; riskThreshold: number; adaptiveAuthentication: boolean; }; monitoring: { enabled: boolean; alertThreshold: number; incidentResponse: boolean; }; compliance: { frameworks: string[]; auditLogging: boolean; dataRetention: number; }; } export interface SecurityEvent { id: string; type: 'authentication' | 'authorization' | 'threat' | 'compliance' | 'audit'; severity: 'low' | 'medium' | 'high' | 'critical'; timestamp: Date; userId?: string; resource?: string; action?: string; details: Record<string, any>; resolved: boolean; } export interface SecurityMetrics { authentication: { totalLogins: number; successfulLogins: number; failedLogins: number; lockedAccounts: number; mfaUsage: number; }; authorization: { totalAccessRequests: number; grantedRequests: number; deniedRequests: number; policyViolations: number; }; threats: { detectedThreats: number; blockedAttacks: number; suspiciousActivities: number; riskScore: number; }; compliance: { auditEvents: number; violations: number; frameworks: string[]; lastAudit: Date; }; } export interface IncidentResponse { id: string; type: string; severity: 'low' | 'medium' | 'high' | 'critical'; status: 'open' | 'investigating' | 'mitigating' | 'resolved'; createdAt: Date; resolvedAt?: Date; actions: IncidentAction[]; affectedUsers: string[]; affectedResources: string[]; } export interface IncidentAction { id: string; type: 'block' | 'alert' | 'quarantine' | 'revoke' | 'investigate'; description: string; timestamp: Date; automated: boolean; result?: string; } export class SecurityOrchestrator extends EventEmitter { private zeroTrust: ZeroTrustFramework; private authService: AuthenticationService; private authzService: AuthorizationService; private events = new Map<string, SecurityEvent>(); private incidents = new Map<string, IncidentResponse>(); private metrics: SecurityMetrics; constructor(private config: SecurityConfig) { super(); this.authService = new AuthenticationService(config.auth); this.authzService = new AuthorizationService(); this.zeroTrust = new ZeroTrustFramework({ policies: new Map(), riskEngine: { enabled: true, threshold: config.zeroTrust.riskThreshold, factors: ['location', 'device', 'behavior', 'network'] }, adaptiveAuth: { enabled: config.zeroTrust.adaptiveAuthentication, riskBasedMFA: true, continuousVerification: true }, monitoring: { enabled: config.monitoring.enabled, realTime: true, alerting: true } }); this.metrics = this.initializeMetrics(); this.setupEventHandlers(); this.startMetricsCollection(); } async initialize(): Promise<void> { try { await this.zeroTrust.initialize(); this.emit('securityInitialized', { timestamp: new Date() }); } catch (error) { this.emit('error', { operation: 'initialize', error }); throw error; } } async authenticateUser(request: any): Promise<any> { try { // Pre-authentication risk assessment const riskAssessment = await this.zeroTrust.evaluateRisk({ userId: request.email, resource: 'authentication', action: 'login', context: { ipAddress: request.ipAddress, userAgent: request.userAgent, deviceId: request.deviceId } }); // Adjust authentication requirements based on risk if (riskAssessment.riskScore > this.config.zeroTrust.riskThreshold) { request.requireStrongMFA = true; this.recordEvent({ type: 'threat', severity: 'medium', details: { operation: 'authentication', riskScore: riskAssessment.riskScore, reasons: riskAssessment.reasons } }); } const authResult = await this.authService.authenticate(request); // Update metrics this.metrics.authentication.totalLogins++; if (authResult.success) { this.metrics.authentication.successfulLogins++; } else { this.metrics.authentication.failedLogins++; } // Post-authentication verification if (authResult.success && this.config.zeroTrust.enabled) { const postAuthVerification = await this.zeroTrust.verifyAccess({ userId: authResult.user!.id, resource: 'system', action: 'access', context: { sessionId: authResult.token?.accessToken, timestamp: new Date() } }); if (!postAuthVerification.allowed) { this.recordEvent({ type: 'authorization', severity: 'high', details: { operation: 'post-auth-verification', userId: authResult.user!.id, reason: postAuthVerification.reason } }); return { success: false, error: 'Access denied by security policy' }; } } return authResult; } catch (error) { this.emit('error', { operation: 'authenticateUser', error }); throw error; } } async authorizeAccess(request: any): Promise<any> { try { // Zero Trust evaluation const zeroTrustResult = await this.zeroTrust.verifyAccess(request); if (!zeroTrustResult.allowed) { this.metrics.authorization.deniedRequests++; this.recordEvent({ type: 'authorization', severity: 'medium', details: { operation: 'access-denied', userId: request.userId, resource: request.resource, reason: zeroTrustResult.reason } }); return { granted: false, reason: zeroTrustResult.reason }; } // Standard authorization check const authzResult = await this.authzService.checkAccess(request); // Update metrics this.metrics.authorization.totalAccessRequests++; if (authzResult.granted) { this.metrics.authorization.grantedRequests++; } else { this.metrics.authorization.deniedRequests++; } // Continuous monitoring if (authzResult.granted && this.config.zeroTrust.enabled) { this.startContinuousMonitoring(request.userId, request.resource); } return authzResult; } catch (error) { this.emit('error', { operation: 'authorizeAccess', error }); throw error; } } async handleSecurityIncident(event: SecurityEvent): Promise<void> { try { const incident: IncidentResponse = { id: this.generateId(), type: event.type, severity: event.severity, status: 'open', createdAt: new Date(), actions: [], affectedUsers: event.userId ? [event.userId] : [], affectedResources: event.resource ? [event.resource] : [] }; // Automated response based on severity if (event.severity === 'critical' || event.severity === 'high') { await this.executeAutomatedResponse(incident, event); } this.incidents.set(incident.id, incident); this.emit('incidentCreated', { incident, event }); // Notify security team if (this.config.monitoring.incidentResponse) { this.notifySecurityTeam(incident); } } catch (error) { this.emit('error', { operation: 'handleSecurityIncident', error }); } } async generateComplianceReport(framework: string): Promise<any> { try { const report = { framework, timestamp: new Date(), period: { start: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000), // 30 days ago end: new Date() }, metrics: this.metrics, events: Array.from(this.events.values()).filter(e => e.type === 'compliance' || e.type === 'audit' ), violations: this.getComplianceViolations(framework), recommendations: this.generateComplianceRecommendations(framework) }; this.emit('complianceReportGenerated', { framework, report }); return report; } catch (error) { this.emit('error', { operation: 'generateComplianceReport', error }); throw error; } } getSecurityMetrics(): SecurityMetrics { return { ...this.metrics }; } getSecurityEvents(filter?: Partial<SecurityEvent>): SecurityEvent[] { let events = Array.from(this.events.values()); if (filter) { events = events.filter(event => { return Object.entries(filter).every(([key, value]) => event[key as keyof SecurityEvent] === value ); }); } return events.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime()); } getActiveIncidents(): IncidentResponse[] { return Array.from(this.incidents.values()) .filter(incident => incident.status !== 'resolved') .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime()); } private async executeAutomatedResponse(incident: IncidentResponse, event: SecurityEvent): Promise<void> { const actions: IncidentAction[] = []; // Block suspicious IPs if (event.details.ipAddress && event.severity === 'critical') { actions.push({ id: this.generateId(), type: 'block', description: `Block IP address ${event.details.ipAddress}`, timestamp: new Date(), automated: true }); } // Revoke user sessions for high-risk activities if (event.userId && event.severity === 'high') { actions.push({ id: this.generateId(), type: 'revoke', description: `Revoke active sessions for user ${event.userId}`, timestamp: new Date(), automated: true }); } // Quarantine affected resources if (event.resource && event.type === 'threat') { actions.push({ id: this.generateId(), type: 'quarantine', description: `Quarantine resource ${event.resource}`, timestamp: new Date(), automated: true }); } incident.actions.push(...actions); incident.status = 'mitigating'; // Execute actions for (const action of actions) { try { await this.executeIncidentAction(action); action.result = 'success'; } catch (error) { action.result = `failed: ${error.message}`; } } } private async executeIncidentAction(action: IncidentAction): Promise<void> { switch (action.type) { case 'block': // Implement IP blocking logic break; case 'revoke': // Implement session revocation logic break; case 'quarantine': // Implement resource quarantine logic break; default: break; } } private recordEvent(eventData: Partial<SecurityEvent>): void { const event: SecurityEvent = { id: this.generateId(), timestamp: new Date(), resolved: false, ...eventData } as SecurityEvent; this.events.set(event.id, event); this.emit('securityEvent', { event }); // Trigger incident response for high severity events if (event.severity === 'high' || event.severity === 'critical') { this.handleSecurityIncident(event); } } private startContinuousMonitoring(userId: string, resource: string): void { // Implement continuous monitoring logic // This would track user behavior and resource access patterns } private notifySecurityTeam(incident: IncidentResponse): void { // Implement security team notification logic this.emit('securityAlert', { incident }); } private getComplianceViolations(framework: string): any[] { // Return violations specific to the compliance framework return Array.from(this.events.values()) .filter(event => event.type === 'compliance') .map(event => ({ eventId: event.id, violation: event.details.violation, framework, timestamp: event.timestamp })); } private generateComplianceRecommendations(framework: string): string[] { const recommendations: string[] = []; if (this.metrics.authentication.failedLogins > 100) { recommendations.push('Consider implementing stronger password policies'); } if (this.metrics.threats.riskScore > 0.7) { recommendations.push('Review and update threat detection rules'); } return recommendations; } private setupEventHandlers(): void { this.authService.on('authenticationFailed', (data) => { this.recordEvent({ type: 'authentication', severity: 'medium', userId: data.userId, details: data }); }); this.authService.on('accountLocked', (data) => { this.recordEvent({ type: 'authentication', severity: 'high', userId: data.userId, details: data }); this.metrics.authentication.lockedAccounts++; }); this.authzService.on('accessDenied', (data) => { this.recordEvent({ type: 'authorization', severity: 'medium', userId: data.request.userId, resource: data.request.resource, details: data }); }); this.zeroTrust.on('threatDetected', (data) => { this.recordEvent({ type: 'threat', severity: data.severity || 'medium', userId: data.userId, details: data }); this.metrics.threats.detectedThreats++; }); } private startMetricsCollection(): void { setInterval(() => { this.updateThreatMetrics(); this.cleanupOldEvents(); }, 5 * 60 * 1000); // Every 5 minutes } private updateThreatMetrics(): void { const recentEvents = Array.from(this.events.values()) .filter(event => event.timestamp > new Date(Date.now() - 24 * 60 * 60 * 1000)); this.metrics.threats.suspiciousActivities = recentEvents .filter(event => event.type === 'threat').length; // Calculate overall risk score const riskFactors = recentEvents.length / 100; // Simplified calculation this.metrics.threats.riskScore = Math.min(riskFactors, 1); } private cleanupOldEvents(): void { const cutoff = new Date(Date.now() - this.config.compliance.dataRetention * 24 * 60 * 60 * 1000); for (const [id, event] of this.events.entries()) { if (event.timestamp < cutoff) { this.events.delete(id); } } } private initializeMetrics(): SecurityMetrics { return { authentication: { totalLogins: 0, successfulLogins: 0, failedLogins: 0, lockedAccounts: 0, mfaUsage: 0 }, authorization: { totalAccessRequests: 0, grantedRequests: 0, deniedRequests: 0, policyViolations: 0 }, threats: { detectedThreats: 0, blockedAttacks: 0, suspiciousActivities: 0, riskScore: 0 }, compliance: { auditEvents: 0, violations: 0, frameworks: this.config.compliance.frameworks, lastAudit: new Date() } }; } private generateId(): string { return Math.random().toString(36).substring(2, 15); } }

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Coder-RL/Claude_MCPServer_Dev1'

If you have feedback or need assistance with the MCP directory API, please join our Discord server