Skip to content

Commit 73a277f

Browse files
authored
chore(m365_powershell): remove unnecessary test_credentials (#9204)
1 parent 822d201 commit 73a277f

File tree

5 files changed

+13
-363
lines changed

5 files changed

+13
-363
lines changed

prowler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ All notable changes to the **Prowler SDK** are documented in this file.
4242

4343
- Update AWS CodeArtifact service metadata to new format [(#8850)](https://github.com/prowler-cloud/prowler/pull/8850)
4444
- Rename OCI provider to oraclecloud with oci alias [(#9126)](https://github.com/prowler-cloud/prowler/pull/9126)
45+
- Remove unnecessary tests for M365_PowerShell module [(#9204)](https://github.com/prowler-cloud/prowler/pull/9204)
4546

4647
---
4748

prowler/providers/m365/lib/powershell/m365_powershell.py

Lines changed: 6 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
from prowler.lib.logger import logger
44
from 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
96
from prowler.providers.m365.lib.jwt.jwt_decoder import decode_msal_token
107
from 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:

prowler/providers/m365/m365_provider.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,12 +444,7 @@ def setup_powershell(
444444
try:
445445
if init_modules:
446446
initialize_m365_powershell_modules()
447-
if test_session.test_credentials(credentials):
448-
return credentials
449-
raise M365ConfigCredentialsError(
450-
file=os.path.basename(__file__),
451-
message="The provided credentials are not valid.",
452-
)
447+
return credentials
453448
finally:
454449
test_session.close()
455450

0 commit comments

Comments
 (0)