|
| 1 | +const mockTrailSend = jest.fn(); |
| 2 | +const mockLogsSend = jest.fn(); |
| 3 | +const mockCwSend = jest.fn(); |
| 4 | + |
| 5 | +jest.mock('@aws-sdk/client-cloudtrail', () => ({ |
| 6 | + CloudTrailClient: jest.fn(() => ({ send: mockTrailSend })), |
| 7 | + DescribeTrailsCommand: jest.fn((input: unknown) => ({ |
| 8 | + _cmd: 'DescribeTrails', |
| 9 | + input, |
| 10 | + })), |
| 11 | +})); |
| 12 | +jest.mock('@aws-sdk/client-cloudwatch-logs', () => ({ |
| 13 | + CloudWatchLogsClient: jest.fn(() => ({ send: mockLogsSend })), |
| 14 | + DescribeMetricFiltersCommand: jest.fn((input: unknown) => ({ |
| 15 | + _cmd: 'DescribeMetricFilters', |
| 16 | + input, |
| 17 | + })), |
| 18 | +})); |
| 19 | +jest.mock('@aws-sdk/client-cloudwatch', () => ({ |
| 20 | + CloudWatchClient: jest.fn(() => ({ send: mockCwSend })), |
| 21 | + DescribeAlarmsForMetricCommand: jest.fn((input: unknown) => ({ |
| 22 | + _cmd: 'DescribeAlarmsForMetric', |
| 23 | + input, |
| 24 | + })), |
| 25 | +})); |
| 26 | + |
| 27 | +import { CloudWatchAdapter, logGroupNameFromArn } from './cloudwatch.adapter'; |
| 28 | + |
| 29 | +const CREDS = { |
| 30 | + accessKeyId: 'AKIA', |
| 31 | + secretAccessKey: 'secret', |
| 32 | + sessionToken: 'token', |
| 33 | +}; |
| 34 | + |
| 35 | +const scan = () => |
| 36 | + new CloudWatchAdapter().scan({ credentials: CREDS, region: 'us-east-1' }); |
| 37 | + |
| 38 | +beforeEach(() => { |
| 39 | + jest.clearAllMocks(); |
| 40 | + mockCwSend.mockResolvedValue({ MetricAlarms: [] }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe('logGroupNameFromArn', () => { |
| 44 | + it('derives the bare name and strips the trailing :*', () => { |
| 45 | + expect( |
| 46 | + logGroupNameFromArn( |
| 47 | + 'arn:aws:logs:us-east-1:123456789012:log-group:aws-cloudtrail-logs-xyz:*', |
| 48 | + ), |
| 49 | + ).toBe('aws-cloudtrail-logs-xyz'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('handles an ARN without a trailing :*', () => { |
| 53 | + expect( |
| 54 | + logGroupNameFromArn('arn:aws:logs:us-east-1:123:log-group:my-lg'), |
| 55 | + ).toBe('my-lg'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('handles the GovCloud partition', () => { |
| 59 | + expect( |
| 60 | + logGroupNameFromArn( |
| 61 | + 'arn:aws-us-gov:logs:us-gov-west-1:123:log-group:ct-logs:*', |
| 62 | + ), |
| 63 | + ).toBe('ct-logs'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns null for a missing or malformed ARN', () => { |
| 67 | + expect(logGroupNameFromArn(undefined)).toBeNull(); |
| 68 | + expect(logGroupNameFromArn('not-an-arn')).toBeNull(); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('CloudWatchAdapter — CloudTrail log group resolution', () => { |
| 73 | + it('injects the real log group name into the metric-filter-missing finding (the customer bug)', async () => { |
| 74 | + mockTrailSend.mockResolvedValue({ |
| 75 | + trailList: [ |
| 76 | + { |
| 77 | + Name: 'main', |
| 78 | + CloudWatchLogsLogGroupArn: |
| 79 | + 'arn:aws:logs:us-east-1:123456789012:log-group:my-ct-logs:*', |
| 80 | + }, |
| 81 | + ], |
| 82 | + }); |
| 83 | + mockLogsSend.mockResolvedValue({ metricFilters: [] }); // nothing configured |
| 84 | + |
| 85 | + const findings = await scan(); |
| 86 | + const missing = findings.find((f) => |
| 87 | + f.title.includes('metric filter missing'), |
| 88 | + ); |
| 89 | + |
| 90 | + expect(missing).toBeDefined(); |
| 91 | + expect(missing!.evidence?.cloudWatchLogGroupName).toBe('my-ct-logs'); |
| 92 | + expect(missing!.remediation).toContain('logGroupName set to "my-ct-logs"'); |
| 93 | + // The old generic phrasing that forced the AI to guess must be gone. |
| 94 | + expect(missing!.remediation).not.toContain( |
| 95 | + 'logGroupName set to the CloudTrail log group', |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('uses the existing filter\'s own log group for the no-transformation update', async () => { |
| 100 | + mockTrailSend.mockResolvedValue({ |
| 101 | + trailList: [ |
| 102 | + { |
| 103 | + Name: 'main', |
| 104 | + CloudWatchLogsLogGroupArn: |
| 105 | + 'arn:aws:logs:us-east-1:123:log-group:my-ct-logs:*', |
| 106 | + }, |
| 107 | + ], |
| 108 | + }); |
| 109 | + // A filter matching CIS 4.3 (Root account usage) keywords, but with no |
| 110 | + // metric transformation → "no metric transformation" finding. |
| 111 | + mockLogsSend.mockResolvedValue({ |
| 112 | + metricFilters: [ |
| 113 | + { |
| 114 | + filterName: 'root-filter', |
| 115 | + filterPattern: '{ $.userIdentity.type = "Root" }', |
| 116 | + logGroupName: 'existing-lg', |
| 117 | + metricTransformations: [], |
| 118 | + }, |
| 119 | + ], |
| 120 | + }); |
| 121 | + |
| 122 | + const findings = await scan(); |
| 123 | + const noTransform = findings.find((f) => |
| 124 | + f.title.includes('no metric transformation'), |
| 125 | + ); |
| 126 | + |
| 127 | + expect(noTransform).toBeDefined(); |
| 128 | + expect(noTransform!.evidence?.logGroupName).toBe('existing-lg'); |
| 129 | + expect(noTransform!.remediation).toContain('logGroupName set to "existing-lg"'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('returns the prerequisite finding when no trail integrates with CloudWatch Logs', async () => { |
| 133 | + mockTrailSend.mockResolvedValue({ |
| 134 | + trailList: [{ Name: 'main' }], // no CloudWatchLogsLogGroupArn |
| 135 | + }); |
| 136 | + |
| 137 | + const findings = await scan(); |
| 138 | + expect(findings).toHaveLength(1); |
| 139 | + expect(findings[0].title).toMatch(/not integrated with CloudWatch Logs/i); |
| 140 | + }); |
| 141 | + |
| 142 | + it('falls back to a generic instruction when DescribeTrails is denied', async () => { |
| 143 | + // DescribeTrails throws (e.g. missing cloudtrail:DescribeTrails) -> the log |
| 144 | + // group stays unknown, so the finding keeps the generic text rather than a |
| 145 | + // wrong name. (Not the customer's case, but must not crash or fabricate.) |
| 146 | + mockTrailSend.mockRejectedValue(new Error('AccessDenied')); |
| 147 | + mockLogsSend.mockResolvedValue({ metricFilters: [] }); |
| 148 | + |
| 149 | + const findings = await scan(); |
| 150 | + const missing = findings.find((f) => |
| 151 | + f.title.includes('metric filter missing'), |
| 152 | + ); |
| 153 | + expect(missing).toBeDefined(); |
| 154 | + expect(missing!.evidence?.cloudWatchLogGroupName).toBeUndefined(); |
| 155 | + expect(missing!.remediation).toContain("CloudWatch Logs log group"); |
| 156 | + }); |
| 157 | +}); |
0 commit comments