Skip to content
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

Add documentation examples for loading credentials externally #2148

Closed
Tracked by #2239
idanov opened this issue Dec 20, 2022 · 0 comments · Fixed by #2299
Closed
Tracked by #2239

Add documentation examples for loading credentials externally #2148

idanov opened this issue Dec 20, 2022 · 0 comments · Fixed by #2299
Assignees
Labels
Component: Documentation 📄 Issue/PR for markdown and API documentation

Comments

@idanov
Copy link
Member

idanov commented Dec 20, 2022

Description

We often get requests on how to use credentials managers with Kedro, especially in platforms like Azure, AWS or GCP. This has always been possible, but not easy to come up with on your own and we haven't had any documentation on it. With the new changes on the AbstractConfigLoader and the addition of the after_context_created hook, this has become extremely easy, but will remain unknown to our users unless we document it.

Context

Our users need to deploy to one of the aforementioned platforms very often and thus they would need to provide credentials to their applications in a more robust way than just through config files.

Possible Implementation

Here's an example stub for adding a hook, which augments the credentials with the ones that could be found in the given vault.

from kedro.framework.hooks import hook_impl
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential

class AzureSecretsHooks:
    @hook_impl
    def after_context_created(self, context) -> None:
        # to learn more details about Azure's keyvault, see https://learn.microsoft.com/en-us/azure/key-vault/secrets/quick-create-python?tabs=azure-cli
        keyVaultName = "your-vault-name" # or os.environ["KEY_VAULT_NAME"] if you would like to provide it through environment variables
        KVUri = f"https://{keyVaultName}.vault.azure.net"
        
        credential = DefaultAzureCredential()
        client = SecretClient(vault_url=KVUri, credential=credential)

        secret_keys = ['key1', 'key2', 'key3', ...]
        azure_creds = {k: client.get_secret("your-secret-name").value for k in secret_keys}

        context.config_loader['credentials'] = {**context.config_loader['credentials'], **azure_creds}
        o

The above example needs to be reworked to show a real and working example, where the credentials might be more complicated, i.e. each Kedro credentials alias contains a couple of key/value mappings, similar to what we have currently in the docs: https://kedro.readthedocs.io/en/stable/data/data_catalog.html?highlight=catalog#example-16-loads-a-model-saved-as-a-pickle-from-azure-blob-storage, where dev_abs is the key used in the catalog, and that key has a couple of key/value mappings in it.

The hook needs to be added to the settings.py file in your project as follows:

HOOKS = (AzureSecretsHooks(),)

Possible Alternatives

Instead of adding this to the docs, we could also write a blog post.

@idanov idanov added this to the Configuration overhaul milestone Dec 20, 2022
@idanov idanov added the Component: Documentation 📄 Issue/PR for markdown and API documentation label Dec 20, 2022
@merelcht merelcht moved this to To Do in Kedro Framework Jan 23, 2023
@merelcht merelcht mentioned this issue Jan 23, 2023
7 tasks
@AhdraMeraliQB AhdraMeraliQB self-assigned this Jan 25, 2023
@AhdraMeraliQB AhdraMeraliQB moved this from To Do to In Progress in Kedro Framework Jan 25, 2023
@AhdraMeraliQB AhdraMeraliQB linked a pull request Feb 8, 2023 that will close this issue
5 tasks
@AhdraMeraliQB AhdraMeraliQB moved this from In Progress to In Review in Kedro Framework Feb 8, 2023
@github-project-automation github-project-automation bot moved this from In Review to Done in Kedro Framework Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Documentation 📄 Issue/PR for markdown and API documentation
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants