-
Notifications
You must be signed in to change notification settings - Fork 903
Make gcp-linux example template use a non-root user #2480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
4531a39
3f841e4
4f9a28e
0db9746
7002bc9
e731e00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -70,21 +70,20 @@ resource "google_compute_instance" "dev" { | |||||
email = data.google_compute_default_service_account.default.email | ||||||
scopes = ["cloud-platform"] | ||||||
} | ||||||
# The startup script runs as root with no $HOME environment set up, which can break workspace applications, so | ||||||
# instead of directly running the agent init script, setup the home directory, write the init script, and then execute | ||||||
# it. | ||||||
# The startup script runs as root with no $HOME environment set up, so instead of directly | ||||||
# running the agent init script, create a user (with a homedir, default shell and sudo | ||||||
# permissions) and execute the init script as that user. | ||||||
metadata_startup_script = <<EOMETA | ||||||
#!/usr/bin/env sh | ||||||
set -eux pipefail | ||||||
|
||||||
mkdir /root || true | ||||||
cat <<'EOCODER' > /root/coder_agent.sh | ||||||
${coder_agent.dev.init_script} | ||||||
EOCODER | ||||||
chmod +x /root/coder_agent.sh | ||||||
|
||||||
export HOME=/root | ||||||
/root/coder_agent.sh | ||||||
|
||||||
useradd -m -s /bin/bash "${local.linux_user}" | ||||||
echo "${local.linux_user} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/coder-user | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
exec sudo -u "${local.linux_user}" sh -c '${coder_agent.dev.init_script}' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it's a persistent root disk, wouldn't this fail on subsequent boots since it doesn't check if the user already exists first? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point actually Dean -- I didn't know this before, but apparently on EC2 the user metadata script only runs on first boot by default, whereas on GCP it runs on every boot. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, good catch. I originally wanted to just take the same approach that we use for AWS and create the user with cloud-init, which I now realize would have handled this. But unfortunately, cloud-init is excluded from the GCE variants of the Debian cloud images: https://salsa.debian.org/cloud-team/debian-cloud-images/-/issues/41 |
||||||
EOMETA | ||||||
} | ||||||
|
||||||
locals { | ||||||
# Ensure Coder username is a valid Linux username | ||||||
linux_user = lower(substr(data.coder_workspace.me.owner, 0, 32)) | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.