Skip to content

exec: Fix incorrect HOME and USER env variables for tasks that have user set#25859

Merged
pkazmierczak merged 8 commits intomainfrom
b-fix-user-home-env-exec
May 16, 2025
Merged

exec: Fix incorrect HOME and USER env variables for tasks that have user set#25859
pkazmierczak merged 8 commits intomainfrom
b-fix-user-home-env-exec

Conversation

@pkazmierczak
Copy link
Copy Markdown
Contributor

@pkazmierczak pkazmierczak commented May 15, 2025

Fixes #25854

tgross
tgross previously approved these changes May 15, 2025
Copy link
Copy Markdown
Member

@tgross tgross left a comment

Choose a reason for hiding this comment

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

LGTM. It looks like we're only setting these environment variables on Linux... should we be setting them on other Unix platforms as well?

@pkazmierczak
Copy link
Copy Markdown
Contributor Author

should we be setting them on other Unix platforms as well?

yeah I got a little confused in the executor code. setCmd implementation that we have in the linux file should actually be called on any Unix, because it calls stdlib methods underneath that should play nicely with all unixes.

tgross
tgross previously approved these changes May 15, 2025
Copy link
Copy Markdown
Member

@tgross tgross left a comment

Choose a reason for hiding this comment

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

LGTM

Comment thread drivers/shared/executor/executor_unix.go Outdated
execCmd, allocDir := testExecCmd.command, testExecCmd.allocDir
execCmd.Cmd = "/bin/bash"
execCmd.Args = []string{"-c", "echo $USER"}
execCmd.User = "runner"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This test is always going to fail anywhere except GitHub runners, right? Is there a testutil flag we can set to skip it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good shout. Look at 92908a9.

Co-authored-by: Tim Gross <tgross@hashicorp.com>
Copy link
Copy Markdown
Member

@tgross tgross left a comment

Choose a reason for hiding this comment

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

LGTM

@pkazmierczak pkazmierczak merged commit 0fa0624 into main May 16, 2025
36 of 37 checks passed
@pkazmierczak pkazmierczak deleted the b-fix-user-home-env-exec branch May 16, 2025 13:02
@schmichael
Copy link
Copy Markdown
Member

Mind updating our docs to mention this behavior?

Unrelated https://developer.hashicorp.com/nomad/docs/job-specification/task#user incldues the weird phrase This can only be set on Linux platforms before describing how to use it on Windows. Maybe clean that up and mention the env var implications?

I think the most important docs would be to add HOME and USER here: https://developer.hashicorp.com/nomad/docs/runtime/environment

pkazmierczak added a commit that referenced this pull request May 19, 2025
…r` setting (#25879)

In #25859 we fixed the task environment variables to account for user field
setting. This PR follows up with documentation adjustments.
pkazmierczak added a commit that referenced this pull request May 19, 2025
…r` setting (#25879)

In #25859 we fixed the task environment variables to account for user field
setting. This PR follows up with documentation adjustments.
pkazmierczak added a commit that referenced this pull request May 19, 2025
…r` setting (#25879)

In #25859 we fixed the task environment variables to account for user field
setting. This PR follows up with documentation adjustments.
schmichael pushed a commit that referenced this pull request May 19, 2025
… custom `user` setting into release/1.9.x (#25887)

* no-op commit due to failed cherry-picking

* docs: emphasize HOME and USER env vars for tasks that use custom `user` setting (#25879)

In #25859 we fixed the task environment variables to account for user field
setting. This PR follows up with documentation adjustments.

---------

Co-authored-by: temp <temp@hashicorp.com>
Co-authored-by: Piotr Kazmierczak <470696+pkazmierczak@users.noreply.github.com>
schmichael pushed a commit that referenced this pull request May 19, 2025
… custom `user` setting into release/1.8.x (#25888)

* no-op commit due to failed cherry-picking

* docs: emphasize HOME and USER env vars for tasks that use custom `user` setting (#25879)

In #25859 we fixed the task environment variables to account for user field
setting. This PR follows up with documentation adjustments.

---------

Co-authored-by: temp <temp@hashicorp.com>
Co-authored-by: Piotr Kazmierczak <470696+pkazmierczak@users.noreply.github.com>
Comment on lines +104 to +106
// Override HOME and USER environment variables
cmd.Env = append(cmd.Env, fmt.Sprintf("USER=%s", u.Username))
cmd.Env = append(cmd.Env, fmt.Sprintf("HOME=%s", u.HomeDir))
Copy link
Copy Markdown

@EugenKon EugenKon May 20, 2025

Choose a reason for hiding this comment

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

There are could be a lot of variables missing. Also this could depend on the system where this is run.

@jbardin , @pkazmierczak
I expect that something like su - <username> -c "<command>" will be run and let the system to decide which variables should be adjusted.

Copy link
Copy Markdown

@EugenKon EugenKon May 20, 2025

Choose a reason for hiding this comment

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

I suspect, if HomeDir will have spaces the code above will not work. Probably it should be "HOME=\"%s\"".

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I expect that something like su - -c "" will be run and let the system to decide which variables should be adjusted.

The exec family of task drivers does not invoke a subshell to execute the task command. Docker, podman, systemd units, etc operate the same way to avoid security issues and crossplatform issues. Tasks which expect shell semantics should invoke the shell themselves. An su/subshell task driver would be an interesting experiment, but would be backward incompatible with the exec family.

I suspect, if HomeDir will have spaces the code above will not work. Probably it should be "HOME="%s"".

Quoting is a shell feature, not inherent to how UNIX encodes environment variables. Quotes should not be necessary even if the values contain spaces or additional equal signs. See: https://go.dev/play/p/FiV8FXiaNPr

Also please don't ping engineers in comments. jbardin hasn't worked on Nomad in years and is quite busy with other things. Commenting on PRs is welcome, but the author should be expected to receive a notification with being explicitly pinged.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jan 4, 2026

I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jan 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

backport/ent/1.8.x+ent Changes are backported to 1.8.x+ent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

exec: task environment does not respect user setting

5 participants