New accounts confirm their email address before signing in: sign-up mails a link valid for 24 hours, and its page confirms only when the visitor presses the button, so mail scanners opening links do not sign in. Confirming signs the account in; unconfirmed accounts are told so only after giving the right password, can ask for a new link, and are deleted after a week. UPTIME_SIGNUP_VERIFY requires confirmation always in the cloud edition and self-hosted wherever mail can be sent; without a mail server, sign-up that needs it stays closed. One account per mailbox: users.email_key drops +suffixes, and dots for Gmail, and is unique, including for accounts created through invitations and SSO, which count as confirmed. A client address (IPv6: its /64) may create UPTIME_SIGNUPS_PER_IP accounts a day, 2 by default; refused sign-ups do not count. Sign-up asks only for name, email and password. Accounts without an organization are led to create one; signed-in users create further organizations from the switcher, within the per-account limit, and POST /api/v1/orgs does the same. Closes #112 Closes #113 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
137 lines
4.2 KiB
Go
137 lines
4.2 KiB
Go
package api
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"slices"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
|
||
"git.zb-server.de/ZB-Server/uptimesebi/internal/datapolicy"
|
||
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
||
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
||
"git.zb-server.de/ZB-Server/uptimesebi/internal/web"
|
||
)
|
||
|
||
func (s *Server) retention() datapolicy.Retention {
|
||
r := s.cfg.Retention
|
||
return datapolicy.Retention{Raw: r.Raw, Hourly: r.Hourly, Daily: r.Daily}
|
||
}
|
||
|
||
// RunHousekeeping aligns TimescaleDB's retention with the configuration and
|
||
// then purges rows TimescaleDB does not manage, hourly, until ctx ends.
|
||
// Every replica runs it; the deletes are idempotent.
|
||
func (s *Server) RunHousekeeping(ctx context.Context) {
|
||
if err := datapolicy.Apply(ctx, s.db.Pool(), s.retention(), s.log); err != nil {
|
||
// The API role may not own the tables; "api migrate up" applies the
|
||
// policies as owner.
|
||
s.log.WarnContext(ctx, "applying retention policies failed", "error", err)
|
||
}
|
||
tick := time.NewTicker(time.Hour)
|
||
defer tick.Stop()
|
||
for {
|
||
if err := s.Housekeep(ctx, time.Now()); err != nil && ctx.Err() == nil {
|
||
s.log.ErrorContext(ctx, "housekeeping failed", "error", err)
|
||
}
|
||
select {
|
||
case <-ctx.Done():
|
||
return
|
||
case <-tick.C:
|
||
}
|
||
}
|
||
}
|
||
|
||
// Housekeep deletes delivered notifications, stale invitations, expired API
|
||
// tokens and incidents older than the daily aggregate.
|
||
func (s *Server) Housekeep(ctx context.Context, now time.Time) error {
|
||
keep := s.cfg.Retention
|
||
var counts [7]int64
|
||
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||
var err error
|
||
if keep.Housekeeping > 0 {
|
||
before := now.Add(-keep.Housekeeping)
|
||
if counts[0], err = q.PurgeNotifications(ctx, before); err != nil {
|
||
return err
|
||
}
|
||
if counts[1], err = q.PurgeInvitations(ctx, before); err != nil {
|
||
return err
|
||
}
|
||
if counts[2], err = q.PurgeExpiredTokens(ctx, &before); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
if keep.Evidence > 0 {
|
||
if counts[5], err = q.PurgeEvidence(ctx, now.Add(-keep.Evidence)); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
if keep.Audit > 0 {
|
||
if counts[4], err = q.PurgeAuditEvents(ctx, now.Add(-keep.Audit)); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
if _, err = q.PurgeAlertmanagerAlerts(ctx, now.Add(-keepAlerts)); err != nil {
|
||
return err
|
||
}
|
||
if counts[6], err = q.PurgeUnverifiedUsers(ctx, now.Add(-unverifiedKeep)); err != nil {
|
||
return err
|
||
}
|
||
if keep.Daily > 0 {
|
||
before := now.Add(-keep.Daily)
|
||
counts[3], err = q.PurgeIncidents(ctx, &before)
|
||
}
|
||
return err
|
||
})
|
||
if err == nil && counts != [7]int64{} {
|
||
s.log.InfoContext(ctx, "housekeeping purged rows",
|
||
"notifications", counts[0], "invitations", counts[1], "api_tokens", counts[2], "incidents", counts[3], "audit_events", counts[4], "evidence", counts[5],
|
||
"unverified_accounts", counts[6])
|
||
}
|
||
return err
|
||
}
|
||
|
||
func storageView(c *gin.Context, st datapolicy.Stats) web.StorageView {
|
||
ctx := c.Request.Context()
|
||
var v web.StorageView
|
||
for _, r := range st.Relations {
|
||
l := web.StorageLevel{
|
||
Key: strings.TrimPrefix(strings.TrimPrefix(r.Name, "check_results"), "_"),
|
||
Size: web.Bytes(r.TotalBytes),
|
||
Chunks: fmt.Sprintf("%d / %d", r.CompressedChunks, r.Chunks),
|
||
Retention: web.Days(ctx, r.Retention),
|
||
CompressAfter: "–",
|
||
}
|
||
if l.Key == "" {
|
||
l.Key = "raw"
|
||
}
|
||
if r.CompressAfter > 0 {
|
||
l.CompressAfter = web.Days(ctx, r.CompressAfter)
|
||
}
|
||
if r.AfterBytes > 0 {
|
||
l.Compressed = fmt.Sprintf("%s → %s", web.Bytes(r.BeforeBytes), web.Bytes(r.AfterBytes))
|
||
}
|
||
v.Levels = append(v.Levels, l)
|
||
}
|
||
const layout = "2006-01-02 15:04"
|
||
for _, j := range st.Jobs {
|
||
sj := web.StorageJob{
|
||
Name: strings.TrimPrefix(j.Proc, "policy_"), Relation: j.Relation,
|
||
OK: j.LastStatus != "Failed", Failures: j.Failures, LastRun: "–", NextRun: "–",
|
||
}
|
||
if j.LastRun != nil {
|
||
sj.LastRun = j.LastRun.Local().Format(layout)
|
||
}
|
||
if j.NextRun != nil {
|
||
sj.NextRun = j.NextRun.Local().Format(layout)
|
||
}
|
||
v.Jobs = append(v.Jobs, sj)
|
||
}
|
||
for name, size := range st.TableBytes {
|
||
v.Tables = append(v.Tables, web.StorageTable{Name: name, Size: web.Bytes(size)})
|
||
}
|
||
slices.SortFunc(v.Tables, func(a, b web.StorageTable) int { return strings.Compare(a.Name, b.Name) })
|
||
return v
|
||
}
|