Skip to content

fix: Preserve individual environment variables with sudo - #5319

Merged
HomayoonAlimohammadi merged 1 commit into
masterfrom
KU-4636/fix-sudo-env
Dec 8, 2025
Merged

fix: Preserve individual environment variables with sudo#5319
HomayoonAlimohammadi merged 1 commit into
masterfrom
KU-4636/fix-sudo-env

Conversation

@HomayoonAlimohammadi

Copy link
Copy Markdown
Contributor

Overview

sudo-rs ignores -E and requires individual environment variables to be preserved.

Fixes: #5266
Fixes: #5280
Fixes: #5302
Fixes: #5282
Fixes: #5283

@HomayoonAlimohammadi

HomayoonAlimohammadi commented Nov 27, 2025

Copy link
Copy Markdown
Contributor Author

The failing lint is being addressed in fe213a0 of this PR: #5318

@HomayoonAlimohammadi
HomayoonAlimohammadi marked this pull request as draft November 27, 2025 14:57
@HomayoonAlimohammadi
HomayoonAlimohammadi force-pushed the KU-4636/fix-sudo-env branch 3 times, most recently from 38f6ad4 to 591658b Compare November 28, 2025 11:33
@HomayoonAlimohammadi
HomayoonAlimohammadi marked this pull request as ready for review November 28, 2025 12:52
Signed-off-by: Homayoon (Hue) Alimohammadi <homayoon.alimohammadi@canonical.com>

@berkayoz berkayoz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, we should be replicating the previous behavior now. We can revisit to limit the environment variables passed through later on.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This pull request addresses compatibility issues with sudo-rs, an alternative sudo implementation, by modifying how environment variables are preserved when executing commands with elevated privileges. The changes replace the use of sudo -E (which sudo-rs ignores) with sudo --preserve-env that explicitly lists individual environment variables.

Key changes:

  • Introduced list_env_vars() function to generate a comma-separated list of all current environment variables
  • Modified run_with_sudo() to use --preserve-env with explicit variable list instead of -E flag
  • Updated scripts/inspect.sh to use run_with_sudo wrapper instead of direct sudo -E calls

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
microk8s-resources/actions/common/utils.sh Added list_env_vars() helper function and updated run_with_sudo() to use --preserve-env with explicit environment variable list for sudo-rs compatibility
scripts/inspect.sh Replaced direct sudo -E calls with run_with_sudo wrapper function for consistent environment variable handling
Comments suppressed due to low confidence (2)

microk8s-resources/actions/common/utils.sh:98

  • [nitpick] The preserve_env parameter is accepted and consumed (via shift) but doesn't affect the function's behavior. The function always preserves environment variables when not in strict mode, regardless of whether this parameter is passed. This parameter appears to be vestigial or intended for documentation purposes only, which can be confusing to maintainers. Consider either implementing different behavior based on this parameter, or removing it and updating all call sites if it's not needed.
  if [ "$1" == "preserve_env" ]; then
    shift
  fi

microk8s-resources/actions/common/utils.sh:93

  • The list_env_vars() function preserves ALL environment variables indiscriminately, which poses a security risk. Sensitive environment variables (such as tokens, credentials, or API keys) should not be passed through sudo. Additionally, environment variable names containing special characters (e.g., BASH_FUNC_*) or newlines could break the comma-separated list format or cause issues with sudo's --preserve-env option. Consider creating a whitelist of specific environment variables that need to be preserved instead.
list_env_vars() {
  env | awk -F= '{print $1}' | paste -sd,
}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread microk8s-resources/actions/common/utils.sh
Comment thread microk8s-resources/actions/common/utils.sh
Comment thread microk8s-resources/actions/common/utils.sh

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@HomayoonAlimohammadi
HomayoonAlimohammadi merged commit eda5ba8 into master Dec 8, 2025
29 checks passed
@HomayoonAlimohammadi
HomayoonAlimohammadi deleted the KU-4636/fix-sudo-env branch December 8, 2025 07:45
HomayoonAlimohammadi added a commit that referenced this pull request Dec 18, 2025
Signed-off-by: Homayoon (Hue) Alimohammadi <homayoon.alimohammadi@canonical.com>
louiseschmidtgen pushed a commit that referenced this pull request May 19, 2026
Signed-off-by: Homayoon (Hue) Alimohammadi <homayoon.alimohammadi@canonical.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants