-
Notifications
You must be signed in to change notification settings - Fork 107
fix(datasets): Fix StudyDataset
to properly propagate RDB password
#1077
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,13 +116,13 @@ def __init__( # noqa: PLR0913 | |
self._study_name = self._validate_study_name(study_name=study_name) | ||
|
||
credentials = self._validate_credentials(backend=backend, credentials=credentials) | ||
storage = URL.create( | ||
storage_url = URL.create( | ||
drivername=backend, | ||
database=database, | ||
**credentials, | ||
) | ||
|
||
self._storage = str(storage) | ||
self._storage_url = storage_url | ||
self.metadata = metadata | ||
|
||
filepath = None | ||
|
@@ -286,8 +286,10 @@ def load(self) -> optuna.Study: | |
pruner_config = load_args.pop("pruner") | ||
pruner = self._get_pruner(pruner_config) | ||
|
||
storage_url_str = self._storage_url.render_as_string(hide_password=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this expose your database password? I have never used Optuna, so don't really know how this works, but ideally this should still be a secure way of handling credentials. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does indeed expose your database password. I am not sure what are the security consequences of this but I am not seeing a workaround. Optuna's Would you have any pointers regarding to the security scenario where that would be problematic or to a discussion regarding secure handling of credentials? I am happy to learn more about this and try and come back with a better solution! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @merelcht There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @gtauzin , sincere apologies for not getting back to you earlier! This slipped my mind during and after the 1.0 release of Kedro. My main concern is whether this could accidentally leak credentials and make it possible for bad actors to retrieve your database keys. Would it work if you just directly call:
And skip the rendering as string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm OK with the password handling and don't necessarily see something better. The rendered password is only in a variable, not in anything saved to the class, right? Unless I'm missing something in my pass through. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @deepyaman Indeed, all credentials are stored in a class variable @merelcht No worries! Initially I figured you were busy with 1.0 and then I forgot about it until I started needing it again :) For your suggestion, the I have had a look at Is the problem related to credentials are stored in the class and can be accessed clearly or the presence of a line of code which exposes them clearly? Thanks to both of you for the help. |
||
storage = optuna.storages.RDBStorage(url=storage_url_str) | ||
study = optuna.load_study( | ||
storage=self._storage, | ||
storage=storage, | ||
study_name=self._get_load_study_name(), | ||
sampler=sampler, | ||
pruner=pruner, | ||
|
@@ -297,37 +299,45 @@ def load(self) -> optuna.Study: | |
|
||
def save(self, study: optuna.Study) -> None: | ||
save_study_name = self._get_save_study_name() | ||
|
||
storage_url_str = self._storage_url.render_as_string(hide_password=False) | ||
if self._backend == "sqlite": | ||
os.makedirs(os.path.dirname(self._filepath), exist_ok=True) | ||
|
||
if not os.path.isfile(self._filepath): | ||
optuna.create_study( | ||
storage=self._storage, | ||
storage=storage_url_str, | ||
) | ||
|
||
storage = optuna.storages.RDBStorage(url=storage_url_str) | ||
|
||
# To overwrite an existing study, we need to first delete it if it exists | ||
if self._study_name_exists(save_study_name): | ||
optuna.delete_study( | ||
storage=self._storage, | ||
storage=storage, | ||
study_name=save_study_name, | ||
) | ||
|
||
optuna.copy_study( | ||
from_study_name=study.study_name, | ||
from_storage=study._storage, | ||
to_storage=self._storage, | ||
to_storage=storage, | ||
to_study_name=save_study_name, | ||
) | ||
|
||
def _study_name_exists(self, study_name) -> bool: | ||
if self._backend == "sqlite" and not os.path.isfile(self._database): | ||
return False | ||
|
||
study_names = optuna.study.get_all_study_names(storage=self._storage) | ||
storage_url_str = self._storage_url.render_as_string(hide_password=False) | ||
storage = optuna.storages.RDBStorage(url=storage_url_str) | ||
study_names = optuna.study.get_all_study_names(storage=storage) | ||
return study_name in study_names | ||
|
||
def _study_name_glob(self, pattern): | ||
study_names = optuna.study.get_all_study_names(storage=self._storage) | ||
storage_url_str = self._storage_url.render_as_string(hide_password=False) | ||
storage = optuna.storages.RDBStorage(url=storage_url_str) | ||
study_names = optuna.study.get_all_study_names(storage=storage) | ||
for study_name in study_names: | ||
if fnmatch.fnmatch(study_name, pattern): | ||
yield study_name | ||
|
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.