|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2026-Present Datadog, Inc. |
| 5 | + |
| 6 | +package handling |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/DataDog/datadog-serverless-functions/aws/logs_monitoring_go/internal/testutil" |
| 13 | + "github.com/google/go-cmp/cmp" |
| 14 | +) |
| 15 | + |
| 16 | +func TestCloudTrailRegex(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + tests := map[string]struct { |
| 20 | + key string |
| 21 | + want bool |
| 22 | + }{ |
| 23 | + "standard cloudtrail": { |
| 24 | + key: "601427279990_CloudTrail_us-east-1_20210503T0000Z_QrttGEk4ZcBTLwj5.json.gz", |
| 25 | + want: true, |
| 26 | + }, |
| 27 | + "cloudtrail digest": { |
| 28 | + key: "601427279990_CloudTrail-Digest_us-east-1_20210503T0000Z_digest.json.gz", |
| 29 | + want: true, |
| 30 | + }, |
| 31 | + "cloudtrail insight": { |
| 32 | + key: "601427279990_CloudTrail-Insight_us-east-1_20210503T0000Z_insight.json.gz", |
| 33 | + want: true, |
| 34 | + }, |
| 35 | + "gov region": { |
| 36 | + key: "601427279990_CloudTrail_us-gov-west-1_20210503T0000Z_abc.json.gz", |
| 37 | + want: true, |
| 38 | + }, |
| 39 | + "cn region": { |
| 40 | + key: "601427279990_CloudTrail_cn-north-1_20210503T0000Z_abc.json.gz", |
| 41 | + want: true, |
| 42 | + }, |
| 43 | + "not cloudtrail": { |
| 44 | + key: "some-random-log-file.json.gz", |
| 45 | + want: false, |
| 46 | + }, |
| 47 | + "waf log": { |
| 48 | + key: "aws-waf-logs-something.json.gz", |
| 49 | + want: false, |
| 50 | + }, |
| 51 | + "plain text file": { |
| 52 | + key: "access.log", |
| 53 | + want: false, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + for name, tc := range tests { |
| 58 | + t.Run(name, func(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + got := cloudTrailRegex().MatchString(tc.key) |
| 61 | + if got != tc.want { |
| 62 | + t.Errorf("cloudTrailRegex().MatchString(%q) = %v, want %v", tc.key, got, tc.want) |
| 63 | + } |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestCloudtrailHost(t *testing.T) { |
| 69 | + t.Parallel() |
| 70 | + |
| 71 | + tests := map[string]struct { |
| 72 | + record map[string]any |
| 73 | + want string |
| 74 | + }{ |
| 75 | + "ec2 instance (17)": { |
| 76 | + record: map[string]any{ |
| 77 | + "userIdentity": map[string]any{ |
| 78 | + "arn": "arn:aws:sts::601427279990:assumed-role/MyRole/i-08014e4f62ccf762d", |
| 79 | + }, |
| 80 | + }, |
| 81 | + want: "i-08014e4f62ccf762d", |
| 82 | + }, |
| 83 | + "ec2 instance (8)": { |
| 84 | + record: map[string]any{ |
| 85 | + "userIdentity": map[string]any{ |
| 86 | + "arn": "arn:aws:sts::601427279990:assumed-role/MyRole/i-abcd1234", |
| 87 | + }, |
| 88 | + }, |
| 89 | + want: "i-abcd1234", |
| 90 | + }, |
| 91 | + "non ec2 arn": { |
| 92 | + record: map[string]any{ |
| 93 | + "userIdentity": map[string]any{ |
| 94 | + "arn": "arn:aws:sts::601427279990:assumed-role/MyRole/my-session", |
| 95 | + }, |
| 96 | + }, |
| 97 | + want: "", |
| 98 | + }, |
| 99 | + "missing userIdentity": { |
| 100 | + record: map[string]any{"eventName": "DescribeTable"}, |
| 101 | + want: "", |
| 102 | + }, |
| 103 | + "missing arn": { |
| 104 | + record: map[string]any{ |
| 105 | + "userIdentity": map[string]any{ |
| 106 | + "type": "AssumedRole", |
| 107 | + }, |
| 108 | + }, |
| 109 | + want: "", |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + for name, tc := range tests { |
| 114 | + t.Run(name, func(t *testing.T) { |
| 115 | + t.Parallel() |
| 116 | + host := cloudtrailHost(tc.record) |
| 117 | + if host != tc.want { |
| 118 | + t.Errorf("want %q, got %q", tc.want, host) |
| 119 | + } |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestDecodeCloudTrail(t *testing.T) { |
| 125 | + t.Parallel() |
| 126 | + |
| 127 | + tests := map[string]struct { |
| 128 | + input []byte |
| 129 | + want []s3Record |
| 130 | + wantErr bool |
| 131 | + }{ |
| 132 | + "single record with ec2 host": { |
| 133 | + input: testutil.MustGzipJSON(t, map[string]any{ |
| 134 | + "Records": []any{ |
| 135 | + map[string]any{ |
| 136 | + "eventName": "DescribeTable", |
| 137 | + "userIdentity": map[string]any{ |
| 138 | + "arn": "arn:aws:sts::601427279990:assumed-role/MyRole/i-08014e4f62ccf762d", |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }), |
| 143 | + want: []s3Record{ |
| 144 | + { |
| 145 | + Message: `{"eventName":"DescribeTable","userIdentity":{"arn":"arn:aws:sts::601427279990:assumed-role/MyRole/i-08014e4f62ccf762d"}}`, |
| 146 | + Host: "i-08014e4f62ccf762d", |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + "single record without ec2 host": { |
| 151 | + input: testutil.MustGzipJSON(t, map[string]any{ |
| 152 | + "Records": []any{ |
| 153 | + map[string]any{ |
| 154 | + "eventName": "DescribeTable", |
| 155 | + "userIdentity": map[string]any{ |
| 156 | + "arn": "arn:aws:iam::601427279990:user/admin", |
| 157 | + }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + }), |
| 161 | + want: []s3Record{ |
| 162 | + { |
| 163 | + Message: `{"eventName":"DescribeTable","userIdentity":{"arn":"arn:aws:iam::601427279990:user/admin"}}`, |
| 164 | + Host: "", |
| 165 | + }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + "multiple records": { |
| 169 | + input: testutil.MustGzipJSON(t, map[string]any{ |
| 170 | + "Records": []any{ |
| 171 | + map[string]any{"eventName": "event1"}, |
| 172 | + map[string]any{"eventName": "event2"}, |
| 173 | + }, |
| 174 | + }), |
| 175 | + want: []s3Record{ |
| 176 | + {Message: `{"eventName":"event1"}`}, |
| 177 | + {Message: `{"eventName":"event2"}`}, |
| 178 | + }, |
| 179 | + }, |
| 180 | + "empty records array": { |
| 181 | + input: testutil.MustGzipJSON(t, map[string]any{ |
| 182 | + "Records": []any{}, |
| 183 | + }), |
| 184 | + want: nil, |
| 185 | + }, |
| 186 | + "invalid gzip": { |
| 187 | + input: []byte("not gzip"), |
| 188 | + wantErr: true, |
| 189 | + }, |
| 190 | + "invalid json": { |
| 191 | + input: testutil.MustGzipJSON(t, "not an object"), |
| 192 | + wantErr: true, |
| 193 | + }, |
| 194 | + } |
| 195 | + |
| 196 | + for name, tc := range tests { |
| 197 | + t.Run(name, func(t *testing.T) { |
| 198 | + t.Parallel() |
| 199 | + |
| 200 | + var got []s3Record |
| 201 | + for rec := range decodeCloudTrail(bytes.NewReader(tc.input)) { |
| 202 | + if rec.Err != nil { |
| 203 | + if !tc.wantErr { |
| 204 | + t.Fatalf("unexpected error: %v", rec.Err) |
| 205 | + } |
| 206 | + return |
| 207 | + } |
| 208 | + got = append(got, rec) |
| 209 | + } |
| 210 | + |
| 211 | + if tc.wantErr { |
| 212 | + t.Fatal("expected error, got none") |
| 213 | + } |
| 214 | + if diff := cmp.Diff(tc.want, got); diff != "" { |
| 215 | + t.Errorf("mismatch (-want +got):\n%s", diff) |
| 216 | + } |
| 217 | + }) |
| 218 | + } |
| 219 | +} |
0 commit comments