Files
silo-server/internal/diagnostics/admin.go
T
Quick104andClaude Fable 5 dee46f9398 fix(diagnostics): address round-5 review findings on PR #445
- service: reject supplied child-profile attribution with a distinct
  ErrChildProfileForbidden (403 child_profile_forbidden) instead of
  silently dropping it as if the profile were not found; a profile that
  is simply not the user's still drops attribution unchanged
- repo: add a manifest-free list projection (reportListSelectSQL /
  scanReportSummary) for admin list and retention/stale cleanup queries
  so they no longer drag the full manifest JSONB per row; keep the full
  projection for GetByID/DeleteByID and mark Manifest omitempty
- cleanup: delete/mark the DB row before the blob in retention and stale
  loops so a mid-run DB failure can't leave a ready report pointing at a
  missing bundle; blob-delete failures are logged with bucket/keys for
  orphan cleanup to reap rather than aborting the run (shared helper with
  the admin DeleteReport path)
- admin: reject diagnostics settings where max_bytes_per_user would fall
  below max_bundle_bytes (and the reciprocal), which would make every
  max-size upload fail quota
- router/demo: route POST /diagnostics/reports through DemoGuard and block
  the reports prefix in demo mode while keeping GET /diagnostics/status
  available

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012e3QjbPo96ed9Mn2qRiUkh
2026-07-21 13:36:08 -04:00

122 lines
3.2 KiB
Go

package diagnostics
import (
"context"
"errors"
"fmt"
"io"
"strings"
"time"
)
const ReportDownloadContentType = BundleContentType
var ErrReportNotReady = errors.New("diagnostic report is not ready")
func (s *Service) ListForAdmin(ctx context.Context, filters ListFilters) (ListResult, error) {
if s.reports == nil {
return ListResult{}, ErrReportStoreUnavailable
}
return s.reports.ListForAdmin(ctx, filters)
}
func (s *Service) GetReport(ctx context.Context, id string) (*Report, error) {
if s.reports == nil {
return nil, ErrReportStoreUnavailable
}
return s.reports.GetByID(ctx, id)
}
func (s *Service) PresignReportDownload(ctx context.Context, report *Report, expiry time.Duration) (string, error) {
bucket, key, err := s.readyReportBlobLocation(report)
if err != nil {
return "", err
}
if expiry <= 0 {
expiry = 15 * time.Minute
}
return s.store.PresignGetURL(ctx, bucket, key, expiry)
}
func (s *Service) EffectiveReportDownloadTTL(requested time.Duration) time.Duration {
if requested <= 0 {
return requested
}
effectiveStore, ok := s.store.(interface {
EffectivePresignTTL(time.Duration) time.Duration
})
if !ok {
return requested
}
effective := effectiveStore.EffectivePresignTTL(requested)
if effective <= 0 {
return requested
}
return effective
}
func (s *Service) OpenReportDownload(ctx context.Context, report *Report) (io.ReadCloser, error) {
bucket, key, err := s.readyReportBlobLocation(report)
if err != nil {
return nil, err
}
return s.store.GetObject(ctx, bucket, key)
}
func (s *Service) DeleteReport(ctx context.Context, id string) (*Report, error) {
if s.reports == nil {
return nil, ErrReportStoreUnavailable
}
// Delete the row before the blob: a DB failure after the object is gone
// would otherwise leave a visible report whose bundle can no longer be
// downloaded. DeleteByID returns the deleted row (and ErrNotFound when
// absent), so the blob location is captured from it. If the blob delete
// fails the row is already gone, so log the bucket/key for an operator (and
// the orphan reconciler) to reap instead of failing the delete.
deleted, err := s.reports.DeleteByID(ctx, id)
if err != nil {
return nil, err
}
if err := deleteReportObjects(ctx, s.store, deleted, s.logger); err != nil {
logDeferredBlobDeletion(ctx, s.logger, s.store, deleted, err)
}
return deleted, nil
}
func reportBlobBucket(report *Report, store ObjectStore) string {
bucket := stringValue(report.BlobBucket)
if bucket == "" && store != nil {
bucket = store.Bucket()
}
return bucket
}
func (s *Service) readyReportBlobLocation(report *Report) (string, string, error) {
if report == nil {
return "", "", ErrNotFound
}
if report.State != StateReady {
return "", "", ErrReportNotReady
}
if s.store == nil || strings.TrimSpace(s.store.Bucket()) == "" {
return "", "", ErrStorageUnavailable
}
bucket := stringValue(report.BlobBucket)
if bucket == "" {
bucket = s.store.Bucket()
}
key := stringValue(report.BlobKey)
if key == "" {
return "", "", fmt.Errorf("%w: diagnostic report has no blob key", ErrStorageUnavailable)
}
return bucket, key, nil
}
func stringValue(value *string) string {
if value == nil {
return ""
}
return strings.TrimSpace(*value)
}