Skip to content

Commit

Permalink
Added support for auto cache update (demisto#12062)
Browse files Browse the repository at this point in the history
* Replaced demisto.set/getIntegrationContext with the wrapper functions from CSP set/get_integration_context.

* Reverted docker update for MicrosoftTeams.yml and MicrosoftManagementActivity.yml because of compatibility issues in pyjwt

* Updated RN

* Check #1
  • Loading branch information
Bargenish committed Apr 7, 2021
1 parent 2be8e19 commit 122224c
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 31 deletions.
Expand Up @@ -30,7 +30,7 @@ def __init__(self, self_deployed, refresh_token, auth_and_token_url, enc_key, re
subscription_id, resource_group_name, workspace_name, verify, proxy):

tenant_id = refresh_token if self_deployed else ''
refresh_token = (demisto.getIntegrationContext().get('current_refresh_token') or refresh_token)
refresh_token = get_integration_context().get('current_refresh_token') or refresh_token
base_url = f'https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/' \
f'{resource_group_name}/providers/Microsoft.OperationalInsights/workspaces/{workspace_name}'
self.ms_client = MicrosoftClient(
Expand Down
4 changes: 4 additions & 0 deletions Packs/AzureLogAnalytics/ReleaseNotes/1_0_8.md
@@ -0,0 +1,4 @@

#### Integrations
##### Azure Log Analytics (Beta)
- Added support for cache to be updated automatically on token expiration.
2 changes: 1 addition & 1 deletion Packs/AzureLogAnalytics/pack_metadata.json
Expand Up @@ -2,7 +2,7 @@
"name": "Azure Log Analytics",
"description": "Log Analytics is a service that helps you collect and analyze data generated by resources in your cloud and on-premises environments.",
"support": "xsoar",
"currentVersion": "1.0.7",
"currentVersion": "1.0.8",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down
Expand Up @@ -39,7 +39,7 @@ def __init__(self, self_deployed, refresh_token, auth_and_token_url, enc_key, re
subscription_id, resource_group_name, workspace_name, verify, proxy):

tenant_id = refresh_token if self_deployed else ''
refresh_token = (demisto.getIntegrationContext().get('current_refresh_token') or refresh_token)
refresh_token = get_integration_context().get('current_refresh_token') or refresh_token
base_url = f'https://management.azure.com/subscriptions/{subscription_id}/' \
f'resourceGroups/{resource_group_name}/providers/Microsoft.OperationalInsights/workspaces/' \
f'{workspace_name}/providers/Microsoft.SecurityInsights'
Expand Down
4 changes: 4 additions & 0 deletions Packs/AzureSentinel/ReleaseNotes/1_0_9.md
@@ -0,0 +1,4 @@

#### Integrations
##### Azure Sentinel (Beta)
- Added support for cache to be updated automatically on token expiration.
2 changes: 1 addition & 1 deletion Packs/AzureSentinel/pack_metadata.json
Expand Up @@ -2,7 +2,7 @@
"name": "AzureSentinel",
"description": "Azure 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.",
"support": "xsoar",
"currentVersion": "1.0.8",
"currentVersion": "1.0.9",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down
Expand Up @@ -819,7 +819,7 @@ def main():
# params related to oproxy
# In case the script is running for the first time, refresh token is retrieved from integration parameters,
# in other case it's retrieved from integration context.
refresh_token = (demisto.getIntegrationContext().get('current_refresh_token') or refresh_token)
refresh_token = get_integration_context().get('current_refresh_token') or refresh_token

client = MsGraphClient(self_deployed, tenant_id, auth_and_token_url, enc_key, app_name, base_url, use_ssl, proxy,
ok_codes, refresh_token, mailbox_to_fetch, folder_to_fetch, first_fetch_interval,
Expand Down
Expand Up @@ -378,7 +378,7 @@ script:
description: Tests connectivity of the email.
execution: false
name: msgraph-mail-test
dockerimage: demisto/crypto:1.0.0.12979
dockerimage: demisto/crypto:1.0.0.18288
isfetch: true
longRunning: false
longRunningPort: false
Expand Down
5 changes: 5 additions & 0 deletions Packs/MicrosoftGraphListener/ReleaseNotes/1_0_14.md
@@ -0,0 +1,5 @@

#### Integrations
##### Microsoft Graph Mail Single User
- Added support for cache to be updated automatically on token expiration.
- Updated the Docker image to: *demisto/crypto:1.0.0.18288*
2 changes: 1 addition & 1 deletion Packs/MicrosoftGraphListener/pack_metadata.json
Expand Up @@ -2,7 +2,7 @@
"name": "Microsoft Graph Mail Single User",
"description": "Microsoft Graph grants Demisto authorized access to a user's Microsoft Outlook mail data in a personal account or organization account.",
"support": "xsoar",
"currentVersion": "1.0.13",
"currentVersion": "1.0.14",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down
Expand Up @@ -524,7 +524,7 @@ def main():
auth_id = params['auth_id']
enc_key = params['enc_key']

refresh_token = (demisto.getIntegrationContext().get('current_refresh_token') or refresh_token)
refresh_token = get_integration_context().get('current_refresh_token') or refresh_token

client = Client(
base_url=base_url,
Expand Down
4 changes: 4 additions & 0 deletions Packs/MicrosoftManagementActivity/ReleaseNotes/1_1_10.md
@@ -0,0 +1,4 @@

#### Integrations
##### Microsoft Management Activity API (O365 Azure Events)
- Added support for cache to be updated automatically on token expiration.
2 changes: 1 addition & 1 deletion Packs/MicrosoftManagementActivity/pack_metadata.json
Expand Up @@ -2,7 +2,7 @@
"name": "Microsoft Management Activity API (O365/Azure Events)",
"description": "An integration for Microsoft's management activity API, which enables you to fetch content records and manage your subscriptions.",
"support": "xsoar",
"currentVersion": "1.1.9",
"currentVersion": "1.1.10",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down
41 changes: 20 additions & 21 deletions Packs/MicrosoftTeams/Integrations/MicrosoftTeams/MicrosoftTeams.py
Expand Up @@ -387,7 +387,7 @@ def get_bot_access_token() -> str:
Retrieves Bot Framework API access token, either from cache or from Microsoft
:return: The Bot Framework API access token
"""
integration_context: dict = demisto.getIntegrationContext()
integration_context: dict = get_integration_context()
access_token: str = integration_context.get('bot_access_token', '')
valid_until: int = integration_context.get('bot_valid_until', int)
if access_token and valid_until:
Expand Down Expand Up @@ -418,7 +418,7 @@ def get_bot_access_token() -> str:
expires_in -= time_buffer
integration_context['bot_access_token'] = access_token
integration_context['bot_valid_until'] = time_now + expires_in
demisto.setIntegrationContext(integration_context)
set_integration_context(integration_context)
return access_token
except ValueError:
raise ValueError('Failed to get bot access token')
Expand All @@ -429,7 +429,7 @@ def get_graph_access_token() -> str:
Retrieves Microsoft Graph API access token, either from cache or from Microsoft
:return: The Microsoft Graph API access token
"""
integration_context: dict = demisto.getIntegrationContext()
integration_context: dict = get_integration_context()
access_token: str = integration_context.get('graph_access_token', '')
valid_until: int = integration_context.get('graph_valid_until', int)
if access_token and valid_until:
Expand Down Expand Up @@ -468,7 +468,7 @@ def get_graph_access_token() -> str:
expires_in -= time_buffer
integration_context['graph_access_token'] = access_token
integration_context['graph_valid_until'] = time_now + expires_in
demisto.setIntegrationContext(integration_context)
set_integration_context(integration_context)
return access_token
except ValueError:
raise ValueError('Failed to get Graph access token')
Expand Down Expand Up @@ -562,7 +562,7 @@ def integration_health():
adi_health_human_readable: str = tableToMarkdown('Microsoft API Health', api_health_output)

mirrored_channels_output = list()
integration_context: dict = demisto.getIntegrationContext()
integration_context: dict = get_integration_context()
teams: list = json.loads(integration_context.get('teams', '[]'))
for team in teams:
mirrored_channels: list = team.get('mirrored_channels', [])
Expand Down Expand Up @@ -611,7 +611,7 @@ def validate_auth_header(headers: dict) -> bool:
demisto.info('Authorization header validation - failed to verify issuer')
return False

integration_context: dict = demisto.getIntegrationContext()
integration_context: dict = get_integration_context()
open_id_metadata: dict = json.loads(integration_context.get('open_id_metadata', '{}'))
keys: list = open_id_metadata.get('keys', [])

Expand Down Expand Up @@ -680,8 +680,7 @@ def validate_auth_header(headers: dict) -> bool:
return False

integration_context['open_id_metadata'] = json.dumps(open_id_metadata)
demisto.setIntegrationContext(integration_context)

set_integration_context(integration_context)
return True


Expand All @@ -694,7 +693,7 @@ def get_team_aad_id(team_name: str) -> str:
:param team_name: Team name to get AAD ID of
:return: team AAD ID
"""
integration_context: dict = demisto.getIntegrationContext()
integration_context: dict = get_integration_context()
if integration_context.get('teams'):
teams: list = json.loads(integration_context['teams'])
for team in teams:
Expand Down Expand Up @@ -888,7 +887,7 @@ def get_channel_id(channel_name: str, team_aad_id: str, investigation_id: str =
:return: Requested channel ID
"""
investigation_id = investigation_id or str()
integration_context: dict = demisto.getIntegrationContext()
integration_context: dict = get_integration_context()
teams: list = json.loads(integration_context.get('teams', '[]'))
for team in teams:
mirrored_channels: list = team.get('mirrored_channels', [])
Expand Down Expand Up @@ -958,7 +957,7 @@ def close_channel():
"""
Deletes a mirrored Microsoft Teams channel
"""
integration_context: dict = demisto.getIntegrationContext()
integration_context: dict = get_integration_context()
channel_name: str = demisto.args().get('channel', '')
investigation: dict = demisto.investigation()
investigation_id: str = investigation.get('id', '')
Expand All @@ -981,7 +980,7 @@ def close_channel():
if not channel_id:
raise ValueError('Could not find Microsoft Teams channel to close.')
integration_context['teams'] = json.dumps(teams)
demisto.setIntegrationContext(integration_context)
set_integration_context(integration_context)
else:
team_name: str = demisto.args().get('team') or demisto.params().get('team')
team_aad_id = get_team_aad_id(team_name)
Expand Down Expand Up @@ -1096,7 +1095,7 @@ def send_message():
if message and adaptive_card:
raise ValueError('Provide either message or adaptive to send, not both.')

integration_context: dict = demisto.getIntegrationContext()
integration_context: dict = get_integration_context()
channel_id: str = str()
personal_conversation_id: str = str()
if channel_name:
Expand Down Expand Up @@ -1160,7 +1159,7 @@ def mirror_investigation():
if investigation.get('type') == PLAYGROUND_INVESTIGATION_TYPE:
raise ValueError('Can not perform this action in playground.')

integration_context: dict = demisto.getIntegrationContext()
integration_context: dict = get_integration_context()

mirror_type: str = demisto.args().get('mirror_type', 'all')
auto_close: str = demisto.args().get('autoclose', 'true')
Expand Down Expand Up @@ -1217,7 +1216,7 @@ def mirror_investigation():
demisto.results(f'Investigation mirrored successfully in channel {channel_name}.')
team['mirrored_channels'] = mirrored_channels
integration_context['teams'] = json.dumps(teams)
demisto.setIntegrationContext(integration_context)
set_integration_context(integration_context)


def channel_mirror_loop():
Expand All @@ -1226,7 +1225,7 @@ def channel_mirror_loop():
"""
while True:
found_channel_to_mirror: bool = False
integration_context = demisto.getIntegrationContext()
integration_context = get_integration_context()
try:
teams: list = json.loads(integration_context.get('teams', '[]'))
for team in teams:
Expand All @@ -1249,7 +1248,7 @@ def channel_mirror_loop():
demisto.info(f'Could not mirror {investigation_id}')
team['mirrored_channels'] = mirrored_channels
integration_context['teams'] = json.dumps(teams)
demisto.setIntegrationContext(integration_context)
set_integration_context(integration_context)
found_channel_to_mirror = True
break
if found_channel_to_mirror:
Expand Down Expand Up @@ -1322,7 +1321,7 @@ def member_added_handler(integration_context: dict, request_body: dict, channel_
'team_members': team_members
})
integration_context['teams'] = json.dumps(teams)
demisto.setIntegrationContext(integration_context)
set_integration_context(integration_context)


def direct_message_handler(integration_context: dict, request_body: dict, conversation: dict, message: str):
Expand Down Expand Up @@ -1479,12 +1478,12 @@ def messages() -> Response:
demisto.info(f'Authorization header failed: {str(headers)}')
else:
request_body: dict = request.json
integration_context: dict = demisto.getIntegrationContext()
integration_context: dict = get_integration_context()
service_url: str = request_body.get('serviceUrl', '')
if service_url:
service_url = service_url[:-1] if service_url.endswith('/') else service_url
integration_context['service_url'] = service_url
demisto.setIntegrationContext(integration_context)
set_integration_context(integration_context)

channel_data: dict = request_body.get('channelData', {})
event_type: str = channel_data.get('eventType', '')
Expand Down Expand Up @@ -1534,7 +1533,7 @@ def ring_user():
None.
"""
bot_id = demisto.params().get('bot_id')
integration_context: dict = demisto.getIntegrationContext()
integration_context: dict = get_integration_context()
tenant_id: str = integration_context.get('tenant_id', '')
if not tenant_id:
raise ValueError(
Expand Down
4 changes: 4 additions & 0 deletions Packs/MicrosoftTeams/ReleaseNotes/1_1_5.md
@@ -0,0 +1,4 @@

#### Integrations
##### Microsoft Teams
- Added support for cache to be updated automatically on token expiration.
2 changes: 1 addition & 1 deletion Packs/MicrosoftTeams/pack_metadata.json
Expand Up @@ -2,7 +2,7 @@
"name": "Microsoft Teams",
"description": "Send messages and notifications to your team members.",
"support": "xsoar",
"currentVersion": "1.1.4",
"currentVersion": "1.1.5",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down

0 comments on commit 122224c

Please sign in to comment.