-
Notifications
You must be signed in to change notification settings - Fork 1
[feature] Add allowlisted secret export for notebook workflow runs #166
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2bebdb3
Add selected secret environment exporter
mbruns91 f7d1954
Add selected secret export action
mbruns91 29d6670
Wire notebook secret allowlist export
mbruns91 517ed75
Document selected secret export action
mbruns91 d4394a0
update .gitignore
mbruns91 94531f5
Align selected secret export shell
mbruns91 8a1e3ad
Avoid GitHub env delimiter collisions
mbruns91 db59260
Document secret export action usage
mbruns91 fd51678
Fix usage example in README
mbruns91 c372fe1
black (.support/export_secret_env.py)
mbruns91 4a98f09
Return parsed secret env as mapping
mbruns91 e20033c
black: .support/export_secret_env_.py
mbruns91 543fac6
run .support/update_actions_tag.sh
mbruns91 f702b90
run .support/update_actions_tag.sh
mbruns91 cab82cd
run .support/update_actions_tag.sh: re-taget main
mbruns91 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,3 +1,6 @@ | ||
| *.pyc | ||
| .DS_Store | ||
| .idea/ | ||
| .dir-locals.el | ||
| .codex | ||
| .codex/ |
This file contains hidden or 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,120 @@ | ||
| """Export selected GitHub Actions secrets into later-step environment variables.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import os | ||
| import re | ||
| import sys | ||
| import uuid | ||
|
|
||
| NAME_PATTERN = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$") | ||
|
|
||
|
|
||
| def workflow_escape(value: str) -> str: | ||
| """Escape text embedded in a GitHub workflow command.""" | ||
| return value.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A") | ||
|
|
||
|
|
||
| def fail(message: str) -> None: | ||
| print(f"::error::{workflow_escape(message)}", file=sys.stderr) | ||
| raise SystemExit(1) | ||
|
|
||
|
|
||
| def parse_secret_env_map(raw_map: str) -> dict[str, str]: | ||
| """Parse mapping lines into environment-name to secret-name pairs.""" | ||
| env_to_secret_name: dict[str, str] = {} | ||
|
|
||
| for line_number, raw_line in enumerate(raw_map.splitlines(), start=1): | ||
| line = raw_line.strip() | ||
| if not line or line.startswith("#"): | ||
| continue | ||
|
|
||
| if "=" in line: | ||
| env_name, secret_name = (part.strip() for part in line.split("=", 1)) | ||
| else: | ||
| env_name = secret_name = line | ||
|
|
||
| if not env_name or not secret_name: | ||
| fail(f"Invalid secret env mapping on line {line_number}.") | ||
| if not NAME_PATTERN.fullmatch(env_name): | ||
| fail( | ||
| f"Invalid environment variable name {env_name!r} on line {line_number}." | ||
| ) | ||
| if not NAME_PATTERN.fullmatch(secret_name): | ||
| fail(f"Invalid secret name {secret_name!r} on line {line_number}.") | ||
| if env_name in env_to_secret_name: | ||
| fail(f"Duplicate environment variable mapping for {env_name!r}.") | ||
|
|
||
| env_to_secret_name[env_name] = secret_name | ||
|
|
||
| return env_to_secret_name | ||
|
|
||
|
|
||
| def load_secrets() -> dict[str, str]: | ||
| """Load the full secret object supplied only to the trusted export step.""" | ||
| raw_secrets = os.environ.get("PYIRON_ALL_SECRETS_JSON") | ||
| if not raw_secrets: | ||
| fail("PYIRON_ALL_SECRETS_JSON is required when exporting selected secrets.") | ||
|
|
||
| try: | ||
| secrets = json.loads(raw_secrets) | ||
| except json.JSONDecodeError as exc: | ||
| fail(f"Failed to parse PYIRON_ALL_SECRETS_JSON: {exc}") | ||
|
|
||
| if not isinstance(secrets, dict): | ||
| fail("PYIRON_ALL_SECRETS_JSON must decode to a JSON object.") | ||
|
|
||
| return {str(name): str(value) for name, value in secrets.items()} | ||
|
|
||
|
|
||
| def choose_github_env_delimiter(value: str) -> str: | ||
| """Choose a delimiter that is not present as a complete value line.""" | ||
| value_lines = set(value.splitlines()) | ||
| while True: | ||
| delimiter = f"PYIRON_SECRET_{uuid.uuid4().hex}" | ||
| if delimiter not in value_lines: | ||
| return delimiter | ||
|
|
||
|
|
||
| def append_github_env(env_name: str, value: str) -> None: | ||
| """Append one environment variable using GitHub's multiline-safe format.""" | ||
| github_env = os.environ.get("GITHUB_ENV") | ||
| if not github_env: | ||
| fail("GITHUB_ENV is not set.") | ||
|
|
||
| delimiter = choose_github_env_delimiter(value) | ||
| with open(github_env, "a", encoding="utf-8") as env_file: | ||
| env_file.write(f"{env_name}<<{delimiter}\n{value}\n{delimiter}\n") | ||
|
|
||
|
|
||
| def main() -> int: | ||
| env_to_secret_name = parse_secret_env_map( | ||
| os.environ.get("PYIRON_SECRET_ENV_MAP", "") | ||
| ) | ||
| if not env_to_secret_name: | ||
| return 0 | ||
|
|
||
| secrets = load_secrets() | ||
| missing = [ | ||
| secret_name | ||
| for secret_name in env_to_secret_name.values() | ||
| if secret_name not in secrets | ||
| ] | ||
| if missing: | ||
| fail("Requested secret(s) are not available: " + ", ".join(sorted(missing))) | ||
|
|
||
| for env_name, secret_name in env_to_secret_name.items(): | ||
| value = secrets[secret_name] | ||
| if value: | ||
| print(f"::add-mask::{workflow_escape(value)}") | ||
| append_github_env(env_name, value) | ||
|
|
||
| print( | ||
| f"Exported {len(env_to_secret_name)} selected secret environment variable(s)." | ||
| ) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.