import { CloudTrailClient, LookupEventsCommand, LookupEventsCommandInput, LookupAttributeKey, LookupAttribute, Event } from '@aws-sdk/client-cloudtrail'; import { cloudTrailClient } from '../../config/aws-config.js'; import { getLogger } from '../../utils/logging.js'; import { handleAwsError } from '../../utils/error-handling.js'; const logger = getLogger(); /** * Interface for CloudTrail service */ export interface CloudTrailService { /** * Look up CloudTrail events with various filtering options */ lookupEvents(options: { eventName?: string; username?: string; resourceName?: string; resourceType?: string; startTime?: Date; endTime?: Date; limit?: number; }): Promise; } /** * Implementation of CloudTrail service */ export class CloudTrailServiceImpl implements CloudTrailService { private client: CloudTrailClient; constructor(client?: CloudTrailClient) { this.client = client || cloudTrailClient; } /** * Look up CloudTrail events with various filtering options * @param options Lookup options * @returns Array of CloudTrail events */ async lookupEvents(options: { eventName?: string; username?: string; resourceName?: string; resourceType?: string; startTime?: Date; endTime?: Date; limit?: number; }): Promise { try { const { eventName, username, resourceName, resourceType, startTime, endTime, limit } = options; logger.info('Looking up CloudTrail events', { eventName: eventName || 'any', username: username || 'any', resourceName: resourceName || 'any', resourceType: resourceType || 'any', startTime: startTime?.toISOString() || 'undefined', endTime: endTime?.toISOString() || 'undefined', limit: limit || 50 }); // Build lookup attributes const lookupAttributes: LookupAttribute[] = []; if (eventName) { lookupAttributes.push({ AttributeKey: LookupAttributeKey.EVENT_NAME, AttributeValue: eventName }); } if (username) { lookupAttributes.push({ AttributeKey: LookupAttributeKey.USERNAME, AttributeValue: username }); } if (resourceName) { lookupAttributes.push({ AttributeKey: LookupAttributeKey.RESOURCE_NAME, AttributeValue: resourceName }); } if (resourceType) { lookupAttributes.push({ AttributeKey: LookupAttributeKey.RESOURCE_TYPE, AttributeValue: resourceType }); } const params: LookupEventsCommandInput = { LookupAttributes: lookupAttributes.length > 0 ? lookupAttributes : undefined, StartTime: startTime, EndTime: endTime, MaxResults: limit || 50 }; const command = new LookupEventsCommand(params); const response = await this.client.send(command); const events = response.Events || []; logger.debug(`Found ${events.length} CloudTrail events`); return events; } catch (error) { logger.error('Error looking up CloudTrail events:', error); throw handleAwsError(error, 'CloudTrail', 'lookupEvents'); } } } /** * Create a new CloudTrail service instance * @param client Optional CloudTrail client * @returns CloudTrail service */ export function createCloudTrailService(client?: CloudTrailClient): CloudTrailService { return new CloudTrailServiceImpl(client); }