Skip to content

Add proxy support to specific AWS BPs. #53

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

Open
wants to merge 2 commits into
base: master
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
http://boto3.readthedocs.io/en/latest/reference/services/route53.html#Route53.Client.change_resource_record_sets
'''
from resourcehandlers.aws.models import AWSHandler
from common.methods import set_progress


#dns zone friendly name -- no trailing period
# dns zone friendly name -- no trailing period
ROUTE_53_DNS_DOMAIN = '{{ r53_domain_name }}'

# 'CREATE'|'DELETE'|'UPSERT'
Expand All @@ -18,6 +16,7 @@
# 60 | 120 | <any Integer> '
TTL = 300


def get_hosted_zone_id(client=None, zone=None, env_vpc_id=None):
'''
This code is intended to work out issues where multiple DNS zones are named
Expand All @@ -30,30 +29,30 @@ def get_hosted_zone_id(client=None, zone=None, env_vpc_id=None):
updated 2018/12/20
'''

#set_progress(f'getting zone: {zone}')
zone_name = f'{zone}.' #zone names have a trailing period
# set_progress(f'getting zone: {zone}')
zone_name = f'{zone}.' # zone names have a trailing period
response = client.list_hosted_zones_by_name(DNSName=zone_name)

#set_progress(f"LEN = {len(response['HostedZones'])}")
# set_progress(f"LEN = {len(response['HostedZones'])}")

if len(response['HostedZones']) == 1:
return response['HostedZones'][0]['Id']
elif len(response['HostedZones']) > 1:
for dns_zone in response['HostedZones']:
#set_progress(dns_zone['Id'], ' -- ', dns_zone['Name'])
# set_progress(dns_zone['Id'], ' -- ', dns_zone['Name'])
hz = client.get_hosted_zone(Id=dns_zone['Id'])
if not hz:
#set_progress(f"ERROR GETTING HOSTED ZONE FROM AWS: {Item['Id']}")
# set_progress(f"ERROR GETTING HOSTED ZONE FROM AWS: {Item['Id']}")
break
if env_vpc_id == hz['VPCs'][0]['VPCId']:
#set_progress(f"returning: {dns_zone['Id']}")
# set_progress(f"returning: {dns_zone['Id']}")
return dns_zone['Id']

#set_progress('returning: False')
# set_progress('returning: False')
return False

#needed more resiliency in this function - see above
#def get_hosted_zone_id(client, zone):
# needed more resiliency in this function - see above
# def get_hosted_zone_id(client, zone):
# response = client.list_hosted_zones_by_name(DNSName=zone)
# # get first hosted zone returned
# hosted_zone = response['HostedZones'][0]
Expand Down Expand Up @@ -84,18 +83,24 @@ def run(job=None, server=None, **kwargs):
msg = 'DNS domain not set on selected NIC: {}'.format(nic)
return "FAILURE", "", msg
rh = server.resource_handler.cast()
wrapper = rh.get_api_wrapper()
if not isinstance(rh, AWSHandler):
msg = 'Route53 not supported on RH Type: {}'.format(rh)
return "FAILURE", "", msg

region = server.environment.get_cfv('aws_region')
client = rh.get_boto3_client(region_name=region, service_name='route53')
client = wrapper.get_boto3_client(
'route53',
rh.serviceaccount,
rh.servicepasswd,
region
)

zone_id = get_hosted_zone_id(client=client,
zone=route_53_dns_zone,
env_vpc_id=server.environment.vpc_id)
name = f'{server.hostname}.{dns_domain}'
#name = '{}.{}'.format(server.hostname, dns_domain)
# name = '{}.{}'.format(server.hostname, dns_domain)

batch = {
'Comment': 'Created by CloudBolt Job ID: {}'.format(job.id),
Expand Down
13 changes: 7 additions & 6 deletions blueprints/aws_rds_instance/create_aws_rds_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
new deployed service.
"""
import json
import boto3

from infrastructure.models import CustomField, Environment
from orders.models import CustomFieldValue
Expand Down Expand Up @@ -42,7 +41,7 @@ def run(job, logger=None, **kwargs):
rds_settings.update(dict(MasterUserPassword=db_password))
response = client.create_db_instance(**rds_settings)

service = job.resource_set.first() # Change resource_set to service_set if you are using this script in CB version pre-8.0
service = job.resource_set.first() # Change resource_set to service_set if you are using this script in CB version pre-8.0
instance = boto_instance_to_dict(response['DBInstance'])
store_instance_data_on_service(instance, service)
store_aws_environment_on_service(env, service)
Expand All @@ -56,11 +55,13 @@ def connect_to_rds(env):
Return boto connection to the RDS in the specified environment's region.
"""
rh = env.resource_handler.cast()
return boto3.client(
wrapper = rh.get_api_wrapper()
return wrapper.get_boto3_client(
'rds',
region_name=env.aws_region,
aws_access_key_id=rh.serviceaccount,
aws_secret_access_key=rh.servicepasswd)
rh.serviceaccount,
rh.servicepasswd,
env.aws_region
)


def boto_instance_to_dict(boto_instance):
Expand Down
19 changes: 11 additions & 8 deletions blueprints/aws_rds_instance/delete_aws_rds_instance.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import json
import boto3

from common.methods import set_progress
from infrastructure.models import Environment


def run(job, logger=None, **kwargs):
service = job.resource_set.first() # Change resource_set to service_set if you are using this script in CB version pre-8.0
service = job.resource_set.first() # Change resource_set to service_set if you are using this script in CB version pre-8.0

# The Environment ID and RDS Instance data dict were stored as attributes on
# this service by a build action.
Expand All @@ -19,7 +19,7 @@ def run(job, logger=None, **kwargs):
identifier = instance['identifier']

job.set_progress('Deleting RDS instance {0}...'.format(identifier))
response = client.delete_db_instance(
client.delete_db_instance(
DBInstanceIdentifier=identifier,
# AWS strongly recommends taking a final snapshot before deleting a DB.
# To do so, either set this to False or let the user choose by making it
Expand All @@ -36,10 +36,13 @@ def connect_to_rds(env):
"""
Return boto connection to the RDS in the specified environment's region.
"""
job.set_progress('Connecting to AWS RDS in region {0}.'.format(env.aws_region))
set_progress('Connecting to AWS RDS in region {0}.'.format(env.aws_region))
rh = env.resource_handler.cast()
return boto3.client(
wrapper = rh.get_api_wrapper()
client = wrapper.get_boto3_client(
'rds',
region_name=env.aws_region,
aws_access_key_id=rh.serviceaccount,
aws_secret_access_key=rh.servicepasswd)
rh.serviceaccount,
rh.servicepasswd,
env.aws_region
)
return client
17 changes: 10 additions & 7 deletions blueprints/aws_rds_instance/refresh_aws_rds_instance_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
Library will automatically import this action.
"""
import json
import boto3

from common.methods import set_progress
from infrastructure.models import Environment
from orders.models import CustomFieldValue


def run(job, logger=None, **kwargs):
service = job.resource_set.first()# Replace resource_set to service_set if you are using this script in CB version pre-8.0
service = job.resource_set.first() # Replace resource_set to service_set if you are using this script in CB version pre-8.0

# The Environment ID and RDS Instance data dict were stored as attributes on
# this service by a build action.
Expand Down Expand Up @@ -39,13 +39,16 @@ def connect_to_rds(env):
"""
Return boto connection to the RDS in the specified environment's region.
"""
job.set_progress('Connecting to AWS RDS in region {0}.'.format(env.aws_region))
set_progress('Connecting to AWS RDS in region {0}.'.format(env.aws_region))
rh = env.resource_handler.cast()
return boto3.client(
wrapper = rh.get_api_wrapper()
client = wrapper.get_boto3_client(
'rds',
region_name=env.aws_region,
aws_access_key_id=rh.serviceaccount,
aws_secret_access_key=rh.servicepasswd)
rh.serviceaccount,
rh.servicepasswd,
env.aws_region
)
return client


def boto_instance_to_dict(boto_instance):
Expand Down
11 changes: 6 additions & 5 deletions blueprints/aws_s3_bucket/create_s3_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def run(job, logger=None, **kwargs):
region = '{{ s3_region }}'
new_bucket_name = '{{ s3_bucket_name_input }}'
rh = AWSHandler.objects.get(id=rh_id)
wrapper = rh.get_api_wrapper()
CustomField.objects.get_or_create(
name='aws_rh_id', label='AWS RH ID', type='STR',
description='Used by the AWS S3 Bucket blueprint'
Expand All @@ -43,11 +44,11 @@ def run(job, logger=None, **kwargs):
resource.save()

set_progress('Connecting to Amazon S3')
conn = boto3.resource(
's3',
region_name=region,
aws_access_key_id=rh.serviceaccount,
aws_secret_access_key=rh.servicepasswd,
conn = wrapper.get_boto3_resource(
rh.serviceaccount,
rh.servicepasswd,
region,
service_name='s3'
)

set_progress('Create S3 bucket "{}"'.format(new_bucket_name))
Expand Down
11 changes: 6 additions & 5 deletions blueprints/aws_s3_bucket/delete_s3_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
from common.methods import set_progress
from resourcehandlers.aws.models import AWSHandler
import boto3


def run(job, logger=None, **kwargs):
Expand All @@ -12,12 +11,14 @@ def run(job, logger=None, **kwargs):
bucket_name = resource.attributes.get(field__name='s3_bucket_name').value
rh_id = resource.attributes.get(field__name='aws_rh_id').value
rh = AWSHandler.objects.get(id=rh_id)
wrapper = rh.get_api_wrapper()

set_progress('Connecting to Amazon S3')
conn = boto3.resource(
's3',
aws_access_key_id=rh.serviceaccount,
aws_secret_access_key=rh.servicepasswd,
conn = wrapper.get_boto3_resource(
rh.serviceaccount,
rh.servicepasswd,
None,
service_name='s3'
)

bucket = conn.Bucket(bucket_name)
Expand Down
16 changes: 8 additions & 8 deletions blueprints/aws_s3_bucket/discover_s3_buckets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
As all Discovery Plug-ins must do, we define the global `RESOURCE_IDENTIFIER` variable
and return a list of dictionaries from the `discover_resources` function.
"""
import boto3
from botocore.client import ClientError
from common.methods import set_progress
from resourcehandlers.aws.models import AWSHandler
Expand All @@ -13,14 +12,15 @@


def discover_resources(**kwargs):

discovered_buckets = []
discovered_buckets = []
for handler in AWSHandler.objects.all():
wrapper = handler.get_api_wrapper()
set_progress('Connecting to Amazon S3 for handler: {}'.format(handler))
conn = boto3.resource(
's3',
aws_access_key_id=handler.serviceaccount,
aws_secret_access_key=handler.servicepasswd,
conn = wrapper.get_boto3_resource(
handler.serviceaccount,
handler.servicepasswd,
None,
service_name='s3'
)

try:
Expand All @@ -33,5 +33,5 @@ def discover_resources(**kwargs):
except ClientError as e:
set_progress('AWS ClientError: {}'.format(e))
continue

return discovered_buckets
13 changes: 6 additions & 7 deletions blueprints/cloudformations/delete_aws_cf_stack.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env python
# This CB plugin is used by the 'LAMP CloudFormation' blueprint

import boto3
from common.methods import set_progress

from resourcehandlers.aws.models import AWSHandler
Expand All @@ -19,13 +17,14 @@ def run(job, logger, resources=None):
"resource action")

rh = AWSHandler.objects.first()
wrapper = rh.get_api_wrapper()
# See http://boto3.readthedocs.io/en/latest/guide/configuration.html#method-parameters
session = boto3.Session(
aws_access_key_id=rh.serviceaccount,
aws_secret_access_key=rh.servicepasswd,
region_name='us-west-2'
client = wrapper.get_boto3_client(
'cloudformation',
rh.serviceaccount,
rh.servicepasswd,
'us-west-2'
)
client = session.client('cloudformation')

stack_name = resource.attributes.filter(field__name="aws_stack_name").first()
if not stack_name:
Expand Down
13 changes: 6 additions & 7 deletions blueprints/cloudformations/deploy_stack_from_cloud_formation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env python
# This CB plugin is used by the 'LAMP CloudFormation' blueprint

import boto3
import time
from infrastructure.models import CustomField
from orders.models import CustomFieldValue
Expand All @@ -11,14 +9,15 @@

def run(job, logger):
rh = AWSHandler.objects.first()
wrapper = rh.get_api_wrapper()

# See http://boto3.readthedocs.io/en/latest/guide/configuration.html#method-parameters
session = boto3.Session(
aws_access_key_id=rh.serviceaccount,
aws_secret_access_key=rh.servicepasswd,
region_name='us-west-2'
client = wrapper.get_boto3_client(
'cloudformation',
rh.serviceaccount,
rh.servicepasswd,
'us-west-2'
)
client = session.client('cloudformation')

timestamp = str(time.time())
timestamp, _ = timestamp.split('.')
Expand Down