def __init__(self, customer_name, credentials, data_source, report_name=None, **kwargs): """ Constructor. Args: customer_name (String) Name of the customer credentials (Dict) credentials credential for S3 bucket (RoleARN) report_name (String) Name of the Cost Usage Report to download (optional) bucket (String) Name of the S3 bucket containing the CUR """ super().__init__(**kwargs) arn = credentials.get("role_arn") bucket = data_source.get("bucket") if customer_name[4:] in settings.DEMO_ACCOUNTS: demo_account = settings.DEMO_ACCOUNTS.get(customer_name[4:]) LOG.info(f"Info found for demo account {customer_name[4:]} = {demo_account}.") if arn in demo_account: demo_info = demo_account.get(arn) self.customer_name = customer_name.replace(" ", "_") self._provider_uuid = kwargs.get("provider_uuid") self.report_name = demo_info.get("report_name") self.report = {"S3Bucket": bucket, "S3Prefix": demo_info.get("report_prefix"), "Compression": "GZIP"} self.bucket = bucket session = utils.get_assume_role_session(utils.AwsArn(arn), "MasuDownloaderSession") self.s3_client = session.client("s3") return self.customer_name = customer_name.replace(" ", "_") self._provider_uuid = kwargs.get("provider_uuid") LOG.debug("Connecting to AWS...") session = utils.get_assume_role_session(utils.AwsArn(arn), "MasuDownloaderSession") self.cur = session.client("cur") # fetch details about the report from the cloud provider defs = self.cur.describe_report_definitions() if not report_name: report_names = [] for report in defs.get("ReportDefinitions", []): if bucket == report.get("S3Bucket"): report_names.append(report["ReportName"]) # FIXME: Get the first report in the bucket until Koku can specify # which report the user wants if report_names: report_name = report_names[0] self.report_name = report_name self.bucket = bucket report_defs = defs.get("ReportDefinitions", []) report = [rep for rep in report_defs if rep["ReportName"] == self.report_name] if not report: raise MasuProviderError("Cost and Usage Report definition not found.") self.report = report.pop() self.s3_client = session.client("s3")
def test_parse_arn_with_region_and_account(self): """Assert successful account ID parsing from a well-formed ARN.""" mock_account_id = fake_aws_account_id() mock_arn = fake_arn(account_id=mock_account_id, region="test-region-1") arn_object = utils.AwsArn(mock_arn) partition = arn_object.partition self.assertIsNotNone(partition) service = arn_object.service self.assertIsNotNone(service) region = arn_object.region self.assertIsNotNone(region) account_id = arn_object.account_id self.assertIsNotNone(account_id) resource_type = arn_object.resource_type self.assertIsNotNone(resource_type) resource_separator = arn_object.resource_separator self.assertIsNotNone(resource_separator) resource = arn_object.resource self.assertIsNotNone(resource) reconstructed_arn = ("arn:" + partition + ":" + service + ":" + region + ":" + account_id + ":" + resource_type + resource_separator + resource) self.assertEqual(mock_account_id, account_id) self.assertEqual(mock_arn, reconstructed_arn)
def test_parse_arn_with_custom_resource_type(self): """Assert valid ARN when resource type contains extra characters.""" mock_arn = 'arn:aws:fakeserv:test-reg-1:012345678901:test.res type:foo' arn_object = utils.AwsArn(mock_arn) resource_type = arn_object.resource_type self.assertIsNotNone(resource_type) resource = arn_object.resource self.assertIsNotNone(resource)
def test_parse_arn_without_region_or_account(self): """Assert successful ARN parsing without a region or an account id.""" mock_arn = fake_arn() arn_object = utils.AwsArn(mock_arn) region = arn_object.region self.assertEqual(region, None) account_id = arn_object.account_id self.assertEqual(account_id, None)
def _init_session(self): """ Set or get a session client for aws organizations Args: session = aws session """ session = utils.get_assume_role_session(utils.AwsArn(self._auth_cred)) session_client = session.client("organizations") LOG.info("Starting aws organizations session for crawler.") self._client = session_client
def __init__(self, task, customer_name, auth_credential, bucket, report_name=None, **kwargs): """ Constructor. Args: task (Object) bound celery object customer_name (String) Name of the customer auth_credential (String) Authentication credential for S3 bucket (RoleARN) report_name (String) Name of the Cost Usage Report to download (optional) bucket (String) Name of the S3 bucket containing the CUR """ super().__init__(task, **kwargs) self.customer_name = customer_name.replace(' ', '_') self._provider_uuid = kwargs.get('provider_uuid') LOG.debug('Connecting to AWS...') session = utils.get_assume_role_session(utils.AwsArn(auth_credential), 'MasuDownloaderSession') self.cur = session.client('cur') # fetch details about the report from the cloud provider defs = self.cur.describe_report_definitions() if not report_name: report_names = [] for report in defs.get('ReportDefinitions', []): if bucket == report.get('S3Bucket'): report_names.append(report['ReportName']) # FIXME: Get the first report in the bucket until Koku can specify # which report the user wants if report_names: report_name = report_names[0] self.report_name = report_name self.bucket = bucket report_defs = defs.get('ReportDefinitions', []) report = [ rep for rep in report_defs if rep['ReportName'] == self.report_name ] if not report: raise MasuProviderError( 'Cost and Usage Report definition not found.') self.report = report.pop() self.s3_client = session.client('s3')
def test_parse_arn_with_slash_separator(self): """Assert successful ARN parsing with a slash separator.""" mock_arn = fake_arn(resource_separator='/') arn_object = utils.AwsArn(mock_arn) resource_type = arn_object.resource_type self.assertIsNotNone(resource_type) resource_separator = arn_object.resource_separator self.assertEqual(resource_separator, '/') resource = arn_object.resource self.assertIsNotNone(resource)
def _init_session(self): """ Set or get a session client for aws organizations Args: session = aws session """ awsarn = utils.AwsArn(self._auth_cred) session = utils.get_assume_role_session(awsarn) session_client = session.client("organizations") self.account_id = awsarn.account_id LOG.info( "Starting aws organizations session for crawler for account with" " provider_uuid: {} and account_id: {}.".format( self.account.get("provider_uuid"), self.account_id)) self._client = session_client
def test_error_from_invalid_arn(self): """Assert error in account ID parsing from a badly-formed ARN.""" mock_arn = self.fake.text() with self.assertRaises(SyntaxError): utils.AwsArn(mock_arn)