Skip to content

Commit d5c81a9

Browse files
Anmol MishraCopilot
andcommitted
fix: EksPodOperator 401 with cross-account AssumeRole via aws_conn_id
Fixes apache#64657 - Redirect stderr to /dev/null instead of merging with stdout (2>&1) to prevent Python warnings/log output from contaminating stdout during bash token parsing. The token output must be the only thing on stdout. - Add token validation: exit with error if token extraction fails, rather than sending a malformed ExecCredential with empty token to the API server (which caused 401 with empty user identity in audit logs). - Remove EKS bearer token from error output printed to stderr when token validation fails (prevents token leakage into task logs). - Keep $output in non-zero exit diagnostics for troubleshooting. Co-Authored-By: Copilot <[email protected]>
1 parent b6472b2 commit d5c81a9

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

  • providers/amazon

‎providers/amazon/src/airflow/providers/amazon/aws/hooks/eks.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,25 @@ class NodegroupStates(Enum):
8282
# Load credentials from secure file using (POSIX-compliant dot operator)
8383
. {credentials_file}
8484
85+
# Redirect stderr to /dev/null to prevent Python warnings, deprecation
86+
# notices, or other log output from contaminating stdout. The token
87+
# output must be the ONLY thing on stdout for bash token parsing to work.
8588
output=$({python_executable} -m airflow.providers.amazon.aws.utils.eks_get_token \
86-
--cluster-name {eks_cluster_name} --sts-url '{sts_url}' {args} 2>&1)
89+
--cluster-name {eks_cluster_name} --sts-url '{sts_url}' {args} 2>/dev/null)
8790
8891
status=$?
8992
9093
# Clear environment variables after use (defense in depth)
9194
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
9295
9396
if [ "$status" -ne 0 ]; then
94-
printf '%s' "$output" >&2
97+
printf 'eks_get_token failed with exit code %s. Output was: %s' \
98+
"$status" "$output" >&2
9599
exit "$status"
96100
fi
97101
98102
# Use pure bash below to parse so that it's posix compliant
103+
# Only the token line should be on stdout (stderr redirected above)
99104
100105
last_line=${{output##*$'\\n'}} # strip everything up to the last newline
101106
@@ -104,6 +109,15 @@ class NodegroupStates(Enum):
104109
105110
token=${{last_line##*, token: }} # text after ", token: "
106111
112+
# Validate that token was extracted — empty token means parsing failed
113+
# or eks_get_token produced unexpected output. Without this check, a
114+
# malformed ExecCredential is sent to the API server, resulting in a
115+
# 401 with an empty user identity in the audit logs.
116+
if [ -z "$token" ]; then
117+
printf 'Failed to extract token from eks_get_token output.' >&2
118+
exit 1
119+
fi
120+
107121
json_string=$(printf '{{"kind": "ExecCredential","apiVersion": \
108122
"{authentication_api_version}","spec": {{}},"status": \
109123
{{"expirationTimestamp": "%s","token": "%s"}}}}' "$timestamp" "$token")

‎providers/amazon/tests/unit/amazon/aws/hooks/test_eks.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,40 @@ def test_generate_config_file(self, mock_conn, aws_conn_id, region_name, expecte
12731273
if expected_region_args:
12741274
assert expected_region_args in command_arg
12751275

1276+
def test_command_template_redirects_stderr(self):
1277+
"""Verify COMMAND template redirects stderr to /dev/null to prevent
1278+
Python warnings/log output from contaminating stdout and breaking
1279+
bash token parsing. This is critical for cross-account AssumeRole
1280+
scenarios where the kubeconfig exec plugin must produce a clean token.
1281+
"""
1282+
from airflow.providers.amazon.aws.hooks.eks import COMMAND
1283+
1284+
# Verify stderr is not merged with stdout (the core correctness requirement)
1285+
assert "2>&1" not in COMMAND, (
1286+
"COMMAND must not use 2>&1 — merging stderr with stdout breaks bash token parsing"
1287+
)
1288+
# Verify stderr is redirected to /dev/null
1289+
assert "2>/dev/null" in COMMAND, (
1290+
"COMMAND must redirect stderr to /dev/null to prevent output contamination"
1291+
)
1292+
1293+
def test_command_template_validates_token(self):
1294+
"""Verify COMMAND template validates that the token was successfully
1295+
extracted before producing the ExecCredential JSON. Without this check,
1296+
a malformed ExecCredential with an empty token is sent to the API server,
1297+
resulting in 401 Unauthorized with an empty user identity in audit logs.
1298+
"""
1299+
from airflow.providers.amazon.aws.hooks.eks import COMMAND
1300+
1301+
# Verify token validation check exists
1302+
assert 'if [ -z "$token" ]' in COMMAND, (
1303+
"COMMAND must validate that token is non-empty before producing ExecCredential JSON"
1304+
)
1305+
# Verify the empty-token validation block exits with an error
1306+
assert "exit 1" in COMMAND, (
1307+
"COMMAND must exit with error when token extraction fails"
1308+
)
1309+
12761310

12771311
# Helper methods for repeated assert combinations.
12781312
def assert_all_arn_values_are_valid(expected_arn_values, pattern, arn_under_test) -> None:

0 commit comments

Comments
 (0)