-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonitorCrossAccountRoles.yml
233 lines (203 loc) · 8.46 KB
/
MonitorCrossAccountRoles.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
44
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
78
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
230
231
232
233
AWSTemplateFormatVersion: 2010-09-09
Parameters:
ManagementAccountId:
Type: Number
Description: Account id of management account.
ManagementAccountRole:
Type: String
Description: Name of the role which lambda function assumes to get all AWS account list.
AllowedPattern: "[a-zA-Z0-9_-]+"
Default: GetAWSAccountsRole
SNSTopicName:
Type: String
Description: Name of the SNS topic.
AllowedPattern: "[a-zA-Z0-9_-]+"
Default: MonitorCrossAccountRolesTopic
EmailList:
Type: String
Description: Email to notify.
AllowedPattern: "[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z]+"
Default: [email protected]
Resources:
MonitorCrossAccountRolesTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint: !Ref EmailList
Protocol: email
TopicName: !Sub "${SNSTopicName}"
MonitorCrossAccountRolesRole:
Type: AWS::IAM::Role
Properties:
RoleName: MonitorCrossAccountRolesRole
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Path: /
Policies:
- PolicyName: lambda-policy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: logs:CreateLogGroup
Resource: arn:aws:logs:us-east-1:*:*
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- arn:aws:logs:us-east-1:*:log-group:*:*
- Effect: Allow
Action: sns:Publish
Resource: "*"
- Effect: Allow
Action: sts:AssumeRole
Resource: !Sub "arn:aws:iam::${ManagementAccountId}:role/${ManagementAccountRole}"
MonitorCrossAccountRolesFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: MonitorCrossAccountRolesFunction
Description: Lambda function to monitor cross account roles.
Handler: index.lambda_handler
Runtime: python3.9
Role: !GetAtt MonitorCrossAccountRolesRole.Arn
Environment:
Variables:
ManagementAccountId: !Sub ${ManagementAccountId}
ManagementAccountRole: !Sub ${ManagementAccountRole}
TopicName: !Sub "${SNSTopicName}"
SecurityToolsAccountId: !Sub ${AWS::AccountId}
Code:
ZipFile: |
import json
import boto3
import logging
import os
ManagementAccountId = os.environ["ManagementAccountId"]
ManagementAccountRole = os.environ["ManagementAccountRole"]
TopicName = os.environ["TopicName"]
SecurityToolsAccountId=os.environ["SecurityToolsAccountId"]
topic_arn=f"arn:aws:sns:us-east-1:{SecurityToolsAccountId}:{TopicName}"
ROLE_SESSION_NAME = "MonitorCrossAccountRolesSession"
logger = logging.getLogger()
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
def get_role_details(request_parameters):
role = {}
role["name"] = request_parameters["roleName"]
if "assumeRolePolicyDocument" in request_parameters:
assume_role_policy_document=request_parameters["assumeRolePolicyDocument"]
if "policyDocument" in request_parameters:
assume_role_policy_document=request_parameters["policyDocument"]
assume_role_policy_document_dict=json.loads(assume_role_policy_document)
statements=assume_role_policy_document_dict["Statement"]
X_access = False
principal_list = []
for statement in statements:
principal = statement["Principal"]
if "AWS" in principal:
X_access = True
principal_list.append(principal["AWS"])
if "Federated" in principal:
X_access = True
principal_list.append(principal["Federated"])
if X_access:
X_account_ids=[]
for item in principal_list:
if ":" in item:
X_account_ids.append(item.split(":")[4])
else:
X_account_ids.append(item)
role["X_account_ids"] = X_account_ids
return role
def get_client(role_arn, service_name):
sts_client = boto3.client("sts")
response = sts_client.assume_role(
RoleArn=role_arn, RoleSessionName=ROLE_SESSION_NAME
)
temp_creds = response["Credentials"]
client = boto3.client(
service_name,
aws_access_key_id=temp_creds["AccessKeyId"],
aws_secret_access_key=temp_creds["SecretAccessKey"],
aws_session_token=temp_creds["SessionToken"],
)
return client
def get_aws_accounts(organizations):
aws_accounts = []
paginator = organizations.get_paginator("list_accounts")
response_iterator = paginator.paginate()
for response in response_iterator:
for account in response["Accounts"]:
aws_accounts.append(account["Id"])
return aws_accounts
def send_sns_notification(role_name, event_account_id, internal_ids, external_ids):
message1=''
message2=''
if internal_ids:
message1=f"Internal AWS accounts: {internal_ids}"
if external_ids:
message2=f"External THIRD-PARTY AWS accounts: {external_ids}"
message=f"""
#####
Cross account access was granted through role {role_name} in account: {event_account_id} to the following AWS account(s) -
{message1}
{message2}
#####
"""
client = boto3.client("sns")
response = client.publish(
TopicArn=topic_arn,
Message=message,
Subject="Monitor Cross Account Roles",
)
def lambda_handler(event, context):
try:
role_arn=f'arn:aws:iam::{ManagementAccountId}:role/{ManagementAccountRole}'
if "errorCode" not in event["detail"]:
request_parameters=event["detail"]["requestParameters"]
event_account_id=event["account"]
role=get_role_details(request_parameters)
client = get_client(role_arn, "organizations")
aws_accounts=get_aws_accounts(client)
internal_ids=[]
external_ids=[]
for id in role["X_account_ids"]:
if id in aws_accounts:
internal_ids.append(id)
else:
external_ids.append(id)
send_sns_notification(role["name"], event_account_id,internal_ids, external_ids)
except Exception as exception:
logger.error("****** An error occured ****** \n" + str(exception))
MonitorCrossAccountRolesRule:
Type: AWS::Events::Rule
Properties:
Name: MonitorCrossAccountRolesRule
Description: EventBridge rule to capture IAM events for create & update roles.
State: ENABLED
EventPattern:
source:
- aws.iam
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- iam.amazonaws.com
eventName:
- CreateRole
- UpdateAssumeRolePolicy
Targets:
- Arn: !GetAtt MonitorCrossAccountRolesFunction.Arn
Id: MonitorCrossAccountRolesRule
MonitorCrossAccountRolesPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref MonitorCrossAccountRolesFunction
Action: lambda:InvokeFunction
Principal: "events.amazonaws.com"
SourceArn: !GetAtt MonitorCrossAccountRolesRule.Arn