Compare commits
6 Commits
kanidm-pam
...
9ffdd4f862
| Author | SHA1 | Date | |
|---|---|---|---|
|
9ffdd4f862
|
|||
|
0b977808ca
|
|||
|
8786113f8f
|
|||
|
fdb2c31f84
|
|||
|
78eb04205f
|
|||
| 19cb61ebbc |
24
CLAUDE.md
24
CLAUDE.md
@@ -39,6 +39,30 @@ Do not automatically deploy changes. Deployments are usually done by updating th
|
||||
|
||||
Do not run SSH commands directly. If a command needs to be run on a remote host, provide the command to the user and ask them to run it manually.
|
||||
|
||||
### Sharing Command Output via Loki
|
||||
|
||||
All hosts have the `pipe-to-loki` script for sending command output or terminal sessions to Loki, allowing users to share output with Claude without copy-pasting.
|
||||
|
||||
**Pipe mode** - send command output:
|
||||
```bash
|
||||
command | pipe-to-loki # Auto-generated ID
|
||||
command | pipe-to-loki --id my-test # Custom ID
|
||||
```
|
||||
|
||||
**Session mode** - record interactive terminal session:
|
||||
```bash
|
||||
pipe-to-loki --record # Start recording, exit to send
|
||||
pipe-to-loki --record --id my-session # With custom ID
|
||||
```
|
||||
|
||||
The script prints the session ID which the user can share. Query results with:
|
||||
```logql
|
||||
{job="pipe-to-loki"} # All entries
|
||||
{job="pipe-to-loki", id="my-test"} # Specific ID
|
||||
{job="pipe-to-loki", host="testvm01"} # From specific host
|
||||
{job="pipe-to-loki", type="session"} # Only sessions
|
||||
```
|
||||
|
||||
### Testing Feature Branches on Hosts
|
||||
|
||||
All hosts have the `nixos-rebuild-test` helper script for testing feature branches before merging:
|
||||
|
||||
108
docs/plans/openbao-kanidm-oidc.md
Normal file
108
docs/plans/openbao-kanidm-oidc.md
Normal file
@@ -0,0 +1,108 @@
|
||||
# OpenBao + Kanidm OIDC Integration
|
||||
|
||||
## Overview
|
||||
|
||||
Enable Kanidm users to authenticate to OpenBao (Vault) using OIDC, allowing access to secrets based on Kanidm group membership.
|
||||
|
||||
## Current State
|
||||
|
||||
**Kanidm:**
|
||||
- Server: `auth.home.2rjus.net` (kanidm01)
|
||||
- Domain: `home.2rjus.net`
|
||||
- Groups: `admins`, `users`, `ssh-users`
|
||||
- No OIDC clients configured yet
|
||||
|
||||
**OpenBao:**
|
||||
- Server: `vault.home.2rjus.net` (vault01)
|
||||
- Auth: AppRole only (machine-to-machine)
|
||||
- No human user authentication configured
|
||||
|
||||
## OpenBao OIDC Auth Method
|
||||
|
||||
OpenBao includes the JWT/OIDC auth method in the open-source version (unlike Vault Enterprise which gates some auth features). Key points:
|
||||
|
||||
- Enable with: `bao auth enable oidc`
|
||||
- Supports browser-based OIDC login flow
|
||||
- Maps OIDC claims/groups to OpenBao policies
|
||||
- Works with both CLI (`bao login`) and Web UI
|
||||
|
||||
### Required Configuration
|
||||
|
||||
```bash
|
||||
bao write auth/oidc/config \
|
||||
oidc_discovery_url="https://auth.home.2rjus.net/oauth2/openid/<client_id>" \
|
||||
oidc_client_id="<client_id>" \
|
||||
oidc_client_secret="<client_secret>" \
|
||||
default_role="default"
|
||||
```
|
||||
|
||||
### Callback URIs
|
||||
|
||||
OpenBao requires specific callback URIs registered in Kanidm:
|
||||
|
||||
- **CLI:** `http://localhost:8250/oidc/callback`
|
||||
- **Web UI:** `https://vault.home.2rjus.net:8200/ui/vault/auth/oidc/oidc/callback`
|
||||
|
||||
## Kanidm OAuth2 Configuration
|
||||
|
||||
Kanidm supports declarative OAuth2 client provisioning via NixOS:
|
||||
|
||||
```nix
|
||||
services.kanidm.provision.systems.oauth2.openbao = {
|
||||
displayName = "OpenBao Secrets";
|
||||
# originUrl - where the client lives
|
||||
# originLanding - where to redirect after auth
|
||||
# basicSecretFile - client secret
|
||||
# scopeMaps - which scopes groups can request
|
||||
# claimMaps - custom claims based on group membership
|
||||
};
|
||||
```
|
||||
|
||||
The `basicSecretFile` should contain the client secret, fetched from Vault.
|
||||
|
||||
## Implementation Approach
|
||||
|
||||
### 1. Create OAuth2 Client in Kanidm
|
||||
|
||||
Add to `services/kanidm/default.nix`:
|
||||
- OAuth2 client `openbao` with callback URIs
|
||||
- Scope maps for `admins` and `users` groups
|
||||
- Claim maps to expose group membership
|
||||
|
||||
### 2. Enable OIDC Auth in OpenBao
|
||||
|
||||
Options:
|
||||
- **Terraform:** Add `vault_jwt_auth_backend` resource in `terraform/vault/`
|
||||
- **NixOS:** Configure in vault01 host config
|
||||
|
||||
Terraform is probably cleaner since we already manage OpenBao config there.
|
||||
|
||||
### 3. Create OpenBao Roles
|
||||
|
||||
Map Kanidm groups to policies:
|
||||
|
||||
| Kanidm Group | OpenBao Role | Policy |
|
||||
|--------------|--------------|--------|
|
||||
| `admins` | `admin` | Full read access to secrets |
|
||||
| `users` | `user` | Limited read access |
|
||||
|
||||
### 4. Chicken-and-Egg Problem
|
||||
|
||||
The OAuth2 client secret needs to be stored in OpenBao, but OpenBao needs the secret to configure OIDC auth. Solutions:
|
||||
|
||||
1. **Bootstrap manually:** Create initial secret via `bao` CLI
|
||||
2. **Two-phase Terraform:** First create the secret, then configure OIDC
|
||||
3. **Static secret:** Use a static secret for the OAuth2 client (less ideal)
|
||||
|
||||
## Open Questions
|
||||
|
||||
1. **Web UI access:** Do we want users logging into the OpenBao web UI, or just CLI?
|
||||
2. **Policy granularity:** What secrets should `admins` vs `users` access?
|
||||
3. **Token TTL:** How long should OIDC-issued tokens last?
|
||||
|
||||
## References
|
||||
|
||||
- [OpenBao JWT/OIDC Auth Method](https://openbao.org/docs/auth/jwt/)
|
||||
- [OpenBao OIDC Provider Configuration](https://openbao.org/docs/auth/jwt/oidc-providers/)
|
||||
- [Kanidm OAuth2 Documentation](https://kanidm.github.io/kanidm/stable/integrations/oauth2.html)
|
||||
- [NixOS Kanidm OAuth2 Options](https://search.nixos.org/options?query=services.kanidm.provision.systems.oauth2)
|
||||
@@ -191,6 +191,15 @@
|
||||
./hosts/kanidm01
|
||||
];
|
||||
};
|
||||
monitoring02 = nixpkgs.lib.nixosSystem {
|
||||
inherit system;
|
||||
specialArgs = {
|
||||
inherit inputs self;
|
||||
};
|
||||
modules = commonModules ++ [
|
||||
./hosts/monitoring02
|
||||
];
|
||||
};
|
||||
};
|
||||
packages = forAllSystems (
|
||||
{ pkgs }:
|
||||
|
||||
72
hosts/monitoring02/configuration.nix
Normal file
72
hosts/monitoring02/configuration.nix
Normal file
@@ -0,0 +1,72 @@
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
|
||||
{
|
||||
imports = [
|
||||
../template2/hardware-configuration.nix
|
||||
|
||||
../../system
|
||||
../../common/vm
|
||||
];
|
||||
|
||||
# Host metadata (adjust as needed)
|
||||
homelab.host = {
|
||||
tier = "test"; # Start in test tier, move to prod after validation
|
||||
};
|
||||
|
||||
# Enable Vault integration
|
||||
vault.enable = true;
|
||||
|
||||
# Enable remote deployment via NATS
|
||||
homelab.deploy.enable = true;
|
||||
|
||||
nixpkgs.config.allowUnfree = true;
|
||||
boot.loader.grub.enable = true;
|
||||
boot.loader.grub.device = "/dev/vda";
|
||||
|
||||
networking.hostName = "monitoring02";
|
||||
networking.domain = "home.2rjus.net";
|
||||
networking.useNetworkd = true;
|
||||
networking.useDHCP = false;
|
||||
services.resolved.enable = true;
|
||||
networking.nameservers = [
|
||||
"10.69.13.5"
|
||||
"10.69.13.6"
|
||||
];
|
||||
|
||||
systemd.network.enable = true;
|
||||
systemd.network.networks."ens18" = {
|
||||
matchConfig.Name = "ens18";
|
||||
address = [
|
||||
"10.69.13.24/24"
|
||||
];
|
||||
routes = [
|
||||
{ Gateway = "10.69.13.1"; }
|
||||
];
|
||||
linkConfig.RequiredForOnline = "routable";
|
||||
};
|
||||
time.timeZone = "Europe/Oslo";
|
||||
|
||||
nix.settings.experimental-features = [
|
||||
"nix-command"
|
||||
"flakes"
|
||||
];
|
||||
nix.settings.tarball-ttl = 0;
|
||||
environment.systemPackages = with pkgs; [
|
||||
vim
|
||||
wget
|
||||
git
|
||||
];
|
||||
|
||||
# Open ports in the firewall.
|
||||
# networking.firewall.allowedTCPPorts = [ ... ];
|
||||
# networking.firewall.allowedUDPPorts = [ ... ];
|
||||
# Or disable the firewall altogether.
|
||||
networking.firewall.enable = false;
|
||||
|
||||
system.stateVersion = "25.11"; # Did you read the comment?
|
||||
}
|
||||
5
hosts/monitoring02/default.nix
Normal file
5
hosts/monitoring02/default.nix
Normal file
@@ -0,0 +1,5 @@
|
||||
{ ... }: {
|
||||
imports = [
|
||||
./configuration.nix
|
||||
];
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
./motd.nix
|
||||
./packages.nix
|
||||
./nix.nix
|
||||
./pipe-to-loki.nix
|
||||
./root-user.nix
|
||||
./pki/root-ca.nix
|
||||
./sshd.nix
|
||||
|
||||
140
system/pipe-to-loki.nix
Normal file
140
system/pipe-to-loki.nix
Normal file
@@ -0,0 +1,140 @@
|
||||
{
|
||||
config,
|
||||
pkgs,
|
||||
lib,
|
||||
...
|
||||
}:
|
||||
let
|
||||
pipe-to-loki = pkgs.writeShellApplication {
|
||||
name = "pipe-to-loki";
|
||||
runtimeInputs = with pkgs; [
|
||||
curl
|
||||
jq
|
||||
util-linux
|
||||
coreutils
|
||||
];
|
||||
text = ''
|
||||
set -euo pipefail
|
||||
|
||||
LOKI_URL="http://monitoring01.home.2rjus.net:3100/loki/api/v1/push"
|
||||
HOSTNAME=$(hostname)
|
||||
SESSION_ID=""
|
||||
RECORD_MODE=false
|
||||
|
||||
usage() {
|
||||
echo "Usage: pipe-to-loki [--id ID] [--record]"
|
||||
echo ""
|
||||
echo "Send command output or interactive sessions to Loki."
|
||||
echo ""
|
||||
echo "Options:"
|
||||
echo " --id ID Set custom session ID (default: auto-generated)"
|
||||
echo " --record Start interactive recording session"
|
||||
echo ""
|
||||
echo "Examples:"
|
||||
echo " command | pipe-to-loki # Pipe command output"
|
||||
echo " command | pipe-to-loki --id foo # Pipe with custom ID"
|
||||
echo " pipe-to-loki --record # Start recording session"
|
||||
exit 1
|
||||
}
|
||||
|
||||
generate_id() {
|
||||
local random_chars
|
||||
random_chars=$(head -c 2 /dev/urandom | od -An -tx1 | tr -d ' \n')
|
||||
echo "''${HOSTNAME}-$(date +%s)-''${random_chars}"
|
||||
}
|
||||
|
||||
send_to_loki() {
|
||||
local content="$1"
|
||||
local type="$2"
|
||||
local timestamp_ns
|
||||
timestamp_ns=$(date +%s%N)
|
||||
|
||||
local payload
|
||||
payload=$(jq -n \
|
||||
--arg job "pipe-to-loki" \
|
||||
--arg host "$HOSTNAME" \
|
||||
--arg type "$type" \
|
||||
--arg id "$SESSION_ID" \
|
||||
--arg ts "$timestamp_ns" \
|
||||
--arg content "$content" \
|
||||
'{
|
||||
streams: [{
|
||||
stream: {
|
||||
job: $job,
|
||||
host: $host,
|
||||
type: $type,
|
||||
id: $id
|
||||
},
|
||||
values: [[$ts, $content]]
|
||||
}]
|
||||
}')
|
||||
|
||||
if curl -s -X POST "$LOKI_URL" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$payload" > /dev/null; then
|
||||
return 0
|
||||
else
|
||||
echo "Error: Failed to send to Loki" >&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Parse arguments
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
--id)
|
||||
SESSION_ID="$2"
|
||||
shift 2
|
||||
;;
|
||||
--record)
|
||||
RECORD_MODE=true
|
||||
shift
|
||||
;;
|
||||
--help|-h)
|
||||
usage
|
||||
;;
|
||||
*)
|
||||
echo "Unknown option: $1" >&2
|
||||
usage
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Generate ID if not provided
|
||||
if [[ -z "$SESSION_ID" ]]; then
|
||||
SESSION_ID=$(generate_id)
|
||||
fi
|
||||
|
||||
if $RECORD_MODE; then
|
||||
# Session recording mode
|
||||
SCRIPT_FILE=$(mktemp)
|
||||
trap 'rm -f "$SCRIPT_FILE"' EXIT
|
||||
|
||||
echo "Recording session $SESSION_ID... (exit to send)"
|
||||
|
||||
# Use script to record the session
|
||||
script -q "$SCRIPT_FILE"
|
||||
|
||||
# Read the transcript and send to Loki
|
||||
content=$(cat "$SCRIPT_FILE")
|
||||
if send_to_loki "$content" "session"; then
|
||||
echo "Session $SESSION_ID sent to Loki"
|
||||
fi
|
||||
else
|
||||
# Pipe mode - read from stdin
|
||||
if [[ -t 0 ]]; then
|
||||
echo "Error: No input provided. Pipe a command or use --record for interactive mode." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
content=$(cat)
|
||||
if send_to_loki "$content" "command"; then
|
||||
echo "Sent to Loki with id: $SESSION_ID"
|
||||
fi
|
||||
fi
|
||||
'';
|
||||
};
|
||||
in
|
||||
{
|
||||
environment.systemPackages = [ pipe-to-loki ];
|
||||
}
|
||||
@@ -39,6 +39,11 @@ locals {
|
||||
"secret/data/kanidm/*",
|
||||
]
|
||||
}
|
||||
"monitoring02" = {
|
||||
paths = [
|
||||
"secret/data/hosts/monitoring02/*",
|
||||
]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -79,6 +79,13 @@ locals {
|
||||
disk_size = "20G"
|
||||
vault_wrapped_token = "s.OOqjEECeIV7dNgCS6jNmyY3K"
|
||||
}
|
||||
"monitoring02" = {
|
||||
ip = "10.69.13.24/24"
|
||||
cpu_cores = 4
|
||||
memory = 4096
|
||||
disk_size = "60G"
|
||||
vault_wrapped_token = "s.uXpdoGxHXpWvTsGbHkZuq1jF"
|
||||
}
|
||||
}
|
||||
|
||||
# Compute VM configurations with defaults applied
|
||||
|
||||
Reference in New Issue
Block a user