Skip to content

Commit 5a0c176

Browse files
committed
fix: correct GCP credential loading
1 parent 62e5386 commit 5a0c176

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

cloudproxy/providers/gcp/functions.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
from cloudproxy.providers.config import set_auth
1010
from cloudproxy.providers.settings import config
1111

12+
gcp = None
13+
compute = None
14+
1215
def get_client(instance_config=None):
1316
"""
1417
Initialize and return a GCP client based on the provided configuration.
@@ -21,21 +24,27 @@ def get_client(instance_config=None):
2124
"""
2225

2326
global gcp, compute
27+
if gcp is not None and compute is not None:
28+
return gcp, compute
2429

2530
if instance_config is None:
2631
instance_config = config["providers"]["gcp"]["instances"]["default"]
2732

2833
gcp = config["providers"]["gcp"]
29-
if gcp["enabled"] == 'True':
30-
try:
34+
try:
35+
if 'sa_json' in instance_config["secrets"]:
36+
credentials = service_account.Credentials.from_service_account_file(
37+
instance_config["secrets"]["sa_json"]
38+
)
39+
else:
3140
credentials = service_account.Credentials.from_service_account_info(
32-
json.loads(gcp["secrets"]["service_account_key"])
41+
json.loads(instance_config["secrets"]["service_account_key"])
3342
)
34-
compute = googleapiclient.discovery.build('compute', 'v1', credentials=credentials)
43+
compute = googleapiclient.discovery.build('compute', 'v1', credentials=credentials)
3544

36-
return gcp, compute
37-
except TypeError:
38-
logger.error("GCP -> Invalid service account key")
45+
return gcp, compute
46+
except TypeError:
47+
logger.error("GCP -> Invalid service account key")
3948

4049

4150
def create_proxy(instance_config=None):

cloudproxy/providers/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
config["providers"]["gcp"]["instances"]["default"]["secrets"]["service_account_key"] = os.environ.get(
149149
"GCP_SERVICE_ACCOUNT_KEY"
150150
)
151+
config["providers"]["gcp"]["instances"]["default"]["secrets"]["sa_json"] = os.environ.get("GCP_SA_JSON")
151152
config["providers"]["gcp"]["instances"]["default"]["scaling"]["min_scaling"] = int(
152153
os.environ.get("GCP_MIN_SCALING", 2)
153154
)

docs/gcp.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ Now you have your credentials, you can use GCP as a proxy provider. Set up the e
2121
#### Required:
2222
``GCP_ENABLED`` - to enable GCP as a provider, set as True. Default value: False
2323

24-
``GCP_SA_JSON`` - the path to the service account JSON key file. For Docker, mount the file to the container and provide the path.
24+
``GCP_SA_JSON`` or `GCP_SERVICE_ACCOUNT_KEY` - the path to the service account JSON key file. For Docker, mount the file to the container and provide the path. If both `GCP_SA_JSON` and `GCP_SERVICE_ACCOUNT_KEY` are set, `GCP_SA_JSON` will override the other.
25+
26+
``GCP_SERVICE_ACCOUNT_KEY`` or (`GCP_SA_JSON`) - service acount JSON key content. If both `GCP_SA_JSON` and `GCP_SERVICE_ACCOUNT_KEY` are set, `GCP_SA_JSON` will override the other.
2527

2628
``GCP_ZONE`` - the GCP zone where the instances will be created. Default value: us-central1-a
2729

0 commit comments

Comments
 (0)