From f568988e3c85fbe3b8555e20ce364a2bb3ae25d8 Mon Sep 17 00:00:00 2001 From: kmcdonough Date: Tue, 5 Mar 2024 12:26:02 -0500 Subject: [PATCH 1/6] Fix: list all service principals --- .../azure_rm_adserviceprincipal_info.py | 59 ++++++++++--------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/plugins/modules/azure_rm_adserviceprincipal_info.py b/plugins/modules/azure_rm_adserviceprincipal_info.py index f67d9a4a6..a053f8040 100644 --- a/plugins/modules/azure_rm_adserviceprincipal_info.py +++ b/plugins/modules/azure_rm_adserviceprincipal_info.py @@ -43,30 +43,34 @@ ''' RETURN = ''' -app_display_name: - description: - - Object's display name or its prefix. - type: str - returned: always - sample: sp -app_id: - description: - - The application ID. - returned: always - type: str - sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx -app_role_assignment_required: - description: - - Whether the Role of the Service Principal is set. - type: bool - returned: always - sample: false -object_id: - description: - - It's service principal's object ID. - returned: always - type: str - sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx +service_principals: + type: list + elements: dict + contains: + app_display_name: + description: + - Object's display name or its prefix. + type: str + returned: always + sample: sp + app_id: + description: + - The application ID. + returned: always + type: str + sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx + app_role_assignment_required: + description: + - Whether the Role of the Service Principal is set. + type: bool + returned: always + sample: false + object_id: + description: + - It's service principal's object ID. + returned: always + type: str + sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx ''' @@ -132,10 +136,11 @@ async def get_service_principal(self): return await self._client.service_principals.by_service_principal_id(self.object_id).get() async def get_service_principals(self): + kwargs = {} + if self.app_id is not None: + kwargs = {"filter": "servicePrincipalNames/any(c:c eq '{0}')".format(self.app_id)} request_configuration = ServicePrincipalsRequestBuilder.ServicePrincipalsRequestBuilderGetRequestConfiguration( - query_parameters=ServicePrincipalsRequestBuilder.ServicePrincipalsRequestBuilderGetQueryParameters( - filter="servicePrincipalNames/any(c:c eq '{0}')".format(self.app_id), - ), + query_parameters=ServicePrincipalsRequestBuilder.ServicePrincipalsRequestBuilderGetQueryParameters(**kwargs) ) return await self._client.service_principals.get(request_configuration=request_configuration) From 099402e54e445bc196013da2c7ae46bc6cb52579 Mon Sep 17 00:00:00 2001 From: kmcdonough Date: Tue, 5 Mar 2024 12:43:03 -0500 Subject: [PATCH 2/6] Fix: page replies --- .../azure_rm_adserviceprincipal_info.py | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/plugins/modules/azure_rm_adserviceprincipal_info.py b/plugins/modules/azure_rm_adserviceprincipal_info.py index a053f8040..e38355096 100644 --- a/plugins/modules/azure_rm_adserviceprincipal_info.py +++ b/plugins/modules/azure_rm_adserviceprincipal_info.py @@ -113,8 +113,7 @@ def exec_module(self, **kwargs): try: if self.object_id is None: - sps = asyncio.get_event_loop().run_until_complete(self.get_service_principals()) - service_principals = list(sps.value) + service_principals = asyncio.get_event_loop().run_until_complete(self.get_service_principals()) else: service_principals = [asyncio.get_event_loop().run_until_complete(self.get_service_principal())] @@ -138,11 +137,20 @@ async def get_service_principal(self): async def get_service_principals(self): kwargs = {} if self.app_id is not None: - kwargs = {"filter": "servicePrincipalNames/any(c:c eq '{0}')".format(self.app_id)} - request_configuration = ServicePrincipalsRequestBuilder.ServicePrincipalsRequestBuilderGetRequestConfiguration( - query_parameters=ServicePrincipalsRequestBuilder.ServicePrincipalsRequestBuilderGetQueryParameters(**kwargs) - ) - return await self._client.service_principals.get(request_configuration=request_configuration) + request_configuration = ServicePrincipalsRequestBuilder.ServicePrincipalsRequestBuilderGetRequestConfiguration( + query_parameters=ServicePrincipalsRequestBuilder.ServicePrincipalsRequestBuilderGetQueryParameters( + filter="servicePrincipalNames/any(c:c eq '{0}')".format(self.app_id)) + ) + kwargs['request_configuration'] = request_configuration + service_principals = [] + response = self._client.service_principals.get(**kwargs) + if response: + service_principals += response.value + while response is not None and response.odata_next_link is not None: + response = await self._client.service_principals.with_url(response.odata_next_link).get(**kwargs) + if response: + service_principals += response.value + return service_principals def main(): From d898864e6f067f136d6b3eb7261545654402b080 Mon Sep 17 00:00:00 2001 From: kmcdonough Date: Wed, 6 Mar 2024 10:56:20 -0500 Subject: [PATCH 3/6] Docs: address PR comments --- plugins/modules/azure_rm_adserviceprincipal_info.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/plugins/modules/azure_rm_adserviceprincipal_info.py b/plugins/modules/azure_rm_adserviceprincipal_info.py index e38355096..17fde24dd 100644 --- a/plugins/modules/azure_rm_adserviceprincipal_info.py +++ b/plugins/modules/azure_rm_adserviceprincipal_info.py @@ -40,12 +40,19 @@ - name: get ad sp info azure_rm_adserviceprincipal_info: app_id: "{{ app_id }}" + +- name: get all service principals + azure_rm_adserviceprincipal_info: ''' RETURN = ''' service_principals: + description: + - A list of service principals in the tenant. If app_id or object_id is set, the maximum length + of this list should be one. type: list elements: dict + returned: always contains: app_display_name: description: From b36acae64d22477eae490c86b157feea1fa0bfbe Mon Sep 17 00:00:00 2001 From: kmcdonough Date: Wed, 6 Mar 2024 12:23:03 -0500 Subject: [PATCH 4/6] Fix: missing `await` --- plugins/modules/azure_rm_adserviceprincipal_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/azure_rm_adserviceprincipal_info.py b/plugins/modules/azure_rm_adserviceprincipal_info.py index 17fde24dd..b2b6d0977 100644 --- a/plugins/modules/azure_rm_adserviceprincipal_info.py +++ b/plugins/modules/azure_rm_adserviceprincipal_info.py @@ -150,7 +150,7 @@ async def get_service_principals(self): ) kwargs['request_configuration'] = request_configuration service_principals = [] - response = self._client.service_principals.get(**kwargs) + response = await self._client.service_principals.get(**kwargs) if response: service_principals += response.value while response is not None and response.odata_next_link is not None: From 5613df5a8f8fb4f385c2c734cdf55dd1c8f106b3 Mon Sep 17 00:00:00 2001 From: Kent McDonough Date: Thu, 14 Mar 2024 15:48:54 -0400 Subject: [PATCH 5/6] Update plugins/modules/azure_rm_adserviceprincipal_info.py Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com> --- plugins/modules/azure_rm_adserviceprincipal_info.py | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/modules/azure_rm_adserviceprincipal_info.py b/plugins/modules/azure_rm_adserviceprincipal_info.py index b2b6d0977..518bf4e52 100644 --- a/plugins/modules/azure_rm_adserviceprincipal_info.py +++ b/plugins/modules/azure_rm_adserviceprincipal_info.py @@ -40,7 +40,6 @@ - name: get ad sp info azure_rm_adserviceprincipal_info: app_id: "{{ app_id }}" - - name: get all service principals azure_rm_adserviceprincipal_info: ''' From 8fb097ca5854be9936822e434c5ed93b30132600 Mon Sep 17 00:00:00 2001 From: Kent McDonough Date: Fri, 15 Mar 2024 10:05:05 -0400 Subject: [PATCH 6/6] Update plugins/modules/azure_rm_adserviceprincipal_info.py Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com> --- plugins/modules/azure_rm_adserviceprincipal_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/azure_rm_adserviceprincipal_info.py b/plugins/modules/azure_rm_adserviceprincipal_info.py index 518bf4e52..d2acfac04 100644 --- a/plugins/modules/azure_rm_adserviceprincipal_info.py +++ b/plugins/modules/azure_rm_adserviceprincipal_info.py @@ -46,7 +46,7 @@ RETURN = ''' service_principals: - description: + description: - A list of service principals in the tenant. If app_id or object_id is set, the maximum length of this list should be one. type: list