Skip to content

Commit 7839428

Browse files
mmhwContent Bot
and
Content Bot
authored
[MicrosoftApiModule] Auth code reconfigurations (#29035)
* Auth code reconfigurations * Update RN * Reduced use of get_integration_context() * Add docs to the unit test * Add docs to the function * Bump pack from version MicrosoftGraphTeams to 1.0.11. --------- Co-authored-by: Content Bot <[email protected]>
1 parent 2098c47 commit 7839428

File tree

68 files changed

+331
-37
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+331
-37
lines changed

Packs/ApiModules/Scripts/MicrosoftApiModule/MicrosoftApiModule.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,8 @@ def __init__(self, tenant_id: str = '',
735735
self.resources = resources if resources else []
736736
self.resource_to_access_token: dict[str, str] = {}
737737

738+
self.auth_code_reconfigured = False
739+
738740
# for Azure Managed Identities purpose
739741
self.managed_identities_client_id = managed_identities_client_id
740742
self.managed_identities_resource_uri = managed_identities_resource_uri
@@ -867,7 +869,11 @@ def get_access_token(self, resource: str = '', scope: str | None = None) -> str:
867869

868870
valid_until = integration_context.get(valid_until_keyword)
869871

870-
if access_token and valid_until and self.epoch_seconds() < valid_until:
872+
self.auth_code_reconfigured = self.is_auth_code_reconfigured(integration_context.get('auth_code', ''))
873+
if self.auth_code_reconfigured:
874+
demisto.debug("Auth code reconfigured, saving new auth code to integration context")
875+
integration_context['auth_code'] = self.auth_code
876+
elif access_token and valid_until and self.epoch_seconds() < valid_until:
871877
return access_token
872878

873879
if self.auth_type == OPROXY_AUTH_TYPE:
@@ -904,6 +910,7 @@ def get_access_token(self, resource: str = '', scope: str | None = None) -> str:
904910
integration_context.update(self.resource_to_access_token)
905911

906912
set_integration_context(integration_context)
913+
demisto.debug('Set integration context successfully.')
907914

908915
if self.multi_resource:
909916
return self.resource_to_access_token[resource]
@@ -1100,7 +1107,7 @@ def _get_self_deployed_token_auth_code(
11001107
data['scope'] = scope
11011108

11021109
refresh_token = refresh_token or self._get_refresh_token_from_auth_code_param()
1103-
if refresh_token:
1110+
if refresh_token and not self.auth_code_reconfigured:
11041111
data['grant_type'] = REFRESH_TOKEN
11051112
data['refresh_token'] = refresh_token
11061113
else:
@@ -1386,6 +1393,29 @@ def start_auth(self, complete_command: str) -> str:
13861393
and enter the code **{user_code}** to authenticate.
13871394
2. Run the **{complete_command}** command in the War Room."""
13881395

1396+
def is_auth_code_reconfigured(self, auth_code) -> bool:
1397+
"""
1398+
Checks if the auth_code is reconfigured by comparing to the self.auth_code from the instance params.
1399+
Args:
1400+
auth_code: The auth_code form the integration context.
1401+
Returns:
1402+
bool: True if the auth_code is reconfigured, otherwise False.
1403+
"""
1404+
# Case of oproxy
1405+
if self.auth_type == OPROXY_AUTH_TYPE:
1406+
return False
1407+
# Case of the next times or after reconfigured the auth_code
1408+
if auth_code and self.auth_code:
1409+
is_reconfigured = auth_code != self.auth_code
1410+
demisto.debug(f'Auth code is reconfigured: {is_reconfigured}')
1411+
return is_reconfigured
1412+
# Case of the first time or after deleting the auth_code
1413+
elif auth_code or self.auth_code:
1414+
demisto.debug('Auth code is only in ' + ('integration_context' if auth_code else 'params'))
1415+
return True
1416+
else:
1417+
return False
1418+
13891419

13901420
class NotFoundError(Exception):
13911421
"""Exception raised for 404 - Not Found errors.

Packs/ApiModules/Scripts/MicrosoftApiModule/MicrosoftApiModule_test.py

+52-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
CLIENT_ID = 'dummy_client'
1919
CLIENT_SECRET = 'dummy_secret'
2020
APP_URL = 'https://login.microsoftonline.com/dummy_tenant/oauth2/v2.0/token'
21+
AUTH_CODE = 'dummy_auth_code'
22+
REDIRECT_URI = 'https://localhost/myapp'
2123
SCOPE = 'https://graph.microsoft.com/.default'
2224
RESOURCE = 'https://defender.windows.com/shtak'
2325
RESOURCES = ['https://resource1.com', 'https://resource2.com']
@@ -62,15 +64,17 @@ def oproxy_client_refresh():
6264
)
6365

6466

65-
def self_deployed_client():
67+
def self_deployed_client(grant_type=CLIENT_CREDENTIALS):
6668
tenant_id = TENANT
6769
client_id = CLIENT_ID
6870
client_secret = CLIENT_SECRET
6971
base_url = BASE_URL
72+
auth_code = AUTH_CODE if grant_type == AUTHORIZATION_CODE else ''
7073
resource = RESOURCE
7174
ok_codes = OK_CODES
7275

7376
return MicrosoftClient(self_deployed=True, tenant_id=tenant_id, auth_id=client_id, enc_key=client_secret,
77+
grant_type=grant_type, auth_code=auth_code,
7478
resource=resource, base_url=base_url, verify=True, proxy=False, ok_codes=ok_codes)
7579

7680

@@ -717,11 +721,57 @@ def test_generate_login_url():
717721
"""
718722
from MicrosoftApiModule import generate_login_url
719723

720-
client = self_deployed_client()
724+
client = self_deployed_client(grant_type=AUTHORIZATION_CODE)
721725

722726
result = generate_login_url(client)
723727

724728
expected_url = f'[login URL](https://login.microsoftonline.com/{TENANT}/oauth2/v2.0/authorize?' \
725729
f'response_type=code&scope=offline_access%20https://graph.microsoft.com/.default' \
726730
f'&client_id={CLIENT_ID}&redirect_uri=https://localhost/myapp)'
727731
assert expected_url in result.readable_output, "Login URL is incorrect"
732+
733+
734+
def test_get_access_token_auth_code_reconfigured(mocker, requests_mock):
735+
"""
736+
Given:
737+
- The auth code was reconfigured
738+
When:
739+
- Calling function get_access_token
740+
Then:
741+
- Ensure the access token is as expected in the body of the request and in the integration context
742+
"""
743+
context = {'auth_code': AUTH_CODE, 'access_token': TOKEN,
744+
'valid_until': 3605, 'current_refresh_token': REFRESH_TOKEN}
745+
746+
mocker.patch.object(demisto, 'getIntegrationContext', return_value=context)
747+
mocker.patch.object(demisto, 'setIntegrationContext')
748+
749+
tenant_id = TENANT
750+
client_id = CLIENT_ID
751+
client_secret = CLIENT_SECRET
752+
base_url = BASE_URL
753+
new_auth_code = 'reconfigured_auth_code'
754+
resource = None
755+
ok_codes = OK_CODES
756+
grant_type = AUTHORIZATION_CODE
757+
758+
client = MicrosoftClient(self_deployed=True, tenant_id=tenant_id, auth_id=client_id, enc_key=client_secret,
759+
grant_type=grant_type, auth_code=new_auth_code,
760+
resource=resource, base_url=base_url, verify=True, proxy=False, ok_codes=ok_codes)
761+
762+
requests_mock.post(
763+
APP_URL,
764+
json={'access_token': TOKEN, 'expires_in': '3600'})
765+
766+
body = {
767+
'client_id': CLIENT_ID,
768+
'client_secret': CLIENT_SECRET,
769+
'redirect_uri': REDIRECT_URI,
770+
'grant_type': AUTHORIZATION_CODE,
771+
'code': new_auth_code,
772+
}
773+
774+
assert client.get_access_token()
775+
req_body = requests_mock._adapter.last_request._request.body
776+
assert urllib.parse.urlencode(body) == req_body
777+
assert demisto.getIntegrationContext().get('auth_code') == new_auth_code
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Integrations
3+
4+
##### Azure Active Directory Identity Protection (Deprecated)
5+
6+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.

Packs/AzureActiveDirectory/pack_metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Deprecated. Use Microsoft Graph Identity and Access instead.",
44
"support": "xsoar",
55
"hidden": true,
6-
"currentVersion": "1.3.15",
6+
"currentVersion": "1.3.16",
77
"author": "Cortex XSOAR",
88
"url": "https://www.paloaltonetworks.com/cortex",
99
"email": "",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Integrations
3+
4+
##### Azure Compute v2
5+
6+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.

Packs/AzureCompute/pack_metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Azure Compute",
33
"description": "Create and Manage Azure Virtual Machines",
44
"support": "xsoar",
5-
"currentVersion": "1.2.12",
5+
"currentVersion": "1.2.13",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Integrations
3+
4+
##### Azure Data Explorer
5+
6+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.

Packs/AzureDataExplorer/pack_metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Azure Data Explorer",
33
"description": "Use Azure Data Explorer integration to collect and analyze data inside clusters of Azure Data Explorer and manage search queries.",
44
"support": "xsoar",
5-
"currentVersion": "1.2.24",
5+
"currentVersion": "1.2.25",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Integrations
3+
4+
##### AzureDevOps
5+
6+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.

Packs/AzureDevOps/pack_metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "AzureDevOps",
33
"description": "Create and manage Git repositories in Azure DevOps Services.",
44
"support": "xsoar",
5-
"currentVersion": "1.2.16",
5+
"currentVersion": "1.2.17",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Integrations
3+
4+
##### Azure Firewall
5+
6+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.

Packs/AzureFirewall/pack_metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Azure Firewall",
33
"description": "Azure Firewall is a cloud-native and intelligent network firewall security service that provides breed threat protection for cloud workloads running in Azure.It's a fully stateful, firewall as a service with built-in high availability and unrestricted cloud scalability.",
44
"support": "xsoar",
5-
"currentVersion": "1.1.24",
5+
"currentVersion": "1.1.25",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Integrations
3+
4+
##### Azure Key Vault
5+
6+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.

Packs/AzureKeyVault/pack_metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Azure Key Vault",
33
"description": "Use Key Vault to safeguard and manage cryptographic keys and secrets used by cloud applications and services.",
44
"support": "xsoar",
5-
"currentVersion": "1.1.25",
5+
"currentVersion": "1.1.26",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Integrations
3+
4+
##### Azure Kubernetes Services
5+
6+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.

Packs/AzureKubernetesServices/pack_metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Azure Kubernetes Services",
33
"description": "Deploy and manage containerized applications with a fully managed Kubernetes service.",
44
"support": "xsoar",
5-
"currentVersion": "1.1.17",
5+
"currentVersion": "1.1.18",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Integrations
3+
4+
##### Azure Log Analytics
5+
6+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.

Packs/AzureLogAnalytics/pack_metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Azure Log Analytics",
33
"description": "Log Analytics is a service that helps you collect and analyze data generated by resources in your cloud and on-premises environments.",
44
"support": "xsoar",
5-
"currentVersion": "1.1.15",
5+
"currentVersion": "1.1.16",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Integrations
3+
4+
##### Azure Network Security Groups
5+
6+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.

Packs/AzureNetworkSecurityGroups/pack_metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Azure Network Security Groups",
33
"description": "Azure Network Security Groups are used to filter network traffic to and from Azure resources in an Azure virtual network",
44
"support": "xsoar",
5-
"currentVersion": "1.2.17",
5+
"currentVersion": "1.2.18",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Integrations
3+
4+
##### Azure Risky Users
5+
6+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.

Packs/AzureRiskyUsers/pack_metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Azure Risky Users",
33
"description": "Azure Risky Users provides access to all at-risk users and risk detections in Azure AD environment.",
44
"support": "xsoar",
5-
"currentVersion": "1.1.15",
5+
"currentVersion": "1.1.16",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Integrations
3+
4+
##### Azure SQL Management
5+
6+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.

Packs/AzureSQLManagement/pack_metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Azure SQL Management",
33
"description": "Microsoft Azure SQL Database is a managed cloud database provided as part of Microsoft Azure",
44
"support": "xsoar",
5-
"currentVersion": "1.1.26",
5+
"currentVersion": "1.1.27",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
#### Integrations
3+
4+
##### Microsoft Defender for Cloud
5+
6+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.
7+
8+
##### Microsoft Defender for Cloud Event Collector
9+
10+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.

Packs/AzureSecurityCenter/pack_metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Microsoft Defender for Cloud",
33
"description": "Unified security management and advanced threat protection across hybrid cloud workloads.",
44
"support": "xsoar",
5-
"currentVersion": "2.0.7",
5+
"currentVersion": "2.0.8",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Integrations
3+
4+
##### Microsoft Sentinel
5+
6+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.

Packs/AzureSentinel/pack_metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Microsoft Sentinel",
33
"description": "Microsoft Sentinel is a cloud-native security information and event manager (SIEM) platform that uses built-in AI to help analyze large volumes of data across an enterprise.",
44
"support": "xsoar",
5-
"currentVersion": "1.5.17",
5+
"currentVersion": "1.5.18",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Integrations
3+
4+
##### Azure Storage Management
5+
6+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.

Packs/AzureStorage/pack_metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Azure Storage Management",
33
"description": "Deploy and manage storage accounts and blob service properties.",
44
"support": "xsoar",
5-
"currentVersion": "1.2.17",
5+
"currentVersion": "1.2.18",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",

Packs/AzureWAF/ReleaseNotes/1_1_16.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Integrations
3+
4+
##### Azure Web Application Firewall
5+
6+
Fixed an issue where changes made to the *Authorization code* parameter were not being reflected in the integration code, resulting in the continued use of the first parameter.

Packs/AzureWAF/pack_metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Azure WAF",
33
"description": "Azure Web Application Firewall is used to detect web related attacks targeting your web servers hosted in azure and allow quick respond to threats",
44
"support": "xsoar",
5-
"currentVersion": "1.1.15",
5+
"currentVersion": "1.1.16",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",

0 commit comments

Comments
 (0)