Skip to content

Cleanup: renames and delete unused file #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 10, 2025
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
4 changes: 2 additions & 2 deletions cmd/gcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var (

httpEndpoint = flag.String("http_endpoint", "localhost:6962", "Endpoint for HTTP (host:port).")
metricsEndpoint = flag.String("metrics_endpoint", "", "Endpoint for serving metrics; if left empty, metrics will be visible on --http_endpoint.")
tesseraDeadline = flag.Duration("tessera_deadline", time.Second*10, "Deadline for Tessera requests.")
httpDeadline = flag.Duration("http_deadline", time.Second*10, "Deadline for HTTP requests.")
maskInternalErrors = flag.Bool("mask_internal_errors", false, "Don't return error strings with Internal Server Error HTTP responses.")
tracing = flag.Bool("tracing", false, "If true opencensus Stackdriver tracing will be enabled. See https://opencensus.io/.")
tracingProjectID = flag.String("tracing_project_id", "", "project ID to pass to stackdriver. Can be empty for GCP, consult docs for other platforms.")
Expand Down Expand Up @@ -104,7 +104,7 @@ func main() {
// Register handlers for all the configured logs.
opts := sctfe.InstanceOptions{
Validated: vCfg,
Deadline: *tesseraDeadline,
Deadline: *httpDeadline,
MetricFactory: prometheus.MetricFactory{},
RequestLog: new(sctfe.DefaultRequestLog),
MaskInternalErrors: *maskInternalErrors,
Expand Down
43 changes: 22 additions & 21 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,20 @@ type AppHandler struct {
// does additional common error and stats processing.
func (a AppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var statusCode int
label0 := a.Info.LogOrigin
label0 := a.Info.Origin
label1 := string(a.Name)
reqsCounter.Inc(label0, label1)
startTime := a.Info.TimeSource.Now()
logCtx := a.Info.RequestLog.Start(r.Context())
a.Info.RequestLog.LogOrigin(logCtx, a.Info.LogOrigin)
a.Info.RequestLog.Origin(logCtx, a.Info.Origin)
defer func() {
latency := a.Info.TimeSource.Now().Sub(startTime).Seconds()
rspLatency.Observe(latency, label0, label1, strconv.Itoa(statusCode))
}()
klog.V(2).Infof("%s: request %v %q => %s", a.Info.LogOrigin, r.Method, r.URL, a.Name)
klog.V(2).Infof("%s: request %v %q => %s", a.Info.Origin, r.Method, r.URL, a.Name)
// TODO(phboneff): add a.Method directly on the handler path and remove this test.
if r.Method != a.Method {
klog.Warningf("%s: %s wrong HTTP method: %v", a.Info.LogOrigin, a.Name, r.Method)
klog.Warningf("%s: %s wrong HTTP method: %v", a.Info.Origin, a.Name, r.Method)
a.Info.SendHTTPError(w, http.StatusMethodNotAllowed, fmt.Errorf("method not allowed: %s", r.Method))
a.Info.RequestLog.Status(logCtx, http.StatusMethodNotAllowed)
return
Comment on lines 115 to 119
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably get rid of this if you use the new style handler mapping on the HTTP mux which allows you to specify the method.

Expand All @@ -135,17 +136,17 @@ func (a AppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var err error
statusCode, err = a.Handler(ctx, a.Info, w, r)
a.Info.RequestLog.Status(ctx, statusCode)
klog.V(2).Infof("%s: %s <= st=%d", a.Info.LogOrigin, a.Name, statusCode)
klog.V(2).Infof("%s: %s <= st=%d", a.Info.Origin, a.Name, statusCode)
rspsCounter.Inc(label0, label1, strconv.Itoa(statusCode))
if err != nil {
klog.Warningf("%s: %s handler error: %v", a.Info.LogOrigin, a.Name, err)
klog.Warningf("%s: %s handler error: %v", a.Info.Origin, a.Name, err)
a.Info.SendHTTPError(w, statusCode, err)
return
}

// Additional check, for consistency the handler must return an error for non-200 st
if statusCode != http.StatusOK {
klog.Warningf("%s: %s handler non 200 without error: %d %v", a.Info.LogOrigin, a.Name, statusCode, err)
klog.Warningf("%s: %s handler non 200 without error: %d %v", a.Info.Origin, a.Name, statusCode, err)
a.Info.SendHTTPError(w, http.StatusInternalServerError, fmt.Errorf("http handler misbehaved, st: %d", statusCode))
return
}
Expand Down Expand Up @@ -190,8 +191,8 @@ func NewCertValidationOpts(trustedRoots *x509util.PEMCertPool, currentTime time.

// logInfo holds information for a specific log instance.
type logInfo struct {
// LogOrigin identifies the log, as per https://c2sp.org/static-ct-api
LogOrigin string
// Origin identifies the log, as per https://c2sp.org/static-ct-api
Origin string
// TimeSource is a TimeSource that can be injected for testing
TimeSource TimeSource
// RequestLog is a logger for various request / processing / response debug
Expand Down Expand Up @@ -219,7 +220,7 @@ func newLogInfo(
cfg := instanceOpts.Validated

li := &logInfo{
LogOrigin: cfg.Origin,
Origin: cfg.Origin,
storage: storage,
signer: signer,
TimeSource: timeSource,
Expand Down Expand Up @@ -297,7 +298,7 @@ func addChainInternal(ctx context.Context, li *logInfo, w http.ResponseWriter, r
// Check the contents of the request and convert to slice of certificates.
addChainReq, err := ParseBodyAsJSONChain(r)
if err != nil {
return http.StatusBadRequest, fmt.Errorf("%s: failed to parse add-chain body: %s", li.LogOrigin, err)
return http.StatusBadRequest, fmt.Errorf("%s: failed to parse add-chain body: %s", li.Origin, err)
}
// Log the DERs now because they might not parse as valid X.509.
for _, der := range addChainReq.Chain {
Expand All @@ -319,22 +320,22 @@ func addChainInternal(ctx context.Context, li *logInfo, w http.ResponseWriter, r
return http.StatusBadRequest, fmt.Errorf("failed to build MerkleTreeLeaf: %s", err)
}

klog.V(2).Infof("%s: %s => storage.GetCertIndex", li.LogOrigin, method)
klog.V(2).Infof("%s: %s => storage.GetCertIndex", li.Origin, method)
sctDedupInfo, isDup, err := li.storage.GetCertDedupInfo(ctx, chain[0])
idx := sctDedupInfo.Idx
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("couldn't deduplicate the request: %s", err)
}

if isDup {
klog.V(3).Infof("%s: %s - found duplicate entry at index %d", li.LogOrigin, method, idx)
klog.V(3).Infof("%s: %s - found duplicate entry at index %d", li.Origin, method, idx)
entry.Timestamp = sctDedupInfo.Timestamp
} else {
if err := li.storage.AddIssuerChain(ctx, chain[1:]); err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to store issuer chain: %s", err)
}

klog.V(2).Infof("%s: %s => storage.Add", li.LogOrigin, method)
klog.V(2).Infof("%s: %s => storage.Add", li.Origin, method)
idx, err = li.storage.Add(ctx, entry)()
if err != nil {
if errors.Is(err, tessera.ErrPushback) {
Expand All @@ -346,7 +347,7 @@ func addChainInternal(ctx context.Context, li *logInfo, w http.ResponseWriter, r
// We store the index for this certificate in the deduplication storage immediately.
// It might be stored again later, if a local deduplication storage is synced, potentially
// with a smaller value.
klog.V(2).Infof("%s: %s => storage.AddCertIndex", li.LogOrigin, method)
klog.V(2).Infof("%s: %s => storage.AddCertIndex", li.Origin, method)
err = li.storage.AddCertDedupInfo(ctx, chain[0], dedup.SCTDedupInfo{Idx: idx, Timestamp: entry.Timestamp})
// TODO: block log writes if deduplication breaks
if err != nil {
Expand Down Expand Up @@ -381,9 +382,9 @@ func addChainInternal(ctx context.Context, li *logInfo, w http.ResponseWriter, r
// reason is logged and http status is already set
return http.StatusInternalServerError, fmt.Errorf("failed to write response: %s", err)
}
klog.V(3).Infof("%s: %s <= SCT", li.LogOrigin, method)
klog.V(3).Infof("%s: %s <= SCT", li.Origin, method)
if sct.Timestamp == timeMillis {
lastSCTTimestamp.Set(float64(sct.Timestamp), li.LogOrigin)
lastSCTTimestamp.Set(float64(sct.Timestamp), li.Origin)
}

return http.StatusOK, nil
Expand All @@ -409,14 +410,14 @@ func getRoots(_ context.Context, li *logInfo, w http.ResponseWriter, _ *http.Req
enc := json.NewEncoder(w)
err := enc.Encode(jsonMap)
if err != nil {
klog.Warningf("%s: get_roots failed: %v", li.LogOrigin, err)
klog.Warningf("%s: get_roots failed: %v", li.Origin, err)
return http.StatusInternalServerError, fmt.Errorf("get-roots failed with: %s", err)
}

return http.StatusOK, nil
}

// deadlineTime calculates the future time an RPC should expire based on our config
// deadlineTime calculates the future time a request should expire based on our config.
func deadlineTime(li *logInfo) time.Time {
return li.TimeSource.Now().Add(li.instanceOpts.Deadline)
}
Expand All @@ -440,9 +441,9 @@ func verifyAddChain(li *logInfo, req ct.AddChainRequest, expectingPrecert bool)
// The type of the leaf must match the one the handler expects
if isPrecert != expectingPrecert {
if expectingPrecert {
klog.Warningf("%s: Cert (or precert with invalid CT ext) submitted as precert chain: %q", li.LogOrigin, req.Chain)
klog.Warningf("%s: Cert (or precert with invalid CT ext) submitted as precert chain: %q", li.Origin, req.Chain)
} else {
klog.Warningf("%s: Precert (or cert with invalid CT ext) submitted as cert chain: %q", li.LogOrigin, req.Chain)
klog.Warningf("%s: Precert (or cert with invalid CT ext) submitted as cert chain: %q", li.Origin, req.Chain)
}
return nil, fmt.Errorf("cert / precert mismatch: %T", expectingPrecert)
}
Expand Down
2 changes: 1 addition & 1 deletion instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type InstanceOptions struct {
Validated *ValidatedLogConfig
// CreateStorage instantiates a Tessera storage implementation with a signer option.
CreateStorage func(context.Context, note.Signer) (*CTStorage, error)
// Deadline is a timeout for Tessera requests.
// Deadline is a timeout for HTTP requests.
Deadline time.Duration
// MetricFactory allows creating metrics.
MetricFactory monitoring.MetricFactory
Expand Down
6 changes: 0 additions & 6 deletions proto_gen.go

This file was deleted.

8 changes: 4 additions & 4 deletions requestlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type RequestLog interface {
// The returned context should be used in all the following calls to
// this API. This is normally arranged by the request handler code.
Start(context.Context) context.Context
// LogOrigin will be called once per request to set the log prefix.
LogOrigin(context.Context, string)
// Origin will be called once per request to set the log prefix.
Origin(context.Context, string)
// AddDERToChain will be called once for each certificate in a submitted
// chain. It's called early in request processing so the supplied bytes
// have not been checked for validity. Calls will be in order of the
Expand Down Expand Up @@ -71,8 +71,8 @@ func (dlr *DefaultRequestLog) Start(ctx context.Context) context.Context {
return ctx
}

// LogOrigin logs the origin of the CT log that this request is for.
func (dlr *DefaultRequestLog) LogOrigin(_ context.Context, p string) {
// Origin logs the origin of the CT log that this request is for.
func (dlr *DefaultRequestLog) Origin(_ context.Context, p string) {
klog.V(vLevel).Infof("RL: LogOrigin: %s", p)
}

Expand Down
Loading