Skip to content

Commit

Permalink
Merge pull request #8 from CrowdStrike/pbathala/add-cloud-to-ctx
Browse files Browse the repository at this point in the history
Add CS Cloud to context object
  • Loading branch information
prvn authored Jul 3, 2024
2 parents 35ab1dd + e57e31a commit a8c2524
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/crowdstrike/foundry/function/falconpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ def falcon_client(client_class: Type) -> ServiceClass:
cloud = cloud.lower().replace('-', '').strip()
if cloud == '':
cloud = _cloud_default

# set cloud on the request object that is in the context
req.cloud = cloud

client = client_class(access_token=access_token, base_url=cloud)
return client
1 change: 1 addition & 0 deletions src/crowdstrike/foundry/function/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Request:
method: str = field(default='')
params: RequestParams = field(default_factory=lambda: RequestParams())
url: str = field(default='')
cloud: str = field(default='')


@dataclass
Expand Down
15 changes: 13 additions & 2 deletions tests/crowdstrike/foundry/function/test_falconpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import unittest
from unittest.mock import patch
from crowdstrike.foundry.function.context import ctx_request
from crowdstrike.foundry.function.falconpy import falcon_client
from crowdstrike.foundry.function.model import Request
from crowdstrike.foundry.function.falconpy import falcon_client
from crowdstrike.foundry.function.model import Request
from falconpy import Hosts

if __name__ == '__main__':
Expand Down Expand Up @@ -54,3 +54,14 @@ def test_request_with_access_token_and_non_default_cloud_returns_prepared_client
self.assertIsInstance(client, Hosts)
self.assertEqual('Bearer foo', client.headers.get('Authorization'))
self.assertEqual(t['expected'], client.base_url)

def test_request_inserts_cloud_into_request(self):
with patch.dict(os.environ, {'CS_CLOUD': 'us-gov-1'}, clear=True):
ctx_request.set(Request(access_token='foo'))
client = falcon_client(Hosts)

self.assertIsInstance(client, Hosts)
self.assertEqual('Bearer foo', client.headers.get('Authorization'))
self.assertEqual('https://api.laggar.gcw.crowdstrike.com', client.base_url)
r = ctx_request.get()
self.assertEqual('usgov1', r.cloud)

0 comments on commit a8c2524

Please sign in to comment.