Class: Terrafying::Components::Security::Trail

Inherits:
Terrafying::Context
  • Object
show all
Defined in:
lib/terrafying/components/security/trail.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bucket_statements(bucket_name) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/terrafying/components/security/trail.rb', line 17

def self.bucket_statements(bucket_name)
  [
    {
      Sid: "AWSCloudTrailAclCheck",
      Effect: "Allow",
      Principal: {
        Service: "cloudtrail.amazonaws.com"
      },
      Action: "s3:GetBucketAcl",
      Resource: "arn:aws:s3:::#{bucket_name}"
    },
    {
      Sid: "AWSCloudTrailWrite",
      Effect: "Allow",
      Principal: {
        Service: "cloudtrail.amazonaws.com"
      },
      Action: "s3:PutObject",
      Resource: "arn:aws:s3:::#{bucket_name}/*",
      Condition: {
        StringEquals: {
          "s3:x-amz-acl" => "bucket-owner-full-control"
        }
      }
    }
  ]
end

.create(*args) ⇒ Object



13
14
15
# File 'lib/terrafying/components/security/trail.rb', line 13

def self.create(*args)
  Trail.new.create(*args)
end

.key_statementsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/terrafying/components/security/trail.rb', line 45

def self.key_statements
  [
    {
      Sid: "Allow CloudTrail to encrypt logs",
      Effect: "Allow",
      Principal: {"Service": ["cloudtrail.amazonaws.com"]},
      Action: "kms:GenerateDataKey*",
      Resource: "*",
      Condition: {"StringLike": {"kms:EncryptionContext:aws:cloudtrail:arn": "arn:aws:cloudtrail:*:#{aws.}:trail/*"}}
    },
    {
      Sid: "Allow CloudTrail to describe key",
      Effect: "Allow",
      Principal: {"Service": ["cloudtrail.amazonaws.com"]},
      Action: "kms:DescribeKey",
      Resource: "*"
    },
    {
      Sid: "Allow principals in the account to decrypt log files",
      Effect: "Allow",
      Principal: {"AWS": "*"},
      Action: [
        "kms:Decrypt",
        "kms:ReEncryptFrom"
      ],
      Resource: "*",
      Condition: {
        StringEquals: {"kms:CallerAccount": aws.},
        StringLike: {"kms:EncryptionContext:aws:cloudtrail:arn": "arn:aws:cloudtrail:*:#{aws.}:trail/*"}
      }
    },
  ]
end

Instance Method Details

#alert!(name:, pattern:, threshold: 1, topic: @topic) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/terrafying/components/security/trail.rb', line 261

def alert!(name:, pattern:, threshold: 1, topic: @topic)

  ident = "cloudwatch-#{@name}-#{name}"

  resource :aws_cloudwatch_log_metric_filter, ident, {
             name: name,
             pattern: pattern,
             log_group_name: @log_group["name"],

             metric_transformation: {
               name: "EventCount",
               namespace: "CloudTrail/#{name}",
               value: "1",
             },
           }

  resource :aws_cloudwatch_metric_alarm, ident, {
             alarm_name: name,
             comparison_operator: "GreaterThanOrEqualToThreshold",
             evaluation_periods: "1",
             metric_name: "EventCount",
             namespace: "CloudTrail/#{name}",
             period: "300",
             statistic: "Sum",
             threshold: "#{threshold}",
             alarm_actions: [ topic ],
           }

end

#basic_selectorObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/terrafying/components/security/trail.rb', line 179

def basic_selector
  {
    event_selector: [
      {
        read_write_type: "All",
        include_management_events: true,

        data_resource: {
          type: "AWS::S3::Object",
          values: ["arn:aws:s3:::"],
        }
      },
      {
        read_write_type: "All",
        include_management_events: true,

        data_resource: {
          type: "AWS::Lambda::Function",
          values: ["arn:aws:lambda"],
        },
      }
    ]
  }
end

#cis_benchmark!Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/terrafying/components/security/trail.rb', line 291

def cis_benchmark!

  # 3.1	Ensure a log metric filter and alarm exist for unauthorized API calls
  alert!(
    name: "UnauthorizedAPICalls",
    pattern: "{ ($.errorCode = \"*UnauthorizedOperation\") || ($.errorCode = \"AccessDenied*\") }",
  )

  # 3.2	Ensure a log metric filter and alarm exist for Management Console sign-in without MFA
  alert!(
    name: "NoMFAConsoleSignin",
    pattern: "{ ($.eventName = \"ConsoleLogin\") && ($.additionalEventData.MFAUsed != \"Yes\") }",
  )

  # 3.3	Ensure a log metric filter and alarm exist for usage of "root" account
  alert!(
    name: "RootUsage",
    pattern: "{ $.userIdentity.type = \"Root\" && $.userIdentity.invokedBy NOT EXISTS && $.eventType != \"AwsServiceEvent\" }",
  )

  # 3.4	Ensure a log metric filter and alarm exist for IAM policy changes
  alert!(
    name: "IAMChanges",
    pattern: "{($.eventName=DeleteGroupPolicy)||($.eventName=DeleteRolePolicy)||($.eventName=DeleteUserPolicy)||($.eventName=PutGroupPolicy)||($.eventName=PutRolePolicy)||($.eventName=PutUserPolicy)||($.eventName=CreatePolicy)||($.eventName=DeletePolicy)||($.eventName=CreatePolicyVersion)||($.eventName=DeletePolicyVersion)||($.eventName=AttachRolePolicy)||($.eventName=DetachRolePolicy)||($.eventName=AttachUserPolicy)||($.eventName=DetachUserPolicy)||($.eventName=AttachGroupPolicy)||($.eventName=DetachGroupPolicy)}",
  )

  # 3.5	Ensure a log metric filter and alarm exist for CloudTrail configuration changes
  alert!(
    name: "CloudTrailCfgChanges",
    pattern: "{ ($.eventName = CreateTrail) || ($.eventName = UpdateTrail) || ($.eventName = DeleteTrail) || ($.eventName = StartLogging) || ($.eventName = StopLogging) }",
  )

  # 3.6	Ensure a log metric filter and alarm exist for AWS Management Console authentication failures
  alert!(
    name: "ConsoleSigninFailures",
    pattern: "{ ($.eventName = ConsoleLogin) && ($.errorMessage = \"Failed authentication\") }",
  )

  # 3.7	Ensure a log metric filter and alarm exist for disabling or scheduled deletion of customer created CMKs
  alert!(
    name: "DisableOrDeleteCMK",
    pattern: "{ ($.eventSource = kms.amazonaws.com) && (($.eventName = DisableKey) || ($.eventName = ScheduleKeyDeletion)) }",
  )

  # 3.8	Ensure a log metric filter and alarm exist for S3 bucket policy changes
  alert!(
    name: "S3BucketPolicyChanges",
    pattern: "{ ($.eventSource = s3.amazonaws.com) && (($.eventName = PutBucketAcl) || ($.eventName = PutBucketPolicy) || ($.eventName = PutBucketCors) || ($.eventName = PutBucketLifecycle) || ($.eventName = PutBucketReplication) || ($.eventName = DeleteBucketPolicy) || ($.eventName = DeleteBucketCors) || ($.eventName = DeleteBucketLifecycle) || ($.eventName = DeleteBucketReplication)) }",
  )

  # 3.9	Ensure a log metric filter and alarm exist for AWS Config configuration changes
  alert!(
    name: "AWSConfigChanges",
    pattern: "{ ($.eventSource = config.amazonaws.com) && (($.eventName=StopConfigurationRecorder)||($.eventName=DeleteDeliveryChannel)||($.eventName=PutDeliveryChannel)||($.eventName=PutConfigurationRecorder)) }",
  )

  # 3.10	Ensure a log metric filter and alarm exist for security group changes
  alert!(
    name: "SecurityGroupChanges",
    pattern: "{ ($.eventName = AuthorizeSecurityGroupIngress) || ($.eventName = AuthorizeSecurityGroupEgress) || ($.eventName = RevokeSecurityGroupIngress) || ($.eventName = RevokeSecurityGroupEgress) || ($.eventName = CreateSecurityGroup) || ($.eventName = DeleteSecurityGroup)}",
  )

  # 3.11	Ensure a log metric filter and alarm exist for changes to Network Access Control Lists (NACL)
  alert!(
    name: "NACLChanges",
    pattern: "{ ($.eventName = CreateNetworkAcl) || ($.eventName = CreateNetworkAclEntry) || ($.eventName = DeleteNetworkAcl) || ($.eventName = DeleteNetworkAclEntry) || ($.eventName = ReplaceNetworkAclEntry) || ($.eventName = ReplaceNetworkAclAssociation) }",
  )

  # 3.12	Ensure a log metric filter and alarm exist for changes to network gateways
  alert!(
    name: "NetworkGWChanges",
    pattern: "{ ($.eventName = CreateCustomerGateway) || ($.eventName = DeleteCustomerGateway) || ($.eventName = AttachInternetGateway) || ($.eventName = CreateInternetGateway) || ($.eventName = DeleteInternetGateway) || ($.eventName = DetachInternetGateway) }",
  )

  # 3.13	Ensure a log metric filter and alarm exist for route table changes
  alert!(
    name: "RouteTableChanges",
    pattern: "{ ($.eventName = CreateRoute) || ($.eventName = CreateRouteTable) || ($.eventName = ReplaceRoute) || ($.eventName = ReplaceRouteTableAssociation) || ($.eventName = DeleteRouteTable) || ($.eventName = DeleteRoute) || ($.eventName = DisassociateRouteTable) }",
  )

  # 3.14	Ensure a log metric filter and alarm exist for VPC changes
  alert!(
    name: "VPCChanges",
    pattern: "{ ($.eventName = CreateVpc) || ($.eventName = DeleteVpc) || ($.eventName = ModifyVpcAttribute) || ($.eventName = AcceptVpcPeeringConnection) || ($.eventName = CreateVpcPeeringConnection) || ($.eventName = DeleteVpcPeeringConnection) || ($.eventName = RejectVpcPeeringConnection) || ($.eventName = AttachClassicLinkVpc) || ($.eventName = DetachClassicLinkVpc) || ($.eventName = DisableVpcClassicLink) || ($.eventName = EnableVpcClassicLink) }",
  )

end

#create(name, store:, topic:, include_all_regions: true, include_all_organisation: true, ignore_buckets: []) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/terrafying/components/security/trail.rb', line 79

def create(
      name,
      store:,
      topic:,
      include_all_regions: true,
      include_all_organisation: true,
      ignore_buckets: []
    )

  @name = name
  @topic = topic

  @log_group = resource :aws_cloudwatch_log_group, "cloudtrail-#{name}", {
                          name: "cloudtrail-#{name}",
                        }

  log_role = resource :aws_iam_role, "cloudtrail-#{name}-logs", {
                        name: "cloudtrail-#{name}-logs",
                        assume_role_policy: {
                          Version: "2012-10-17",
                          Statement: [
                            {
                              Sid: "",
                              Effect: "Allow",
                              Principal: {
                                Service: "cloudtrail.amazonaws.com"
                              },
                              Action: "sts:AssumeRole",
                            },
                          ],
                        }.to_json,
                      }

  log_role_policy = resource :aws_iam_policy, "cloudtrail-#{name}-logs", {
                               name: "cloudtrail-#{name}-logs",
                               policy: {
                                 Version: "2012-10-17",
                                 Statement: [
                                   {
                                     Sid: "AWSCloudTrailCreateLogStream2014110",
                                     Effect: "Allow",
                                     Action: [
                                       "logs:CreateLogStream"
                                     ],
                                     Resource: [
                                       "#{@log_group["arn"]}:*",
                                     ]
                                   },
                                   {
                                     Sid: "AWSCloudTrailPutLogEvents20141101",
                                     Effect: "Allow",
                                     Action: [
                                       "logs:PutLogEvents"
                                     ],
                                     Resource: [
                                       "#{@log_group["arn"]}:*",
                                     ]
                                   }
                                 ]
                               }.to_json
                             }

  resource :aws_iam_role_policy_attachment, "cloudtrail-#{name}-logs", {
             role: log_role["name"],
             policy_arn: log_role_policy["arn"],
           }

  data_event_selectors = event_selector(ignore_buckets)

  resource :aws_cloudtrail, "#{name}", {
             name: "#{name}",
             s3_bucket_name: store.name,
             s3_key_prefix: "cloudtrail",
             include_global_service_events: true,
             is_multi_region_trail: include_all_regions,
             is_organization_trail: include_all_organisation,
             enable_log_file_validation: true,
             kms_key_id: store.key_arn,

             cloud_watch_logs_group_arn: "#{@log_group["arn"]}:*",
             cloud_watch_logs_role_arn: log_role["arn"],

           }.deep_merge(data_event_selectors)
  self
end

#event_selector(buckets) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/terrafying/components/security/trail.rb', line 165

def event_selector(buckets)
  buckets = Array(buckets)

  return basic_selector if buckets.empty?

  {
    advanced_event_selector: [
      ignore_buckets_selectors(buckets),
      management_events_selector,
      lambda_events
    ]
  }
end

#ignore_buckets_selectors(buckets) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/terrafying/components/security/trail.rb', line 204

def ignore_buckets_selectors(buckets)
  ignore_bucket_arns = Array(buckets).map { |bucket|
    data_name = Digest::SHA256.hexdigest("#{@name}-#{bucket}")[0..16]
    arn = data(:aws_s3_bucket, "ct-ignore-#{data_name}", bucket: bucket)['arn']
    "#{arn}/"
  }

  {
    name: 'Log all S3 buckets objects events except these',

    field_selector: [
      {
        field: 'eventCategory',
        equals: ['Data']
      },
      {
        field: 'resources.type',
        equals: ['AWS::S3::Object']
      },
      {
        field: 'resources.ARN',
        not_starts_with: ignore_bucket_arns
      }
    ],
  }
end

#lambda_eventsObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/terrafying/components/security/trail.rb', line 244

def lambda_events
  {
    name: 'Log Lambda data events',

    field_selector: [
      {
        field: 'eventCategory',
        equals: ['Data']
      },
      {
        field: 'resources.type',
        equals: ['AWS::Lambda::Function']
      }
    ]
  }
end

#management_events_selectorObject



231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/terrafying/components/security/trail.rb', line 231

def management_events_selector
  {
    name: 'Log readOnly and writeOnly management events',

    field_selector: [
      {
        field: "eventCategory",
        equals: ["Management"]
      }
    ]
  }
end