audit message when server starts and shuts down

This commit is contained in:
2026-02-21 22:05:33 -07:00
parent 6189b474d0
commit 6e06c7a3a8
5 changed files with 46 additions and 0 deletions
+2
View File
@@ -76,6 +76,8 @@ type AuditRecord struct {
// at all times! // at all times!
const ( const (
AuditPublishToFrontPage = 1 AuditPublishToFrontPage = 1
AuditStartup = 2
AuditShutdown = 3
AuditLoginOK = 101 AuditLoginOK = 101
AuditLoginFail = 102 AuditLoginFail = 102
AuditAccountCreated = 103 AuditAccountCreated = 103
+4
View File
@@ -12,6 +12,10 @@
auditReference: auditReference:
- code: 1 - code: 1
text: "Publish Message to Front Page" text: "Publish Message to Front Page"
- code: 2
text: "Server Startup"
- code: 3
text: "Server Shutdown"
- code: 101 - code: 101
text: "Login OK" text: "Login OK"
- code: 102 - code: 102
+11
View File
@@ -561,6 +561,17 @@ func AmGetAnonUser(ctx context.Context) (*User, error) {
return rc, err return rc, err
} }
// AmGetBOFH returns the user account of the global system administrator.
func AmGetBOFH(ctx context.Context) (*User, error) {
row := amdb.QueryRowContext(ctx, "SELECT uid FROM users WHERE base_lvl = ?", AmRole("Global.BOFH").Level())
var uid int32
err := row.Scan(&uid)
if err != nil {
return nil, err
}
return AmGetUser(ctx, uid)
}
// hashPassword hashes the password value. // hashPassword hashes the password value.
func hashPassword(password string) string { func hashPassword(password string) string {
if len(password) == 0 { if len(password) == 0 {
+17
View File
@@ -190,6 +190,16 @@ func main() {
closer = ui.SetupUILayer() closer = ui.SetupUILayer()
defer closer() defer closer()
// Determine my IP address and the admin user.
myIP, err := util.MyIPAddress()
if err != nil {
panic(err)
}
bofh, err := database.AmGetBOFH(context.Background())
if err != nil {
panic(err)
}
// Set up to trap SIGINT/SIGTERM and shut down gracefully // Set up to trap SIGINT/SIGTERM and shut down gracefully
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop() defer stop()
@@ -204,6 +214,10 @@ func main() {
// Set up Echo. // Set up Echo.
e := setupEcho() e := setupEcho()
// Audit the startup
database.AmStoreAudit(database.AmNewAudit(database.AuditStartup, bofh.Uid, myIP.String(),
fmt.Sprintf("version=%s", config.AMSTERDAM_VERSION)))
// Start server // Start server
go func() { go func() {
if err := e.Start(":1323"); err != nil && err != http.ErrServerClosed { if err := e.Start(":1323"); err != nil && err != http.ErrServerClosed {
@@ -218,4 +232,7 @@ func main() {
if err := e.Shutdown(ctx); err != nil { if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err) e.Logger.Fatal(err)
} }
// Audit the shutdown
database.AmStoreAudit(database.AmNewAudit(database.AuditShutdown, bofh.Uid, myIP.String()))
} }
+12
View File
@@ -11,6 +11,7 @@
package util package util
import ( import (
"net"
"regexp" "regexp"
"strings" "strings"
"time" "time"
@@ -168,3 +169,14 @@ func Map[A, B any](in []A, fn func(A) B) []B {
} }
return rc return rc
} }
// MyIPAddress returns the local IP address of this machine.
func MyIPAddress() (net.IP, error) {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return nil, err
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP, nil
}