-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENTERPRISE-1418] Add support for plain JWT authentication (#1078)
* [ENTERPRISE-1418] Add support for plain JWT authentication * Run changie new * wip: functional test for JWT * clean up, and add some comments
- Loading branch information
Showing
4 changed files
with
160 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Support JWT Authentication | ||
time: 2024-06-10T17:10:26.421463-04:00 | ||
custom: | ||
Author: llam15 | ||
Issue: 1079 726 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
""" | ||
Please follow the instructions in test_oauth.py for instructions on how to set up | ||
the security integration required to retrieve a JWT from Snowflake. | ||
""" | ||
|
||
import pytest | ||
import os | ||
from dbt.tests.util import run_dbt, check_relations_equal | ||
|
||
from dbt.adapters.snowflake import SnowflakeCredentials | ||
|
||
_MODELS__MODEL_1_SQL = """ | ||
select 1 as id | ||
""" | ||
|
||
|
||
_MODELS__MODEL_2_SQL = """ | ||
select 2 as id | ||
""" | ||
|
||
|
||
_MODELS__MODEL_3_SQL = """ | ||
select * from {{ ref('model_1') }} | ||
union all | ||
select * from {{ ref('model_2') }} | ||
""" | ||
|
||
|
||
_MODELS__MODEL_4_SQL = """ | ||
select 1 as id | ||
union all | ||
select 2 as id | ||
""" | ||
|
||
|
||
class TestSnowflakeJWT: | ||
"""Tests that setting authenticator: jwt allows setting token to a plain JWT | ||
that will be passed into the Snowflake connection without modification.""" | ||
|
||
@pytest.fixture(scope="class", autouse=True) | ||
def access_token(self): | ||
"""Because JWTs are short-lived, we need to get a fresh JWT via the refresh | ||
token flow before running the test. | ||
This fixture leverages the existing SnowflakeCredentials._get_access_token | ||
method to retrieve a valid JWT from Snowflake. | ||
""" | ||
client_id = os.getenv("SNOWFLAKE_TEST_OAUTH_CLIENT_ID") | ||
client_secret = os.getenv("SNOWFLAKE_TEST_OAUTH_CLIENT_SECRET") | ||
refresh_token = os.getenv("SNOWFLAKE_TEST_OAUTH_REFRESH_TOKEN") | ||
|
||
credentials = SnowflakeCredentials( | ||
account=os.getenv("SNOWFLAKE_TEST_ACCOUNT"), | ||
database="", | ||
schema="", | ||
authenticator="oauth", | ||
oauth_client_id=client_id, | ||
oauth_client_secret=client_secret, | ||
token=refresh_token, | ||
) | ||
|
||
yield credentials._get_access_token() | ||
|
||
@pytest.fixture(scope="class", autouse=True) | ||
def dbt_profile_target(self, access_token): | ||
"""A dbt_profile that has authenticator set to JWT, and token set to | ||
a JWT accepted by Snowflake. Also omits the user, as the user attribute | ||
is optional when the authenticator is set to JWT. | ||
""" | ||
return { | ||
"type": "snowflake", | ||
"threads": 4, | ||
"account": os.getenv("SNOWFLAKE_TEST_ACCOUNT"), | ||
"database": os.getenv("SNOWFLAKE_TEST_DATABASE"), | ||
"warehouse": os.getenv("SNOWFLAKE_TEST_WAREHOUSE"), | ||
"authenticator": "jwt", | ||
"token": access_token, | ||
} | ||
|
||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"model_1.sql": _MODELS__MODEL_1_SQL, | ||
"model_2.sql": _MODELS__MODEL_2_SQL, | ||
"model_3.sql": _MODELS__MODEL_3_SQL, | ||
"model_4.sql": _MODELS__MODEL_4_SQL, | ||
} | ||
|
||
def test_snowflake_basic(self, project): | ||
run_dbt() | ||
check_relations_equal(project.adapter, ["MODEL_3", "MODEL_4"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters