-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgrant-principal-scope.sh
More file actions
executable file
·157 lines (144 loc) · 4.03 KB
/
Copy pathgrant-principal-scope.sh
File metadata and controls
executable file
·157 lines (144 loc) · 4.03 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env bash
set -euo pipefail
usage() {
echo "Usage: $0 SIGNING_KEY_ID DESTINATION... [--valid-days DAYS]" >&2
echo "Example: $0 org-alice api.anthropic.com example.com:443 --valid-days 30" >&2
}
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
usage
exit 0
fi
if [[ $# -lt 2 ]]; then
usage
exit 2
fi
SIGNING_KEY_ID="$1"
shift
VALID_DAYS=365
DESTINATIONS=()
DATABASE_URL="${AGENT_GATEWAY_DATABASE_URL:-${DATABASE_URL:-}}"
while [[ $# -gt 0 ]]; do
case "$1" in
--valid-days)
[[ $# -ge 2 ]] || { echo "error: --valid-days requires a value" >&2; exit 2; }
VALID_DAYS="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
--*)
echo "Unknown option: $1" >&2
usage
exit 2
;;
*)
DESTINATIONS+=("$1")
shift
;;
esac
done
if [[ -z "$DATABASE_URL" ]]; then
echo "Set AGENT_GATEWAY_DATABASE_URL or DATABASE_URL" >&2
exit 2
fi
if [[ ${#DESTINATIONS[@]} -eq 0 ]]; then
echo "error: at least one destination is required" >&2
usage
exit 2
fi
command -v psql >/dev/null || { echo "psql is required" >&2; exit 1; }
normalize_destination() {
local input="$1"
local trimmed host port rest colon_count
trimmed="$(printf '%s' "$input" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
if [[ -z "$trimmed" ]]; then
echo "empty destination" >&2
return 1
fi
if [[ "$trimmed" == \[* ]]; then
host="${trimmed#\[}"
host="${host%%\]*}"
rest="${trimmed#*\]}"
if [[ "$rest" == :* ]]; then
port="${rest#:}"
elif [[ -z "$rest" ]]; then
port="443"
else
echo "invalid bracketed destination: $input" >&2
return 1
fi
[[ -n "$host" ]] || { echo "empty destination host: $input" >&2; return 1; }
validate_port "$port" || return 1
printf '[%s]:%s\n' "$(printf '%s' "$host" | tr '[:upper:]' '[:lower:]')" "$port"
return
fi
colon_count="$(awk -F: '{ print NF - 1 }' <<<"$trimmed")"
if [[ "$colon_count" -eq 0 ]]; then
host="$trimmed"
port="443"
[[ -n "$host" ]] || { echo "empty destination host: $input" >&2; return 1; }
printf '%s:%s\n' "$(printf '%s' "$host" | tr '[:upper:]' '[:lower:]')" "$port"
elif [[ "$colon_count" -eq 1 ]]; then
host="${trimmed%:*}"
port="${trimmed##*:}"
[[ -n "$host" ]] || { echo "empty destination host: $input" >&2; return 1; }
validate_port "$port" || return 1
printf '%s:%s\n' "$(printf '%s' "$host" | tr '[:upper:]' '[:lower:]')" "$port"
else
printf '[%s]:443\n' "$(printf '%s' "$trimmed" | tr '[:upper:]' '[:lower:]')"
fi
}
validate_port() {
local port="$1"
if [[ ! "$port" =~ ^[0-9]+$ ]] || (( port < 1 || port > 65535 )); then
echo "invalid destination port: $port" >&2
return 1
fi
}
for destination in "${DESTINATIONS[@]}"; do
normalized_destination="$(normalize_destination "$destination")"
psql "$DATABASE_URL" \
--set=ON_ERROR_STOP=1 \
--set=signing_key_id="$SIGNING_KEY_ID" \
--set=destination="$normalized_destination" \
--set=valid_days="$VALID_DAYS" <<'SQL'
WITH input AS (
SELECT
:'signing_key_id'::text AS signing_key_id,
:'destination'::text AS destination,
:'valid_days'::int AS valid_days
),
refreshed AS (
UPDATE principal_key_permissions p
SET
not_before = now(),
not_after = now() + make_interval(days => input.valid_days),
revoked_at = NULL,
updated_at = now()
FROM input
WHERE p.signing_key_id = input.signing_key_id
AND p.destination = input.destination
AND p.revoked_at IS NULL
RETURNING p.signing_key_id, p.destination, p.not_after
),
inserted AS (
INSERT INTO principal_key_permissions (
signing_key_id, destination, not_before, not_after, revoked_at
)
SELECT
input.signing_key_id,
input.destination,
now(),
now() + make_interval(days => input.valid_days),
NULL
FROM input
WHERE NOT EXISTS (SELECT 1 FROM refreshed)
RETURNING signing_key_id, destination, not_after
)
SELECT signing_key_id, destination, not_after FROM refreshed
UNION ALL
SELECT signing_key_id, destination, not_after FROM inserted;
SQL
done