-
Notifications
You must be signed in to change notification settings - Fork 0
Add per-container Linux Secret Service for VS Code SecretStorage #22
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
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/devcontainer-linux-secret-service
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+260
−0
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7a49b65
Add Linux Secret Service keyring setup to devcontainer
Copilot 42fce39
Remove committed __pycache__ and add .gitignore
Copilot 3acc730
Document setting keyring master password without Codespaces
Copilot e365430
Document KEYRING_PASSWORD environment trade-offs in README
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #!/usr/bin/env python3 | ||
| """Headlessly create a persistent default Secret Service collection. | ||
|
|
||
| gnome-keyring's spec-compliant CreateCollection requires gcr-prompter | ||
| (GTK/Wayland), which devcontainers don't have. This script calls the | ||
| non-spec InternalUnsupportedGuiltRiddenInterface.CreateWithMasterPassword | ||
| method, which accepts the master password inline. Read from | ||
| KEYRING_PASSWORD env (empty by default). | ||
|
|
||
| Idempotent: exits 0 if the 'default' alias already resolves. | ||
| """ | ||
| from __future__ import annotations | ||
| import os, sys | ||
| from jeepney import DBusAddress, new_method_call | ||
| from jeepney.io.blocking import open_dbus_connection | ||
|
|
||
| SP, SB = "/org/freedesktop/secrets", "org.freedesktop.secrets" | ||
| SS = "org.freedesktop.Secret.Service" | ||
| GI = "org.gnome.keyring.InternalUnsupportedGuiltRiddenInterface" | ||
|
|
||
|
|
||
| def main() -> int: | ||
| conn = open_dbus_connection(bus="SESSION") | ||
| svc = DBusAddress(SP, bus_name=SB, interface=SS) | ||
|
|
||
| reply = conn.send_and_get_reply(new_method_call(svc, "ReadAlias", "s", ("default",))) | ||
| if reply.body and reply.body[0] != "/": | ||
| print(f"[keyring-bootstrap] default alias already set to {reply.body[0]}") | ||
| return 0 | ||
|
|
||
| # 'plain' session: master password travels in cleartext over the | ||
| # local unix socket. That's fine because the password is empty (or | ||
| # an environment variable already inside the container). | ||
| reply = conn.send_and_get_reply( | ||
| new_method_call(svc, "OpenSession", "sv", ("plain", ("s", ""))) | ||
| ) | ||
| _, session_path = reply.body | ||
|
|
||
| password = os.environ.get("KEYRING_PASSWORD", "").encode("utf-8") | ||
| master = (session_path, b"", password, "text/plain") | ||
| attrs = {"org.freedesktop.Secret.Collection.Label": ("s", "Login")} | ||
|
|
||
| guilt = DBusAddress(SP, bus_name=SB, interface=GI) | ||
| reply = conn.send_and_get_reply( | ||
| new_method_call(guilt, "CreateWithMasterPassword", | ||
| "a{sv}(oayays)", (attrs, master)) | ||
| ) | ||
| if reply.header.message_type.name != "method_return": | ||
| print(f"[keyring-bootstrap] CreateWithMasterPassword failed: {reply.body}", file=sys.stderr) | ||
| return 1 | ||
| coll_path = reply.body[0] | ||
| print(f"[keyring-bootstrap] created collection {coll_path}") | ||
|
|
||
| reply = conn.send_and_get_reply( | ||
| new_method_call(svc, "SetAlias", "so", ("default", coll_path)) | ||
| ) | ||
| if reply.header.message_type.name != "method_return": | ||
| print(f"[keyring-bootstrap] SetAlias failed: {reply.body}", file=sys.stderr) | ||
| return 1 | ||
| print("[keyring-bootstrap] bound 'default' alias") | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| #!/usr/bin/env bash | ||
| # Bootstraps a per-container Linux Secret Service so VS Code's | ||
| # SecretStorage / keytar / libsecret consumers (incl. GitHub Copilot) | ||
| # have a local encrypted credential vault. | ||
| # | ||
| # Idempotent: safe to run on every container start and VS Code attach. | ||
| # Run as the remote user; do NOT run as root. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| log() { printf '[keyring-init] %s\n' "$*"; } | ||
| warn() { printf '[keyring-init] WARNING: %s\n' "$*" >&2; } | ||
|
|
||
| UID_NUM="$(id -u)" | ||
| XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/tmp/runtime-${UID_NUM}}" | ||
| mkdir -p "$XDG_RUNTIME_DIR" | ||
| chmod 700 "$XDG_RUNTIME_DIR" | ||
| export XDG_RUNTIME_DIR | ||
|
|
||
| DBUS_SOCKET="${XDG_RUNTIME_DIR}/bus" | ||
| DBUS_PID_FILE="${XDG_RUNTIME_DIR}/dbus.pid" | ||
| ENV_FILE="${XDG_RUNTIME_DIR}/keyring.env" | ||
|
|
||
| # --- 1. DBus session bus on a stable socket path -------------------------- | ||
| dbus_alive() { | ||
| [[ -S "$DBUS_SOCKET" ]] || return 1 | ||
| [[ -f "$DBUS_PID_FILE" ]] || return 1 | ||
| local pid; pid="$(cat "$DBUS_PID_FILE" 2>/dev/null || true)" | ||
| [[ -n "$pid" ]] && kill -0 "$pid" 2>/dev/null | ||
| } | ||
|
|
||
| if dbus_alive; then | ||
| log "reusing existing dbus-daemon on $DBUS_SOCKET" | ||
| else | ||
| rm -f "$DBUS_SOCKET" "$DBUS_PID_FILE" | ||
| if ! dbus-daemon --session --address="unix:path=${DBUS_SOCKET}" \ | ||
| --nopidfile --fork --print-pid=3 3>"$DBUS_PID_FILE"; then | ||
| warn "failed to start dbus-daemon"; exit 0 | ||
| fi | ||
| for _ in 1 2 3 4 5 6 7 8 9 10; do | ||
| [[ -S "$DBUS_SOCKET" ]] && break | ||
| sleep 0.1 | ||
| done | ||
| [[ -S "$DBUS_SOCKET" ]] || { warn "dbus socket never appeared"; exit 0; } | ||
| log "started dbus-daemon (pid $(cat "$DBUS_PID_FILE")) on $DBUS_SOCKET" | ||
| fi | ||
| export DBUS_SESSION_BUS_ADDRESS="unix:path=${DBUS_SOCKET}" | ||
|
|
||
| # --- 2. gnome-keyring-daemon ---------------------------------------------- | ||
| keyring_alive() { | ||
| pgrep -u "$UID_NUM" -x gnome-keyring-d >/dev/null 2>&1 | ||
| } | ||
|
|
||
| if keyring_alive; then | ||
| log "reusing existing gnome-keyring-daemon" | ||
| else | ||
| KEYRING_PASS="${KEYRING_PASSWORD-}" | ||
| gk_env="$(printf '%s' "$KEYRING_PASS" \ | ||
| | gnome-keyring-daemon --daemonize --unlock --components=secrets 2>/dev/null)" || { | ||
| warn "gnome-keyring-daemon failed to start"; exit 0 | ||
| } | ||
| log "started gnome-keyring-daemon" | ||
| printf '%s\n' "$gk_env" > "${XDG_RUNTIME_DIR}/keyring.daemon-env" | ||
| fi | ||
|
|
||
| # --- 3. Bootstrap the persistent default collection (first run only) ------ | ||
| if command -v python3 >/dev/null 2>&1 \ | ||
| && python3 -c 'import jeepney' >/dev/null 2>&1; then | ||
| KEYRING_PASSWORD="${KEYRING_PASSWORD-}" \ | ||
| python3 "$(dirname "$0")/keyring-bootstrap.py" \ | ||
| || warn "default-collection bootstrap failed; SecretStorage may be read-only" | ||
| else | ||
| warn "python3-jeepney not available; cannot bootstrap default collection" | ||
| fi | ||
|
|
||
| # --- 4. Persist env for non-VS-Code shells -------------------------------- | ||
| { | ||
| echo "export XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR}" | ||
| echo "export DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS}" | ||
| [[ -f "${XDG_RUNTIME_DIR}/keyring.daemon-env" ]] \ | ||
| && sed -n 's/^\([A-Z_]\+\)=\(.*\)$/export \1=\2/p' \ | ||
| "${XDG_RUNTIME_DIR}/keyring.daemon-env" | ||
| } > "$ENV_FILE" | ||
|
|
||
| # --- 5. Verify Secret Service round-trip ---------------------------------- | ||
| if command -v secret-tool >/dev/null 2>&1; then | ||
| probe_value="ok-$$" | ||
| if printf '%s' "$probe_value" \ | ||
| | secret-tool store --label=devcontainer-keyring-probe \ | ||
| app devcontainer-keyring-probe 2>/dev/null \ | ||
| && [[ "$(secret-tool lookup app devcontainer-keyring-probe 2>/dev/null)" == "$probe_value" ]]; then | ||
| secret-tool clear app devcontainer-keyring-probe 2>/dev/null || true | ||
| log "Secret Service verified on $DBUS_SESSION_BUS_ADDRESS" | ||
| else | ||
| warn "Secret Service probe failed; VS Code extensions may fall back to the plaintext basic store" | ||
| fi | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Python bytecode cache | ||
| __pycache__/ | ||
| *.py[cod] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@copilot are there any risks about keeping this in the environment? is this still more secure than plaintext or no keyring password?
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.
Good question — yes, it's still strictly better than plaintext or an empty password, with one caveat. I documented the trade-offs in e365430:
/proc/<pid>/environ) and unlock the vault. But that same process could already read the secrets directly via SecretStorage, so it doesn't lower the bar below where it already is.${localEnv:...}/ Codespaces secret, never baked into the image or committed. The main thing to avoid is hardcoding it indevcontainer.jsonor leaving it in shell history.Net: empty password protects almost nothing at rest; a real
KEYRING_PASSWORDmeaningfully protects the on-disk vault while leaving runtime exposure unchanged.