Skip to content

Add report-only S3 carve orphan scan - #1276

Merged
javuto merged 1 commit into
developfrom
s3-scanned-orphans
Nov 25, 2026
Merged

javuto merged 1 commit into
developfrom
s3-scanned-orphans

Conversation

@javuto

@javuto javuto commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Add report-only S3 carve orphan scan

Summary

Adds a read-only scan for S3 carve objects that no longer have matching metadata. It is available through:

  • GET /api/v1/carves/{env}/orphans
  • osctrl-cli carve orphans --env <env> [--grace <seconds>]

The scan lists objects under the environment prefix, compares them with carved_blocks and carved_files references, and reports orphaned keys. It never deletes objects.

Closes #1260.

Motivation

Retention deletes stored carve objects before deleting their metadata, so a normal sweep does not create orphans. Orphans can still exist after:

  • a crash between object deletion and row deletion
  • a partial S3 failure
  • manual database cleanup
  • an interrupted or older deployment operation
  • a shared bucket containing objects from another deployment

Without a reference-based scan, those objects consume bucket space indefinitely.

Changes

Carve orphan detection

  • Added pkg/carves/orphans.go.
    • FindOrphans(ctx, environment, olderThan) lists S3 objects under the environment prefix.
    • Collects referenced keys from:
      • carved_blocks.data
      • carved_files.archive_path
    • Reports objects whose keys are not referenced.
    • Ignores objects newer than the grace period so in-flight uploads are not reported.
    • Returns a deterministic, key-sorted report.
  • Added CarverS3.ListObjects(ctx, prefix) using paginated ListObjectsV2, with size and last-modified metadata.

API

  • Added GET /api/v1/carves/{env}/orphans.
  • Requires environment admin access.
  • Supports grace_seconds:
    • default 3600
    • maximum 604800 (7 days)
  • Returns 409 Conflict when the configured carver is not S3.
  • Response shape:
{
  "environment": "production",
  "scanned": 1240,
  "referenced": 1237,
  "orphans": [
    {
      "key": "production:node-uuid:session-id:17",
      "size": 1048576,
      "last_modified": "2026-11-20T10:15:00Z"
    }
  ]
}

CLI

Added:

osctrl-cli carve orphans --env production --grace 3600
  • API mode only; S3 credentials live in osctrl-api.
  • Supports the existing json, csv, and pretty output formats.
  • Added OsctrlAPI.GetCarveOrphans to the REST client.

Documentation and OpenAPI

  • Documented the scan in ARCHITECTURE.md under Data Retention.
  • Linked it from README.md.
  • Regenerated osctrl-api.yaml; make openapi-check passes.

Safety and limitations

  • The scan is report-only. There is no deletion endpoint or CLI flag in this PR.
  • Objects are scoped by the environment prefix (<environment>:).
  • In-flight uploads are protected by the grace period.
  • Soft-deleted carve rows still count as metadata, avoiding false positives for intentionally retained carves.
  • The scan supports the S3 carver only. DB blocks and local archives are already covered by their metadata path.
  • Shared buckets used by multiple deployments with the same environment name can still produce false positives; those are safe because the scan never deletes.

Tests

  • Non-S3 carver returns ErrOrphanScanUnsupported.
  • Referenced block and archive keys are loaded correctly.
  • Orphan classification honors references, grace periods, and deterministic ordering.
  • API client sends grace_seconds and decodes the report.
  • Existing carve, API, CLI, and OpenAPI tests continue to pass.

Validation

  • go test ./... — passed.
  • make openapi-check — passed.
  • git diff --check — clean.

Follow-ups

  • Optional orphan deletion behind an explicit confirmation flag and audit entry.
  • Metrics for orphan object count and bytes.
  • Bucket-wide scan mode for operators who want orphan reporting outside a single environment prefix.

@javuto javuto added ✨ enhancement New feature or request carves File carving related issues backend Backend related issues 🗄️ logging Logging related issues 🎅🏼 santa Santa related issues labels Nov 25, 2026
@javuto
javuto merged commit 40e2c6b into develop Nov 25, 2026
8 checks passed
@javuto
javuto deleted the s3-scanned-orphans branch November 25, 2026 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend related issues carves File carving related issues ✨ enhancement New feature or request 🗄️ logging Logging related issues 🎅🏼 santa Santa related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extend database retention to carves, Santa events, query metadata, and stale sessions

1 participant