Skip to content

Commit dc0e21a

Browse files
smtakedaankit-bhatnagar167
authored andcommitted
SNOW-69778: Internal change for future enhancement
1 parent 389bd5e commit dc0e21a

File tree

2 files changed

+119
-13
lines changed

2 files changed

+119
-13
lines changed

ocsp_snowflake.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,9 @@ def clear_ssd_cache():
641641
SFSsd.SSD_CACHE = {}
642642

643643
@staticmethod
644-
def find_in_ssd_cache(sfc_endpoint):
645-
if sfc_endpoint in SFSsd.SSD_CACHE:
646-
return True, SFSsd.SSD_CACHE[sfc_endpoint]
644+
def find_in_ssd_cache(account_name):
645+
if account_name in SFSsd.SSD_CACHE:
646+
return True, SFSsd.SSD_CACHE[account_name]
647647
return False, None
648648

649649
@staticmethod
@@ -801,6 +801,20 @@ def is_cert_id_in_cache(self, cert_id, subject):
801801
self, cert_id, subject)
802802
return found, cache
803803

804+
def get_account_from_hostname(self, hostname):
805+
"""
806+
Extract the account name part
807+
from the hostname
808+
:param hostname:
809+
:return: account name
810+
"""
811+
split_hname = hostname.split('.')
812+
if "global" in split_hname:
813+
acc_name = split_hname[0].split('-')[0]
814+
else:
815+
acc_name = split_hname[0]
816+
return acc_name
817+
804818
def validate_by_direct_connection(self, issuer, subject, hostname=None, do_retry=True):
805819
ssd_cache_status = False
806820
cache_status = False
@@ -809,7 +823,7 @@ def validate_by_direct_connection(self, issuer, subject, hostname=None, do_retry
809823

810824
cert_id, req = self.create_ocsp_request(issuer, subject)
811825
if SnowflakeOCSP.SSD.ACTIVATE_SSD:
812-
ssd_cache_status, ssd = SnowflakeOCSP.SSD.find_in_ssd_cache(hostname)
826+
ssd_cache_status, ssd = SnowflakeOCSP.SSD.find_in_ssd_cache(self.get_account_from_hostname(hostname))
813827

814828
if not ssd_cache_status:
815829
cache_status, ocsp_response = \
@@ -1173,8 +1187,8 @@ def read_directives():
11731187
with codecs. open(host_specific_ssd, 'r', encoding='utf-8',
11741188
errors='ignore') as f:
11751189
ssd_json = json.load(f)
1176-
for hostname, ssd in ssd_json.items():
1177-
SnowflakeOCSP.SSD.add_to_ssd_persistent_cache(hostname, ssd)
1190+
for account_name, ssd in ssd_json.items():
1191+
SnowflakeOCSP.SSD.add_to_ssd_persistent_cache(account_name, ssd)
11781192

11791193
def process_ocsp_bypass_directive(self, ssd_dir_enc, sfc_cert_id, sfc_endpoint):
11801194
"""
@@ -1211,11 +1225,22 @@ def process_ocsp_bypass_directive(self, ssd_dir_enc, sfc_cert_id, sfc_endpoint):
12111225
return False
12121226

12131227
# Check if the directive is generic (endpoint = *)
1214-
# or if it is meant for a specific endpoint
1228+
# or if it is meant for a specific account
12151229
if jwt_ssd_decoded['sfcEndpoint'] != '*':
1216-
if sfc_endpoint != jwt_ssd_decoded['sfcEndpoint']:
1217-
return False
1230+
"""
1231+
In case there are multiple hostnames
1232+
associated with the same account,
1233+
(client failover, different region
1234+
same account, the sfc_endpoint field
1235+
would be expected to have a space separated
1236+
list of all the hostnames that can be
1237+
associated with the account in question.
1238+
"""
1239+
split_string = jwt_ssd_decoded['sfcEndpoint'].split()
1240+
if sfc_endpoint in split_string:
12181241
return True
1242+
else:
1243+
return False
12191244

12201245
ssd_cert_id_b64 = jwt_ssd_decoded['certId']
12211246
ssd_cert_id = self.decode_cert_id_base64(ssd_cert_id_b64)

test/test_ocsp_ssd.py

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,14 @@ def _create_host_spec_ocsp_bypass_ssd(ocsp, priv_key, hostname):
111111
exp_val = nbf_val+tdelta
112112
header = {'ssd_iss':'dep1'}
113113
payload = {}
114-
payload.update({'sfcEndpoint': hostname})
114+
hname_string = " ".join(hostname)
115+
acc_name = ocsp.get_account_from_hostname(hostname[0])
116+
payload.update({'sfcEndpoint': hname_string})
115117
payload.update({'certId': '*'})
116118
payload.update({'nbf': nbf_val})
117119
payload.update({'exp': exp_val})
118120
host_spec_jwt_token = jwt.encode(payload, priv_key, algorithm='RS512', headers=header)
119-
host_spec_bypass_ssd = {hostname: host_spec_jwt_token.decode("utf-8")}
121+
host_spec_bypass_ssd = {acc_name: host_spec_jwt_token.decode("utf-8")}
120122
json.dump(host_spec_bypass_ssd, jwt_host_spec_fp)
121123

122124

@@ -137,15 +139,16 @@ def test_host_spec_ocsp_bypass_ssd():
137139
ocsp = _setup_ssd_test(temp_ocsp_file_path)
138140
priv_key = _get_test_priv_key(1)
139141

140-
hostname = 'sfcsupport.us-east-1.snowflakecomputing.com'
142+
hostname = ['sfcsupport.us-east-1.snowflakecomputing.com']
141143
try:
142144
_create_host_spec_ocsp_bypass_ssd(ocsp, priv_key, hostname)
143145
except Exception as ex:
144146
print("Exception occurred %s" %ex.message)
145147

146148
ocsp.read_directives()
147149

148-
cache_status, cur_host_spec_token = ocsp.SSD.find_in_ssd_cache(hostname)
150+
acc_name = ocsp.get_account_from_hostname(hostname[0])
151+
cache_status, cur_host_spec_token = ocsp.SSD.find_in_ssd_cache(acc_name)
149152
assert((cur_host_spec_token is not None), "Failed to read host specific directive")
150153

151154
try:
@@ -155,6 +158,84 @@ def test_host_spec_ocsp_bypass_ssd():
155158
print("Exception while processing SSD :"+ex)
156159

157160

161+
def test_host_spec_ocsp_bypass_updated_ssd():
162+
163+
"""
164+
Clean any skeletons of past tests
165+
"""
166+
_teardown_ssd_test_setup()
167+
168+
"""
169+
Setup OCSP instance to use test keys
170+
for authenticating SSD
171+
"""
172+
tmp_dir = str(tempfile.gettempdir())
173+
temp_ocsp_file_path = path.join(tmp_dir, "ocsp_cache_backup.json")
174+
copy(OCSP_RESPONSE_CACHE_URI, temp_ocsp_file_path)
175+
ocsp = _setup_ssd_test(temp_ocsp_file_path)
176+
priv_key = _get_test_priv_key(1)
177+
178+
hostname = ['sfcsupport-test12345.global.us-east-1.snowflakecomputing.com',
179+
'sfcsupport-test67890.global.us-east-1.snowflakecomputing.com',
180+
'sfcsupport.us-east-1.snowflakecomputing.com',
181+
'sfcsupport.us-east-2.snowflakecomputing.com']
182+
try:
183+
_create_host_spec_ocsp_bypass_ssd(ocsp, priv_key, hostname)
184+
except Exception as ex:
185+
print("Exception occurred %s" %ex.message)
186+
187+
ocsp.read_directives()
188+
189+
acc_name = ocsp.get_account_from_hostname(hostname[0])
190+
cache_status, cur_host_spec_token = ocsp.SSD.find_in_ssd_cache(acc_name)
191+
assert((cur_host_spec_token is not None), "Failed to read host specific directive")
192+
193+
try:
194+
assert ocsp.process_ocsp_bypass_directive(cur_host_spec_token, '*', hostname[1]),\
195+
"Failed to process host specific bypass ssd"
196+
except Exception as ex:
197+
print("Exception while processing SSD :"+ex)
198+
199+
200+
def test_invalid_host_spec_ocsp_bypass_updated_ssd():
201+
202+
"""
203+
Clean any skeletons of past tests
204+
"""
205+
_teardown_ssd_test_setup()
206+
207+
"""
208+
Setup OCSP instance to use test keys
209+
for authenticating SSD
210+
"""
211+
tmp_dir = str(tempfile.gettempdir())
212+
temp_ocsp_file_path = path.join(tmp_dir, "ocsp_cache_backup.json")
213+
copy(OCSP_RESPONSE_CACHE_URI, temp_ocsp_file_path)
214+
ocsp = _setup_ssd_test(temp_ocsp_file_path)
215+
priv_key = _get_test_priv_key(1)
216+
217+
hostname = ['sfcsupport-test12345.global.us-east-1.snowflakecomputing.com',
218+
'sfcsupport-test67890.global.us-east-1.snowflakecomputing.com',
219+
'sfcsupport.us-east-1.snowflakecomputing.com',
220+
'sfcsupport.us-east-2.snowflakecomputing.com']
221+
try:
222+
_create_host_spec_ocsp_bypass_ssd(ocsp, priv_key, hostname)
223+
except Exception as ex:
224+
print("Exception occurred %s" %ex.message)
225+
226+
ocsp.read_directives()
227+
228+
acc_name = ocsp.get_account_from_hostname(hostname[0])
229+
cache_status, cur_host_spec_token = ocsp.SSD.find_in_ssd_cache(acc_name)
230+
assert((cur_host_spec_token is not None), "Failed to read host specific directive")
231+
232+
try:
233+
assert ocsp.process_ocsp_bypass_directive(cur_host_spec_token, '*', "sonytv.snowflakecomputing.com") is False,\
234+
"SSD should not match hostname specified"
235+
except Exception as ex:
236+
print("Exception while processing SSD :"+ex)
237+
238+
158239
def _create_cert_spec_ocsp_bypass_token(priv_key, cid, validity_days=1):
159240

160241
tdelta = timedelta(days=validity_days)

0 commit comments

Comments
 (0)