-
Notifications
You must be signed in to change notification settings - Fork 260
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
Abstract Secret Management Support #703
Open
xiaoyongzhu
wants to merge
24
commits into
feathr-ai:main
Choose a base branch
from
xiaoyongzhu:secret_manager
base: main
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 19 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
c31906c
Add more secret manager support
xiaoyongzhu e96459a
Add abstract class
xiaoyongzhu 2d6c135
Update feathr-configuration-and-env.md
xiaoyongzhu f616522
Update _envvariableutil.py
xiaoyongzhu cdcd612
add tests for aws secrets manager
xiaoyongzhu aa5fdda
Update test_secrets_read.py
xiaoyongzhu a6870d9
fix tests
xiaoyongzhu 997a2b1
Update test_secrets_read.py
xiaoyongzhu 8be6a42
fix test
xiaoyongzhu e617b99
Update pull_request_push_test.yml
xiaoyongzhu 435e24f
Merge branch 'main' into secret_manager
xiaoyongzhu 9cb332c
get_secrets_update
aabbasi-hbo 218123f
move import statement
aabbasi-hbo 07a8cf0
update spelling
aabbasi-hbo 44a3ce0
update raise exception
aabbasi-hbo 87cd083
revert
aabbasi-hbo b9a004a
Merge pull request #1 from aabbasi-hbo/hbo_secret_manager_update
xiaoyongzhu 4f1f98d
Merge branch 'main' into secret_manager
xiaoyongzhu 9551473
Merge branch 'main' into secret_manager
xiaoyongzhu 7054d07
Merge branch 'main' into secret_manager
xiaoyongzhu 6c2a415
udpate branch
xiaoyongzhu 0617f41
Revert "udpate branch"
xiaoyongzhu b4cdfa1
Merge branch 'main' into secret_manager
xiaoyongzhu dc79000
Merge branch 'main' into secret_manager
xiaoyongzhu 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
from typing import Any, Dict, List, Optional, Tuple | ||
|
||
|
||
class FeathrSecretsManagementClient(ABC): | ||
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. the file should not be named as abc.py? |
||
"""This is the abstract class for all the secrets management service, which are used to store the credentials that Feathr might use. | ||
""" | ||
|
||
@abstractmethod | ||
def __init__(self, secret_namespace: str, secret_client) -> None: | ||
"""Initialize the FeathrSecretsManagementClient class. | ||
|
||
Args: | ||
secret_namespace (str): a namespace that Feathr needs to get secrets from. | ||
For Azure Key Vault, it is something like the key vault name. | ||
For AWS secrets manager, it is something like a secret name. | ||
|
||
secret_client: A client that will be used to retrieve Feathr secrets. | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def get_feathr_secret(self, secret_name: str) -> str: | ||
"""Get Feathr Secrets from a certain secret management service, such as Azure Key Vault or AWS Secrets Manager. | ||
|
||
Returns: | ||
str: returned secret from secret management service | ||
""" | ||
pass |
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 |
---|---|---|
@@ -1,31 +1,37 @@ | ||
from azure.keyvault.secrets import SecretClient | ||
from azure.identity import DefaultAzureCredential | ||
from loguru import logger | ||
from azure.core.exceptions import ResourceNotFoundError | ||
from feathr.secrets.abc import FeathrSecretsManagementClient | ||
|
||
class AzureKeyVaultClient: | ||
def __init__(self, akv_name: str): | ||
self.akv_name = akv_name | ||
self.secret_client = None | ||
|
||
def get_feathr_akv_secret(self, secret_name: str): | ||
class AzureKeyVaultClient(FeathrSecretsManagementClient): | ||
def __init__(self, secret_namespace: str, secret_client: SecretClient = None): | ||
"""Initializes the AzureKeyVaultClient. Note that `secret_namespace` is not used, since the namespace information will be included in secret_client. | ||
""" | ||
self.secret_client = secret_client | ||
if self.secret_client is not None and not isinstance(secret_client, SecretClient): | ||
raise RuntimeError( | ||
"You need to pass an azure.keyvault.secrets.SecretClient instance.") | ||
|
||
def get_feathr_secret(self, secret_name: str) -> str: | ||
"""Get Feathr Secrets from Azure Key Vault. Note that this function will replace '_' in `secret_name` with '-' since Azure Key Vault doesn't support it | ||
|
||
Returns: | ||
_type_: _description_ | ||
str: returned secret from secret management service | ||
""" | ||
if self.secret_client is None: | ||
self.secret_client = SecretClient( | ||
vault_url = f"https://{self.akv_name}.vault.azure.net", | ||
credential=DefaultAzureCredential() | ||
) | ||
raise RuntimeError("You need to pass an azure.keyvault.secrets.SecretClient instance when initializing FeathrClient.") | ||
|
||
try: | ||
# replace '_' with '-' since Azure Key Vault doesn't support it | ||
variable_replaced = secret_name.replace('_','-') #.upper() | ||
logger.info('Fetching the secret {} from Key Vault {}.', variable_replaced, self.akv_name) | ||
variable_replaced = secret_name.replace('_', '-') # .upper() | ||
logger.info('Fetching the secret {} from Key Vault {}.', | ||
variable_replaced, self.secret_client.vault_url) | ||
secret = self.secret_client.get_secret(variable_replaced) | ||
logger.info('Secret {} fetched from Key Vault {}.', variable_replaced, self.akv_name) | ||
logger.info('Secret {} fetched from Key Vault {}.', | ||
variable_replaced, self.secret_client.vault_url) | ||
return secret.value | ||
except ResourceNotFoundError as e: | ||
logger.error(f"Secret {secret_name} cannot be found in Key Vault {self.akv_name}.") | ||
raise | ||
except ResourceNotFoundError: | ||
logger.error( | ||
f"Secret {secret_name} cannot be found in Key Vault {self.secret_client.vault_url}.") | ||
raise |
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,33 @@ | ||
from loguru import logger | ||
import json | ||
from feathr.secrets.abc import FeathrSecretsManagementClient | ||
from aws_secretsmanager_caching.secret_cache import SecretCache | ||
|
||
|
||
class AWSSecretManagerClient(FeathrSecretsManagementClient): | ||
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. the file name: aws_secret_manager |
||
def __init__(self, secret_namespace: str = None, secret_client: SecretCache = None): | ||
self.secret_id = secret_namespace | ||
self.secret_client = secret_client | ||
# make sure secret_client is a SecretCache type | ||
if secret_client is not None and not isinstance(secret_client, SecretCache): | ||
raise RuntimeError( | ||
"You need to pass a aws_secretsmanager_caching.secret_cache.SecretCache instance. Please refer to https://docs.aws.amazon.com/secretsmanager/latest/userguide/retrieving-secrets_cache-python.html for more details.") | ||
|
||
def get_feathr_secret(self, secret_name: str): | ||
"""Get Feathr Secrets from AWS Secrets manager. It's also recommended that the client passes a cache objects to reduce cost. | ||
See more details here: https://docs.aws.amazon.com/secretsmanager/latest/userguide/retrieving-secrets_cache-python.html | ||
""" | ||
if self.secret_client is None: | ||
raise RuntimeError( | ||
"You need to pass a aws_secretsmanager_caching.secret_cache.SecretCache instance when initializing FeathrClient.") | ||
|
||
try: | ||
get_secret_value_response = self.secret_client.get_secret_string( | ||
self.secret_id) | ||
# result is in str format, so we need to load it as a dict | ||
secret = json.loads(get_secret_value_response) | ||
return secret[secret_name] | ||
except KeyError as e: | ||
logger.error( | ||
f"Secret {secret_name} cannot be found in secretsmanager {self.secret_id}.") | ||
raise e |
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
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.
As an alternative, we can include in the documentation, that IRSA based authentication is possible as well if the users are running feathr in k8s. More documentation here: https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html
I don't believe any code changes are required for this auth to be supported.