-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecutable_kasm-secrets
More file actions
120 lines (101 loc) · 2.84 KB
/
Copy pathexecutable_kasm-secrets
File metadata and controls
120 lines (101 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
set -euo pipefail
# Configuration
TARGET_SECRETS_FILE="$HOME/dotfiles/.chezmoidata/kasm_secrets.yml"
# Edit this template to add/remove secrets
generate_config() {
local output_file="$1"
op inject --out-file "$output_file" <<EOF
# Mike Kasberg's Dotfiles Secrets
#
# This file is installed and managed by the kasm-secrets script.
# Check if secrets are installed in Chezmoi templates with 'if hasKey . "kasm_secrets"'.
# Reference secrets like '.kasm_secrets.foo.bar'.
#
# DO NOT commit this file to git.
kasm_secrets:
ssh:
nfs_user: "op://Private/jzuwmdmlxeeph64ccsofukc3vq/username"
csysk_user: "op://Private/lsfoc7enro2pn4or5wqgkqljsi/username"
id_ed25519: |
$(fetch_with_indent 'op://Private/SSH Mike id_ed25519/private key' 6)
kas_catholic_id_rsa: |
$(fetch_with_indent 'op://Private/SSH kas_catholic_id_rsa/private key' 6)
borg:
passphrase: "op://Private/BorgBase/Borg Backup Passphrase"
EOF
}
usage() {
echo "Usage: $(basename "$0") [install|diff|help]"
echo ""
echo "Commands:"
echo " install (default) Fetch secrets and write to target file."
echo " diff Fetch secrets and show diff against existing target file."
echo " help Show this help message."
}
log_info() {
echo -e "ℹ️ $1" >&2
}
log_success() {
echo -e "✅ $1" >&2
}
log_error() {
echo -e "❌ $1" >&2
}
check_deps() {
local missing_deps=0
for dep in op; do
if ! command -v "$dep" &> /dev/null; then
log_error "Missing dependency: $dep"
missing_deps=1
fi
done
if [ "$missing_deps" -ne 0 ]; then
exit 1
fi
}
fetch_with_indent() {
local indent
printf -v indent '%*s' "$2" ''
op read "$1" | sed "s/^/$indent/"
}
main() {
local cmd="${1:-install}"
case "$cmd" in
help|-h|--help)
usage
exit 0
;;
install|diff)
;;
*)
usage
exit 1
;;
esac
check_deps
# Create a temporary file
local temp_file
temp_file=$(mktemp)
# Ensure temp file is cleaned up on exit
trap "rm -f '$temp_file'" EXIT
# Generate content
generate_config "$temp_file"
if [ "$cmd" = "diff" ]; then
if [ -f "$TARGET_SECRETS_FILE" ]; then
log_info "Diffing against $TARGET_SECRETS_FILE:"
if diff -u "$TARGET_SECRETS_FILE" "$temp_file"; then
log_success "Up to date: $TARGET_SECRETS_FILE"
fi
else
log_info "Target file does not exist. Content would be:"
cat "$temp_file"
fi
elif [ "$cmd" = "install" ]; then
mkdir -p "$(dirname "$TARGET_SECRETS_FILE")"
mv "$temp_file" "$TARGET_SECRETS_FILE"
chmod 600 "$TARGET_SECRETS_FILE"
log_success "Secrets installed to $TARGET_SECRETS_FILE"
fi
}
main "$@"