Skip to content

Commit d45a6a5

Browse files
committed
add PUBLIC_CA_BUNDLE environment variable
(cherry picked from commit b965267)
1 parent efbff00 commit d45a6a5

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/systemathics/apis/helpers/token_helpers.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import http.client
1111
import json
12+
import ssl
1213

1314
DEFAULT_AUDIENCE = "https://prod.ganymede-prod"
1415

@@ -24,11 +25,12 @@ def get_token_as_metadata() -> []:
2425
"""
2526
return [('authorization', get_token())]
2627

27-
def get_token() -> str:
28+
def get_token() -> str:
2829
"""
2930
Get a JWT Authorization token suitable to call Ganymede gRPC API.
3031
We either use 'AUTH0_TOKEN' environment variable (if present) to create a bearer token from it.
31-
Or 'CLIENT_ID' and 'CLIENT_SECRET' environment variables (optionally 'AUDIENCE' can override DEFAULT_AUDIENCE, and 'TENANT' can override DEFAULT_TENANT).
32+
Or 'CLIENT_ID' and 'CLIENT_SECRET' environment variables (optionally 'AUDIENCE' can override DEFAULT_AUDIENCE, and 'TENANT' can override DEFAULT_TENANT).
33+
Optionally a 'PUBLIC_CA_BUNDLE' environment variable can be provided to set a specific certificate store.
3234
Returns:
3335
A JWT Authorization token suitable to call Ganymede gRPC API.
3436
"""
@@ -37,6 +39,7 @@ def get_token() -> str:
3739
client_secret = os.getenv("CLIENT_SECRET","")
3840
audience = os.getenv("AUDIENCE","")
3941
tenant = os.getenv("TENANT","")
42+
public_ca_bundle = os.getenv("PUBLIC_CA_BUNDLE","")
4043

4144
# If we have AUTH0_TOKEN, generate a bearer token
4245
if(auth0_token != ""):
@@ -48,11 +51,22 @@ def get_token() -> str:
4851
client_id,
4952
client_secret,
5053
audience if audience else DEFAULT_AUDIENCE,
51-
tenant if tenant else DEFAULT_TENANT)
54+
tenant if tenant else DEFAULT_TENANT,
55+
public_ca_bundle)
5256
else:
5357
raise Exception(f"AUTH0_TOKEN environment variable is not set, therefore CLIENT_ID and CLIENT_SECRET (and optionally AUDIENCE and TENANT) environment variables must be set")
5458

55-
def _create_bearer_token_using_rest(client_id, client_secret, audience, tenant) -> str:
59+
60+
def _get_client(tenant, public_ca_bundle) -> http.client.HTTPSConnection:
61+
if(public_ca_bundle == "" or not os.path.exists(public_ca_bundle)):
62+
return http.client.HTTPSConnection(tenant)
63+
else:
64+
ssl_context = ssl.create_default_context()
65+
ssl_context.load_verify_locations(public_ca_bundle)
66+
return http.client.HTTPSConnection(tenant, context=ssl_context)
67+
68+
69+
def _create_bearer_token_using_rest(client_id, client_secret, audience, tenant, public_ca_bundle) -> str:
5670
if (client_id == ""):
5771
raise Exception(f"client_id cannot be null")
5872
if (client_secret == ""):
@@ -63,7 +77,7 @@ def _create_bearer_token_using_rest(client_id, client_secret, audience, tenant)
6377
raise Exception(f"tenant cannot be null")
6478

6579
# Setup connection and payload
66-
conn = http.client.HTTPSConnection(tenant)
80+
conn = _get_client(tenant, public_ca_bundle)
6781
headers = { "content-type": "application/json" }
6882
params = {"client_id": client_id, "client_secret": client_secret, "grant_type" : "client_credentials", "audience": audience }
6983
payload = json.dumps(params)

0 commit comments

Comments
 (0)