feat(templates): add starter runbook workspace (#5)
Document the notebook format and provide SSH-key, hardening-check, and package-install examples with inventory and credential exclusions.
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
# Local credentials and runtime data never belong in the workspace repository.
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
known_hosts
|
||||
data/
|
||||
*.pem
|
||||
*.key
|
||||
id_ed25519*
|
||||
id_rsa*
|
||||
*.swp
|
||||
*~
|
||||
@@ -0,0 +1,113 @@
|
||||
# Writing ShellBook notebooks
|
||||
|
||||
This repository is the user's workspace. Write runbooks as ordinary Markdown in
|
||||
`notebooks/**/*.md`. ShellBook renders these files live as you edit them. This
|
||||
terminal is a real shell in a persistent tmux session; start `claude` here using
|
||||
the user's CLI login. Do not add API clients, subscription tokens, or chat wrappers.
|
||||
|
||||
## Style and safety
|
||||
|
||||
- Show literal shell commands, directly in `sh` or `bash` fences. The displayed
|
||||
script is exactly what executes. No Python/JavaScript wrappers, generated command
|
||||
strings, hidden scripts, or `eval`.
|
||||
- One logical action per step. Explain the target, effect and prerequisites in prose.
|
||||
- Put checks before runs. Checks are strictly side-effect free: no writes, installs,
|
||||
service restarts, uploads or redirection to files. Exit 0 means passed; nonzero fails.
|
||||
- Make runs idempotent; test existing state before appending or replacing things.
|
||||
- Never put secrets in notebooks, defaults, prose, commands, captures, logs or artifact
|
||||
metadata. Never use `set -x` with secrets or print them. A secret parameter is an
|
||||
environment variable named exactly as declared; reference `$TOKEN`, never `{{TOKEN}}`.
|
||||
- Scripts execute as `bash -euo pipefail -s` without a TTY. Interactive tools belong
|
||||
in the terminal. Steps with `sudo` need passwordless `sudo -n`.
|
||||
- Never disable SSH host-key checking. Inventory names come from `hosts.yaml`.
|
||||
|
||||
## Frontmatter and substitution
|
||||
|
||||
```yaml
|
||||
---
|
||||
title: Example runbook
|
||||
params:
|
||||
host: { type: host, required: true, label: Target host }
|
||||
user: { type: name, default: admin, label: Login user }
|
||||
keyname: { type: name, default: "id_ed25519_{{host}}" }
|
||||
destination: { type: path, default: /etc/example.conf }
|
||||
comment: { type: string, label: Description }
|
||||
mode: { type: choice, options: [safe, full], default: safe }
|
||||
TOKEN: { type: secret, required: true, label: Access token }
|
||||
---
|
||||
```
|
||||
|
||||
All parameter fields: `type`, `required`, `default`, `label`, `options`. Omitted
|
||||
`required` is false. `options` is for `choice`; never set a default for a secret.
|
||||
Types: `host` selects an inventory name; `name` allows `[A-Za-z0-9._-]+`;
|
||||
`path` allows absolute/relative paths without NUL or newline; `string` is free text;
|
||||
`choice` must match an option; `secret` is supplied only through the environment.
|
||||
Non-secret defaults may reference other parameters.
|
||||
|
||||
Use `{{name}}` in prose, attributes and scripts. Script substitution is already
|
||||
shell-quoted by ShellBook's core: write `id {{user}}`, not `id '{{user}}'`.
|
||||
Do not invent escaping rules or assume templates expand like shell variables.
|
||||
Missing required parameters disable execution. In shell strings use ordinary
|
||||
shell variables where needed, e.g. `destination={{destination}}` then `"$destination"`.
|
||||
|
||||
## Step syntax and every attribute
|
||||
|
||||
````markdown
|
||||
```sh {check host="{{host}}" id=user-exists title="User exists" timeout=10}
|
||||
id {{user}}
|
||||
```
|
||||
|
||||
```sh {check host="{{host}}" id=os title="Read operating system" capture=os_id}
|
||||
. /etc/os-release
|
||||
printf '%s\n' "$ID"
|
||||
```
|
||||
|
||||
```sh {run host="{{host}}" id=reload title="Reload SSH configuration" sudo confirm timeout=60}
|
||||
sshd -t
|
||||
systemctl reload ssh
|
||||
```
|
||||
````
|
||||
|
||||
- `run` or `check`: required kind. `sh` and `bash` retain Markdown highlighting.
|
||||
- `host="{{host}}"`: remote target. `on=local`: ShellBook service host instead;
|
||||
use one target form per step. Local does **not** mean the browser's computer.
|
||||
- `id=...`: unique, stable identifier; default is the title's slug. Use explicit
|
||||
IDs for references. `title="..."`: human-readable heading.
|
||||
- `sudo`: passwordless elevation, both kinds. `confirm`: run only, prompts first;
|
||||
remote runs against hosts tagged `prod` already default to confirmation.
|
||||
- `timeout=60`: seconds; defaults are 300 for run and 30 for check.
|
||||
- `capture=var`: both kinds, stores trimmed stdout as a runtime value for subsequent
|
||||
steps. Print only the desired value; use `{{var}}` later. Never capture secrets.
|
||||
- `artifacts="a,b"`: run only, comma-separated files relative to that run's fresh
|
||||
workdir. Offered after success; remote files are fetched using SFTP.
|
||||
- `upload="src:dst"`: run only, copies a prior artifact to a remote destination
|
||||
before executing. Use explicit, non-colliding temporary destinations and clean up.
|
||||
|
||||
## Artifacts and conditional follow-up
|
||||
|
||||
````markdown
|
||||
```sh {run on=local id=keygen title="Generate key pair" artifacts="{{keyname}},{{keyname}}.pub"}
|
||||
ssh-keygen -q -t ed25519 -N '' -C {{user}}@{{host}} -f {{keyname}}
|
||||
```
|
||||
|
||||
```sh {run host="{{host}}" id=inspect-key title="Inspect uploaded public key" upload="{{keyname}}.pub:/tmp/shellbook-key.pub"}
|
||||
ssh-keygen -lf /tmp/shellbook-key.pub
|
||||
rm -f /tmp/shellbook-key.pub
|
||||
```
|
||||
|
||||
:::after{step=keygen}
|
||||
Download the private key and store it securely with mode 600.
|
||||
:::
|
||||
|
||||
:::after{step=inspect-key failed}
|
||||
Inspect the error before retrying; check that the earlier artifact is still available.
|
||||
:::
|
||||
````
|
||||
|
||||
`:::after{step=id}` appears after that step succeeds in the current browser session;
|
||||
add `failed` for failure. Refer to a real step ID. Each local run gets a fresh
|
||||
`data/runs/<run-id>/` workdir; do not assume files survive between steps. Use artifact
|
||||
uploads to transfer earlier results. Artifacts expire after 24 hours by default;
|
||||
private keys are sensitive, downloadable once, then removed. Do not commit them.
|
||||
|
||||
See the three notebooks for complete SSH-key, read-only hardening and Debian package examples.
|
||||
@@ -0,0 +1,13 @@
|
||||
# Replace these examples with your own inventory. No passwords or private keys.
|
||||
# ShellBook uses its own SSH identity and confirms unknown host keys explicitly.
|
||||
hosts:
|
||||
- name: staging
|
||||
address: 192.0.2.10
|
||||
user: admin
|
||||
port: 22
|
||||
tags: [staging]
|
||||
# - name: production
|
||||
# address: 192.0.2.20
|
||||
# user: admin
|
||||
# port: 22
|
||||
# tags: [prod] # Remote run steps ask for confirmation by default.
|
||||
@@ -0,0 +1,43 @@
|
||||
---
|
||||
title: Base hardening checks
|
||||
params:
|
||||
host: { type: host, required: true, label: Target server }
|
||||
---
|
||||
|
||||
# Base hardening checks
|
||||
|
||||
Read-only baseline for Debian servers. These checks report state; they do not
|
||||
change configuration. A failed check needs investigation, not an automatic fix.
|
||||
SSH configuration checks need passwordless sudo to read all included files.
|
||||
|
||||
```sh {check host="{{host}}" id=os title="Identify operating system" capture=os_id}
|
||||
. /etc/os-release
|
||||
printf '%s\n' "$ID"
|
||||
```
|
||||
|
||||
```sh {check host="{{host}}" id=no-root-password title="Root password login disabled" sudo}
|
||||
sshd -T | grep -Eq '^permitrootlogin (no|prohibit-password|without-password)$'
|
||||
```
|
||||
|
||||
```sh {check host="{{host}}" id=no-password-auth title="SSH password authentication disabled" sudo}
|
||||
sshd -T | grep -qx 'passwordauthentication no'
|
||||
```
|
||||
|
||||
```sh {check host="{{host}}" id=no-interactive-auth title="SSH keyboard-interactive authentication disabled" sudo}
|
||||
sshd -T | grep -qx 'kbdinteractiveauthentication no'
|
||||
```
|
||||
|
||||
```sh {check host="{{host}}" id=firewall title="Firewall rules present" sudo timeout=10}
|
||||
nft list ruleset | grep -q 'hook input'
|
||||
```
|
||||
|
||||
This confirms an input hook exists, not that the complete firewall policy is safe.
|
||||
|
||||
```sh {check host="{{host}}" id=security-updates title="Automatic security updates active"}
|
||||
systemctl is-enabled unattended-upgrades.service
|
||||
systemctl is-active unattended-upgrades.service
|
||||
```
|
||||
|
||||
```sh {check host="{{host}}" id=no-failed-units title="No failed systemd units"}
|
||||
test "$(systemctl --failed --no-legend --plain | wc -l)" -eq 0
|
||||
```
|
||||
@@ -0,0 +1,52 @@
|
||||
---
|
||||
title: Install and enable a package
|
||||
params:
|
||||
host: { type: host, required: true, label: Debian server }
|
||||
package: { type: name, required: true, default: nginx, label: Package }
|
||||
service: { type: name, required: true, default: nginx, label: Systemd service }
|
||||
---
|
||||
|
||||
# Install and enable {{package}}
|
||||
|
||||
For Debian hosts using apt and systemd. Check the package/service pairing before
|
||||
running; installing packages can start services and expose network ports. Existing
|
||||
apt indexes must be available. Runs require passwordless sudo and confirmation.
|
||||
|
||||
```sh {check host="{{host}}" id=apt-available title="APT is available"}
|
||||
command -v apt-get
|
||||
command -v systemctl
|
||||
```
|
||||
|
||||
```sh {check host="{{host}}" id=package-available title="Package exists in configured repositories"}
|
||||
apt-cache show -- {{package}} > /dev/null
|
||||
```
|
||||
|
||||
```sh {check host="{{host}}" id=installed title="Package already installed"}
|
||||
dpkg-query -W -f='${Status}\n' -- {{package}} | grep -qx 'install ok installed'
|
||||
```
|
||||
|
||||
An absent package is an expected failed check. Proceed with installation only
|
||||
after the repository check succeeds.
|
||||
|
||||
```sh {run host="{{host}}" id=install title="Install package if absent" sudo confirm timeout=300}
|
||||
if ! dpkg-query -W -f='${Status}\n' -- {{package}} 2>/dev/null | grep -qx 'install ok installed'; then
|
||||
DEBIAN_FRONTEND=noninteractive apt-get install -y -- {{package}}
|
||||
fi
|
||||
```
|
||||
|
||||
```sh {run host="{{host}}" id=enable title="Enable and start service" sudo confirm timeout=60}
|
||||
systemctl enable --now -- {{service}}
|
||||
```
|
||||
|
||||
```sh {check host="{{host}}" id=enabled title="Service enabled"}
|
||||
systemctl is-enabled -- {{service}}
|
||||
```
|
||||
|
||||
```sh {check host="{{host}}" id=active title="Service active"}
|
||||
systemctl is-active -- {{service}}
|
||||
```
|
||||
|
||||
:::after{step=enable}
|
||||
The enable command succeeded. Run the final checks and verify application health
|
||||
and firewall policy before directing traffic to **{{host}}**.
|
||||
:::
|
||||
@@ -0,0 +1,56 @@
|
||||
---
|
||||
title: New server SSH key
|
||||
params:
|
||||
host: { type: host, required: true, label: Target server }
|
||||
user: { type: name, required: true, default: admin, label: Login user }
|
||||
keyname: { type: name, default: "id_ed25519_{{host}}" }
|
||||
---
|
||||
|
||||
# New server SSH key
|
||||
|
||||
Creates a key pair for **{{user}}** on **{{host}}** and authorizes it.
|
||||
The host needs OpenSSH and passwordless sudo. The private key stays local to
|
||||
ShellBook until downloaded; only the public key is uploaded.
|
||||
|
||||
```sh {check host="{{host}}" id=user-exists title="User exists"}
|
||||
id {{user}}
|
||||
```
|
||||
|
||||
```sh {check host="{{host}}" id=password-login-disabled title="Password login disabled" sudo}
|
||||
sshd -T | grep -qx 'passwordauthentication no'
|
||||
```
|
||||
|
||||
```sh {run on=local id=keygen title="Generate key pair" artifacts="{{keyname}},{{keyname}}.pub"}
|
||||
ssh-keygen -q -t ed25519 -N '' -C {{user}}@{{host}} -f {{keyname}}
|
||||
```
|
||||
|
||||
Each execution generates a fresh pair in its own workdir. Rerunning this step
|
||||
intentionally creates a new key; it does not overwrite a key from an earlier run.
|
||||
|
||||
```sh {run host="{{host}}" id=authorize title="Authorize key" sudo confirm upload="{{keyname}}.pub:/tmp/shellbook-new-key.pub"}
|
||||
home=$(getent passwd {{user}} | cut -d: -f6)
|
||||
test -n "$home"
|
||||
group=$(id -gn {{user}})
|
||||
install -d -m 700 -o {{user}} -g "$group" "$home/.ssh"
|
||||
touch "$home/.ssh/authorized_keys"
|
||||
key=$(cat /tmp/shellbook-new-key.pub)
|
||||
if ! grep -qxF -- "$key" "$home/.ssh/authorized_keys"; then
|
||||
printf '%s\n' "$key" >> "$home/.ssh/authorized_keys"
|
||||
fi
|
||||
chown {{user}}:"$group" "$home/.ssh/authorized_keys"
|
||||
chmod 600 "$home/.ssh/authorized_keys"
|
||||
rm -f /tmp/shellbook-new-key.pub
|
||||
```
|
||||
|
||||
:::after{step=keygen}
|
||||
Download the private key once and save it securely:
|
||||
|
||||
- **Linux:** `~/.ssh/{{keyname}}`, then `chmod 600 ~/.ssh/{{keyname}}`.
|
||||
- **Windows:** `C:\Users\<you>\.ssh\{{keyname}}`, accessible only to your user.
|
||||
|
||||
Keep the public artifact available for the authorization step. Never commit the private key.
|
||||
:::
|
||||
|
||||
:::after{step=authorize failed}
|
||||
Inspect the output and confirm the account and sudo access before retrying.
|
||||
:::
|
||||
Reference in New Issue
Block a user