def _auto_register(cp_provider, log):
    """
    Try to perform auto-registration
    :param cp_provider: provider of connection to candlepin server
    :param log: logging object
    :return: None
    """
    log.debug("Trying to do auto-registration of this system")

    identity = inj.require(inj.IDENTITY)
    if identity.is_valid() is True:
        log.debug('System already registered. Skipping auto-registration')
        return

    log.debug('Trying to detect cloud provider')

    # Try to detect cloud provider first
    cloud_list = detect_cloud_provider()
    if len(cloud_list) == 0:
        log.warning('This system does not run on any supported cloud provider. Skipping auto-registration')
        sys.exit(-1)

    # When some cloud provider(s) was detected, then try to collect metadata
    # and signature
    cloud_info = collect_cloud_info(cloud_list)
    if len(cloud_info) == 0:
        log.warning('It was not possible to collect any cloud metadata. Unable to perform auto-registration')
        sys.exit(-1)

    # Get connection not using any authentication
    cp = cp_provider.get_no_auth_cp()

    # Try to get JWT token from candlepin (cloud registration adapter)
    try:
        jwt_token = cp.getJWToken(
            cloud_id=cloud_info['cloud_id'],
            metadata=cloud_info['metadata'],
            signature=cloud_info['signature']
        )
    except Exception as err:
        log.error('Unable to get JWT token: {err}'.format(err=str(err)))
        log.warning('Canceling auto-registration')
        sys.exit(-1)

    # Try to register using JWT token
    register_service = RegisterService(cp=cp)
    # Organization ID is set to None, because organization ID is
    # included in JWT token
    try:
        register_service.register(org=None, jwt_token=jwt_token)
    except Exception as err:
        log.error("Unable to auto-register: {err}".format(err=err))
        sys.exit(-1)
    else:
        log.debug("Auto-registration performed successfully")
        sys.exit(0)
Exemplo n.º 2
0
 def test_detect_cloud_provider_gcp(self):
     """
     Test the case, when detecting of gcp works as expected
     """
     host_facts = {'virt.is_guest': True, 'virt.host_type': 'kvm'}
     hw_facts = {'dmi.bios.vendor': 'Google', 'dmi.bios.version': 'Google'}
     self.host_fact_collector_instance.get_all.return_value = host_facts
     self.hw_fact_collector_instance.get_all.return_value = hw_facts
     detected_clouds = detect_cloud_provider()
     self.assertEqual(detected_clouds, ['gcp'])
Exemplo n.º 3
0
 def test_detect_cloud_provider_aws(self):
     """
     Test the case, when detecting of aws works as expected
     """
     host_facts = {'virt.is_guest': True, 'virt.host_type': 'kvm'}
     hw_facts = {'dmi.bios.vendor': 'Amazon EC2'}
     self.host_fact_collector_instance.get_all.return_value = host_facts
     self.hw_fact_collector_instance.get_all.return_value = hw_facts
     detected_clouds = detect_cloud_provider()
     self.assertEqual(detected_clouds, ['aws'])
Exemplo n.º 4
0
 def test_detect_cloud_provider_aws_heuristics(self):
     """
     Test the case, when detecting of aws does not work using strong signs, but it is necessary
     to use heuristics method
     """
     host_facts = {'virt.is_guest': True, 'virt.host_type': 'kvm'}
     hw_facts = {'dmi.bios.vendor': 'AWS', 'dmi.bios.version': '1.0'}
     self.host_fact_collector_instance.get_all.return_value = host_facts
     self.hw_fact_collector_instance.get_all.return_value = hw_facts
     detected_clouds = detect_cloud_provider()
     self.assertEqual(detected_clouds, ['aws', 'gcp'])
Exemplo n.º 5
0
 def test_detect_cloud_provider_azure(self):
     """
     Test the case, when detecting of azure works as expected
     """
     host_facts = {
         'virt.is_guest': True,
         'virt.host_type': 'hyperv',
     }
     hw_facts = {
         'dmi.bios.vendor': 'Foo company',
         'dmi.bios.version': '1.0',
         'dmi.chassis.asset_tag': '7783-7084-3265-9085-8269-3286-77'
     }
     self.host_fact_collector_instance.get_all.return_value = host_facts
     self.hw_fact_collector_instance.get_all.return_value = hw_facts
     detected_clouds = detect_cloud_provider()
     self.assertEqual(detected_clouds, ['azure'])
Exemplo n.º 6
0
 def test_detect_cloud_provider_azure_heuristics(self):
     """
     Test the case, when detecting of azure does not work using strong signs, but it is necessary
     to use heuristics method
     """
     host_facts = {
         'virt.is_guest': True,
         'virt.host_type': 'hyperv',
     }
     hw_facts = {
         'dmi.bios.vendor': 'Microsoft',
         'dmi.bios.version': '1.0',
         'dmi.system.manufacturer': 'Google',
         'dmi.chassis.manufacturer': 'Amazon'
     }
     self.host_fact_collector_instance.get_all.return_value = host_facts
     self.hw_fact_collector_instance.get_all.return_value = hw_facts
     detected_clouds = detect_cloud_provider()
     self.assertEqual(detected_clouds, ['azure', 'gcp', 'aws'])
Exemplo n.º 7
0
 def test_conclict_in_strong_signs(self):
     """
     Test the case, when cloud providers change strong signs and there is conflict (two providers
     are detected using strong signs). In such case result using strong signs should be dropped
     and heuristics should be used, because strong signs do not work with probability and original
     order is influenced by the order of classes in 'constant' CLOUD_DETECTORS.
     """
     host_facts = {
         'virt.is_guest': True,
         'virt.host_type': 'kvm',
     }
     hw_facts = {
         'dmi.bios.vendor': 'Google',
         'dmi.bios.version': 'Google',
         'dmi.chassis.asset_tag': '7783-7084-3265-9085-8269-3286-77',
         'dmi.chassis.manufacturer': 'Microsoft'
     }
     self.host_fact_collector_instance.get_all.return_value = host_facts
     self.hw_fact_collector_instance.get_all.return_value = hw_facts
     detected_clouds = detect_cloud_provider()
     self.assertEqual(detected_clouds, ['gcp', 'azure', 'aws'])