-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
duckdb filesystem custom secrets #2017
Open
sh-rp
wants to merge
12
commits into
devel
Choose a base branch
from
fix/duckdb_filesystem_custom_secrets
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ce6845a
disable secrets clearing on CI
sh-rp dd50f69
first version of custom secrets directory
sh-rp 0c69056
add test and remove secret directory from create_authentication method
sh-rp 933ee06
revert changes to authentication creation for in mem secrets
sh-rp 1a2df53
format
sh-rp cd30eb8
fix warning message
sh-rp 554cb95
don't actually load anything in secrets test
sh-rp bc591bd
run test with mock spy
sh-rp 40b8b80
add s3 extra
sh-rp 441e015
close connection after use
sh-rp 842fae5
ensure at least aiohttp 3.9 for python 3.12
sh-rp 6e8f65b
move secrets tests further down and fix deps
sh-rp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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,106 @@ | ||
from typing import Any | ||
|
||
import pytest | ||
import os | ||
|
||
from tests.utils import TEST_STORAGE_ROOT | ||
from tests.load.utils import ( | ||
destinations_configs, | ||
DestinationTestConfiguration, | ||
AWS_BUCKET, | ||
) | ||
from dlt.common.utils import uniq_id | ||
|
||
|
||
@pytest.mark.essential | ||
@pytest.mark.parametrize( | ||
"destination_config", | ||
destinations_configs(all_buckets_filesystem_configs=True, bucket_subset=(AWS_BUCKET,)), | ||
ids=lambda x: x.name, | ||
) | ||
def test_secrets_management( | ||
destination_config: DestinationTestConfiguration, capfd: pytest.CaptureFixture[Any] | ||
) -> None: | ||
"""Test the handling of secrets by the sql_client, we only need to do this on s3 | ||
as the other destinations work accordingly""" | ||
|
||
# we can use fake keys | ||
os.environ["DESTINATION__FILESYSTEM__CREDENTIALS__AWS_SECRET_ACCESS_KEY"] = "secret_key" | ||
os.environ["DESTINATION__FILESYSTEM__CREDENTIALS__AWS_ACCESS_KEY_ID"] = "key" | ||
|
||
warning_mesage = "You are persisting duckdb secrets but are storing them in the default folder" | ||
|
||
pipeline = destination_config.setup_pipeline( | ||
"read_pipeline", | ||
dataset_name="read_test", | ||
) | ||
|
||
import duckdb | ||
from duckdb import HTTPException | ||
from dlt.destinations.impl.filesystem.sql_client import ( | ||
FilesystemSqlClient, | ||
DuckDbCredentials, | ||
) | ||
|
||
duck_db_location = TEST_STORAGE_ROOT + "/" + uniq_id() | ||
secrets_dir = f"{TEST_STORAGE_ROOT}/duck_secrets_{uniq_id()}" | ||
|
||
def _external_duckdb_connection() -> duckdb.DuckDBPyConnection: | ||
external_db = duckdb.connect(duck_db_location) | ||
external_db.sql(f"SET secret_directory = '{secrets_dir}';") | ||
external_db.execute("CREATE SCHEMA IF NOT EXISTS first;") | ||
return external_db | ||
|
||
def _fs_sql_client_for_external_db( | ||
connection: duckdb.DuckDBPyConnection, | ||
) -> FilesystemSqlClient: | ||
return FilesystemSqlClient( | ||
dataset_name="second", | ||
fs_client=pipeline.destination_client(), # type: ignore | ||
credentials=DuckDbCredentials(connection), | ||
) | ||
|
||
def _secrets_exist() -> bool: | ||
return os.path.isdir(secrets_dir) and len(os.listdir(secrets_dir)) > 0 | ||
|
||
# first test what happens if there are no external secrets | ||
external_db = _external_duckdb_connection() | ||
fs_sql_client = _fs_sql_client_for_external_db(external_db) | ||
with fs_sql_client as sql_client: | ||
sql_client.create_views_for_tables({"items": "items"}) | ||
external_db.close() | ||
assert not _secrets_exist() | ||
|
||
# add secrets and check that they are there | ||
external_db = _external_duckdb_connection() | ||
fs_sql_client = _fs_sql_client_for_external_db(external_db) | ||
with fs_sql_client as sql_client: | ||
fs_sql_client.create_authentication(persistent=True) | ||
assert _secrets_exist() | ||
|
||
# remove secrets and check that they are removed | ||
with fs_sql_client as sql_client: | ||
fs_sql_client.drop_authentication() | ||
assert not _secrets_exist() | ||
external_db.close() | ||
|
||
# prevent creating persistent secrets on in mem databases | ||
fs_sql_client = FilesystemSqlClient( | ||
dataset_name="second", | ||
fs_client=pipeline.destination_client(), # type: ignore | ||
) | ||
with pytest.raises(Exception): | ||
with fs_sql_client as sql_client: | ||
fs_sql_client.create_authentication(persistent=True) | ||
|
||
# check that no warning was logged | ||
assert warning_mesage not in capfd.readouterr().err | ||
|
||
# check that warning is logged when secrets are persisted in the default folder | ||
duck_db_location = TEST_STORAGE_ROOT + "/" + uniq_id() | ||
secrets_dir = f"{TEST_STORAGE_ROOT}/duck_secrets_{uniq_id()}" | ||
duck_db = duckdb.connect(duck_db_location) | ||
fs_sql_client = _fs_sql_client_for_external_db(duck_db) | ||
with fs_sql_client as sql_client: | ||
sql_client.create_authentication(persistent=True) | ||
assert warning_mesage in capfd.readouterr().err |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, somehow it does not work on ci...