Skip to content

Commit 8ff6f56

Browse files
committed
added test in tests/integ/test_secrets.py
1 parent df7672c commit 8ff6f56

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

tests/integ/conftest.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ def set_up_external_access_integration_resources(
7474
integration1,
7575
integration2,
7676
integration3,
77+
key4,
78+
integration4,
79+
wif_audience,
7780
):
7881
try:
7982
# IMPORTANT SETUP NOTES: the test role needs to be granted the creation privilege
@@ -128,6 +131,12 @@ def set_up_external_access_integration_resources(
128131
).collect()
129132
session.sql(
130133
f"""
134+
CREATE SECRET IF NOT EXISTS {key4}
135+
TYPE = WORKLOAD_IDENTITY_FEDERATION;
136+
"""
137+
).collect()
138+
session.sql(
139+
f"""
131140
CREATE IF NOT EXISTS EXTERNAL ACCESS INTEGRATION {integration1}
132141
ALLOWED_NETWORK_RULES = ({rule1})
133142
ALLOWED_AUTHENTICATION_SECRETS = ({key1})
@@ -148,6 +157,13 @@ def set_up_external_access_integration_resources(
148157
ALLOWED_NETWORK_RULES = ({rule3})
149158
ALLOWED_AUTHENTICATION_SECRETS = ({key3})
150159
ENABLED = true;
160+
"""
161+
).collect()
162+
session.sql(
163+
f"""
164+
CREATE IF NOT EXISTS EXTERNAL ACCESS INTEGRATION {integration4}
165+
ALLOWED_AUTHENTICATION_SECRETS = ({key4})
166+
ENABLED = true;
151167
"""
152168
).collect()
153169
CONNECTION_PARAMETERS["external_access_rule1"] = rule1
@@ -156,9 +172,12 @@ def set_up_external_access_integration_resources(
156172
CONNECTION_PARAMETERS["external_access_key1"] = key1
157173
CONNECTION_PARAMETERS["external_access_key2"] = key2
158174
CONNECTION_PARAMETERS["external_access_key3"] = key3
175+
CONNECTION_PARAMETERS["external_access_key4"] = key4
159176
CONNECTION_PARAMETERS["external_access_integration1"] = integration1
160177
CONNECTION_PARAMETERS["external_access_integration2"] = integration2
161178
CONNECTION_PARAMETERS["external_access_integration3"] = integration3
179+
CONNECTION_PARAMETERS["external_access_integration4"] = integration4
180+
CONNECTION_PARAMETERS["wif_audience"] = wif_audience
162181
except SnowparkSQLException:
163182
# GCP currently does not support external access integration
164183
# we can remove the exception once the integration is available on GCP
@@ -184,9 +203,12 @@ def clean_up_external_access_integration_resources():
184203
CONNECTION_PARAMETERS.pop("external_access_key1", None)
185204
CONNECTION_PARAMETERS.pop("external_access_key2", None)
186205
CONNECTION_PARAMETERS.pop("external_access_key3", None)
206+
CONNECTION_PARAMETERS.pop("external_access_key4", None)
187207
CONNECTION_PARAMETERS.pop("external_access_integration1", None)
188208
CONNECTION_PARAMETERS.pop("external_access_integration2", None)
189209
CONNECTION_PARAMETERS.pop("external_access_integration3", None)
210+
CONNECTION_PARAMETERS.pop("external_access_integration4", None)
211+
CONNECTION_PARAMETERS.pop("wif_audience", None)
190212

191213

192214
def set_up_dataframe_processor_parameters(
@@ -315,9 +337,12 @@ def session(
315337
key1 = "snowpark_python_test_key1"
316338
key2 = "snowpark_python_test_key2"
317339
key3 = "snowpark_python_test_key3"
340+
key4 = "snowpark_python_test_key4"
318341
integration1 = "snowpark_python_test_integration1"
319342
integration2 = "snowpark_python_test_integration2"
320343
integration3 = "snowpark_python_test_integration3"
344+
integration4 = "snowpark_python_test_integration4"
345+
wif_audience = "https://replace-with-your-wif-audience"
321346

322347
session = (
323348
Session.builder.configs(db_parameters)
@@ -351,6 +376,9 @@ def session(
351376
integration1,
352377
integration2,
353378
integration3,
379+
key4,
380+
integration4,
381+
wif_audience,
354382
)
355383

356384
if validate_ast:
@@ -387,9 +415,12 @@ def profiler_session(
387415
key1 = "snowpark_python_profiler_test_key1"
388416
key2 = "snowpark_python_profiler_test_key2"
389417
key3 = "snowpark_python_profiler_test_key3"
418+
key4 = "snowpark_python_profiler_test_key4"
390419
integration1 = "snowpark_python_profiler_test_integration1"
391420
integration2 = "snowpark_python_profiler_test_integration2"
392421
integration3 = "snowpark_python_profiler_test_integration3"
422+
integration4 = "snowpark_python_profiler_test_integration4"
423+
wif_audience = "https://replace-with-your-wif-audience"
393424
session = (
394425
Session.builder.configs(db_parameters)
395426
.config("local_testing", local_testing_mode)
@@ -409,6 +440,9 @@ def profiler_session(
409440
integration1,
410441
integration2,
411442
integration3,
443+
key4,
444+
integration4,
445+
wif_audience,
412446
)
413447
set_up_test_session_parameters(session, local_testing_mode)
414448
try:

tests/integ/test_secrets.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,56 @@ def get_secret():
153153
)
154154

155155

156+
@pytest.mark.skipif(
157+
IS_NOT_ON_GITHUB or not RUNNING_ON_JENKINS,
158+
reason="Secret API is only supported on Snowflake server environment",
159+
)
160+
def test_get_wif_token_udf(session, db_parameters):
161+
def get_wif():
162+
token = get_wif_token("cred", db_parameters["wif_audience"])
163+
return len(token) > 0
164+
165+
try:
166+
get_wif_udf = session.udf.register(
167+
get_wif,
168+
return_type=BooleanType(),
169+
packages=["snowflake-snowpark-python"],
170+
external_access_integrations=[
171+
db_parameters["external_access_integration4"]
172+
],
173+
secrets={"cred": f"{db_parameters['external_access_key4']}"},
174+
)
175+
df = session.create_dataframe([[1], [2]]).to_df("x")
176+
Utils.check_answer(df.select(get_wif_udf()), [Row(True), Row(True)])
177+
except KeyError:
178+
pytest.skip("External Access Integration is not supported on the deployment.")
179+
180+
181+
@pytest.mark.skipif(
182+
IS_NOT_ON_GITHUB or not RUNNING_ON_JENKINS,
183+
reason="Secret API is only supported on Snowflake server environment",
184+
)
185+
def test_get_wif_token_sproc(session, db_parameters):
186+
def get_wif_in_sproc(session_):
187+
token = get_wif_token("cred", db_parameters["wif_audience"])
188+
return len(token) > 0
189+
190+
try:
191+
get_wif_sp = session.sproc.register(
192+
get_wif_in_sproc,
193+
return_type=BooleanType(),
194+
packages=["snowflake-snowpark-python"],
195+
external_access_integrations=[
196+
db_parameters["external_access_integration4"]
197+
],
198+
secrets={"cred": f"{db_parameters['external_access_key4']}"},
199+
anonymous=True,
200+
)
201+
assert get_wif_sp()
202+
except KeyError:
203+
pytest.skip("External Access Integration is not supported on the deployment.")
204+
205+
156206
@pytest.mark.skipif(
157207
IS_IN_STORED_PROC,
158208
reason="Run only outside Snowflake server to validate NotImplementedError",

0 commit comments

Comments
 (0)