-
Notifications
You must be signed in to change notification settings - Fork 312
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
Introduces new "env_var" and "file" fields to Secret to allow specifying name/mountPath on injection #1726
Closed
gpgn
wants to merge
6
commits into
flyteorg:master
from
gpgn:feat-add-optional-environment-variable-name-to-secret
+79
−12
Closed
Introduces new "env_var" and "file" fields to Secret to allow specifying name/mountPath on injection #1726
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
27e4a6f
Updates Secret model to include new env_name field
gpgn b541a21
Updates Secret to use env_name field as environment variable name on …
gpgn c9afca2
First draft to add oneof
gpgn 4082866
Updates Makefile to use python3
gpgn 6a59568
Adds test security contexts and fixes test cases
gpgn 1529e9b
Adds default value for Optional[str] in key check
gpgn 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
Updates Secret to use env_name field as environment variable name on …
…injection if exists Signed-off-by: Geert Pingen <[email protected]>
commit b541a215dfa6696392d6d81ab07e1d8a39050e72
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 |
---|---|---|
|
@@ -349,32 +349,51 @@ def __getattr__(self, item: str) -> _GroupSecrets: | |
""" | ||
return self._GroupSecrets(item, self) | ||
|
||
def get(self, group: str, key: Optional[str] = None, group_version: Optional[str] = None) -> str: | ||
def get( | ||
self, | ||
group: Optional[str] = None, | ||
key: Optional[str] = None, | ||
group_version: Optional[str] = None, | ||
env_name: Optional[str] = None, | ||
) -> str: | ||
""" | ||
Retrieves a secret using the resolution order -> Env followed by file. If not found raises a ValueError | ||
""" | ||
self.check_group_key(group) | ||
env_var = self.get_secrets_env_var(group, key, group_version) | ||
fpath = self.get_secrets_file(group, key, group_version) | ||
self.check_env_name_key(env_name) | ||
env_var = self.get_secrets_env_var(group, key, group_version, env_name) | ||
if env_name is None: | ||
fpath = self.get_secrets_file(group, key, group_version) | ||
v = os.environ.get(env_var) | ||
if v is not None: | ||
return v | ||
if os.path.exists(fpath): | ||
with open(fpath, "r") as f: | ||
return f.read().strip() | ||
raise ValueError( | ||
f"Unable to find secret for key {key} in group {group} " f"in Env Var:{env_var} and FilePath: {fpath}" | ||
f"Unable to find secret for key {key} in group {group} or name {env_name}" | ||
f"in Env Var:{env_var} and FilePath: {fpath}" | ||
) | ||
|
||
def get_secrets_env_var(self, group: str, key: Optional[str] = None, group_version: Optional[str] = None) -> str: | ||
def get_secrets_env_var( | ||
self, | ||
group: Optional[str] = None, | ||
key: Optional[str] = None, | ||
group_version: Optional[str] = None, | ||
env_name: Optional[str] = None, | ||
) -> str: | ||
""" | ||
Returns a string that matches the ENV Variable to look for the secrets | ||
""" | ||
self.check_env_name_key(env_name) | ||
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. is this redundant? |
||
if env_name is not None: | ||
return env_name | ||
self.check_group_key(group) | ||
l = [k.upper() for k in filter(None, (group, group_version, key))] | ||
return f"{self._env_prefix}{'_'.join(l)}" | ||
|
||
def get_secrets_file(self, group: str, key: Optional[str] = None, group_version: Optional[str] = None) -> str: | ||
def get_secrets_file( | ||
self, group: Optional[str] = None, key: Optional[str] = None, group_version: Optional[str] = None | ||
) -> str: | ||
""" | ||
Returns a path that matches the file to look for the secrets | ||
""" | ||
|
@@ -384,10 +403,15 @@ def get_secrets_file(self, group: str, key: Optional[str] = None, group_version: | |
return os.path.join(self._base_dir, *l) | ||
|
||
@staticmethod | ||
def check_group_key(group: str): | ||
def check_group_key(group: Optional[str]): | ||
gpgn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if group is None or group == "": | ||
raise ValueError("secrets group is a mandatory field.") | ||
|
||
@staticmethod | ||
def check_env_name_key(env_name: Optional[str]): | ||
gpgn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if env_name is not None and len(env_name) <= 0: | ||
raise ValueError(f"Invalid env_name {env_name}") | ||
|
||
|
||
@dataclass(frozen=True) | ||
class CompilationState(object): | ||
|
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
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.