Skip to content

Commit 1d17086

Browse files
sfc-gh-stakedaankit-bhatnagar167
authored andcommitted
SNOW-82268: Privatelink OCSP URL Update
1 parent 8017be7 commit 1d17086

File tree

2 files changed

+51
-21
lines changed

2 files changed

+51
-21
lines changed

connection.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
HeartBeatTimer, get_time_millis)
6969
from .util_text import split_statements, construct_hostname, parse_account
7070

71+
from snowflake.connector.network import APPLICATION_SNOWSQL
72+
7173

7274
def DefaultConverterClass():
7375
if PY_ISSUE_23517 or IS_WINDOWS:
@@ -599,6 +601,33 @@ def __set_error_attributes(self):
599601
callable(getattr(errors, method))]:
600602
setattr(self, m, getattr(errors, m))
601603

604+
@staticmethod
605+
def setup_ocsp_privatelink(app, hostname):
606+
if app == APPLICATION_SNOWSQL:
607+
ocsp_cache_server = u'http://ocsp{}/ocsp_response_cache.json'.format(
608+
hostname[hostname.index('.'):])
609+
else:
610+
ocsp_cache_server = \
611+
u'http://ocsp.{}/ocsp_response_cache.json'.format(
612+
hostname)
613+
if 'SF_OCSP_RESPONSE_CACHE_SERVER_URL' not in os.environ:
614+
os.environ[
615+
'SF_OCSP_RESPONSE_CACHE_SERVER_URL'] = ocsp_cache_server
616+
else:
617+
if not os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL']. \
618+
startswith("http://"):
619+
ocsp_cache_server = "http://{0}/{1}".format(
620+
os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'],
621+
"ocsp_response_cache.json")
622+
else:
623+
ocsp_cache_server = "{0}/{1}".format(
624+
os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'],
625+
"ocsp_response_cache.json")
626+
627+
os.environ[
628+
'SF_OCSP_RESPONSE_CACHE_SERVER_URL'] = ocsp_cache_server
629+
logger.debug(u"OCSP Cache Server is updated: %s", ocsp_cache_server)
630+
602631
def __open_connection(self):
603632
u"""
604633
Opens a new network connection
@@ -627,26 +656,7 @@ def __open_connection(self):
627656
os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'])
628657

629658
if self.host.endswith(u".privatelink.snowflakecomputing.com"):
630-
ocsp_cache_server = \
631-
u'http://ocsp.{}/ocsp_response_cache.json'.format(
632-
self.host)
633-
if 'SF_OCSP_RESPONSE_CACHE_SERVER_URL' not in os.environ:
634-
os.environ[
635-
'SF_OCSP_RESPONSE_CACHE_SERVER_URL'] = ocsp_cache_server
636-
else:
637-
if not os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL']. \
638-
startswith("http://"):
639-
ocsp_cache_server = "http://{0}/{1}".format(
640-
os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'],
641-
"ocsp_response_cache.json")
642-
else:
643-
ocsp_cache_server = "{0}/{1}".format(
644-
os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'],
645-
"ocsp_response_cache.json")
646-
647-
os.environ[
648-
'SF_OCSP_RESPONSE_CACHE_SERVER_URL'] = ocsp_cache_server
649-
logger.debug(u"OCSP Cache Server is updated: %s", ocsp_cache_server)
659+
SnowflakeConnection.setup_ocsp_privatelink(self.application, self.host)
650660
else:
651661
if 'SF_OCSP_RESPONSE_CACHE_SERVER_URL' in os.environ:
652662
del os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL']

test/test_connection.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
except:
2020
CONNECTION_PARAMETERS_ADMIN = {}
2121

22+
from snowflake.connector.network import APPLICATION_SNOWSQL, CLIENT_NAME
23+
from snowflake.connector.connection import SnowflakeConnection
24+
2225

2326
def test_basic(conn_testaccount):
2427
"""
@@ -543,4 +546,21 @@ def test_disable_request_pooling(db_parameters):
543546
try:
544547
assert cnx.disable_request_pooling
545548
finally:
546-
cnx.close()
549+
cnx.close()
550+
551+
552+
def test_privatelink_ocsp_url_creation():
553+
hostname = "testaccount.us-east-1.privatelink.snowflakecomputing.com"
554+
SnowflakeConnection.setup_ocsp_privatelink(APPLICATION_SNOWSQL, hostname)
555+
556+
ocsp_cache_server = os.getenv("SF_OCSP_RESPONSE_CACHE_SERVER_URL", None)
557+
assert ocsp_cache_server == \
558+
"http://ocsp.us-east-1.privatelink.snowflakecomputing.com/ocsp_response_cache.json"
559+
560+
del os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL']
561+
562+
SnowflakeConnection.setup_ocsp_privatelink(CLIENT_NAME, hostname)
563+
ocsp_cache_server = os.getenv("SF_OCSP_RESPONSE_CACHE_SERVER_URL", None)
564+
assert ocsp_cache_server == \
565+
"http://ocsp.testaccount.us-east-1.privatelink.snowflakecomputing.com/ocsp_response_cache.json"
566+

0 commit comments

Comments
 (0)