feat: implement systemd drop-in override for LocalAGI service hardening

This commit is contained in:
John Doe
2026-03-04 20:43:45 -05:00
parent 92e3192cb7
commit a6f13f3e30
2 changed files with 33 additions and 35 deletions

View File

@@ -68,21 +68,14 @@ function update_script() {
msg_info "Setting ownership of /opt/localagi to localagi:localagi" msg_info "Setting ownership of /opt/localagi to localagi:localagi"
chown -R localagi:localagi /opt/localagi || msg_warn "Failed to chown /opt/localagi" chown -R localagi:localagi /opt/localagi || msg_warn "Failed to chown /opt/localagi"
# Ensure systemd unit has basic hardening; if not, rewrite it # Ensure systemd unit has basic hardening via drop-in override
if ! grep -q '^User=localagi' /etc/systemd/system/localagi.service 2>/dev/null || \ mkdir -p /etc/systemd/system/localagi.service.d
! grep -q '^NoNewPrivileges=true' /etc/systemd/system/localagi.service 2>/dev/null; then override_file=/etc/systemd/system/localagi.service.d/override.conf
msg_info "Installing hardened systemd unit for LocalAGI" if [[ ! -f "$override_file" ]]; then
cat <<EOF >/etc/systemd/system/localagi.service msg_info "Creating systemd drop-in override for LocalAGI"
[Unit] cat <<EOF >"$override_file"
Description=LocalAGI Service
After=network.target
[Service] [Service]
Type=simple
WorkingDirectory=/opt/localagi
EnvironmentFile=/opt/localagi/.env
User=localagi User=localagi
ExecStart=/usr/local/bin/localagi
NoNewPrivileges=true NoNewPrivileges=true
PrivateTmp=true PrivateTmp=true
ProtectSystem=full ProtectSystem=full
@@ -90,14 +83,17 @@ ProtectHome=true
AmbientCapabilities= AmbientCapabilities=
StandardOutput=journal StandardOutput=journal
StandardError=journal StandardError=journal
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF EOF
systemctl daemon-reload systemctl daemon-reload
msg_ok "Installed systemd unit" msg_ok "Installed systemd drop-in"
else
msg_info "Systemd drop-in exists; ensuring required directives"
for d in "User=localagi" "NoNewPrivileges=true" "PrivateTmp=true" "ProtectSystem=full" "ProtectHome=true" "AmbientCapabilities=" "StandardOutput=journal" "StandardError=journal"; do
if ! grep -q "^${d}" "$override_file" 2>/dev/null; then
echo "$d" >>"$override_file"
fi
done
systemctl daemon-reload
fi fi
if [[ "${env_backup_valid:-0}" == "1" && -n "${env_backup:-}" && -s "$env_backup" ]]; then if [[ "${env_backup_valid:-0}" == "1" && -n "${env_backup:-}" && -s "$env_backup" ]]; then

View File

@@ -76,17 +76,13 @@ chmod 755 /usr/local/bin/localagi || msg_warn "Failed to chmod /usr/local/bin/lo
msg_ok "Built LocalAGI from source" msg_ok "Built LocalAGI from source"
msg_info "Creating Service" msg_info "Creating Service"
cat <<EOF >/etc/systemd/system/localagi.service mkdir -p /etc/systemd/system/localagi.service.d
[Unit] override_file=/etc/systemd/system/localagi.service.d/override.conf
Description=LocalAGI Service if [[ ! -f "$override_file" ]]; then
After=network.target msg_info "Creating systemd drop-in override for LocalAGI"
cat <<EOF >"$override_file"
[Service] [Service]
Type=simple
WorkingDirectory=/opt/localagi
EnvironmentFile=/opt/localagi/.env
User=localagi User=localagi
ExecStart=/usr/local/bin/localagi
NoNewPrivileges=true NoNewPrivileges=true
PrivateTmp=true PrivateTmp=true
ProtectSystem=full ProtectSystem=full
@@ -94,16 +90,22 @@ ProtectHome=true
AmbientCapabilities= AmbientCapabilities=
StandardOutput=journal StandardOutput=journal
StandardError=journal StandardError=journal
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF EOF
systemctl daemon-reload systemctl daemon-reload
else
msg_info "Systemd drop-in exists; ensuring required directives"
# Ensure required directives present; add if missing
for d in "User=localagi" "NoNewPrivileges=true" "PrivateTmp=true" "ProtectSystem=full" "ProtectHome=true" "AmbientCapabilities=" "StandardOutput=journal" "StandardError=journal"; do
if ! grep -q "^${d}" "$override_file" 2>/dev/null; then
echo "$d" >>"$override_file"
fi
done
systemctl daemon-reload
fi
LOCALAGI_SERVICE_NEEDS_RECOVERY=1 LOCALAGI_SERVICE_NEEDS_RECOVERY=1
systemctl enable -q --now localagi systemctl enable -q --now localagi
msg_ok "Created Service" msg_ok "Created Service (drop-in override)"
if ! systemctl is-active -q localagi; then if ! systemctl is-active -q localagi; then
msg_error "Failed to start LocalAGI service" msg_error "Failed to start LocalAGI service"