Beispiel #1
0
    def integration_add_permission_to_lambda(self,api_id, lambda_name):
        # create permission to allow lambda function to be invoked by API Gateway
        iam           = IAM()
        aws_acct_id   = iam.account_id()
        aws_region    = iam.region()
        aws_lambda    = Lambda(lambda_name)
        function_arn  = aws_lambda.function_Arn()#'gw_bot.lambdas.dev.hello_world'
        statement_id  = 'allow-api-gateway-invoke'
        action        = 'lambda:InvokeFunction'
        principal     = 'apigateway.amazonaws.com'
        source_arn    = f'arn:aws:execute-api:{aws_region}:{aws_acct_id}:{api_id}/*/GET/'

        aws_lambda.permission_delete(function_arn, statement_id) # remove in case there was already a permission with this name
        return aws_lambda.permission_add(function_arn, statement_id, action, principal, source_arn)
    def test_log_files__send_to_elk(self):
        iam = IAM()
        account_id = iam.account_id()
        region = iam.region()
        year = '2020'
        month = '02'
        day = '16'
        max = 200
        log_files = self.cloud_trail.log_files(self.trail_name, account_id,
                                               region, year, month, day)
        records = self.cloud_trail.log_files_records(self.trail_name,
                                                     log_files[0:max])

        self.result = self.send_to_elk(records, 'eventID')
Beispiel #3
0
    def integration_create__lambda(self, api_id, resource_id, lambda_name,
                                   http_method):
        iam = IAM()
        aws_acct_id = iam.account_id()
        aws_region = iam.region()

        input_type = 'AWS_PROXY'
        uri = f'arn:aws:apigateway:{aws_region}:lambda:path/2015-03-31/functions/arn:aws:lambda:{aws_region}:{aws_acct_id}:function:{lambda_name}/invocations'
        integration_http_method = 'POST'
        try:
            return self.api_gateway().put_integration(
                restApiId=api_id,
                resourceId=resource_id,
                httpMethod=http_method,
                integrationHttpMethod=integration_http_method,
                type=input_type,
                uri=uri)
        except Exception as error:
            return f'{error}'
 def test_get_available_log_files(self):
     iam        = IAM()
     account_id = iam.account_id()
     region     = iam.region()
     log_type   = 'CloudTrail'
     year       = '2020'
     month      = '02'
     day        = '16'
     hour       = ''
     minute     = ''
     s3_prefix = f'{self.s3_key_prefix}/AWSLogs/{account_id}/'                           \
                 f'{log_type}/{region}/{year}/{month}/{day}/'
     s3          = self.cloud_trail.s3
     s3_files    = s3.find_files(self.s3_bucket, prefix=s3_prefix)
     print('-------')
     total = 0
     for s3_file in sorted(s3_files):
         contents    = s3.file_contents_from_gzip(self.s3_bucket,s3_file)
         records = json.loads(contents).get("Records")
         total += len(records)
         print(f'{len(records): 4} : {total : 4} : {s3_file.split("/").pop()}')
Beispiel #5
0
class test_OSBot_Jupyter_Create_Code_Build(Test_Helper):
    def setUp(self):
        super().setUp()
        #Deploy().setup()
        self.project_name = 'OSBot-Jupyter'
        self.iam = IAM()
        self.account_id = self.iam.account_id()
        self.region = self.iam.region()
        self.github_org = 'filetrust'
        self.source_version = 'master'
        self.build_spec = 'buildspec.yml'
        self.docker_type = 'LINUX_CONTAINER'
        #self.docker_image    = '{0}.dkr.ecr.eu-west-1.amazonaws.com/osbot-jupyter:latest'.format(self.account_id),
        self.compute_type = 'BUILD_GENERAL1_MEDIUM'
        #self.api             = Create_Code_Build(project_name=self.project_name)
        self.api = Create_Code_Build(project_name=self.project_name,
                                     github_org=self.github_org,
                                     source_version=self.source_version,
                                     docker_type=self.docker_type,
                                     compute_type=self.compute_type,
                                     build_spec=self.build_spec)

    #def create_project_with_container__osbot_jupyter(self):
    # kvargs = {
    #     'name'        : self.api.project_name,
    #     'source'      : {'type'                    : 'GITHUB',
    #                      'location'                : self.api.project_repo                 },
    #     'artifacts'   : {'type'                    : 'NO_ARTIFACTS'                    },
    #     'environment' : {'type'                    : 'LINUX_CONTAINER'                  ,
    #                     'image'                    : '{0}.dkr.ecr.eu-west-1.amazonaws.com/osbot-jupyter:latest'.format(self.account_id)     ,
    #                     'computeType'              : 'BUILD_GENERAL1_SMALL'            ,
    #                     'imagePullCredentialsType' : 'SERVICE_ROLE'                    },
    #     'serviceRole' : self.api.service_role
    # }
    # return self.api.code_build.codebuild.create_project(**kvargs)

    # this only needs to run once
    def test_create_policies(self):
        policies = self.api.policies__with_ecr_and_3_secrets()
        self.api.create_role_and_policies(policies)

    def test_create_code_build_and_trigger_first_build(self):
        self.api.code_build.project_delete()
        self.api.create_project_with_container__gs_docker_codebuild()
        #self.create_project_with_container__osbot_jupyter()
        #self.api.code_build.build_start()

    def test_get_task_details(self):
        from osbot_aws.apis.Logs import Logs

        def find_starts(array, text):
            return [item for item in array if item.startswith(text)]

        def find_in(array, text):
            return [item for item in array if text in item]

        #build_id = 'OSBot-Jupyter:a553dda5-953a-41b8-ae91-e068cba4f56b'

        result = self.api.code_build.project_builds_ids(self.api.project_name)
        build_id = result.__next__()  # get last one
        build_info = self.api.code_build.build_info(build_id)
        group_name = build_info.get('logs').get('groupName')
        stream_name = build_info.get('logs').get('streamName')
        #Dev.pprint(group_name,stream_name)
        logs = Logs(group_name=group_name, stream_name=stream_name)

        messages = logs.messages()
        #ngrok_messages = find_starts(messages,'t=')
        ngrok_url = find_in(
            messages, 'name=command_line addr')[0].split('url=')[1].strip()
        jupyter_token = find_in(messages,
                                'token=')[0].split('token=')[1].strip()

        Dev.pprint("{0}?token={1}".format(ngrok_url, jupyter_token))