Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions phala-cloud-prelaunch-script/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This pre-launch script for Phala Cloud handles:
- Pull private images from AWS ECR
- Remove unused images and containers from local disk
- Expose App ID via `DSTACK_APP_ID` environment variable
- Install root SSH keys and the root password from encrypted environment variables

## Private registry support

Expand Down Expand Up @@ -37,3 +38,20 @@ The following three environment variables will be automatically added by the def
- `DSTACK_APP_DOMAIN`: The default domain of the Dstack App.

NOTE: `DSTACK_APP_DOMAIN` equals to `${DSTACK_APP_ID}.${DSTACK_GATEWAY_DOMAIN}`, which acccessing port 80. It equals to `${DSTACK_APP_ID}-80.${DSTACK_GATEWAY_DOMAIN}`.

## SSH access

On images that ship `sshd`, which are the dstack 0.5.x dev images, the script sets up root access from these encrypted environment variables:

- `DSTACK_AUTHORIZED_KEYS` / `DSTACK_ROOT_PUBLIC_KEY`: written to root's `authorized_keys`
- `DSTACK_ROOT_PASSWORD`: root password

The boot log prints the SHA256 fingerprint of every installed key. Compare these fingerprints with your own keys.

**Encrypted env is not authenticated.** It is sealed to the app's public encryption key, so anyone can produce a valid ciphertext, including whoever runs the host. A host that relaunches your app with its own encrypted env can therefore set these variables and log in as root. Use them only with a development image, and only on a host you trust. Production images ship no `sshd`. To detect a replaced env, use the checks in dstack's [security best practices](https://github.com/Dstack-TEE/dstack/blob/next/docs/security/security-best-practices.md#authenticated-envs-and-user_config), such as `APP_LAUNCH_TOKEN` or KMS launch tickets.

### `user_config.ssh_authorized_keys` is ignored (v0.0.22)

Up to v0.0.21 the script also appended `ssh_authorized_keys` from `/dstack/user_config` to root's `authorized_keys`. The host supplies `user_config`. That file is not measured, not authenticated, and not filtered by `allowed_envs`. Any host could therefore give itself root SSH on every image with `sshd`. From v0.0.22 the script ignores that key and logs that it did so.

Migration: to keep SSH access through the pre-launch script, put your public keys in the `DSTACK_AUTHORIZED_KEYS` encrypted environment variable. The risk described above applies. Changing the pre-launch script changes the compose hash, so for apps governed on chain the new hash must be whitelisted before the upgrade. `/home` is not persistent, so keys installed by an older version are gone after the first boot with v0.0.22.
22 changes: 11 additions & 11 deletions phala-cloud-prelaunch-script/prelaunch.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
echo "----------------------------------------------"
echo "Running Phala Cloud Pre-Launch Script v0.0.21"
echo "Running Phala Cloud Pre-Launch Script v0.0.22"
echo "----------------------------------------------"
set -e

Expand Down Expand Up @@ -357,20 +357,20 @@ if mkdir -p /home/root/.ssh 2>/dev/null; then
echo "Root authorized_keys set"
fi

# user_config is supplied by the host: it is not measured, not
# authenticated, and not filtered by allowed_envs. Installing its
# ssh_authorized_keys would let whoever runs the host log in as root on
# any image that ships sshd, so the keys are ignored.
if [[ -f /dstack/user_config ]] && jq empty /dstack/user_config 2>/dev/null; then
if [[ $(jq 'has("ssh_authorized_keys")' /dstack/user_config 2>/dev/null) == "true" ]]; then
# jq -r terminates the value with a newline; jq -j does not, which
# both concatenated the next appended key onto the same line and
# made line-based counting report one key fewer than the file holds.
jq -r '.ssh_authorized_keys' /dstack/user_config >> /home/root/.ssh/authorized_keys
# Drop duplicates and the blank line an empty key list produces.
sort -u /home/root/.ssh/authorized_keys > /home/root/.ssh/authorized_keys.tmp
mv /home/root/.ssh/authorized_keys.tmp /home/root/.ssh/authorized_keys
KEY_COUNT=$(grep -c '^[^[:space:]]' /home/root/.ssh/authorized_keys || true)
echo "Set root authorized_keys from user preferences, total $KEY_COUNT keys"
print_authorized_key_fingerprints /home/root/.ssh/authorized_keys
echo "Ignoring ssh_authorized_keys from user_config (host-supplied, unauthenticated)"
fi
fi
if [[ -f /home/root/.ssh/authorized_keys ]]; then
KEY_COUNT=$(grep -c '^[^[:space:]]' /home/root/.ssh/authorized_keys || true)
echo "Root authorized_keys holds $KEY_COUNT keys"
print_authorized_key_fingerprints /home/root/.ssh/authorized_keys
fi
else
echo "Warning: Cannot create /home/root/.ssh directory (read-only file system?)"
echo "Skipping SSH key setup"
Expand Down
Loading