Skip to content

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

Merged
merged 6 commits into from
Aug 3, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions examples/templates/gcp-linux/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
useradd -m -s /bin/bash "${local.linux_user}"
grep "${local.linux_user}" /etc/passwd > /dev/null 2>&1 || useradd -m -s /bin/bash "${local.linux_user}"

echo "${local.linux_user} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/coder-user
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
echo "${local.linux_user} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/coder-user
[ ! -e /etc/sudoers.d/coder-user ] && echo "${local.linux_user} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/coder-user

exec sudo -u "${local.linux_user}" sh -c '${coder_agent.dev.init_script}'
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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))
}