package api import ( "errors" "io" "net/http" "slices" "strings" "time" "github.com/gin-gonic/gin" "github.com/jackc/pgx/v5" "git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi" "git.zb-server.de/ZB-Server/uptimesebi/internal/store" "git.zb-server.de/ZB-Server/uptimesebi/internal/tenant" ) const ( maxStorageValue = 1 << 20 storageLockTTL = 2 * time.Minute ) // storageOwner namespaces storage per agent owner: an organization's status // page servers never see platform keys or another organization's. func storageOwner(c *gin.Context) string { if org := agentOf(c).OrgID; org != nil { return "org:" + org.String() } return "platform" } func storageAAD(owner, key string) []byte { return []byte(owner + "\x00" + key) } func storageKey(c *gin.Context, param string) (string, bool) { k := c.Query(param) if k == "" || len(k) > 1000 || strings.Contains(k, "\x00") { fail(c, http.StatusBadRequest, "invalid_input") return "", false } return k, true } func (s *Server) storageGet(c *gin.Context) { key, ok := storageKey(c, "key") if !ok { return } ctx := c.Request.Context() owner := storageOwner(c) var row store.StorageGetRow err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error { var err error row, err = q.StorageGet(ctx, store.StorageGetParams{Owner: owner, Key: key}) return err }) if errors.Is(err, pgx.ErrNoRows) { fail(c, http.StatusNotFound, "not_found") return } if err != nil { internalError(c, err) return } value, err := s.keys.Open(row.Value, storageAAD(owner, key)) if err != nil { internalError(c, err) return } c.Data(http.StatusOK, "application/octet-stream", value) } func (s *Server) storagePut(c *gin.Context) { key, ok := storageKey(c, "key") if !ok { return } value, err := io.ReadAll(io.LimitReader(c.Request.Body, maxStorageValue+1)) if err != nil || len(value) > maxStorageValue { fail(c, http.StatusRequestEntityTooLarge, "invalid_input") return } ctx := c.Request.Context() owner := storageOwner(c) err = s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error { return q.StoragePut(ctx, store.StoragePutParams{Owner: owner, Key: key, Value: s.keys.Seal(value, storageAAD(owner, key))}) }) if err != nil { internalError(c, err) return } c.Status(http.StatusNoContent) } func (s *Server) storageDelete(c *gin.Context) { key, ok := storageKey(c, "key") if !ok { return } ctx := c.Request.Context() var n int64 err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error { var err error n, err = q.StorageDelete(ctx, store.StorageDeleteParams{Owner: storageOwner(c), Key: key}) return err }) if err != nil { internalError(c, err) return } if n == 0 { fail(c, http.StatusNotFound, "not_found") return } c.Status(http.StatusNoContent) } func (s *Server) storageEntries(c *gin.Context, prefix string) ([]store.StorageListRow, error) { ctx := c.Request.Context() var rows []store.StorageListRow err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error { var err error rows, err = q.StorageList(ctx, store.StorageListParams{Owner: storageOwner(c), Prefix: prefix}) return err }) return rows, err } func (s *Server) storageStat(c *gin.Context) { key, ok := storageKey(c, "key") if !ok { return } ctx := c.Request.Context() var row store.StorageGetRow err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error { var err error row, err = q.StorageGet(ctx, store.StorageGetParams{Owner: storageOwner(c), Key: key}) return err }) switch { case err == nil: c.JSON(http.StatusOK, agentapi.StorageInfo{Key: key, Modified: row.Modified, Size: int64(len(row.Value)), IsTerminal: true}) return case !errors.Is(err, pgx.ErrNoRows): internalError(c, err) return } // Not a value, but possibly a directory of values. below, err := s.storageEntries(c, key) if err != nil { internalError(c, err) return } if len(below) == 0 { fail(c, http.StatusNotFound, "not_found") return } c.JSON(http.StatusOK, agentapi.StorageInfo{Key: key, IsTerminal: false}) } func (s *Server) storageList(c *gin.Context) { prefix := strings.TrimSuffix(c.Query("prefix"), "/") recursive := c.Query("recursive") == "true" rows, err := s.storageEntries(c, prefix) if err != nil { internalError(c, err) return } keys := []string{} for _, r := range rows { k := r.Key if !recursive { rest := strings.TrimPrefix(k, prefix) rest = strings.TrimPrefix(rest, "/") first, _, _ := strings.Cut(rest, "/") k = first if prefix != "" { k = prefix + "/" + first } } if !slices.Contains(keys, k) { keys = append(keys, k) } } c.JSON(http.StatusOK, gin.H{"keys": keys}) } func lockKey(c *gin.Context, name string) string { return "storage-lock:" + storageOwner(c) + ":" + name } // storageLock tries once; agents retry until they get the lock. func (s *Server) storageLock(c *gin.Context) { name, ok := storageKey(c, "name") if !ok { return } got, err := s.rdb.SetNX(c.Request.Context(), lockKey(c, name), "1", storageLockTTL).Result() if err != nil { internalError(c, err) return } if !got { fail(c, http.StatusConflict, "locked") return } c.Status(http.StatusNoContent) } func (s *Server) storageUnlock(c *gin.Context) { name, ok := storageKey(c, "name") if !ok { return } if err := s.rdb.Del(c.Request.Context(), lockKey(c, name)).Err(); err != nil { internalError(c, err) return } c.Status(http.StatusNoContent) }