Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Adding IAM rules #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions aws/aws_blueprints_template/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,16 @@ module "port_load_balancer" {
source = "../load_balancer"
count = contains(var.resources, "load_balancer") ? 1 : 0
depends_on = [port-labs_blueprint.region]
}

module "port_iam_role" {
source = "../iam_role"
count = contains(var.resources, "iam_role") ? 1 : 0
depends_on = [port-labs_blueprint.region]
}

module "port_iam_policy" {
source = "../iam_policy"
count = contains(var.resources, "iam_policy") ? 1 : 0
depends_on = [port-labs_blueprint.region]
}
35 changes: 35 additions & 0 deletions aws/iam_policy/blueprint.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
terraform {
required_providers {
port-labs = {
source = "port-labs/port-labs"
version = "0.10.4"
}
}
}

resource "port-labs_blueprint" "iam_policy" {
title = "IAM Policy"
icon = "AWS"
identifier = "iam_policy"

properties {
identifier = "link"
type = "string"
format = "url"
title = "Link"
}

properties {
identifier = "arn"
type = "string"
title = "ARN"
}

relations {
target = "region"
title = "Region"
identifier = "region"
many = false
required = false
}
}
18 changes: 18 additions & 0 deletions aws/iam_policy/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"kind": "AWS::IAM::Policy",
"port": {
"entity": {
"mappings": [
{
"identifier": ".PolicyName",
"title": ".PolicyName",
"blueprint": "iam_policy",
"properties": {
"link": "\"https://console.aws.amazon.com/go/view?arn=\" + .Arn",
"arn": ".Arn"
}
}
]
}
}
}
49 changes: 49 additions & 0 deletions aws/iam_policy/event_rule.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
SQSEventRule:
Type: AWS::Events::Rule
Properties:
EventBusName: default
EventPattern:
source:
- aws.iam
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- iam.amazonaws.com
eventName:
- prefix: AttachGroupPolicy
- prefix: AttachRolePolicy
- prefix: AttachUserPolicy
- prefix: CreatePolicy
- prefix: CreatePolicyVersion
- prefix: DeleteGroupPolicy
- prefix: DeletePolicy
- prefix: DeletePolicyVersion
- prefix: DeleteRolePolicy
- prefix: DeleteUserPolicy
- prefix: DetachGroupPolicy
- prefix: DetachRolePolicy
- prefix: DetachUserPolicy
- prefix: PutGroupPolicy
- prefix: PutRolePolicy
- prefix: PutUserPolicy
- prefix: SetDefaultPolicyVersion
Name: port-aws-exporter-sync-iam-trails
State: ENABLED
Targets:
- Id: PortAWSExporterEventsQueue
Arn:
Fn::ImportValue:
Fn::Sub: ${PortAWSExporterStackName}-EventsQueueARN
InputTransformer:
InputPathsMap:
awsRegion: $.detail.awsRegion
eventName: $.detail.eventName
requestPolicyName: $.detail.requestParameters.policyName
InputTemplate: |-
{
"resource_type": "AWS::IAM::Policy",
"region": "\"<awsRegion>\"",
"identifier": "\"<requestPolicyName>\"",
"action": "if \"<eventName>\" | test(\"DeletePolicy[^a-zA-Z]*$\") then \"delete\" else \"upsert\" end"
}
16 changes: 16 additions & 0 deletions aws/iam_policy/policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
"iam:ListPolicies",
"iam:GetPolicyVersion",
"iam:GetPolicy",
"iam:ListGroupPolicies",
"iam:ListPoliciesGrantingServiceAccess",
"iam:ListEntitiesForPolicy",
"iam:ListUserPolicies",
"iam:ListPolicyVersions",
"iam:GetUserPolicy",
"iam:ListAttachedRolePolicies",
"iam:ListAttachedUserPolicies",
"iam:ListAttachedGroupPolicies",
"iam:ListPolicyTags",
"iam:ListRolePolicies"
]
41 changes: 41 additions & 0 deletions aws/iam_role/blueprint.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
terraform {
required_providers {
port-labs = {
source = "port-labs/port-labs"
version = "0.10.4"
}
}
}

resource "port-labs_blueprint" "iam_role" {
title = "IAM Role"
icon = "AWS"
identifier = "iam_role"

properties {
identifier = "link"
type = "string"
format = "url"
title = "Link"
}

properties {
identifier = "description"
type = "string"
title = "Description"
}

properties {
identifier = "arn"
type = "string"
title = "ARN"
}

relations {
target = "region"
title = "Region"
identifier = "region"
many = false
required = false
}
}
19 changes: 19 additions & 0 deletions aws/iam_role/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"kind": "AWS::IAM::Role",
"port": {
"entity": {
"mappings": [
{
"identifier": ".RoleName",
"title": ".RoleName",
"blueprint": "iam_role",
"properties": {
"link": "\"https://console.aws.amazon.com/go/view?arn=\" + .Arn",
"arn": ".Arn",
"description": ".Description"
}
}
]
}
}
}
41 changes: 41 additions & 0 deletions aws/iam_role/event_rule.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
SQSEventRule:
Type: AWS::Events::Rule
Properties:
EventBusName: default
EventPattern:
source:
- aws.iam
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- iam.amazonaws.com
eventName:
- prefix: AddRoleToInstanceProfile
- prefix: AttachRolePolicy
- prefix: CreateRole
- prefix: DeleteRole
- prefix: DeleteRolePolicy
- prefix: DetachRolePolicy
- prefix: PutRolePolicy
- prefix: RemoveRoleFromInstanceProfile
- prefix: UpdateAssumeRolePolicy
Name: port-aws-exporter-sync-iam-trails
State: ENABLED
Targets:
- Id: PortAWSExporterEventsQueue
Arn:
Fn::ImportValue:
Fn::Sub: ${PortAWSExporterStackName}-EventsQueueARN
InputTransformer:
InputPathsMap:
awsRegion: $.detail.awsRegion
eventName: $.detail.eventName
requestRoleName: $.detail.requestParameters.roleName
InputTemplate: |-
{
"resource_type": "AWS::IAM::Role",
"region": "\"<awsRegion>\"",
"identifier": "\"<requestRoleName>\"",
"action": "if \"<eventName>\" | test(\"DeleteRole[^a-zA-Z]*$\") then \"delete\" else \"upsert\" end"
}
9 changes: 9 additions & 0 deletions aws/iam_role/policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
"iam:ListInstanceProfilesForRole",
"iam:ListRoleTags",
"iam:ListAttachedRolePolicies",
"iam:ListRoles",
"iam:ListRolePolicies",
"iam:GetRole",
"iam:GetRolePolicy"
]
49 changes: 49 additions & 0 deletions aws/iam_user/blueprint.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
terraform {
required_providers {
port-labs = {
source = "port-labs/port-labs"
version = "0.10.4"
}
}
}

resource "port-labs_blueprint" "iam_user" {
title = "IAM User"
icon = "AWS"
identifier = "iam_user"

properties {
identifier = "link"
type = "string"
format = "url"
title = "Link"
}

properties {
identifier = "description"
type = "string"
title = "Description"
}

properties {
identifier = "arn"
type = "string"
title = "ARN"
}

relations {
target = "region"
title = "Region"
identifier = "region"
many = false
required = false
}

relations {
target = "role"
title = "Role"
identifier = "role"
many = true
required = false
}
}
19 changes: 19 additions & 0 deletions aws/iam_user/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"kind": "AWS::IAM::User",
"port": {
"entity": {
"mappings": [
{
"identifier": ".UserName",
"title": ".RoleName",
"blueprint": "iam_role",
"properties": {
"link": "\"https://console.aws.amazon.com/go/view?arn=\" + .Arn",
"arn": ".Arn",
"description": ".Description"
}
}
]
}
}
}
41 changes: 41 additions & 0 deletions aws/iam_user/event_rule.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
SQSEventRule:
Type: AWS::Events::Rule
Properties:
EventBusName: default
EventPattern:
source:
- aws.iam
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- iam.amazonaws.com
eventName:
- prefix: CreateUser
- prefix: AddUserToGroup
- prefix: AttachUserPolicy
- prefix: DeleteUser
- prefix: DeleteUserPolicy
- prefix: DetachUserPolicy
- prefix: PutUserPolicy
- prefix: RemoveUserFromGroup
- prefix: UpdateUser
Name: port-aws-exporter-sync-iam-user-trails
State: ENABLED
Targets:
- Id: PortAWSExporterEventsQueue
Arn:
Fn::ImportValue:
Fn::Sub: ${PortAWSExporterStackName}-EventsQueueARN
InputTransformer:
InputPathsMap:
awsRegion: $.detail.awsRegion
eventName: $.detail.eventName
requestUserName: $.detail.requestParameters.userName
InputTemplate: |-
{
"resource_type": "AWS::IAM::User",
"region": "\"<awsRegion>\"",
"identifier": "\"<userName>\"",
"action": "if \"<eventName>\" | test(\"DeleteUser[^a-zA-Z]*$\") then \"delete\" else \"upsert\" end"
}
8 changes: 8 additions & 0 deletions aws/iam_user/policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
"iam:ListInstanceProfilesForRole",
"iam:ListRoleTags",
"iam:ListAttachedRolePolicies",
"iam:ListRoles",
"iam:ListRolePolicies",
"iam:GetRole"
]