예제 #1
0
    def test_add_provider_sources_auth_info_with_sub_id(self):
        """Test to add authentication to a source with subscription_id."""
        test_source_id = 3
        test_endpoint_id = 4
        test_authentication = {'credentials': {'client_id': 'new-client-id'}}
        azure_obj = Sources(source_id=test_source_id,
                            auth_header=self.test_header,
                            offset=3,
                            endpoint_id=test_endpoint_id,
                            source_type=Provider.PROVIDER_AZURE,
                            name='Test AZURE Source',
                            authentication={
                                'credentials': {
                                    'subscription_id': 'orig-sub-id',
                                    'client_id': 'test-client-id'
                                }
                            })
        azure_obj.save()

        storage.add_provider_sources_auth_info(test_source_id,
                                               test_authentication)
        response = Sources.objects.filter(source_id=test_source_id).first()
        self.assertEquals(
            response.authentication.get('credentials').get('subscription_id'),
            'orig-sub-id')
        self.assertEquals(
            response.authentication.get('credentials').get('client_id'),
            'new-client-id')
예제 #2
0
    def test_add_provider_sources_auth_info_with_sub_id(self):
        """Test to add authentication to a source with subscription_id."""
        test_source_id = 3
        test_endpoint_id = 4
        test_authentication = {"credentials": {"client_id": "new-client-id"}}
        azure_obj = Sources(
            source_id=test_source_id,
            auth_header=self.test_header,
            offset=3,
            endpoint_id=test_endpoint_id,
            source_type=Provider.PROVIDER_AZURE,
            name="Test AZURE Source",
            authentication={
                "credentials": {
                    "subscription_id": "orig-sub-id",
                    "client_id": "test-client-id"
                }
            },
        )
        azure_obj.save()

        storage.add_provider_sources_auth_info(test_source_id,
                                               test_authentication)
        response = Sources.objects.filter(source_id=test_source_id).first()
        self.assertEquals(
            response.authentication.get("credentials").get("subscription_id"),
            "orig-sub-id")
        self.assertEquals(
            response.authentication.get("credentials").get("client_id"),
            "new-client-id")
예제 #3
0
def save_auth_info(auth_header, source_id):
    """
    Store Sources Authentication information given an Source ID.

    This method is called when a Cost Management application is
    attached to a given Source as well as when an Authentication
    is created.  We have to handle both cases since an
    Authentication.create event can occur before a Source is
    attached to the Cost Management application.

    Authentication is stored in the Sources database table.

    Args:
        source_id (Integer): Platform Sources ID.
        auth_header (String): Authentication Header.

    Returns:
        None

    """
    source_type = storage.get_source_type(source_id)

    if source_type:
        sources_network = SourcesHTTPClient(auth_header, source_id)
    else:
        LOG.info(f'Source ID not found for ID: {source_id}')
        return

    try:
        if source_type == 'OCP':
            source_details = sources_network.get_source_details()
            # Check for imported to maintain temporary backwards compatibility
            # until the Sources Front End creates 'imported' entry with OCP Cluster ID.
            if source_details.get('source_ref'):
                authentication = {
                    'resource_name': source_details.get('source_ref')
                }
            else:
                uid = source_details.get('uid')
                LOG.info(
                    f'OCP is using fallback Source UID ({str(uid)} for authentication.'
                    ' Update frontend to add Cluster ID to the source_ref field on the Source.'
                )
                authentication = {'resource_name': uid}
        elif source_type == 'AWS':
            authentication = {
                'resource_name': sources_network.get_aws_role_arn()
            }
        elif source_type == 'AZURE':
            authentication = {
                'credentials': sources_network.get_azure_credentials()
            }
        else:
            LOG.error(f'Unexpected source type: {source_type}')
            return
        storage.add_provider_sources_auth_info(source_id, authentication)
    except SourcesHTTPClientError:
        LOG.info(
            f'Authentication info not available for Source ID: {source_id}')
예제 #4
0
def save_auth_info(auth_header, source_id):
    """
    Store Sources Authentication information given an Source ID.

    This method is called when a Cost Management application is
    attached to a given Source as well as when an Authentication
    is created.  We have to handle both cases since an
    Authentication.create event can occur before a Source is
    attached to the Cost Management application.

    Authentication is stored in the Sources database table.

    Args:
        source_id (Integer): Platform Sources ID.
        auth_header (String): Authentication Header.

    Returns:
        None

    """
    source_type = storage.get_source_type(source_id)

    if source_type:
        sources_network = SourcesHTTPClient(auth_header, source_id)
    else:
        LOG.info(f"Source ID not found for ID: {source_id}")
        return

    try:
        if source_type == Provider.PROVIDER_OCP:
            source_details = sources_network.get_source_details()
            if source_details.get("source_ref"):
                authentication = {
                    "resource_name": source_details.get("source_ref")
                }
            else:
                raise SourcesHTTPClientError("Unable to find Cluster ID")
        elif source_type in (Provider.PROVIDER_AWS,
                             Provider.PROVIDER_AWS_LOCAL):
            authentication = {
                "resource_name": sources_network.get_aws_role_arn()
            }
        elif source_type in (Provider.PROVIDER_AZURE,
                             Provider.PROVIDER_AZURE_LOCAL):
            authentication = {
                "credentials": sources_network.get_azure_credentials()
            }
        else:
            LOG.error(f"Unexpected source type: {source_type}")
            return
        storage.add_provider_sources_auth_info(source_id, authentication)
        storage.clear_update_flag(source_id)
        LOG.info(f"Authentication attached to Source ID: {source_id}")
    except SourcesHTTPClientError as error:
        LOG.info(
            f"Authentication info not available for Source ID: {source_id}")
        sources_network.set_source_status(str(error))
예제 #5
0
def save_auth_info(auth_header, source_id):
    """
    Store Sources Authentication information given an Source ID.

    This method is called when a Cost Management application is
    attached to a given Source as well as when an Authentication
    is created.  We have to handle both cases since an
    Authentication.create event can occur before a Source is
    attached to the Cost Management application.

    Authentication is stored in the Sources database table.

    Args:
        source_id (Integer): Platform Sources ID.
        auth_header (String): Authentication Header.

    Returns:
        None

    """
    source_type = storage.get_source_type(source_id)

    if source_type:
        sources_network = SourcesHTTPClient(auth_header, source_id)
    else:
        LOG.info(f'Source ID not found for ID: {source_id}')
        return

    try:
        if source_type == 'OCP':
            source_details = sources_network.get_source_details()
            authentication = {'resource_name': source_details.get('uid')}
        elif source_type == 'AWS':
            authentication = {
                'resource_name': sources_network.get_aws_role_arn()
            }
        elif source_type == 'AZURE':
            authentication = {
                'credentials': sources_network.get_azure_credentials()
            }
        else:
            LOG.error(f'Unexpected source type: {source_type}')
            return
        storage.add_provider_sources_auth_info(source_id, authentication)
    except SourcesHTTPClientError:
        LOG.info(
            f'Authentication info not available for Source ID: {source_id}')
예제 #6
0
    def test_add_provider_sources_auth_info(self):
        """Test to add authentication to a source."""
        test_source_id = 3
        test_endpoint_id = 4
        test_authentication = {'resource_name': 'arn:test'}
        aws_obj = Sources(source_id=test_source_id,
                          auth_header=self.test_header,
                          offset=3,
                          endpoint_id=test_endpoint_id,
                          source_type='AWS',
                          name='Test AWS Source',
                          billing_source={'bucket': 'test-bucket'})
        aws_obj.save()

        storage.add_provider_sources_auth_info(test_source_id, test_authentication)
        response = Sources.objects.filter(source_id=test_source_id).first()
        self.assertEquals(response.authentication, test_authentication)
예제 #7
0
    def test_add_provider_sources_auth_info(self):
        """Test to add authentication to a source."""
        test_source_id = 3
        test_authentication = {"role_arn": "arn:test"}
        aws_obj = Sources(
            source_id=test_source_id,
            auth_header=self.test_header,
            offset=3,
            source_type=Provider.PROVIDER_AWS,
            name="Test AWS Source",
            billing_source={"bucket": "test-bucket"},
        )
        aws_obj.save()

        storage.add_provider_sources_auth_info(test_source_id, test_authentication)
        response = Sources.objects.filter(source_id=test_source_id).first()
        self.assertEquals(response.authentication, test_authentication)
예제 #8
0
def save_auth_info(auth_header, source_id):
    """
    Store Sources Authentication information given an Source ID.

    This method is called when a Cost Management application is
    attached to a given Source as well as when an Authentication
    is created.  We have to handle both cases since an
    Authentication.create event can occur before a Source is
    attached to the Cost Management application.

    Authentication is stored in the Sources database table.

    Args:
        source_id (Integer): Platform Sources ID.
        auth_header (String): Authentication Header.

    Returns:
        None

    """
    source_type = storage.get_source_type(source_id)

    if not source_type:
        LOG.info(f"Source ID not found for ID: {source_id}")
        return

    sources_network = SourcesHTTPClient(auth_header, source_id)

    try:
        authentication = get_authentication(source_type, sources_network)
    except SourcesHTTPClientError as error:
        LOG.info(
            f"Authentication info not available for Source ID: {source_id}")
        sources_network.set_source_status(error)
    else:
        if not authentication:
            return
        storage.add_provider_sources_auth_info(source_id, authentication)
        storage.clear_update_flag(source_id)
        LOG.info(f"Authentication attached to Source ID: {source_id}")
예제 #9
0
    def save_credentials(self):
        """Store Sources Authentication information."""
        LOG.info(f"[save_credentials] starting for source_id {self.source_id} ...")
        source_type = storage.get_source_type(self.source_id)

        if not source_type:
            LOG.info(f"[save_credentials] source_type not found for source_id: {self.source_id}")
            return

        sources_network = self.get_sources_client()

        try:
            authentication = {"credentials": sources_network.get_credentials(source_type)}
        except SourcesHTTPClientError as error:
            LOG.info(f"[save_credentials] authentication info not available for source_id: {self.source_id}")
            sources_network.set_source_status(error)
            raise error
        else:
            if not authentication.get("credentials"):  # TODO: is this check needed?
                return
            result = bool(storage.add_provider_sources_auth_info(self.source_id, authentication))
            LOG.info(f"[save_credentials] complete for source_id: {self.source_id}: {result}")
            return result