| 
									
										
										
										
											2022-02-19 23:45:36 -01:00
										 |  |  | import boto3 | 
					
						
							|  |  |  | import pytest | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from moto import mock_cloudtrail, mock_s3 | 
					
						
							| 
									
										
										
										
											2022-08-13 09:49:43 +00:00
										 |  |  | from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID | 
					
						
							| 
									
										
										
										
											2022-02-19 23:45:36 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | from .test_cloudtrail import create_trail_simple | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_cloudtrail | 
					
						
							|  |  |  | @mock_s3 | 
					
						
							|  |  |  | def test_put_event_selectors(): | 
					
						
							|  |  |  |     client = boto3.client("cloudtrail", region_name="eu-west-1") | 
					
						
							|  |  |  |     _, _, trail_name = create_trail_simple(region_name="eu-west-1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.put_event_selectors( | 
					
						
							|  |  |  |         TrailName=trail_name, | 
					
						
							|  |  |  |         EventSelectors=[ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "ReadWriteType": "All", | 
					
						
							|  |  |  |                 "IncludeManagementEvents": True, | 
					
						
							|  |  |  |                 "DataResources": [ | 
					
						
							|  |  |  |                     {"Type": "AWS::S3::Object", "Values": ["arn:aws:s3:::*/*"]} | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-06 14:48:33 +00:00
										 |  |  |     assert "TrailARN" in resp | 
					
						
							|  |  |  |     assert resp["EventSelectors"] == [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "ReadWriteType": "All", | 
					
						
							|  |  |  |             "IncludeManagementEvents": True, | 
					
						
							|  |  |  |             "DataResources": [ | 
					
						
							|  |  |  |                 {"Type": "AWS::S3::Object", "Values": ["arn:aws:s3:::*/*"]} | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     assert "AdvancedEventSelectors" not in resp | 
					
						
							| 
									
										
										
										
											2022-02-19 23:45:36 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_cloudtrail | 
					
						
							|  |  |  | @mock_s3 | 
					
						
							|  |  |  | def test_put_event_selectors_advanced(): | 
					
						
							|  |  |  |     client = boto3.client("cloudtrail", region_name="eu-west-1") | 
					
						
							|  |  |  |     _, _, trail_name = create_trail_simple(region_name="eu-west-1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.put_event_selectors( | 
					
						
							|  |  |  |         TrailName=trail_name, | 
					
						
							|  |  |  |         EventSelectors=[ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "ReadWriteType": "All", | 
					
						
							|  |  |  |                 "IncludeManagementEvents": True, | 
					
						
							|  |  |  |                 "DataResources": [ | 
					
						
							|  |  |  |                     {"Type": "AWS::S3::Object", "Values": ["arn:aws:s3:::*/*"]} | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         AdvancedEventSelectors=[ | 
					
						
							|  |  |  |             {"Name": "aes1", "FieldSelectors": [{"Field": "f", "Equals": ["fs1"]}]} | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-06 14:48:33 +00:00
										 |  |  |     assert "TrailARN" in resp | 
					
						
							|  |  |  |     assert resp["EventSelectors"] == [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "ReadWriteType": "All", | 
					
						
							|  |  |  |             "IncludeManagementEvents": True, | 
					
						
							|  |  |  |             "DataResources": [ | 
					
						
							|  |  |  |                 {"Type": "AWS::S3::Object", "Values": ["arn:aws:s3:::*/*"]} | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     assert resp["AdvancedEventSelectors"] == [ | 
					
						
							|  |  |  |         {"Name": "aes1", "FieldSelectors": [{"Field": "f", "Equals": ["fs1"]}]} | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2022-02-19 23:45:36 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_cloudtrail | 
					
						
							|  |  |  | @mock_s3 | 
					
						
							|  |  |  | def test_get_event_selectors_empty(): | 
					
						
							|  |  |  |     client = boto3.client("cloudtrail", region_name="ap-southeast-1") | 
					
						
							|  |  |  |     _, _, trail_name = create_trail_simple(region_name="ap-southeast-1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.get_event_selectors(TrailName=trail_name) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-06 14:48:33 +00:00
										 |  |  |     assert ( | 
					
						
							|  |  |  |         resp["TrailARN"] | 
					
						
							|  |  |  |         == f"arn:aws:cloudtrail:ap-southeast-1:{ACCOUNT_ID}:trail/{trail_name}" | 
					
						
							| 
									
										
										
										
											2022-02-19 23:45:36 -01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2023-07-06 14:48:33 +00:00
										 |  |  |     assert resp["EventSelectors"] == [] | 
					
						
							|  |  |  |     assert resp["AdvancedEventSelectors"] == [] | 
					
						
							| 
									
										
										
										
											2022-02-19 23:45:36 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_cloudtrail | 
					
						
							|  |  |  | @mock_s3 | 
					
						
							|  |  |  | def test_get_event_selectors(): | 
					
						
							|  |  |  |     client = boto3.client("cloudtrail", region_name="ap-southeast-2") | 
					
						
							|  |  |  |     _, _, trail_name = create_trail_simple(region_name="ap-southeast-2") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     client.put_event_selectors( | 
					
						
							|  |  |  |         TrailName=trail_name, | 
					
						
							|  |  |  |         EventSelectors=[ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "ReadWriteType": "All", | 
					
						
							|  |  |  |                 "IncludeManagementEvents": False, | 
					
						
							|  |  |  |                 "DataResources": [ | 
					
						
							|  |  |  |                     {"Type": "AWS::S3::Object", "Values": ["arn:aws:s3:::*/*"]} | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.get_event_selectors(TrailName=trail_name) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-06 14:48:33 +00:00
										 |  |  |     assert ( | 
					
						
							|  |  |  |         resp["TrailARN"] | 
					
						
							|  |  |  |         == f"arn:aws:cloudtrail:ap-southeast-2:{ACCOUNT_ID}:trail/{trail_name}" | 
					
						
							| 
									
										
										
										
											2022-02-19 23:45:36 -01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2023-07-06 14:48:33 +00:00
										 |  |  |     assert resp["EventSelectors"] == [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "ReadWriteType": "All", | 
					
						
							|  |  |  |             "IncludeManagementEvents": False, | 
					
						
							|  |  |  |             "DataResources": [ | 
					
						
							|  |  |  |                 {"Type": "AWS::S3::Object", "Values": ["arn:aws:s3:::*/*"]} | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2022-02-19 23:45:36 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_cloudtrail | 
					
						
							|  |  |  | @mock_s3 | 
					
						
							|  |  |  | def test_get_event_selectors_multiple(): | 
					
						
							|  |  |  |     client = boto3.client("cloudtrail", region_name="ap-southeast-1") | 
					
						
							|  |  |  |     _, _, trail_name = create_trail_simple(region_name="ap-southeast-1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     client.put_event_selectors( | 
					
						
							|  |  |  |         TrailName=trail_name, | 
					
						
							|  |  |  |         EventSelectors=[ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "ReadWriteType": "All", | 
					
						
							|  |  |  |                 "IncludeManagementEvents": False, | 
					
						
							|  |  |  |                 "DataResources": [ | 
					
						
							|  |  |  |                     {"Type": "AWS::S3::Object", "Values": ["arn:aws:s3:::*/*"]} | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     client.put_event_selectors( | 
					
						
							|  |  |  |         TrailName=trail_name, | 
					
						
							|  |  |  |         AdvancedEventSelectors=[ | 
					
						
							|  |  |  |             {"Name": "aes1", "FieldSelectors": [{"Field": "f", "Equals": ["fs1"]}]} | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.get_event_selectors(TrailName=trail_name) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-06 14:48:33 +00:00
										 |  |  |     assert "TrailARN" in resp | 
					
						
							| 
									
										
										
										
											2022-02-19 23:45:36 -01:00
										 |  |  |     # Setting advanced selectors cancels any existing event selectors | 
					
						
							| 
									
										
										
										
											2023-07-06 14:48:33 +00:00
										 |  |  |     assert resp["EventSelectors"] == [] | 
					
						
							|  |  |  |     assert resp["AdvancedEventSelectors"] == [ | 
					
						
							|  |  |  |         {"Name": "aes1", "FieldSelectors": [{"Field": "f", "Equals": ["fs1"]}]} | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2022-02-19 23:45:36 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_cloudtrail | 
					
						
							|  |  |  | @mock_s3 | 
					
						
							|  |  |  | @pytest.mark.parametrize("using_arn", [True, False]) | 
					
						
							|  |  |  | def test_put_insight_selectors(using_arn): | 
					
						
							|  |  |  |     client = boto3.client("cloudtrail", region_name="us-east-2") | 
					
						
							|  |  |  |     _, resp, trail_name = create_trail_simple(region_name="us-east-2") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.put_insight_selectors( | 
					
						
							|  |  |  |         TrailName=trail_name, InsightSelectors=[{"InsightType": "ApiCallRateInsight"}] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-06 14:48:33 +00:00
										 |  |  |     assert "TrailARN" in resp | 
					
						
							|  |  |  |     assert resp["InsightSelectors"] == [{"InsightType": "ApiCallRateInsight"}] | 
					
						
							| 
									
										
										
										
											2022-02-19 23:45:36 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if using_arn: | 
					
						
							|  |  |  |         trail_arn = resp["TrailARN"] | 
					
						
							|  |  |  |         resp = client.get_insight_selectors(TrailName=trail_arn) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         resp = client.get_insight_selectors(TrailName=trail_name) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-06 14:48:33 +00:00
										 |  |  |     assert "TrailARN" in resp | 
					
						
							|  |  |  |     assert resp["InsightSelectors"] == [{"InsightType": "ApiCallRateInsight"}] | 
					
						
							| 
									
										
										
										
											2022-02-19 23:45:36 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_cloudtrail | 
					
						
							|  |  |  | @mock_s3 | 
					
						
							|  |  |  | def test_get_insight_selectors(): | 
					
						
							|  |  |  |     client = boto3.client("cloudtrail", region_name="eu-west-1") | 
					
						
							|  |  |  |     _, resp, trail_name = create_trail_simple(region_name="eu-west-1") | 
					
						
							|  |  |  |     resp = client.get_insight_selectors(TrailName=trail_name) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-06 14:48:33 +00:00
										 |  |  |     assert "TrailARN" in resp | 
					
						
							|  |  |  |     assert "InsightSelectors" not in resp |