mirror of
https://github.com/Matir/skel.git
synced 2026-07-24 21:26:57 -07:00
Update update-authorized-keys
This commit is contained in:
@@ -76,7 +76,8 @@ run_self_test() {
|
|||||||
|
|
||||||
cat <<EOF > "${target}"
|
cat <<EOF > "${target}"
|
||||||
${key_man}
|
${key_man}
|
||||||
${key1} # This should be removed as it's now managed
|
# This is a manual annotation for key1
|
||||||
|
${key1}
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
echo "Executing script in test mode..."
|
echo "Executing script in test mode..."
|
||||||
@@ -107,6 +108,9 @@ EOF
|
|||||||
echo -n "Check masking... "
|
echo -n "Check masking... "
|
||||||
if ! grep -q "masked" "${target}"; then echo "OK"; else echo "FAIL"; exit 1; fi
|
if ! grep -q "masked" "${target}"; then echo "OK"; else echo "FAIL"; exit 1; fi
|
||||||
|
|
||||||
|
echo -n "Check user comment preservation... "
|
||||||
|
if grep -q "This is a manual annotation for key1" "${target}"; then echo "OK"; else echo "FAIL"; exit 1; fi
|
||||||
|
|
||||||
echo "Self-test passed successfully!"
|
echo "Self-test passed successfully!"
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
@@ -138,6 +142,40 @@ mkdir -p "$(dirname "${TARGET_FILE}")"
|
|||||||
TMP_FILE=$(mktemp)
|
TMP_FILE=$(mktemp)
|
||||||
CLEANUP_FILES+=("${TMP_FILE}")
|
CLEANUP_FILES+=("${TMP_FILE}")
|
||||||
|
|
||||||
|
# Build a map of key-signature -> user comments from the existing target file.
|
||||||
|
# Comments above keys are preserved into the managed block (# Source: lines are excluded).
|
||||||
|
# Multiple comment lines are joined with SUBSEP (\034) for safe transport through AWK.
|
||||||
|
USER_COMMENTS_FILE=$(mktemp)
|
||||||
|
CLEANUP_FILES+=("${USER_COMMENTS_FILE}")
|
||||||
|
if [[ -f "${TARGET_FILE}" ]]; then
|
||||||
|
awk -v begin="${BEGIN_MARKER}" -v end="${END_MARKER}" '
|
||||||
|
BEGIN { pending="" }
|
||||||
|
$0 == begin { pending=""; next }
|
||||||
|
$0 == end { pending=""; next }
|
||||||
|
/^# Source:/ { next }
|
||||||
|
/^[[:space:]]*#/ {
|
||||||
|
pending = (pending == "" ? $0 : pending SUBSEP $0)
|
||||||
|
next
|
||||||
|
}
|
||||||
|
/^[[:space:]]*$/ { pending=""; next }
|
||||||
|
{
|
||||||
|
n = split($0, parts, " ")
|
||||||
|
sig = ""
|
||||||
|
for (i=1; i<=n; i++) {
|
||||||
|
sig = (sig == "" ? parts[i] : sig " " parts[i])
|
||||||
|
if (parts[i] ~ /^(ssh-|ecdsa-|sk-)/ && i < n) {
|
||||||
|
sig = sig " " parts[i+1]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sig != "" && pending != "") {
|
||||||
|
print sig "\t" pending
|
||||||
|
}
|
||||||
|
pending = ""
|
||||||
|
}
|
||||||
|
' "${TARGET_FILE}" > "${USER_COMMENTS_FILE}"
|
||||||
|
fi
|
||||||
|
|
||||||
collect_keys() {
|
collect_keys() {
|
||||||
local dirs=("${@}")
|
local dirs=("${@}")
|
||||||
for dir in "${dirs[@]}"; do
|
for dir in "${dirs[@]}"; do
|
||||||
@@ -158,7 +196,17 @@ collect_keys() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Use a HEREDOC for the complex AWK script to avoid shell interpolation issues
|
# Use a HEREDOC for the complex AWK script to avoid shell interpolation issues
|
||||||
MANAGED_BLOCK=$(collect_keys "${SOURCE_DIRS[@]}" | awk -F'\t' '
|
MANAGED_BLOCK=$(collect_keys "${SOURCE_DIRS[@]}" | awk -F'\t' -v user_comments_file="${USER_COMMENTS_FILE}" '
|
||||||
|
BEGIN {
|
||||||
|
while ((getline line < user_comments_file) > 0) {
|
||||||
|
tab = index(line, "\t")
|
||||||
|
if (tab > 0) {
|
||||||
|
sig = substr(line, 1, tab - 1)
|
||||||
|
user_comments[sig] = substr(line, tab + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close(user_comments_file)
|
||||||
|
}
|
||||||
{
|
{
|
||||||
# Splitting on the first tab manually to be robust
|
# Splitting on the first tab manually to be robust
|
||||||
tab_idx = index($0, "\t")
|
tab_idx = index($0, "\t")
|
||||||
@@ -191,6 +239,12 @@ END {
|
|||||||
for (i=1; i<=count; i++) {
|
for (i=1; i<=count; i++) {
|
||||||
sig = order[i]
|
sig = order[i]
|
||||||
print "# Source: " sources[sig]
|
print "# Source: " sources[sig]
|
||||||
|
if (sig in user_comments) {
|
||||||
|
n = split(user_comments[sig], comment_lines, SUBSEP)
|
||||||
|
for (j=1; j<=n; j++) {
|
||||||
|
print comment_lines[j]
|
||||||
|
}
|
||||||
|
}
|
||||||
print keys[sig]
|
print keys[sig]
|
||||||
}
|
}
|
||||||
}')
|
}')
|
||||||
|
|||||||
Reference in New Issue
Block a user