Skip to content

Commit 844faba

Browse files
committed
caching plan, blinking eyes
1 parent b99d07d commit 844faba

File tree

1 file changed

+58
-23
lines changed

1 file changed

+58
-23
lines changed

ide/auth.py

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from . import app
1818
from . import routes
19+
from . import models
1920

2021
try:
2122
from . import secret
@@ -32,32 +33,66 @@
3233
AUTH_REDIRECT_URI = os.environ.get("FN_AUTH_REDIRECT_URI", default=False)
3334
BASE_URI = os.environ.get("FN_BASE_URI", default=False)
3435

36+
def get_project_name():
37+
web_setting = models.Setting.get('google_project_name')
38+
return web_setting.value
39+
3540
#
3641
# Robust way to check for running locally. Also easy to modify.
3742
#
3843
GRL = os.environ.get("GLOWSCRIPT_RUNNING_LOCALLY")
3944
GRL = GRL and GRL.lower() # let's keep it case insenstive
4045
GRL = GRL not in (None, 'false') # Anything but None or 'false'
4146

42-
if (not GRL):
43-
#
44-
# If we're not running locally, we should get the secrets from the secret manager.
45-
#
46-
47-
GOOGLE_PROJECT_ID=os.environ.get('GOOGLE_PROJECT_ID') or 'glowscript-py38'
48-
CLIENT_SECRET_VERSION=os.environ.get('CLIENT_SECRET_VERSION') or '1'
49-
from google.cloud import secretmanager
50-
secrets = secretmanager.SecretManagerServiceClient()
51-
secret_path = f"projects/{GOOGLE_PROJECT_ID}/secrets/OAUTH_CLIENT_SECRETS/versions/{CLIENT_SECRET_VERSION}"
52-
theSecret = secrets.access_secret_version(secret_path).payload.data.decode("utf-8")
53-
client_secrets = json.loads(theSecret)
54-
CLIENT_ID = client_secrets.get("FN_CLIENT_ID")
55-
CLIENT_SECRET = client_secrets.get("FN_CLIENT_SECRET")
56-
if CLIENT_ID is None:
57-
raise RuntimeError("We are not running locally, but CLIENT_ID is not set. Dang. Did you mean to set GLOWSCRIPT_RUNNING_LOCALLY?")
58-
else:
59-
CLIENT_ID = ''
60-
CLIENT_SECRET = ''
47+
class ModuleCache:
48+
49+
def __init__(self):
50+
self.cache = {}
51+
52+
def fillCache(self):
53+
if (not GRL):
54+
#
55+
# If we're not running locally, we should get the secrets from the secret manager.
56+
#
57+
# this got much more complicated by storing the project id in the datastore.
58+
# we cannot accesss the datastore unless we are in application context but that
59+
# means it's got to be cached somehow if we don't want to have to keep pulling
60+
# it from the datastore on every request. Bleah.
61+
#
62+
63+
GOOGLE_PROJECT_ID=get_project_name()
64+
CLIENT_SECRET_VERSION=os.environ.get('CLIENT_SECRET_VERSION') or '1'
65+
from google.cloud import secretmanager
66+
secrets = secretmanager.SecretManagerServiceClient()
67+
secret_path = f"projects/{GOOGLE_PROJECT_ID}/secrets/OAUTH_CLIENT_SECRETS/versions/{CLIENT_SECRET_VERSION}"
68+
theSecret = secrets.access_secret_version(secret_path).payload.data.decode("utf-8")
69+
client_secrets = json.loads(theSecret)
70+
CLIENT_ID = client_secrets.get("FN_CLIENT_ID")
71+
CLIENT_SECRET = client_secrets.get("FN_CLIENT_SECRET")
72+
if CLIENT_ID is None:
73+
raise RuntimeError("We are not running locally, but CLIENT_ID is not set. Dang. Did you mean to set GLOWSCRIPT_RUNNING_LOCALLY?")
74+
else:
75+
CLIENT_ID = ''
76+
CLIENT_SECRET = ''
77+
78+
self.cache['CLIENT_ID'] = CLIENT_ID
79+
self.cache['CLINET_SECRET'] = CLIENT_SECRET
80+
81+
def getID(self):
82+
theID = self.cache.get('CLIENT_ID')
83+
if theID is None:
84+
self.fillCache()
85+
theID = self.cache.get('CLIENT_ID')
86+
return theID
87+
88+
def getSecret(self):
89+
theSecret = self.cache.get('CLINET_SECRET')
90+
if theSecret is None:
91+
self.fillCache()
92+
theSecret = self.cache.get('CLINET_SECRET')
93+
return theSecret
94+
95+
module_cache = ModuleCache()
6196

6297
AUTH_TOKEN_KEY = 'auth_token'
6398
AUTH_STATE_KEY = 'auth_state'
@@ -74,8 +109,8 @@ def build_credentials():
74109
return google.oauth2.credentials.Credentials(
75110
oauth2_tokens['access_token'],
76111
refresh_token=oauth2_tokens['refresh_token'],
77-
client_id=CLIENT_ID,
78-
client_secret=CLIENT_SECRET,
112+
client_id=module_cache.getID(),
113+
client_secret=module_cache.getSecret(),
79114
token_uri=ACCESS_TOKEN_URI)
80115

81116
def get_user_info():
@@ -107,7 +142,7 @@ def no_cache_impl(*args, **kwargs):
107142
@no_cache
108143
def google_login():
109144

110-
session = OAuth2Session(CLIENT_ID, CLIENT_SECRET,
145+
session = OAuth2Session(module_cache.getID(), module_cache.getSecret(),
111146
scope=AUTHORIZATION_SCOPE,
112147
redirect_uri=AUTH_REDIRECT_URI)
113148

@@ -133,7 +168,7 @@ def google_auth_redirect():
133168
"""
134169
return flask.redirect('/index')
135170

136-
session = OAuth2Session(CLIENT_ID, CLIENT_SECRET,
171+
session = OAuth2Session(module_cache.getID(), module_cache.getSecret(),
137172
scope=AUTHORIZATION_SCOPE,
138173
state=flask.session.get(AUTH_STATE_KEY),
139174
redirect_uri=AUTH_REDIRECT_URI)

0 commit comments

Comments
 (0)