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

Add policy statement for opensearch service #187

Open
wants to merge 8 commits 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: 11 additions & 1 deletion aws/policy/data-services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Statement:
- glue:GetConnections
- rds:DescribeDB*
- rds:List*
- es:Describe*
- es:Get*
- es:List*
Resource: "*"
- Sid: AllowGlobalResourceRestrictedActionsWhichIncurNoFees
Effect: Allow
Expand Down Expand Up @@ -127,14 +130,21 @@ Statement:
- rds:CreateDBCluster
- elasticache:CreateCacheCluster
- redshift:CreateCluster
- es:AddTags
- es:CreateDomain
- es:DeleteDomain
- es:RemoveTags
- es:UpdateDomainConfig
- es:UpgradeDomain
Resource:
- 'arn:aws:rds:{{ aws_region }}:{{ aws_account_id }}:cluster:*'
- 'arn:aws:elasticache:{{ aws_region }}:{{ aws_account_id }}:cluster:*'
- 'arn:aws:elasticache:{{ aws_region }}:{{ aws_account_id }}:subnetgroup:*'
- 'arn:aws:elasticache:{{ aws_region }}:{{ aws_account_id }}:parametergroup:*'
- 'arn:aws:elasticache:{{ aws_region }}:{{ aws_account_id }}:securitygroup:*'
- 'arn:aws:redshift:{{ aws_region }}:{{ aws_account_id }}:cluster:*'
# This allows AWS Services to autmatically create their Default Service Linked Roles
- 'arn:aws:es:{{ aws_region }}:{{ aws_account_id }}:domain:*'
# This allows AWS Services to automatically create their Default Service Linked Roles
# These have fixed policies and can only be assumed by the service itself.
- Sid: AllowServiceLinkedRoleCreation
Effect: Allow
Expand Down
45 changes: 45 additions & 0 deletions aws/terminator/data_services.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import datetime

import botocore.exceptions

from . import DbTerminator, Terminator, get_tag_dict_from_tag_list


Expand Down Expand Up @@ -297,3 +299,46 @@ def age_limit(self):

def terminate(self):
self.client.delete_cluster(ClusterArn=self.id)


class OpenSearch(Terminator):

@staticmethod
def create(credentials):
def get_available_clusters(client):
domains = []
for domain in client.list_domain_names()['DomainNames']:
try:
domain_status = client.describe_domain(DomainName=domain['DomainName'])['DomainStatus']
if not domain_status['Deleted']:
# 'Deleted' is true if a delete request has been received for the domain
# but resource cleanup is still in progress.
domain_config = client.describe_domain_config(DomainName=domain['DomainName'])
domain_status["CreationDate"] = domain_config['DomainConfig']['Status']['CreationDate']
domains.append(domain_status)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError): # pylint: disable=duplicate-except
# Unlikely, but possible. The domain may have been deleted after invoking
# list_domain_names().
pass
return domains

return Terminator._create(credentials, OpenSearch, 'opensearch', get_available_clusters)

@property
def id(self):
return self.instance['DomainId']

@property
def name(self):
return self.instance['DomainName']

@property
def created_time(self):
return self.instance['CreationDate']

@property
def age_limit(self):
return datetime.timedelta(minutes=60)

def terminate(self):
self.client.delete_domain(DomainName=self.name)