Add CA hash sidecar for onboarding
This commit is contained in:
@@ -235,6 +235,7 @@ prep_image() {
|
|||||||
clean_file /etc/pikit/certs/pikit.local.key
|
clean_file /etc/pikit/certs/pikit.local.key
|
||||||
clean_file /etc/pikit/certs/pikit.local.csr
|
clean_file /etc/pikit/certs/pikit.local.csr
|
||||||
clean_file /var/www/pikit-web/assets/pikit-ca.crt
|
clean_file /var/www/pikit-web/assets/pikit-ca.crt
|
||||||
|
clean_file /var/www/pikit-web/assets/pikit-ca.sha256
|
||||||
|
|
||||||
# --- Backup/editor cruft ---
|
# --- Backup/editor cruft ---
|
||||||
clean_backups /var/www/pikit-web
|
clean_backups /var/www/pikit-web
|
||||||
@@ -515,6 +516,7 @@ check_image() {
|
|||||||
check_file_missing /etc/pikit/certs/pikit.local.crt
|
check_file_missing /etc/pikit/certs/pikit.local.crt
|
||||||
check_file_missing /etc/pikit/certs/pikit.local.key
|
check_file_missing /etc/pikit/certs/pikit.local.key
|
||||||
check_file_missing /var/www/pikit-web/assets/pikit-ca.crt
|
check_file_missing /var/www/pikit-web/assets/pikit-ca.crt
|
||||||
|
check_file_missing /var/www/pikit-web/assets/pikit-ca.sha256
|
||||||
|
|
||||||
section "Logs"
|
section "Logs"
|
||||||
if [ -d /var/log ]; then
|
if [ -d /var/log ]; then
|
||||||
|
|||||||
@@ -108,13 +108,45 @@
|
|||||||
if (el) el.textContent = cmd;
|
if (el) el.textContent = cmd;
|
||||||
});
|
});
|
||||||
|
|
||||||
async function loadCaHash() {
|
async function fetchText(url) {
|
||||||
|
const res = await fetch(url, { cache: "no-store" });
|
||||||
|
if (!res.ok) return "";
|
||||||
|
return (await res.text()).trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchCaHashFromApi() {
|
||||||
|
const res = await fetch("/api/firstboot", { cache: "no-store" });
|
||||||
|
if (!res.ok) return "";
|
||||||
|
const data = await res.json();
|
||||||
|
return data?.ca_hash || "";
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadCaHash(retries = 10) {
|
||||||
if (!caHash) return;
|
if (!caHash) return;
|
||||||
try {
|
try {
|
||||||
const res = await fetch("/api/firstboot");
|
const assetHash = await fetchText("/assets/pikit-ca.sha256");
|
||||||
const data = await res.json();
|
if (assetHash) {
|
||||||
caHash.textContent = data?.ca_hash || "Unavailable";
|
caHash.textContent = assetHash.split(/\s+/)[0];
|
||||||
|
return;
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
// ignore and try API
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const apiHash = await fetchCaHashFromApi();
|
||||||
|
if (apiHash) {
|
||||||
|
caHash.textContent = apiHash;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
// fall through
|
||||||
|
}
|
||||||
|
|
||||||
|
if (retries > 0) {
|
||||||
|
caHash.textContent = "Generating...";
|
||||||
|
setTimeout(() => loadCaHash(retries - 1), 2000);
|
||||||
|
} else {
|
||||||
caHash.textContent = "Unavailable";
|
caHash.textContent = "Unavailable";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,19 @@ log() {
|
|||||||
printf '[pikit-certgen] %s\n' "$*"
|
printf '[pikit-certgen] %s\n' "$*"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
write_ca_hash() {
|
||||||
|
if [ -s "$WEB_ASSETS/pikit-ca.crt" ]; then
|
||||||
|
if command -v sha256sum >/dev/null 2>&1; then
|
||||||
|
sha256sum "$WEB_ASSETS/pikit-ca.crt" | awk '{print $1}' > "$WEB_ASSETS/pikit-ca.sha256"
|
||||||
|
elif command -v openssl >/dev/null 2>&1; then
|
||||||
|
openssl dgst -sha256 "$WEB_ASSETS/pikit-ca.crt" | awk '{print $2}' > "$WEB_ASSETS/pikit-ca.sha256"
|
||||||
|
fi
|
||||||
|
if [ -s "$WEB_ASSETS/pikit-ca.sha256" ]; then
|
||||||
|
chmod 644 "$WEB_ASSETS/pikit-ca.sha256"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
ensure_group() {
|
ensure_group() {
|
||||||
if ! getent group "$CERT_GROUP" >/dev/null 2>&1; then
|
if ! getent group "$CERT_GROUP" >/dev/null 2>&1; then
|
||||||
groupadd "$CERT_GROUP" || true
|
groupadd "$CERT_GROUP" || true
|
||||||
@@ -52,6 +65,7 @@ if [ -s "$CA_CRT" ] && [ -s "$CA_KEY" ] && [ -s "$SRV_KEY" ] && [ -s "$SRV_CRT"
|
|||||||
chmod 644 "$WEB_ASSETS/pikit-ca.crt"
|
chmod 644 "$WEB_ASSETS/pikit-ca.crt"
|
||||||
log "Copied CA to web assets."
|
log "Copied CA to web assets."
|
||||||
fi
|
fi
|
||||||
|
write_ca_hash
|
||||||
fix_perms
|
fix_perms
|
||||||
log "TLS certs already present; skipping generation."
|
log "TLS certs already present; skipping generation."
|
||||||
exit 0
|
exit 0
|
||||||
@@ -97,5 +111,6 @@ fix_perms
|
|||||||
|
|
||||||
cp "$CA_CRT" "$WEB_ASSETS/pikit-ca.crt"
|
cp "$CA_CRT" "$WEB_ASSETS/pikit-ca.crt"
|
||||||
chmod 644 "$WEB_ASSETS/pikit-ca.crt"
|
chmod 644 "$WEB_ASSETS/pikit-ca.crt"
|
||||||
|
write_ca_hash
|
||||||
|
|
||||||
log "TLS certs generated."
|
log "TLS certs generated."
|
||||||
|
|||||||
@@ -192,6 +192,14 @@ finish_step 1
|
|||||||
begin_step 2
|
begin_step 2
|
||||||
cp "$CERT_DIR/pikit-ca.crt" "$WEB_ASSETS/pikit-ca.crt"
|
cp "$CERT_DIR/pikit-ca.crt" "$WEB_ASSETS/pikit-ca.crt"
|
||||||
chmod 644 "$WEB_ASSETS/pikit-ca.crt"
|
chmod 644 "$WEB_ASSETS/pikit-ca.crt"
|
||||||
|
if command -v sha256sum >/dev/null 2>&1; then
|
||||||
|
sha256sum "$WEB_ASSETS/pikit-ca.crt" | awk '{print $1}' > "$WEB_ASSETS/pikit-ca.sha256"
|
||||||
|
elif command -v openssl >/dev/null 2>&1; then
|
||||||
|
openssl dgst -sha256 "$WEB_ASSETS/pikit-ca.crt" | awk '{print $2}' > "$WEB_ASSETS/pikit-ca.sha256"
|
||||||
|
fi
|
||||||
|
if [ -s "$WEB_ASSETS/pikit-ca.sha256" ]; then
|
||||||
|
chmod 644 "$WEB_ASSETS/pikit-ca.sha256"
|
||||||
|
fi
|
||||||
if command -v systemctl >/dev/null 2>&1; then
|
if command -v systemctl >/dev/null 2>&1; then
|
||||||
systemctl reload nginx || systemctl restart nginx
|
systemctl reload nginx || systemctl restart nginx
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user