From 737ae1119f2b8c0054ffa4e33969acb4515d05f9 Mon Sep 17 00:00:00 2001 From: Filippo Valsorda Date: Sun, 15 Dec 2024 17:52:20 +0100 Subject: [PATCH] cmd/sunlight: print a helpful error if the inception date is wrong Fixes #26 --- cmd/sunlight/main.go | 6 +++++- internal/ctlog/ctlog.go | 4 ++++ internal/ctlog/dynamodb.go | 3 +-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cmd/sunlight/main.go b/cmd/sunlight/main.go index ec98dac..d9b047e 100644 --- a/cmd/sunlight/main.go +++ b/cmd/sunlight/main.go @@ -26,6 +26,7 @@ import ( "encoding/base64" "encoding/json" "encoding/pem" + "errors" "flag" "io" "log/slog" @@ -434,7 +435,10 @@ func main() { } l, err := ctlog.LoadLog(ctx, cc) - if err != nil { + if errors.Is(err, ctlog.ErrLogNotFound) { + fatalError(logger, "log not found, but today is not the Inception date", + "today", time.Now().Format(time.DateOnly), "inception", lc.Inception) + } else if err != nil { fatalError(logger, "failed to load log", "err", err) } defer l.CloseCache() diff --git a/internal/ctlog/ctlog.go b/internal/ctlog/ctlog.go index 7a757e7..ef870cf 100644 --- a/internal/ctlog/ctlog.go +++ b/internal/ctlog/ctlog.go @@ -392,6 +392,8 @@ var optsStaging = &UploadOptions{Compress: true} var optsIssuer = &UploadOptions{ContentType: "application/pkix-cert", Immutable: true} var optsCheckpoint = &UploadOptions{ContentType: "text/plain; charset=utf-8"} +var ErrLogNotFound = errors.New("log not found") + // A LockBackend is a database that supports compare-and-swap operations. // // It is shared across multiple Log instances, and is used only to store the @@ -401,6 +403,8 @@ var optsCheckpoint = &UploadOptions{ContentType: "text/plain; charset=utf-8"} type LockBackend interface { // Fetch obtains the current checkpoint for a given log, as well as the data // necessary to perform a compare-and-swap operation. + // + // It must return ErrLogNotFound if the log doesn't exist. Fetch(ctx context.Context, logID [sha256.Size]byte) (LockedCheckpoint, error) // Replace uploads a new checkpoint, atomically checking that the old diff --git a/internal/ctlog/dynamodb.go b/internal/ctlog/dynamodb.go index 56e96d0..846bddd 100644 --- a/internal/ctlog/dynamodb.go +++ b/internal/ctlog/dynamodb.go @@ -3,7 +3,6 @@ package ctlog import ( "context" "crypto/sha256" - "errors" "fmt" "log/slog" "net/http" @@ -91,7 +90,7 @@ func (b *DynamoDBBackend) Fetch(ctx context.Context, logID [sha256.Size]byte) (L return nil, err } if resp.Item == nil { - return nil, errors.New("checkpoint not found") + return nil, ErrLogNotFound } return &dynamoDBCheckpoint{logID: logID, body: resp.Item["checkpoint"].(*types.AttributeValueMemberB).Value}, nil