-
Notifications
You must be signed in to change notification settings - Fork 20
Redact secrets during the workflow file upload #702
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 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2fbdad6
Redact secrets during the workflow file upload
cypres 5221974
Redact pod specs as well.
cypres de877b5
Lint
cypres 6681c9e
Improve masking logic
cypres 7f5eb6b
Allow alternate casing of True FALSE
cypres f66ff46
Improve tests
cypres 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -182,3 +182,8 @@ osmo_py_library( | |
| ":osmo_errors", | ||
| ], | ||
| ) | ||
|
|
||
| osmo_py_library( | ||
| name = "redact", | ||
| srcs = ["redact.py"], | ||
| ) | ||
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,110 @@ | ||
| """ | ||
| SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| SPDX-License-Identifier: Apache-2.0 | ||
| """ | ||
| import base64 | ||
| import collections | ||
| import copy | ||
| import math | ||
| import re | ||
| from typing import Dict, Generator, Iterable | ||
|
|
||
|
|
||
| # Regex to match secrets in the spec. While this is not a perfect solution, it solves the majority | ||
| # of cases. Regex from: https://lookingatcomputer.substack.com/p/regex-is-almost-all-you-need | ||
| # Proper secret management: | ||
| # https://nvidia.github.io/OSMO/main/user_guide/getting_started/credentials.html | ||
| SECRET_REDACTION_RE = re.compile( | ||
| r'''(?i)[\w.-]{0,50}?(?:access|auth|(?-i:[Aa]pi|API)|credential|creds|key|passw(?:or)?d|secret|token)(?:[ \t\w.-]{0,20})[\s'"]{0,3}(?:=|>|:{1,3}=|\|\||:|=>|\?=|,)[\x60'"\s=]{0,5}([\w.=-]{10,150}|[a-z0-9][a-z0-9+/]{11,}={0,3})(?:[\x60'"\s;]|\\[nr]|$)''' # pylint: disable=line-too-long | ||
| ) | ||
|
|
||
| # Matches base64-encoded fragments: at least 16 chars of base64 alphabet with optional padding, | ||
| # not adjacent to other base64 characters (to capture complete tokens). | ||
| _BASE64_FRAGMENT_RE = re.compile( | ||
| r'(?<![A-Za-z0-9+/])[A-Za-z0-9+/]{16,}={0,2}(?![A-Za-z0-9+/=])' | ||
| ) | ||
|
|
||
|
|
||
| _ENTROPY_THRESHOLD = 3.0 | ||
|
|
||
|
|
||
| def _shannon_entropy(data: str) -> float: | ||
| """ | ||
| Calculate the Shannon entropy of a string (bits per character). | ||
| https://en.wiktionary.org/wiki/Shannon_entropy | ||
| """ | ||
| if not data: | ||
| return 0.0 | ||
| inv_length = 1.0 / len(data) | ||
| entropy = 0.0 | ||
| for count in collections.Counter(data).values(): | ||
| freq = count * inv_length | ||
| entropy -= freq * math.log2(freq) | ||
| return entropy | ||
|
|
||
|
|
||
| def redact_pod_spec_env(pod_spec: Dict) -> Dict: | ||
| """ | ||
| Return a deep copy of pod_spec with high-entropy env var values replaced by [MASKED]. | ||
|
|
||
| Only values whose Shannon entropy exceeds _ENTROPY_THRESHOLD are masked, leaving | ||
| low-entropy values like 'true', 'false', or plain URLs untouched. Covers both | ||
| 'containers' and 'initContainers'. Entries that use 'valueFrom' (i.e. have no | ||
| 'value' key) are left untouched. | ||
| """ | ||
| pod_spec = copy.deepcopy(pod_spec) | ||
| for container_list_key in ('containers', 'initContainers'): | ||
| for container in pod_spec.get('spec', pod_spec).get(container_list_key, []): | ||
| for env_entry in container.get('env', []): | ||
| if 'value' in env_entry and \ | ||
| _shannon_entropy(env_entry['value']) > _ENTROPY_THRESHOLD: | ||
| env_entry['value'] = '[MASKED]' | ||
| return pod_spec | ||
|
|
||
|
|
||
| def redact_secrets(lines: Iterable[str]) -> Generator[str, None, None]: | ||
| """ | ||
| Yield lines with secrets redacted. | ||
|
|
||
| Scans each line for key=value patterns that look like secrets and replaces | ||
| the value with [MASKED]. Also detects base64-encoded fragments, decodes them, | ||
| and replaces the whole fragment with [MASKED] if secrets are found inside. | ||
| """ | ||
| def redact_base64_fragments(line: str) -> str: | ||
| """ | ||
| Find base64-encoded fragments in a line, decode them, redact any secrets found inside, | ||
| and replace the whole fragment with [MASKED]. | ||
| """ | ||
| def replace_if_secrets(m: re.Match) -> str: | ||
| fragment = m.group(0) | ||
| try: | ||
| padded = fragment + '=' * (-len(fragment) % 4) | ||
| decoded = base64.b64decode(padded, validate=True).decode('utf-8') | ||
| except (ValueError, UnicodeDecodeError): | ||
| return fragment | ||
cypres marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| redacted = SECRET_REDACTION_RE.sub( | ||
| lambda sm: sm.group(0).replace(sm.group(1), '[MASKED]'), | ||
| decoded, | ||
| ) | ||
| if redacted == decoded: | ||
| return fragment | ||
| return '[MASKED]' | ||
| return _BASE64_FRAGMENT_RE.sub(replace_if_secrets, line) | ||
|
|
||
| for line in lines: | ||
| line = redact_base64_fragments(line) | ||
| yield SECRET_REDACTION_RE.sub( | ||
| lambda m: m.group(0).replace(m.group(1), '[MASKED]'), line) | ||
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
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.