Skip to content

Conversation

@aemada-aws
Copy link
Contributor

@aemada-aws aemada-aws commented Aug 4, 2025

Improve subprocess handling in EKS Helm custom resource handler.

Reason for this change

The EKS Helm custom resource handler used shell=True with subprocess calls, which is not aligned with security best practices. Following Python's recommended approach for subprocess execution improves code robustness and follows secure coding guidelines.

Description of changes

Refactor subprocess execution to follow Python best practices
https://docs.python.org/3/library/subprocess.html#replacing-shell-pipeline

  • Replaced shell command strings with array-based commands: Refactored get_oci_cmd() to return structured command objects instead of shell strings
  • Implemented proper subprocess pipelines: Used Popen with PIPE to chain aws ecr get-login-password and helm registry login commands following Python documentation recommendations
  • Removed shell=True: Adopted array-based command execution as recommended by Python subprocess documentation
  • Maintained functionality: Preserved all existing behavior for private ECR, public ECR, and fallback scenarios

Files modified:

  • packages/@aws-cdk/custom-resource-handlers/lib/aws-eks/kubectl-handler/helm/__init__.py
  • packages/@aws-cdk/aws-eks-v2-alpha/lib/kubectl-handler/helm/__init__.py
  • packages/@aws-cdk/aws-eks-v2-alpha/test/integ.alb-controller.js.snapshot/asset.*/helm/__init__.py

Technical approach:

  • Commands are now built as arrays: ['helm', 'pull', repository, '--version', version, '--untar']
  • Pipeline implementation follows Python subprocess best practices using Popen with proper PIPE connections
  • User inputs are passed as separate array elements, ensuring proper argument handling

Describe any new or updated permissions being added

No new IAM permissions required. The change maintains the same AWS API calls and functionality.

Description of how you validated changes

  • Functionality testing: Confirmed that ECR authentication and Helm chart pulling continues to work correctly for all scenarios
  • Code review: Verified implementation follows Python subprocess best practices as documented in the Python documentation
  • Compatibility testing: Ensured backward compatibility with existing CDK Helm chart deployments

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions bot added the p2 label Aug 4, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team August 4, 2025 14:25
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Aug 4, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

@Abogical Abogical self-assigned this Aug 4, 2025
@aemada-aws aemada-aws added the @aws-cdk/aws-eks-v2-alpha Related to the @aws-cdk/aws-eks-v2-alpha package label Aug 4, 2025
@vishaalmehrishi
Copy link
Contributor

I think it would help reviewers if you can share the relevant Python documentation which is referenced in the description.

logger.info("Found AWS private repository")
cmnd = [
f"aws ecr get-login-password --region {private_registry['region']} | " \
f"helm registry login --username AWS --password-stdin {private_registry['registry']}; helm pull {repository} --version {version} --untar"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking - is the removal of helm pull... intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is there, not removed. It is built at the top

  # Build helm pull command as array
    helm_cmd = ['helm', 'pull', repository]
    ```

@aemada-aws aemada-aws force-pushed the remove-shell branch 2 times, most recently from 7ed7bf9 to 5afe70b Compare August 4, 2025 16:00
@aemada-aws
Copy link
Contributor Author

I think it would help reviewers if you can share the relevant Python documentation which is referenced in the description.

Added: https://docs.python.org/3/library/subprocess.html#replacing-shell-pipeline

@aemada-aws aemada-aws force-pushed the remove-shell branch 2 times, most recently from 5300e1e to 891b510 Compare August 4, 2025 16:10
Copy link
Member

@Abogical Abogical left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@aemada-aws aemada-aws marked this pull request as ready for review August 4, 2025 16:15
@aemada-aws aemada-aws changed the title fix(eks-v2-alpha): remove usage of shell=True in helm commands fix(eks-v2): remove usage of shell=True in helm commands Aug 5, 2025
@aemada-aws aemada-aws added the pr-linter/exempt-integ-test The PR linter will not require integ test changes label Aug 5, 2025
@aws-cdk-automation aws-cdk-automation dismissed their stale review August 5, 2025 06:36

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@mergify
Copy link
Contributor

mergify bot commented Aug 5, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 74a0380
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit cd9d69c into aws:main Aug 5, 2025
25 of 26 checks passed
@github-actions
Copy link
Contributor

github-actions bot commented Aug 5, 2025

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

@aws-cdk/aws-eks-v2-alpha Related to the @aws-cdk/aws-eks-v2-alpha package contribution/core This is a PR that came from AWS. p2 pr-linter/exempt-integ-test The PR linter will not require integ test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants