Skip to content

Commit

Permalink
have monitor use an interface for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
aditsachde committed Jul 29, 2024
1 parent e14330b commit 092e5f3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 31 deletions.
37 changes: 8 additions & 29 deletions internal/ctmonitor/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,29 @@ import (
"encoding/binary"
"encoding/json"
"errors"
"io"
"net/http"

ct "github.com/google/certificate-transparency-go"
"golang.org/x/mod/sumdb/tlog"
"itko.dev/internal/sunlight"
)

type Fetch struct {
urlPrefix string
maskSize int
s Storage
maskSize int
}

func newFetch(urlPrefix string, maskSize int) Fetch {
func newFetch(storage Storage, maskSize int) Fetch {
return Fetch{
urlPrefix: urlPrefix,
maskSize: maskSize,
s: storage,
maskSize: maskSize,
}
}

func (f *Fetch) get(ctx context.Context, key string) ([]byte, error) {
resp, err, _ := f.getWithStatus(ctx, key)
resp, _, err := f.s.Get(ctx, key)
return resp, err
}

func (f *Fetch) getWithStatus(ctx context.Context, key string) ([]byte, error, int) {
req, err := http.NewRequestWithContext(ctx, "GET", f.urlPrefix+key, nil)
if err != nil {
return nil, err, 500
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err, 500
}
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status), resp.StatusCode
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err, 500
}
return body, nil, 200
}

func (f *Fetch) getSth(ctx context.Context) (ct.SignedTreeHead, error) {
sthBytes, err := f.get(ctx, "ct/v1/get-sth")
if err != nil {
Expand All @@ -66,9 +45,9 @@ func (f *Fetch) getSth(ctx context.Context) (ct.SignedTreeHead, error) {
func (f *Fetch) getTile(ctx context.Context, tile tlog.Tile) ([]byte, error) {
fallbackWidth := tile.W
tile.W = sunlight.TileWidth
resp, err, status := f.getWithStatus(ctx, tile.Path())
resp, notfound, err := f.s.Get(ctx, tile.Path())
// In case the tile is not found, try to fetch the partial tile
if status == 404 {
if notfound == true {
if fallbackWidth != sunlight.TileWidth {
tile.W = fallbackWidth
return f.get(ctx, tile.Path())
Expand Down
3 changes: 2 additions & 1 deletion internal/ctmonitor/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (

// TODO: Evaluate if the context is actually needed
func Start(ctx context.Context, tileStoreUrl string, maskSize int) (http.Handler, error) {
f := newFetch(tileStoreUrl, maskSize)
storage := &UrlStorage{urlPrefix: tileStoreUrl}
f := newFetch(storage, maskSize)

// Wrap the HTTP handler function with OTel instrumentation
wGetSth := otelhttp.NewHandler(http.HandlerFunc(wrapper(f.get_sth)), "get-sth")
Expand Down
41 changes: 41 additions & 0 deletions internal/ctmonitor/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ctmonitor

import (
"context"
"errors"
"io"
"net/http"
)

type Storage interface {
Get(ctx context.Context, key string) (data []byte, found bool, err error)
}

// ------------------------------------------------------------

type UrlStorage struct {
urlPrefix string
}

func (f *UrlStorage) Get(ctx context.Context, key string) (data []byte, notfounderr bool, err error) {
req, err := http.NewRequestWithContext(ctx, "GET", f.urlPrefix+key, nil)
if err != nil {
return nil, false, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, false, err
}
if resp.StatusCode != 200 {
if resp.StatusCode == 404 {
return nil, true, errors.New(resp.Status)
} else {
return nil, false, errors.New(resp.Status)
}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, false, err
}
return body, false, nil
}
2 changes: 1 addition & 1 deletion internal/ctsubmit/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func LoadLog(ctx context.Context, kvpath, consulAddress string) (*Log, error) {
stageOneCommChan := make(chan UnsequencedEntryWithReturnPath, 200)
stageTwoCommChan := make(chan []LogEntryWithReturnPath, 2)
s3Storage := NewS3Storage(gc.S3Region, gc.S3Bucket, gc.S3EndpointUrl, gc.S3StaticCredentialUserName, gc.S3StaticCredentialPassword)
bucket := Bucket{&s3Storage}
bucket := Bucket{S: &s3Storage}

// Get the latest STH
var sth ct.SignedTreeHead
Expand Down

0 comments on commit 092e5f3

Please sign in to comment.