Skip to content

Commit 29840ed

Browse files
committed
feat: Adding support for redacted environment variable values through openjd_redacted_env
Signed-off-by: Brian Axelson <[email protected]>
1 parent 77a443e commit 29840ed

File tree

6 files changed

+119
-3
lines changed

6 files changed

+119
-3
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ classifiers = [
2929
"Intended Audience :: End Users/Desktop"
3030
]
3131
dependencies = [
32-
"openjd-sessions >= 0.10.1,< 0.11",
33-
"openjd-model >= 0.7,< 0.9"
32+
"openjd-sessions >= 0.10.3,< 0.11",
33+
"openjd-model >= 0.8,< 0.9"
3434
]
3535

3636
[project.urls]

src/openjd/cli/_common/_extensions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Optional
55

66
# This is the list of Open Job Description extensions with implemented support
7-
SUPPORTED_EXTENSIONS = ["TASK_CHUNKING"]
7+
SUPPORTED_EXTENSIONS = ["TASK_CHUNKING", "REDACTED_ENV_VARS"]
88

99

1010
def add_extensions_argument(run_parser: ArgumentParser):

src/openjd/cli/_run/_local_session/_session_manager.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
IntRangeExpr,
2222
Job,
2323
JobParameterValues,
24+
RevisionExtensions,
25+
SpecificationRevision,
2426
Step,
2527
StepParameterSpaceIterator,
2628
TaskParameterSet,
@@ -83,6 +85,9 @@ def __init__(
8385
environments: Optional[list[Any]] = None,
8486
should_print_logs: bool = True,
8587
retain_working_dir: bool = False,
88+
revision_extensions: RevisionExtensions = RevisionExtensions(
89+
spec_rev=SpecificationRevision.v2023_09, supported_extensions=[]
90+
),
8691
):
8792
self.session_id = session_id
8893
self._action_ended = Event()
@@ -98,6 +103,7 @@ def __init__(
98103
path_mapping_rules=self._path_mapping_rules,
99104
callback=self._action_callback,
100105
retain_working_dir=retain_working_dir,
106+
revision_extensions=revision_extensions,
101107
)
102108

103109
self._should_print_logs = should_print_logs

src/openjd/cli/_run/_run_command.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
StepParameterSpaceIterator,
3535
ParameterValue,
3636
ParameterValueType,
37+
RevisionExtensions,
38+
SpecificationRevision,
3739
TaskParameterSet,
3840
)
3941
from openjd.sessions import PathMappingRule, LOG
@@ -328,6 +330,9 @@ def _run_local_session(
328330
path_mapping_rules: Optional[list[PathMappingRule]],
329331
should_print_logs: bool = True,
330332
retain_working_dir: bool = False,
333+
revision_extensions: RevisionExtensions = RevisionExtensions(
334+
spec_rev=SpecificationRevision.v2023_09, supported_extensions=[]
335+
),
331336
) -> OpenJDCliResult:
332337
"""
333338
Creates a Session object and listens for log messages to synchronously end the session.
@@ -346,6 +351,7 @@ def _run_local_session(
346351
environments=[env.environment for env in environments] if environments else [],
347352
should_print_logs=should_print_logs,
348353
retain_working_dir=retain_working_dir,
354+
revision_extensions=revision_extensions,
349355
) as session:
350356
for dep_step in step_list:
351357
step_name = dep_step.name
@@ -512,6 +518,12 @@ def do_run(args: Namespace) -> OpenJDCliResult:
512518
except RuntimeError as rte:
513519
return OpenJDCliResult(status="error", message=str(rte))
514520

521+
# Create a RevisionExtensions object with the default specification version and enabled extensions
522+
# We use the default v2023_09 since that's what we're currently supporting
523+
revision_extensions = RevisionExtensions(
524+
spec_rev=SpecificationRevision.v2023_09, supported_extensions=extensions
525+
)
526+
515527
return _run_local_session(
516528
job=the_job,
517529
job_parameter_values=job_parameter_values,
@@ -524,4 +536,5 @@ def do_run(args: Namespace) -> OpenJDCliResult:
524536
path_mapping_rules=path_mapping_rules,
525537
should_print_logs=(args.output == "human-readable"),
526538
retain_working_dir=args.preserve,
539+
revision_extensions=revision_extensions,
527540
)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
specificationVersion: "jobtemplate-2023-09"
2+
extensions:
3+
- REDACTED_ENV_VARS
4+
name: Test Redacted Env
5+
description: Test redacted environment variables
6+
7+
jobEnvironments:
8+
- name: RedactedEnv
9+
script:
10+
actions:
11+
onEnter:
12+
command: python
13+
args: ["{{Env.File.Enter}}"]
14+
onExit:
15+
command: python
16+
args: ["{{Env.File.Exit}}"]
17+
embeddedFiles:
18+
- name: Enter
19+
type: TEXT
20+
data: |
21+
print("Setting redacted vars..")
22+
print(f"openjd_redacted_env: SECRETVAR=SECRETVAL")
23+
print(f"openjd_redacted_env: KEYSPACE =SECRETVAL")
24+
print(f"openjd_redacted_env: VALSPACE= SPACEVAL")
25+
print(f'openjd_redacted_env: "MULTILINE=first_line\\nsecond_line\\nthird_line"')
26+
- name: Exit
27+
type: TEXT
28+
data: |
29+
import os
30+
print(f"SECRETVAR is {os.environ.get('SECRETVAR')}")
31+
print(f"KEYSPACE is {os.environ.get('KEYSPACE')}")
32+
print(f"VALSPACE is {os.environ.get('VALSPACE')}")
33+
print(f"MULTILINE is {os.environ.get('VALSPACE')} END")
34+
print("first_line")
35+
print("second_line")
36+
print("third_line")
37+
steps:
38+
- name: CheckVars
39+
script:
40+
actions:
41+
onRun:
42+
command: python
43+
args: ["{{Task.File.Run}}"]
44+
embeddedFiles:
45+
- name: Run
46+
type: TEXT
47+
data: |
48+
import os
49+
print(f"SECRETVAR is {os.environ.get('SECRETVAR')}")
50+
print(f"KEYSPACE is {os.environ.get('KEYSPACE')}")
51+
print(f"VALSPACE is {os.environ.get('VALSPACE')}")
52+
print(f"MULTILINE is {os.environ.get('VALSPACE')} END")
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
from pathlib import Path
4+
import re
5+
6+
from . import run_openjd_cli_main, format_capsys_outerr
7+
8+
TEMPLATE_DIR = Path(__file__).parent / "templates"
9+
10+
11+
def test_run_job_with_redacted_env(capsys):
12+
"""Test that environment variables set with openjd_redacted_env are properly handled."""
13+
outerr = run_openjd_cli_main(
14+
capsys,
15+
args=[
16+
"run",
17+
str(TEMPLATE_DIR / "redacted_env.yaml"),
18+
],
19+
expected_exit_code=0,
20+
)
21+
22+
# Verify the environment variables were set
23+
for expected_message_regex in [
24+
"Setting redacted vars",
25+
"SECRETVAR is \\*\\*\\*\\*\\*\\*\\*\\*",
26+
"KEYSPACE is None",
27+
"VALSPACE is \\*\\*\\*\\*\\*\\*\\*\\*",
28+
"MULTILINE is \\*\\*\\*\\*\\*\\*\\*\\*",
29+
]:
30+
assert re.search(
31+
expected_message_regex, outerr.out
32+
), f"Regex r'{expected_message_regex}' not matched in:\n{format_capsys_outerr(outerr)}"
33+
34+
# Verify the openjd_redacted_env lines are not in the output
35+
for unexpected_message in [
36+
"openjd_redacted_env: SECRETVAR=SECRETVAL",
37+
"openjd_redacted_env: KEYSPACE =SECRETVAL",
38+
"openjd_redacted_env: VALSPACE= SPACEVAL",
39+
"first_line",
40+
"second_line",
41+
"third_line",
42+
]:
43+
assert (
44+
unexpected_message not in outerr.out
45+
), f"Found unexpected line in output:\n{format_capsys_outerr(outerr)}"

0 commit comments

Comments
 (0)