diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index ef36622..4334720 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -48,7 +48,7 @@ jobs: with: python-version: ${{ matrix.python-version }} - name: Install apt dependencies - run: sudo apt-get install -y python3-dev openssl libssl-dev gcc pkg-config libffi-dev libxml2-dev libxmlsec1-dev + run: sudo apt-get update && sudo apt-get install -y python3-dev openssl libssl-dev gcc pkg-config libffi-dev libxml2-dev libxmlsec1-dev - name: Install dependencies run: pip install -r requirements.txt && pip install -e . - name: Run python tests diff --git a/confidant_client/__init__.py b/confidant_client/__init__.py index 18a6df9..453f508 100644 --- a/confidant_client/__init__.py +++ b/confidant_client/__init__.py @@ -404,6 +404,44 @@ def get_credential(self, id): ret['result'] = True return ret + def get_credential_services(self, id): + """ + Get the list of services that currently use this credential + and whether they are enabled or not + """ + # Return a dict, always with an attribute that specifies whether or not + # the function was able to successfully get a result. + ret = {'result': False} + + # Make a request to confidant with the provided url, to fetch the + # services using the credential providing the credential id and + # base64 encoded token for authentication. + try: + response = self._execute_request( + 'get', + '{0}/v1/credentials/{1}/services'.format(self.config['url'], + id), + expected_return_codes=[200, 404] + ) + except RequestExecutionError: + logging.exception('Error with executing request') + return ret + + if response.status_code == 404: + logging.debug('Credential not found in confidant.') + ret['result'] = False + return ret + + try: + data = response.json() + except ValueError: + logging.error('Received badly formatted json data from confidant.') + return ret + + ret['data'] = data + ret['result'] = True + return ret + def update_credential( self, id, diff --git a/setup.py b/setup.py index af0d197..4452182 100644 --- a/setup.py +++ b/setup.py @@ -18,7 +18,7 @@ setup( name="confidant-client", - version="2.3.0", + version="2.3.1", packages=find_packages(exclude=["test*"]), install_requires=[ # Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) diff --git a/tests/unit/confidant_client/client_test.py b/tests/unit/confidant_client/client_test.py index ad459b8..875850b 100644 --- a/tests/unit/confidant_client/client_test.py +++ b/tests/unit/confidant_client/client_test.py @@ -586,6 +586,48 @@ def test_get_credential_not_found(self): {'result': False} ) + @patch( + 'confidant_client.services.get_boto_client', + MagicMock() + ) + def test_get_credential_services(self): + client = confidant_client.ConfidantClient( + 'http://localhost/', + 'alias/authnz-testing', + {'from': 'confidant-unittest', + 'to': 'test', + 'user_type': 'service'}, + ) + client._get_token = MagicMock() + client.request_session.request = mock_200 + self.assertEqual( + client.get_credential_services( + 'confidant-development' + ), + {'result': True, 'data': {}} + ) + + @patch( + 'confidant_client.services.get_boto_client', + MagicMock() + ) + def test_get_credential_services_not_found(self): + client = confidant_client.ConfidantClient( + 'http://localhost/', + 'alias/authnz-testing', + {'from': 'confidant-unittest', + 'to': 'test', + 'user_type': 'service'}, + ) + client._get_token = MagicMock() + client.request_session.request = mock_404 + self.assertEqual( + client.get_credential_services( + 'confidant-development', + ), + {'result': False} + ) + @patch( 'confidant_client.services.get_boto_client', MagicMock()