Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions vindex/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func DomainToCanonicalKey(domain string) (string, error) {
}

labels := strings.Split(string(buf), ".")
if len(labels) < 2 {
return "", fmt.Errorf("%w: domain %q must have at least two labels", ErrBadRequest, domain)
}
for _, lbl := range labels {
if lbl == "" || len(lbl) > 63 {
return "", fmt.Errorf("%w: empty or overlong DNS label in %q", ErrBadRequest, domain)
Expand All @@ -138,6 +141,17 @@ func DomainToCanonicalKey(domain string) (string, error) {
}
}
}
tld := labels[len(labels)-1]
allNumericTLD := true
for i := 0; i < len(tld); i++ {
if tld[i] < '0' || tld[i] > '9' {
allNumericTLD = false
break
}
}
if allNumericTLD {
return "", fmt.Errorf("%w: domain %q has all-numeric TLD", ErrBadRequest, domain)
}

var b strings.Builder
b.Grow(totalLen + 1)
Expand Down Expand Up @@ -362,6 +376,9 @@ func verifyKeysBody(rawBody []byte, target string, isPrefix bool, cfg Config, cl
if bindSec.InputSize == 0 && bindSec.InputRoot != wire.EmptyRoot() {
return nil, fmt.Errorf("%w: non-empty input-root at input_size 0", ErrVerificationFailed)
}
if bindSec.InputSize > 0 && bindSec.InputRoot == ([sha256.Size]byte{}) {
return nil, fmt.Errorf("%w: zero input-root at input_size %d", ErrVerificationFailed, bindSec.InputSize)
}
if bindSec.InputSize == bindSec.From && (bindSec.MPTRoot != wire.EmptyRoot() || len(keysSec.Matches) > 0) {
return nil, fmt.Errorf("%w: non-empty MPT state when input_size (%d) == from (%d)", ErrVerificationFailed, bindSec.InputSize, bindSec.From)
}
Expand Down
3 changes: 3 additions & 0 deletions vindex/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ func TestDomainToCanonicalKey(t *testing.T) {
"-leading.example.com",
"trailing-.example.com",
"\u00a0example.com", // Non-ASCII NBSP must be rejected by IDNA2008, not stripped as whitespace
"localhost",
"*.com",
"192.0.2.1",
overlongLabel,
overlongDomain,
} {
Expand Down
14 changes: 14 additions & 0 deletions vindex/internal/auditor/auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ type Verifier struct {
stateMu sync.RWMutex
isHalted bool
haltErr error
metricsAddr string
verifiedOutputSize uint64
verifiedInputSize uint64
lastVerifiedRoot [sha256.Size]byte
Expand Down Expand Up @@ -401,10 +402,12 @@ func New(cfg Config) (*Verifier, error) {
}

metrics.ResetAuditorRootMismatch()
metrics.ResetAuditorHalted()
return v, nil
}

func (v *Verifier) halt(err error) error {
metrics.SetAuditorHalted(1)
v.stateMu.Lock()
v.isHalted = true
v.haltErr = err
Expand Down Expand Up @@ -1059,6 +1062,7 @@ func (v *Verifier) commitInMemoryWatermarks(

func (v *Verifier) updateMetrics(nextOutputSize, targetInputSize uint64, latestInCP *log.Checkpoint) {
metrics.ResetAuditorRootMismatch()
metrics.ResetAuditorHalted()
metrics.OutputTreeSize.Set(float64(nextOutputSize))
metrics.ServingTreeSize.Set(float64(targetInputSize))
metrics.KVCommittedSize.Set(float64(targetInputSize))
Expand All @@ -1069,6 +1073,13 @@ func (v *Verifier) updateMetrics(nextOutputSize, targetInputSize uint64, latestI
}
}

// MetricsAddr returns the bound operational TCP address once Run has opened MetricsAddr.
func (v *Verifier) MetricsAddr() string {
v.stateMu.RLock()
defer v.stateMu.RUnlock()
return v.metricsAddr
}

// Run executes the periodic polling loop and optionally serves operational endpoints on MetricsAddr.
func (v *Verifier) Run(ctx context.Context) error {
g, ctx := errgroup.WithContext(ctx)
Expand All @@ -1079,6 +1090,9 @@ func (v *Verifier) Run(ctx context.Context) error {
return fmt.Errorf("auditor admin listener failed on %s: %w", v.cfg.MetricsAddr, err)
}
actualAddr := ln.Addr().String()
v.stateMu.Lock()
v.metricsAddr = actualAddr
v.stateMu.Unlock()
mServer := server.NewHTTPServer(actualAddr, v.AdminHandler())
g.Go(func() error {
klog.Infof("Serving auditor metrics at http://%s/metrics", actualAddr)
Expand Down
24 changes: 23 additions & 1 deletion vindex/internal/coordinator/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,14 @@ func (c *Coordinator) recoverGenesisMPT(ctx context.Context, treeSize uint64, tr

// Recover restores the MPT and ServingState after startup or an unclean shutdown.
func (c *Coordinator) Recover(ctx context.Context) error {
if fErr := c.FatalError(); fErr != nil {
return fErr
}
c.syncMu.Lock()
defer c.syncMu.Unlock()
if fErr := c.FatalError(); fErr != nil {
return fErr
}

rawOutCP, outCP, err := c.readVerifiedOutputCheckpoint(ctx)
if err != nil {
Expand Down Expand Up @@ -569,8 +575,14 @@ func (c *Coordinator) reconcilePublisherMetadata(tipLeaf *wire.OutputLogLeaf) er
// SyncOnce fetches the latest checkpoint from the Input Log, verifies append-only consistency,
// indexes missing leaf batches, and publishes the resulting MPT state commitment to the Output Log.
func (c *Coordinator) SyncOnce(ctx context.Context) error {
if fErr := c.FatalError(); fErr != nil {
return fErr
}
c.syncMu.Lock()
defer c.syncMu.Unlock()
if fErr := c.FatalError(); fErr != nil {
return fErr
}

if c.fetcher == nil {
return nil
Expand Down Expand Up @@ -987,6 +999,14 @@ func (c *Coordinator) RecordFatalError(err error) {
}
}

// FatalError returns the recorded fatal error if the coordinator has permanently halted, or nil otherwise.
func (c *Coordinator) FatalError() error {
if fErr := c.fatalErr.Load(); fErr != nil && *fErr != nil {
return *fErr
}
return nil
}

// RunPollLoop enters the periodic ingestion polling loop until ctx is canceled or a fatal error occurs.
func (c *Coordinator) RunPollLoop(ctx context.Context, pollInterval time.Duration) error {
if pollInterval <= 0 {
Expand Down Expand Up @@ -1018,7 +1038,9 @@ func (c *Coordinator) RunPollLoop(ctx context.Context, pollInterval time.Duratio
func (c *Coordinator) Run(ctx context.Context, pollInterval time.Duration) error {
if err := c.Recover(ctx); err != nil {
fErr := fmt.Errorf("startup recovery failed: %w", err)
c.RecordFatalError(fErr)
if ctx.Err() == nil || IsFatalSyncError(err) {
c.RecordFatalError(fErr)
}
return fErr
}
if err := c.SyncOnce(ctx); err != nil {
Expand Down
12 changes: 10 additions & 2 deletions vindex/internal/ingest/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"path/filepath"
"strings"
"sync"

"github.com/transparency-dev/incubator/vindex/internal/metrics"
)

// maxInMemoryBundles bounds the in-memory fallback cache when cacheDir is empty.
Expand All @@ -47,11 +49,17 @@ func NewManagedTileCache(cacheDir string, bundleSz uint64) (*ManagedTileCache, e
return nil, fmt.Errorf("failed to create tile cache dir %q: %w", cacheDir, err)
}
}
return &ManagedTileCache{
c := &ManagedTileCache{
cacheDir: cacheDir,
memory: make(map[uint64]*LeafBundle),
bundleSz: bundleSz,
}, nil
}
if cacheDir != "" {
if sz, err := c.DirSize(); err == nil {
metrics.TileCacheBytes.Set(float64(sz))
}
}
return c, nil
}

// bundlePath returns the sharded filepath for a bundle index.
Expand Down
124 changes: 66 additions & 58 deletions vindex/internal/ingest/deref.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"errors"
"fmt"
"math"
"net/http"
"net/url"
"os"
Expand All @@ -40,18 +41,21 @@ type bundleCandidate struct {
sumDB bool
}

// FetchVerifiedEntryByIndex fetches the entry at idx from baseURL and, when treeSize > 0 and treeRoot is non-zero,
// cryptographically authenticates the entry against (treeSize, treeRoot) via an RFC 6962 inclusion proof.
// FetchVerifiedEntryByIndex fetches the entry at idx from baseURL and cryptographically
// authenticates the entry against (treeSize, treeRoot) via an RFC 6962 inclusion proof.
func FetchVerifiedEntryByIndex(ctx context.Context, httpClient *http.Client, baseURL string, idx, treeSize uint64, treeRoot [32]byte) ([]byte, error) {
base := strings.TrimSpace(baseURL)
if base == "" {
return nil, errors.New("empty baseURL")
}
if treeSize > 0 && idx >= treeSize {
if treeSize == 0 {
return nil, errors.New("cannot verify entry inclusion against tree size 0")
}
if idx >= treeSize {
return nil, fmt.Errorf("entry index %d out of bounds for tree size %d", idx, treeSize)
}
if treeRoot != ([32]byte{}) && treeSize == 0 {
return nil, errors.New("cannot verify entry inclusion against tree size 0")
if treeRoot == ([32]byte{}) {
return nil, errors.New("cannot verify entry inclusion against zero treeRoot")
}

u, err := url.Parse(base)
Expand All @@ -60,6 +64,9 @@ func FetchVerifiedEntryByIndex(ctx context.Context, httpClient *http.Client, bas
}

bIdx := idx / uint64(layout.EntryBundleWidth)
if bIdx > math.MaxInt64 {
return nil, fmt.Errorf("bundle index %d overflows int64", bIdx)
}
offset := int(idx % uint64(layout.EntryBundleWidth))
p := layout.PartialTileSize(0, bIdx, treeSize)
isSumDB := strings.Contains(base, "sum.golang.org")
Expand Down Expand Up @@ -142,68 +149,69 @@ func FetchVerifiedEntryByIndex(ctx context.Context, httpClient *http.Client, bas
return nil, fmt.Errorf("entry bundle for index %d not found at %s", idx, baseURL)
}

if treeSize > 0 && treeRoot != ([32]byte{}) {
readTile := func(ctx context.Context, level, index uint64, p uint8) ([]byte, error) {
var tileCandidates []string
addSumDBTiles := func() {
w := int(p)
if w == 0 {
w = 256
}
tileCandidates = append(tileCandidates, tlog.Tile{H: 8, L: int(level), N: int64(index), W: w}.Path())
if p != 0 {
tileCandidates = append(tileCandidates, tlog.Tile{H: 8, L: int(level), N: int64(index), W: 256}.Path())
}
}
if usedSumDB {
addSumDBTiles()
readTile := func(ctx context.Context, level, index uint64, p uint8) ([]byte, error) {
if level > 63 || index > math.MaxInt64 {
return nil, fmt.Errorf("tile coordinates (level=%d, index=%d) overflow signed bounds", level, index)
}
var tileCandidates []string
addSumDBTiles := func() {
w := int(p)
if w == 0 {
w = 256
}
tileCandidates = append(tileCandidates, layout.TilePath(level, index, p))
tileCandidates = append(tileCandidates, tlog.Tile{H: 8, L: int(level), N: int64(index), W: w}.Path())
if p != 0 {
tileCandidates = append(tileCandidates, layout.TilePath(level, index, 0))
tileCandidates = append(tileCandidates, tlog.Tile{H: 8, L: int(level), N: int64(index), W: 256}.Path())
}
if !usedSumDB {
addSumDBTiles()
}
if usedSumDB {
addSumDBTiles()
}
tileCandidates = append(tileCandidates, layout.TilePath(level, index, p))
if p != 0 {
tileCandidates = append(tileCandidates, layout.TilePath(level, index, 0))
}
if !usedSumDB {
addSumDBTiles()
}
wantBytes := 256 * 32
if p != 0 {
wantBytes = int(p) * 32
}
var tileErr error
for _, tc := range tileCandidates {
data, ok, err := readBundleResource(ctx, httpClient, u, tc)
if err != nil {
tileErr = err
continue
}
wantBytes := 256 * 32
if p != 0 {
wantBytes = int(p) * 32
if !ok {
continue
}
var tileErr error
for _, tc := range tileCandidates {
data, ok, err := readBundleResource(ctx, httpClient, u, tc)
if err != nil {
tileErr = err
continue
}
if !ok {
continue
}
if len(data) < wantBytes {
tileErr = fmt.Errorf("tile %s too short: got %d bytes, want at least %d", tc, len(data), wantBytes)
continue
}
if len(data) > wantBytes {
data = data[:wantBytes]
}
return data, nil
if len(data) < wantBytes {
tileErr = fmt.Errorf("tile %s too short: got %d bytes, want at least %d", tc, len(data), wantBytes)
continue
}
if tileErr != nil {
return nil, tileErr
if len(data) > wantBytes {
data = data[:wantBytes]
}
return nil, fmt.Errorf("hash tile level=%d index=%d p=%d not found at %s", level, index, p, baseURL)
}
pb, err := tclient.NewProofBuilder(ctx, treeSize, readTile)
if err != nil {
return nil, fmt.Errorf("failed to initialize inclusion proof builder for entry %d: %w", idx, err)
}
incProof, err := pb.InclusionProof(ctx, idx)
if err != nil {
return nil, fmt.Errorf("failed to build inclusion proof for entry %d: %w", idx, err)
return data, nil
}
if err := proof.VerifyInclusion(rfc6962.DefaultHasher, idx, treeSize, leafHash, incProof, treeRoot[:]); err != nil {
return nil, fmt.Errorf("entry %d failed Merkle inclusion verification against input root %x: %w", idx, treeRoot, err)
if tileErr != nil {
return nil, tileErr
}
return nil, fmt.Errorf("hash tile level=%d index=%d p=%d not found at %s", level, index, p, baseURL)
}
pb, err := tclient.NewProofBuilder(ctx, treeSize, readTile)
if err != nil {
return nil, fmt.Errorf("failed to initialize inclusion proof builder for entry %d: %w", idx, err)
}
incProof, err := pb.InclusionProof(ctx, idx)
if err != nil {
return nil, fmt.Errorf("failed to build inclusion proof for entry %d: %w", idx, err)
}
if err := proof.VerifyInclusion(rfc6962.DefaultHasher, idx, treeSize, leafHash, incProof, treeRoot[:]); err != nil {
return nil, fmt.Errorf("entry %d failed Merkle inclusion verification against input root %x: %w", idx, treeRoot, err)
}

return entry, nil
Expand Down
Loading
Loading