22
33from prowler .lib .logger import logger
44from prowler .lib .powershell .powershell import PowerShellSession
5- from prowler .providers .m365 .exceptions .exceptions import (
6- M365CertificateCreationError ,
7- M365GraphConnectionError ,
8- )
5+ from prowler .providers .m365 .exceptions .exceptions import M365CertificateCreationError
96from prowler .providers .m365 .lib .jwt .jwt_decoder import decode_msal_token
107from prowler .providers .m365 .models import M365Credentials , M365IdentityInfo
118
@@ -138,79 +135,6 @@ def execute_connect(self, command: str) -> str:
138135 result = self .execute (command , timeout = connect_timeout )
139136 return result or "'execute_connect' command timeout reached"
140137
141- def test_credentials (self , credentials : M365Credentials ) -> bool :
142- """
143- Test Microsoft 365 credentials by attempting to authenticate against Entra ID.
144-
145- Supports testing two authentication methods:
146- 1. Application authentication (client_id/client_secret)
147- 2. Certificate authentication (certificate_content in base64/client_id)
148-
149- Args:
150- credentials (M365Credentials): The credentials object containing
151- authentication information to test.
152-
153- Returns:
154- bool: True if credentials are valid and authentication succeeds, False otherwise.
155- """
156- # Test Certificate Auth
157- if credentials .certificate_content and credentials .client_id :
158- try :
159- logger .info ("Testing Microsoft Graph Certificate connection..." )
160- self .test_graph_certificate_connection ()
161- logger .info ("Microsoft Graph Certificate connection successful" )
162- teams_connection_successful = self .test_teams_certificate_connection ()
163- if not teams_connection_successful :
164- self .test_exchange_certificate_connection ()
165- return True
166- except Exception as e :
167- logger .error (f"Microsoft Graph Cer connection failed: { e } " )
168- raise M365GraphConnectionError (
169- file = os .path .basename (__file__ ),
170- original_exception = e ,
171- message = "Check your Microsoft Application Certificate and ensure the app has proper permissions" ,
172- )
173- else :
174- try :
175- logger .info ("Testing Microsoft Graph Client Secret connection..." )
176- self .test_graph_connection ()
177- logger .info ("Microsoft Graph Client Secret connection successful" )
178- return True
179- except Exception as e :
180- logger .error (f"Microsoft Graph Client Secret connection failed: { e } " )
181- raise M365GraphConnectionError (
182- file = os .path .basename (__file__ ),
183- original_exception = e ,
184- message = "Check your Microsoft Application Client Secret and ensure the app has proper permissions" ,
185- )
186-
187- def test_graph_connection (self ) -> bool :
188- """Test Microsoft Graph API connection and raise exception if it fails."""
189- try :
190- if self .execute ("Write-Output $graphToken" ) == "" :
191- raise M365GraphConnectionError (
192- file = os .path .basename (__file__ ),
193- message = "Microsoft Graph token is empty or invalid." ,
194- )
195- return True
196- except Exception as e :
197- logger .error (f"Microsoft Graph connection failed: { e } " )
198- raise M365GraphConnectionError (
199- file = os .path .basename (__file__ ),
200- original_exception = e ,
201- message = f"Failed to connect to Microsoft Graph API: { str (e )} " ,
202- )
203-
204- def test_graph_certificate_connection (self ) -> bool :
205- """Test Microsoft Graph API connection using certificate and raise exception if it fails."""
206- result = self .execute_connect (
207- "Connect-Graph -Certificate $certificate -AppId $clientID -TenantId $tenantID"
208- )
209- if "Welcome to Microsoft Graph!" not in result :
210- logger .error (f"Microsoft Graph Certificate connection failed: { result } " )
211- return False
212- return True
213-
214138 def test_teams_connection (self ) -> bool :
215139 """Test Microsoft Teams API connection and raise exception if it fails."""
216140 try :
@@ -926,7 +850,10 @@ def initialize_m365_powershell_modules():
926850 bool: True if all modules were successfully initialized, False otherwise
927851 """
928852
929- REQUIRED_MODULES = ["ExchangeOnlineManagement" , "MicrosoftTeams" , "MSAL.PS" ]
853+ REQUIRED_MODULES = [
854+ "ExchangeOnlineManagement" ,
855+ "MicrosoftTeams" ,
856+ ]
930857
931858 pwsh = PowerShellSession ()
932859 try :
@@ -938,7 +865,7 @@ def initialize_m365_powershell_modules():
938865 # Install module if not installed
939866 if not result :
940867 install_result = pwsh .execute (
941- f' Install-Module " { module } " -Force -AllowClobber -Scope CurrentUser' ,
868+ f" Install-Module { module } -Force -AllowClobber -Scope CurrentUser" ,
942869 timeout = 60 ,
943870 )
944871 if install_result :
0 commit comments