Exemple #1
0
def update_auth_details_from_rbac_registration(auth_details, service_name):
    print('Please authenticate with the RBAC service to register this MAAS')
    client = RBACUserClient(auth_details.rbac_url)
    services = {
        service['name']: service
        for service in client.get_registerable_services()
    }
    if service_name is None:
        if not services:
            raise CommandError(
                'No registerable MAAS service on the specified RBAC server')
        service = _pick_service(services)
    else:
        service = services.get(service_name)
        if service is None:
            create_service = prompt_for_choices(
                'A service with the specified name was not found, '
                'do you want to create one? (yes/no) [default=no]? ',
                ['yes', 'no'],
                default='no')
            if create_service == 'no':
                raise CommandError('Registration with RBAC service canceled')
            try:
                service = client.create_service(service_name)
            except APIError as error:
                if error.status_code == 409:
                    raise CommandError(
                        'User not allowed to register this service')
                raise CommandError(str(error))
    _register_service(client, service, auth_details)
    print('Service "{}" registered'.format(service['name']))
Exemple #2
0
def update_auth_details_from_rbac_registration(auth_details, service_name):
    print("Please authenticate with the RBAC service to register this MAAS")
    client = RBACUserClient(auth_details.rbac_url)
    services = {
        service["name"]: service
        for service in client.get_registerable_services()
    }
    if service_name is None:
        if not services:
            raise CommandError(
                "No registerable MAAS service on the specified RBAC server")
        service = _pick_service(services)
    else:
        service = services.get(service_name)
        if service is None:
            create_service = prompt_for_choices(
                "A service with the specified name was not found, "
                "do you want to create one? (yes/no) [default=no]? ",
                ["yes", "no"],
                default="no",
            )
            if create_service == "no":
                raise CommandError("Registration with RBAC service canceled")
            try:
                service = client.create_service(service_name)
            except APIError as error:
                if error.status_code == 409:
                    raise CommandError(
                        "User not allowed to register this service")
                raise CommandError(str(error))
    _register_service(client, service, auth_details)
    print('Service "{}" registered'.format(service["name"]))
Exemple #3
0
def update_auth_details_from_rbac_registration(auth_details, service_name):
    client = RBACUserClient(auth_details.rbac_url)
    services = {
        service['name']: service
        for service in client.get_registerable_services()
    }
    if not services:
        raise CommandError(
            'No registerable MAAS service on the specified RBAC server')
    if service_name is not None:
        service = services.get(service_name)
        if service is None:
            raise CommandError(
                'Service "{}" is not known, available choices: {}'.format(
                    service_name, ', '.join(sorted(services))))

    else:
        service = _pick_service(services)

    _register_service(client, service, auth_details)
    print('Service "{}" registered'.format(service['name']))
Exemple #4
0
 def setUp(self):
     super().setUp()
     self.url = 'https://rbac.example.com'
     self.client = RBACUserClient(self.url)
     self.mock_request = self.patch(requests, 'request')
Exemple #5
0
class TestRBACUserClient(MAASServerTestCase):
    def setUp(self):
        super().setUp()
        self.url = 'https://rbac.example.com'
        self.client = RBACUserClient(self.url)
        self.mock_request = self.patch(requests, 'request')

    def mock_responses(self, *responses):
        response = mock.MagicMock(status_code=200)
        response.json.side_effect = responses
        self.mock_request.return_value = response

    def test__get_maas_product(self):
        maas = {
            '$uri': '/api/rbac/1.0/product/2',
            'label': 'maas',
            'name': 'MAAS',
        }
        products = [
            {
                '$uri': '/api/rbac/1.0/product/1',
                'label': 'product-1',
                'name': 'Product 1',
            },
            maas,
        ]
        self.mock_responses(products)
        self.assertEqual(self.client._get_maas_product(), maas)
        self.assertThat(
            self.mock_request,
            MockCalledOnceWith('GET', 'https://rbac.example.com/api/'
                               'rbac/1.0/product',
                               auth=mock.ANY,
                               cookies=mock.ANY,
                               json=None))

    def test_get_registerable_services(self):
        products = [
            {
                '$uri': '/api/rbac/1.0/product/1',
                'label': 'product-1',
                'name': 'Product 1',
            },
            {
                '$uri': '/api/rbac/1.0/product/2',
                'label': 'maas',
                'name': 'MAAS',
            },
        ]
        maas1 = {
            '$uri': '/api/rbac/1.0/service/3',
            'name': 'maas-1',
            'description': 'MAAS 1',
            'pending': True,
            'product': {
                '$ref': '/api/rbac/1.0/product/2'
            },
        }
        maas2 = {
            '$uri': '/api/rbac/1.0/service/4',
            'name': 'maas-2',
            'description': 'MAAS 2',
            'pending': True,
            'product': {
                '$ref': '/api/rbac/1.0/product/2'
            },
        }
        services = [
            {
                '$uri': '/api/rbac/1.0/service/1',
                'name': 'service-1',
                'description': 'Service 1',
                'pending': True,
                'product': {
                    '$ref': '/api/rbac/1.0/product/1'
                },
            },
            {
                '$uri': '/api/rbac/1.0/service/2',
                'name': 'service-2',
                'description': 'Service 2',
                'pending': True,
                'product': {
                    '$ref': '/api/rbac/1.0/product/1'
                },
            },
            maas1,
            maas2,
        ]
        self.mock_responses(products, services)
        self.assertEqual(self.client.get_registerable_services(),
                         [maas1, maas2])
        self.assertThat(
            self.mock_request,
            MockCallsMatch(
                mock.call('GET',
                          'https://rbac.example.com/api/rbac/1.0/product',
                          auth=mock.ANY,
                          cookies=mock.ANY,
                          json=None),
                mock.call('GET', 'https://rbac.example.com/api/'
                          'rbac/1.0/service/registerable',
                          auth=mock.ANY,
                          cookies=mock.ANY,
                          json=None)))

    def test_register_service(self):
        response = {'url': self.url, 'username': '******'}
        self.mock_responses(response)
        self.assertEqual(
            self.client.register_service('/api/rbac/1.0/service/3',
                                         'dead-beef'), response)
        json = {'public-key': 'dead-beef'}
        self.assertThat(
            self.mock_request,
            MockCalledOnceWith('POST', 'https://rbac.example.com/api/'
                               'rbac/1.0/service/3/credentials',
                               auth=mock.ANY,
                               cookies=mock.ANY,
                               json=json))
Exemple #6
0
class TestRBACUserClient(MAASServerTestCase):
    def setUp(self):
        super().setUp()
        self.url = "https://rbac.example.com"
        self.client = RBACUserClient(self.url)
        self.mock_request = self.patch(requests, "request")

    def mock_responses(self, *responses):
        response = mock.MagicMock(status_code=200)
        response.json.side_effect = responses
        self.mock_request.return_value = response

    def test__get_maas_product(self):
        maas = {
            "$uri": "/api/rbac/v1/product/2",
            "label": "maas",
            "name": "MAAS",
        }
        products = [
            {
                "$uri": "/api/rbac/v1/product/1",
                "label": "product-1",
                "name": "Product 1",
            },
            maas,
        ]
        self.mock_responses(products)
        self.assertEqual(self.client._get_maas_product(), maas)
        self.assertThat(
            self.mock_request,
            MockCalledOnceWith(
                "GET",
                "https://rbac.example.com/api/"
                "rbac/v1/product",
                auth=mock.ANY,
                cookies=mock.ANY,
                json=None,
            ),
        )

    def test_get_registerable_services(self):
        products = [
            {
                "$uri": "/api/rbac/v1/product/1",
                "label": "product-1",
                "name": "Product 1",
            },
            {
                "$uri": "/api/rbac/v1/product/2",
                "label": "maas",
                "name": "MAAS",
            },
        ]
        maas1 = {
            "$uri": "/api/rbac/v1/service/3",
            "name": "maas-1",
            "description": "MAAS 1",
            "pending": True,
            "product": {
                "$ref": "/api/rbac/v1/product/2"
            },
        }
        maas2 = {
            "$uri": "/api/rbac/v1/service/4",
            "name": "maas-2",
            "description": "MAAS 2",
            "pending": True,
            "product": {
                "$ref": "/api/rbac/v1/product/2"
            },
        }
        services = [
            {
                "$uri": "/api/rbac/v1/service/1",
                "name": "service-1",
                "description": "Service 1",
                "pending": True,
                "product": {
                    "$ref": "/api/rbac/v1/product/1"
                },
            },
            {
                "$uri": "/api/rbac/v1/service/2",
                "name": "service-2",
                "description": "Service 2",
                "pending": True,
                "product": {
                    "$ref": "/api/rbac/v1/product/1"
                },
            },
            maas1,
            maas2,
        ]
        self.mock_responses(products, services)
        self.assertEqual(self.client.get_registerable_services(),
                         [maas1, maas2])
        self.assertThat(
            self.mock_request,
            MockCallsMatch(
                mock.call(
                    "GET",
                    "https://rbac.example.com/api/rbac/v1/product",
                    auth=mock.ANY,
                    cookies=mock.ANY,
                    json=None,
                ),
                mock.call(
                    "GET",
                    "https://rbac.example.com/api/"
                    "rbac/v1/service/registerable",
                    auth=mock.ANY,
                    cookies=mock.ANY,
                    json=None,
                ),
            ),
        )

    def test_register_service(self):
        response = {"url": self.url, "username": "******"}
        self.mock_responses(response)
        self.assertEqual(
            self.client.register_service("/api/rbac/v1/service/3",
                                         "dead-beef"),
            response,
        )
        json = {"public-key": "dead-beef"}
        self.assertThat(
            self.mock_request,
            MockCalledOnceWith(
                "POST",
                "https://rbac.example.com/api/"
                "rbac/v1/service/3/credentials",
                auth=mock.ANY,
                cookies=mock.ANY,
                json=json,
            ),
        )

    def test_create_service(self):
        products = [{
            "$uri": "/api/rbac/v1/product/1",
            "label": "maas",
            "name": "MAAS"
        }]
        maas = {
            "$uri": "/api/rbac/v1/service/2",
            "name": "maas",
            "description": "",
            "pending": True,
            "product": {
                "$ref": "/api/rbac/v1/product/1"
            },
        }
        self.mock_responses(products, maas)
        self.assertEqual(self.client.create_service("maas"), maas)
        json = {"name": "maas", "product": {"$ref": "/api/rbac/v1/product/1"}}
        self.assertThat(
            self.mock_request,
            MockCallsMatch(
                mock.call(
                    "GET",
                    "https://rbac.example.com/api/rbac/v1/product",
                    auth=mock.ANY,
                    cookies=mock.ANY,
                    json=None,
                ),
                mock.call(
                    "POST",
                    "https://rbac.example.com/api/rbac/v1/service",
                    auth=mock.ANY,
                    cookies=mock.ANY,
                    json=json,
                ),
            ),
        )