This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79220bf
commit 72a263a
Showing
2 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
from troposphere import ( | ||
Ref, | ||
GetAtt, | ||
Output, | ||
iam, | ||
logs, | ||
ec2, | ||
) | ||
|
||
from troposphere.iam import Policy as TropoPolicy | ||
|
||
from stacker.blueprints.base import Blueprint | ||
|
||
from awacs.aws import ( | ||
Statement, | ||
Policy, | ||
) | ||
|
||
import awacs | ||
|
||
from .policies import flowlogs_assumerole_policy | ||
|
||
ALLOWED_TRAFFIC_TYPES = ["ACCEPT", "REJECT", "ALL"] | ||
JOINED_TRAFFIC_TYPES = '/'.join(ALLOWED_TRAFFIC_TYPES) | ||
LOG_RETENTION_DEFAULT = 1 | ||
CLOUDWATCH_ROLE_NAME = "Role" | ||
FLOW_LOG_GROUP_NAME = "LogGroup" | ||
FLOW_LOG_STREAM_NAME = "LogStream" | ||
|
||
|
||
def vpc_flow_log_cloudwatch_policy(): | ||
return Policy( | ||
Statement=[ | ||
Statement( | ||
Effect="Allow", | ||
Action=[ | ||
awacs.logs.PutSubscriptionFilter, | ||
awacs.logs.CreateLogStream, | ||
awacs.logs.DescribeLogGroups, | ||
awacs.logs.DescribeLogStreams, | ||
awacs.logs.DescribeMetricFilters, | ||
], | ||
Resource=["*"], | ||
) | ||
] | ||
) | ||
|
||
|
||
def validate_traffic_type(traffic_type): | ||
if traffic_type not in ALLOWED_TRAFFIC_TYPES: | ||
raise ValueError( | ||
"Traffic type must be one of the following: " + | ||
"%s" % JOINED_TRAFFIC_TYPES | ||
) | ||
|
||
return traffic_type | ||
|
||
|
||
class App(Blueprint): | ||
VARIABLES = { | ||
"Retention": { | ||
"type": int, | ||
"description": "Log group retention time in days.", | ||
"default": LOG_RETENTION_DEFAULT, | ||
}, | ||
"VpcId": { | ||
"type": str, | ||
"description": "ID of the VPC that flow logs will be enabled " | ||
"for.", | ||
}, | ||
"TrafficType": { | ||
"type": str, | ||
"description": "Type of traffic to log. Must be one of the " | ||
"following: %s" % JOINED_TRAFFIC_TYPES, | ||
"validator": validate_traffic_type, | ||
}, | ||
} | ||
|
||
def create_template(self): | ||
t = self.template | ||
variables = self.get_variables() | ||
|
||
t.add_resource( | ||
iam.Role( | ||
CLOUDWATCH_ROLE_NAME, | ||
AssumeRolePolicyDocument=flowlogs_assumerole_policy(), | ||
Path="/", | ||
Policies=[ | ||
TropoPolicy( | ||
PolicyName="vpc_cloudwatch_flowlog_policy", | ||
PolicyDocument=vpc_flow_log_cloudwatch_policy(), | ||
), | ||
] | ||
) | ||
) | ||
|
||
t.add_output( | ||
Output( | ||
"%sName" % CLOUDWATCH_ROLE_NAME, | ||
Value=Ref(CLOUDWATCH_ROLE_NAME) | ||
) | ||
) | ||
role_arn = GetAtt(CLOUDWATCH_ROLE_NAME, "Arn") | ||
t.add_output( | ||
Output( | ||
"%sArn" % CLOUDWATCH_ROLE_NAME, | ||
Value=role_arn | ||
) | ||
) | ||
|
||
t.add_resource( | ||
logs.LogGroup( | ||
FLOW_LOG_GROUP_NAME, | ||
RetentionInDays=variables["Retention"], | ||
) | ||
) | ||
|
||
t.add_output( | ||
Output( | ||
"%sName" % FLOW_LOG_GROUP_NAME, | ||
Value=Ref(FLOW_LOG_GROUP_NAME) | ||
) | ||
) | ||
t.add_output( | ||
Output( | ||
"%sArn" % FLOW_LOG_GROUP_NAME, | ||
Value=GetAtt(FLOW_LOG_GROUP_NAME, "Arn") | ||
) | ||
) | ||
|
||
t.add_resource( | ||
ec2.FlowLog( | ||
FLOW_LOG_STREAM_NAME, | ||
DeliverLogsPermissionArn=role_arn, | ||
LogGroupName=Ref(FLOW_LOG_GROUP_NAME), | ||
ResourceId=variables["VpcId"], | ||
ResourceType="VPC", | ||
TrafficType=variables["TrafficType"], | ||
) | ||
) | ||
|
||
t.add_output( | ||
Output( | ||
"%sName" % FLOW_LOG_STREAM_NAME, | ||
Value=Ref(FLOW_LOG_STREAM_NAME) | ||
) | ||
) |