def test_raise_error_when_not_aws_or_azure(self, m_get_cloud_type):
     """Raise appropriate error when unable to determine cloud_type."""
     m_get_cloud_type.return_value = "unsupported-cloud"
     with pytest.raises(exceptions.UserFacingError) as excinfo:
         cloud_instance_factory()
     error_msg = status.MESSAGE_UNSUPPORTED_AUTO_ATTACH_CLOUD_TYPE.format(
         cloud_type="unsupported-cloud")
     assert error_msg == str(excinfo.value)
 def test_raise_error_when_unable_to_get_cloud_type(self, m_get_cloud_type):
     """Raise appropriate error when unable to determine cloud_type."""
     m_get_cloud_type.return_value = None
     with pytest.raises(exceptions.UserFacingError) as excinfo:
         cloud_instance_factory()
     assert 1 == m_get_cloud_type.call_count
     assert status.MESSAGE_UNABLE_TO_DETERMINE_CLOUD_TYPE == str(
         excinfo.value)
Esempio n. 3
0
 def test_raise_error_when_unable_to_get_cloud_type(self, m_get_cloud_type):
     """Raise appropriate error when unable to determine cloud_type."""
     m_get_cloud_type.return_value = (
         None,
         NoCloudTypeReason.NO_CLOUD_DETECTED,
     )
     with pytest.raises(exceptions.CloudFactoryNoCloudError):
         cloud_instance_factory()
     assert 1 == m_get_cloud_type.call_count
Esempio n. 4
0
def _get_contract_token_from_cloud_identity(cfg: config.UAConfig) -> str:
    """Detect cloud_type and request a contract token from identity info.

    :param cfg: a ``config.UAConfig`` instance

    :raise NonAutoAttachImageError: When not on an auto-attach image type.
    :raise UrlError: On unexpected connectivity issues to contract
        server or inability to access identity doc from metadata service.
    :raise ContractAPIError: On unexpected errors when talking to the contract
        server.

    :return: contract token obtained from identity doc
    """
    cloud_type = identity.get_cloud_type()
    if cloud_type not in ("aws", ):  # TODO(avoid hard-coding supported types)
        raise exceptions.NonAutoAttachImageError(
            ua_status.MESSAGE_UNSUPPORTED_AUTO_ATTACH_CLOUD_TYPE.format(
                cloud_type=cloud_type))
    instance = identity.cloud_instance_factory()
    contract_client = contract.UAContractClient(cfg)
    pkcs7 = instance.identity_doc
    try:
        # TODO(make this logic cloud-agnostic if possible)
        tokenResponse = contract_client.request_aws_contract_token(pkcs7)
    except contract.ContractAPIError as e:
        if contract.API_ERROR_MISSING_INSTANCE_INFORMATION in e:
            raise exceptions.NonAutoAttachImageError(
                ua_status.MESSAGE_UNSUPPORTED_AUTO_ATTACH)
        raise e
    return tokenResponse["contractToken"]
def _get_contract_token_from_cloud_identity(cfg: config.UAConfig) -> str:
    """Detect cloud_type and request a contract token from identity info.

    :param cfg: a ``config.UAConfig`` instance

    :raise NonAutoAttachImageError: When not on an auto-attach image type.
    :raise UrlError: On unexpected connectivity issues to contract
        server or inability to access identity doc from metadata service.
    :raise ContractAPIError: On unexpected errors when talking to the contract
        server.
    :raise NonAutoAttachImageError: If this cloud type does not have
        auto-attach support.

    :return: contract token obtained from identity doc
    """
    instance = identity.cloud_instance_factory()
    contract_client = contract.UAContractClient(cfg)
    try:
        tokenResponse = contract_client.request_auto_attach_contract_token(
            instance=instance)
    except contract.ContractAPIError as e:
        if contract.API_ERROR_MISSING_INSTANCE_INFORMATION in e:
            raise exceptions.NonAutoAttachImageError(
                ua_status.MESSAGE_UNSUPPORTED_AUTO_ATTACH)
        raise e
    return tokenResponse["contractToken"]
Esempio n. 6
0
    def test_raise_error_when_not_viable_for_ubuntu_pro(
            self, m_get_cloud_type, cloud_type):
        """Raise error when AWS or Azure instance is not viable auto-attach."""
        m_get_cloud_type.return_value = (cloud_type, None)

        def fake_invalid_instance():
            instance = mock.Mock()
            instance.is_viable = False
            return instance

        if cloud_type == "aws":
            M_INSTANCE_PATH = "uaclient.clouds.aws.UAAutoAttachAWSInstance"
        else:
            M_INSTANCE_PATH = "uaclient.clouds.azure.UAAutoAttachAzureInstance"

        with mock.patch(M_INSTANCE_PATH) as m_instance:
            m_instance.side_effect = fake_invalid_instance
            with pytest.raises(exceptions.CloudFactoryNonViableCloudError):
                cloud_instance_factory()
    def test_raise_error_when_not_viable_for_ubuntu_pro(
            self, m_get_cloud_type, cloud_type):
        """Raise error when AWS or Azure instance is not viable auto-attach."""
        m_get_cloud_type.return_value = cloud_type

        def fake_invalid_instance():
            instance = mock.Mock()
            instance.is_viable = False
            return instance

        if cloud_type == "aws":
            M_INSTANCE_PATH = "uaclient.clouds.aws.UAAutoAttachAWSInstance"
        else:
            M_INSTANCE_PATH = "uaclient.clouds.azure.UAAutoAttachAzureInstance"

        with mock.patch(M_INSTANCE_PATH) as m_instance:
            m_instance.side_effect = fake_invalid_instance
            with pytest.raises(exceptions.UserFacingError) as excinfo:
                cloud_instance_factory()
        error_msg = status.MESSAGE_UNSUPPORTED_AUTO_ATTACH
        assert error_msg == str(excinfo.value)
Esempio n. 8
0
def _get_contract_token_from_cloud_identity(cfg: config.UAConfig) -> str:
    """Detect cloud_type and request a contract token from identity info.

    :param cfg: a ``config.UAConfig`` instance

    :raise NonAutoAttachImageError: When not on an auto-attach image type.
    :raise UrlError: On unexpected connectivity issues to contract
        server or inability to access identity doc from metadata service.
    :raise ContractAPIError: On unexpected errors when talking to the contract
        server.
    :raise NonAutoAttachImageError: If this cloud type does not have
        auto-attach support.

    :return: contract token obtained from identity doc
    """
    try:
        instance = identity.cloud_instance_factory()
    except exceptions.UserFacingError as e:
        if cfg.is_attached:
            # We are attached on non-Pro Image, just report already attached
            raise exceptions.AlreadyAttachedError(cfg)
        # Unattached on non-Pro return UserFacing error msg details
        raise e
    current_iid = identity.get_instance_id()
    if cfg.is_attached:
        prev_iid = cfg.read_cache("instance-id")
        if current_iid == prev_iid:
            raise exceptions.AlreadyAttachedError(cfg)
        print("Re-attaching Ubuntu Advantage subscription on new instance")
        if _detach(cfg, assume_yes=True) != 0:
            raise exceptions.UserFacingError(
                ua_status.MESSAGE_DETACH_AUTOMATION_FAILURE
            )
    contract_client = contract.UAContractClient(cfg)
    try:
        tokenResponse = contract_client.request_auto_attach_contract_token(
            instance=instance
        )
    except contract.ContractAPIError as e:
        if e.code and 400 <= e.code < 500:
            raise exceptions.NonAutoAttachImageError(
                ua_status.MESSAGE_UNSUPPORTED_AUTO_ATTACH
            )
        raise e
    if current_iid:
        cfg.write_cache("instance-id", current_iid)

    return tokenResponse["contractToken"]
    def test_return_cloud_instance_on_viable_clouds(self, m_get_cloud_type,
                                                    cloud_type):
        """Return UAAutoAttachInstance when matching cloud_type is viable."""
        m_get_cloud_type.return_value = cloud_type

        fake_instance = mock.Mock()
        fake_instance.is_viable = True

        def fake_viable_instance():
            return fake_instance

        if cloud_type == "azure":
            M_INSTANCE_PATH = "uaclient.clouds.azure.UAAutoAttachAzureInstance"
        else:
            M_INSTANCE_PATH = "uaclient.clouds.aws.UAAutoAttachAWSInstance"

        with mock.patch(M_INSTANCE_PATH) as m_instance:
            m_instance.side_effect = fake_viable_instance
            assert fake_instance == cloud_instance_factory()
Esempio n. 10
0
def poll_for_pro_license(cfg: UAConfig):
    if util.is_config_value_true(config=cfg.cfg,
                                 path_to_value="features.disable_auto_attach"):
        LOG.debug("Configured to not auto attach, shutting down")
        return
    if cfg.is_attached:
        LOG.debug("Already attached, shutting down")
        return
    if not util.is_current_series_lts():
        LOG.debug("Not on LTS, shutting down")
        return

    try:
        cloud = cloud_instance_factory()
    except exceptions.CloudFactoryError:
        LOG.debug("Not on cloud, shutting down")
        return

    if not isinstance(cloud, UAAutoAttachGCPInstance):
        LOG.debug("Not on gcp, shutting down")
        return

    if not cloud.should_poll_for_pro_license():
        LOG.debug("Not on supported instance, shutting down")
        return

    try:
        pro_license_present = cloud.is_pro_license_present(
            wait_for_change=False)
    except exceptions.CancelProLicensePolling:
        LOG.debug("Cancelling polling")
        return
    except exceptions.DelayProLicensePolling:
        # Continue to polling loop anyway and handle error there if it occurs
        # again
        pass
    else:
        if pro_license_present:
            attempt_auto_attach(cfg, cloud)
            return

    if not cfg.poll_for_pro_license:
        LOG.debug("Configured to not poll for pro license, shutting down")
        return

    while True:
        try:
            start = time.time()
            pro_license_present = cloud.is_pro_license_present(
                wait_for_change=True)
            end = time.time()
        except exceptions.CancelProLicensePolling:
            LOG.debug("Cancelling polling")
            return
        except exceptions.DelayProLicensePolling:
            time.sleep(cfg.polling_error_retry_delay)
            continue
        else:
            if cfg.is_attached:
                # This could have changed during the long poll or sleep
                LOG.debug("Already attached, shutting down")
                return
            if pro_license_present:
                attempt_auto_attach(cfg, cloud)
                return
            if end - start < 10:
                LOG.debug(
                    "wait_for_change returned quickly and no pro license"
                    " present. Waiting {} seconds before polling again".format(
                        cfg.polling_error_retry_delay))
                time.sleep(cfg.polling_error_retry_delay)
                continue
Esempio n. 11
0
 def test_raise_error_when_not_supported(self, m_get_cloud_type):
     """Raise appropriate error when unable to determine cloud_type."""
     m_get_cloud_type.return_value = ("unsupported-cloud", None)
     with pytest.raises(exceptions.CloudFactoryUnsupportedCloudError):
         cloud_instance_factory()