Esempio n. 1
0
def validate_license(conductr_version, core_addr, nr_of_agent_instances,
                     license_file):
    """
    Validates the license contained within `license_file`.
    Waits for ConductR to be available on the `core_addr` by checking the `/members` endpoint.
    Once ConductR is available, `license_file` is posted, and subsequently retrieved from ConductR as JSON.

    The resulting JSON payload is then validated:
    - Version is in the list of what is allowed.
    - Number of agent requested < what is allowed.
    - Expiry: license must not expire.
    - Grants must include `conductr`.

     If validation failed, sandbox will be stopped and `LicenseValidationError` exception will be raised.

    :param conductr_version: Version of ConductR to be run as sandbox.
    :param core_addr: Host address of one of the core node.
    :param nr_of_agent_instances: Number of agents requested.
    :param license_file: License file to be validated.
    :return:
    """

    args = LicenseArgs(core_addr)

    has_license_support, license_existing = license.get_license(args)
    if has_license_support:
        if os.path.exists(license_file):
            license.post_license(args, license_file)

        has_license_support, license_updated = license.get_license(args)
        validate_license_data(conductr_version, nr_of_agent_instances,
                              license_updated)
def load_license(args):
    log = logging.getLogger(__name__)

    if license.get_license(args) == (False, None):
        if not args.quiet:
            # Only log the info if quite is False.
            # This is the default when the user executes conduct load-license
            # If the function is called from another Python function it might makes sense to set quiet to True
            # to do not print any license output for ConductR versions prior to 2.1
            log.info('conduct load-license is only supported by ConductR 2.1+')
        return True
    else:
        license_file = DEFAULT_LICENSE_FILE
        if args.offline_mode:
            log.info('Skipping downloading license from Lightbend.com')
        else:
            license.download_license(args, save_to=license_file)

        if os.path.exists(license_file):
            log.info('Loading license into ConductR at {}'.format(args.host))
            license.post_license(args, license_file)

            _, uploaded_license = license.get_license(args)
            if uploaded_license:
                license_to_display = license.format_license(uploaded_license)
                log.info('\n{}\n'.format(license_to_display))
                log.info('License successfully loaded')
                return True
            else:
                raise LicenseLoadError('Unable to find recently loaded license')
        else:
            raise LicenseLoadError('Please ensure the license file exists at {}'.format(license_file))
Esempio n. 3
0
def display_bundles(args, bundles):
    if args.quiet:
        display_bundles_quiet(args, bundles)
    else:
        is_license_success, conductr_license = license.get_license(args)
        display_bundles_default(args, is_license_success, conductr_license,
                                bundles)

    return True
Esempio n. 4
0
    def test_endpoint_not_supported(self):
        mock_get = self.respond_with(status_code=503)

        input_args = MagicMock(**self.args)

        with patch('conductr_cli.conduct_request.get', mock_get):
            is_license_success, license_result = license.get_license(
                input_args)
            self.assertFalse(is_license_success)
            self.assertIsNone(license_result)

        mock_get.assert_called_once_with(False,
                                         '10.0.0.1',
                                         'https://10.0.0.1:9005/v2/license',
                                         auth=self.auth)
Esempio n. 5
0
    def test_license_found(self):
        mock_get = self.respond_with(status_code=200,
                                     text=self.license_json_text)

        input_args = MagicMock(**self.args)

        with patch('conductr_cli.conduct_request.get', mock_get):
            is_license_success, license_result = license.get_license(
                input_args)
            self.assertTrue(is_license_success)
            self.assertEqual(self.license, license_result)

        mock_get.assert_called_once_with(False,
                                         '10.0.0.1',
                                         'https://10.0.0.1:9005/v2/license',
                                         auth=self.auth)
Esempio n. 6
0
    def test_dcos_error_endpoint_not_supported(self):
        mock_response = MagicMock(**{
            'status_code': 503,
            'url': 'dummy',
            'reason': 'test',
        })
        mock_get = MagicMock(side_effect=DCOSHTTPException(mock_response))

        input_args = MagicMock(**self.args)

        with patch('conductr_cli.conduct_request.get', mock_get):
            is_license_success, license_result = license.get_license(
                input_args)
            self.assertFalse(is_license_success)
            self.assertIsNone(license_result)

        mock_get.assert_called_once_with(False,
                                         '10.0.0.1',
                                         'https://10.0.0.1:9005/v2/license',
                                         auth=self.auth)