Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate ADGraph to MSGraph #1325

Merged
merged 38 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
03e5e41
migrate to msgraph
xuzhang3 Sep 13, 2023
602173c
migrate to msgraph
xuzhang3 Sep 14, 2023
1c95ce0
migrate to msgraph
xuzhang3 Sep 14, 2023
068dc8e
migrate to ms graph API
xuzhang3 Sep 21, 2023
c97b054
ad group info
xuzhang3 Sep 26, 2023
f485ff4
merge dev
xuzhang3 Oct 8, 2023
0543e81
app info and group info
xuzhang3 Oct 8, 2023
a90df86
migrate to msgraph
xuzhang3 Oct 8, 2023
34d33e7
spn msgraph
xuzhang3 Oct 8, 2023
145299a
migrate AD to MS graph
xuzhang3 Oct 11, 2023
6c88ad0
Migrate AAD API to MSGraph API for ServicePrincipal, User and Passwor…
neil-yechenwei Oct 11, 2023
5721adf
cli auth
xuzhang3 Oct 11, 2023
b58ec8f
auth with CLI
xuzhang3 Oct 12, 2023
7d82076
Get the existing user after create (#1286)
neil-yechenwei Oct 16, 2023
0449baf
Add advanced query filter to get the existing user by list (#1287)
neil-yechenwei Oct 16, 2023
43b4163
get app by app id
xuzhang3 Oct 17, 2023
3a09918
fix lint
xuzhang3 Oct 17, 2023
ada96c1
fix sanity
xuzhang3 Oct 18, 2023
67ee47a
fix sanity
xuzhang3 Oct 18, 2023
914a0c2
fix sanity
xuzhang3 Oct 18, 2023
295ca14
fix sanity
xuzhang3 Oct 18, 2023
fed0bba
deprecate tenant ID
xuzhang3 Oct 19, 2023
7e8f680
Mark the property for deprecating and renaming property (#1294)
neil-yechenwei Oct 23, 2023
5f67f32
deprecate tenant ID
xuzhang3 Oct 23, 2023
28eaf00
merge upstream
xuzhang3 Nov 2, 2023
29ccc71
msgraph sdk v1.0.0
xuzhang3 Nov 2, 2023
d11956a
deprecate tenant ID
xuzhang3 Nov 3, 2023
c0dbd52
update doc
xuzhang3 Nov 3, 2023
2e45c4b
fix lint
xuzhang3 Nov 3, 2023
b38b1aa
fix lint
xuzhang3 Nov 3, 2023
3f3d0b2
remove blank space
xuzhang3 Nov 3, 2023
276a7bb
fix lint
xuzhang3 Nov 3, 2023
8861359
fix lint
xuzhang3 Nov 3, 2023
5758b6d
fix lint
xuzhang3 Nov 3, 2023
9fc506d
Merge branch 'dev' into f/msgraph_beta
xuzhang3 Nov 8, 2023
818af4a
remove tenant
xuzhang3 Nov 8, 2023
c650f81
extend test timeout
xuzhang3 Nov 10, 2023
45013c6
update adapp response
xuzhang3 Nov 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions plugins/module_utils/azure_rm_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ def default_api_version(self):
from azure.mgmt.datafactory import DataFactoryManagementClient
import azure.mgmt.datafactory.models as DataFactoryModel
from azure.identity._credentials import client_secret, user_password, certificate, managed_identity
from azure.identity import AzureCliCredential
from msgraph import GraphServiceClient

except ImportError as exc:
Authentication = object
Expand Down Expand Up @@ -484,8 +486,8 @@ def fail(self, msg, **kwargs):
'''
self.module.fail_json(msg=msg, **kwargs)

def deprecate(self, msg, version=None):
self.module.deprecate(msg, version)
def deprecate(self, msg, version=None, collection_name='azure.azcollection'):
self.module.deprecate(msg, version, collection_name=collection_name)

def log(self, msg, pretty_print=False):
if pretty_print:
Expand Down Expand Up @@ -858,9 +860,11 @@ def get_graphrbac_client(self, tenant_id):
cred = self.azure_auth.azure_credentials
base_url = self.azure_auth._cloud_environment.endpoints.active_directory_graph_resource_id
client = GraphRbacManagementClient(cred, tenant_id, base_url)

return client

def get_msgraph_client(self):
return GraphServiceClient(self.azure_auth.azure_credential_track2)

def get_mgmt_svc_client(self, client_type, base_url=None, api_version=None, suppress_subscription_id=False, is_track2=False):
self.log('Getting management service client {0}'.format(client_type.__name__))
self.check_client_version(client_type)
Expand Down Expand Up @@ -1669,12 +1673,13 @@ def _get_azure_cli_credentials(self, subscription_id=None, resource=None):
except Exception as exc:
self.fail("Failed to load CLI profile {0}.".format(str(exc)))

credentials, subscription_id, tenant = profile.get_login_credentials(
subscription_id=subscription_id, resource=resource)
cred, subscription_id, tenant = profile.get_login_credentials(
subscription_id=subscription_id)
cloud_environment = get_cli_active_cloud()

az_cli = AzureCliCredential()
cli_credentials = {
'credentials': credentials,
'credentials': az_cli,
'subscription_id': subscription_id,
'cloud_environment': cloud_environment
}
Expand Down
48 changes: 21 additions & 27 deletions plugins/modules/azure_rm_account_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,13 @@


try:
from azure.graphrbac import GraphRbacManagementClient
from azure.graphrbac.models import GraphErrorException
import asyncio
from msgraph.generated.education.me.user.user_request_builder import UserRequestBuilder
except ImportError:
# This is handled in azure_rm_common
pass

from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMAuth


class AzureRMAccountInfo(AzureRMModuleBase):
Expand All @@ -126,19 +125,15 @@ def __init__(self):

# Different return info is gathered using 2 different clients
# 1. All except "user" section of the return value uses azure.mgmt.subsctiption.operations.subscriptionoperations
# 2. "user" section of the return value uses different client (graphrbac)
# 2. "user" section of the return value uses different client (GraphServiceClient)

super(AzureRMAccountInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
supports_check_mode=True,
supports_tags=False,
is_ad_resource=False)

def exec_module(self, **kwargs):

result = []
result = self.list_items()

self.results['account_info'] = result
self.results['account_info'] = self.list_items()
return self.results

def list_items(self):
Expand Down Expand Up @@ -179,41 +174,40 @@ def list_items(self):
results['state'] = subscription_list_response[0].state
results['managedByTenants'] = self.get_managed_by_tenants_list(subscription_list_response[0].managed_by_tenants)
results['environmentName'] = self.azure_auth._cloud_environment.name
results['user'] = self.get_aduser_info(subscription_list_response[0].tenant_id)
results['user'] = self.get_aduser_info()

return results

def get_managed_by_tenants_list(self, object_list):

return [dict(tenantId=item.tenant_id) for item in object_list]

def get_aduser_info(self, tenant_id):
def get_aduser_info(self):

# Create GraphRbacManagementClient for getting
# Create GraphServiceClient for getting
# "user": {
# "name": "[email protected]",
# "type": "user"self.
# "type": "Member"
# }

# Makes use of azure graphrbac
# https://docs.microsoft.com/en-us/python/api/overview/azure/microsoft-graph?view=azure-python#client-library
# Makes use of azure MSGraph
# https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http

user = {}
self.azure_auth_graphrbac = AzureRMAuth(is_ad_resource=True)
cred = self.azure_auth_graphrbac.azure_credentials
base_url = self.azure_auth_graphrbac._cloud_environment.endpoints.active_directory_graph_resource_id
client = GraphRbacManagementClient(cred, tenant_id, base_url)

try:
user_info = client.signed_in_user.get()
user['name'] = user_info.user_principal_name
user['type'] = user_info.object_type

except GraphErrorException as e:
self.fail("failed to get ad user info {0}".format(str(e)))

user_info = asyncio.get_event_loop().run_until_complete(self.getAccount())
user['name'] = user_info.user_principal_name
user['type'] = user_info.user_type
return user

async def getAccount(self):
return await self.get_msgraph_client().me.get(
request_configuration=UserRequestBuilder.UserRequestBuilderGetRequestConfiguration(
query_parameters=UserRequestBuilder.UserRequestBuilderGetQueryParameters(
select=["userType", "userPrincipalName", "postalCode", "identities"], ),
)
)


def main():
AzureRMAccountInfo()
Expand Down
Loading