-
-
Notifications
You must be signed in to change notification settings - Fork 629
Add visibility into nonce redemption failure causes #8448
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
Open
aarongable
wants to merge
6
commits into
main
Choose a base branch
from
nonce-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1d49e4f
Lightly refactor nonce service to clean up abstraction
aarongable 788005c
Lean in to making errors first-class citizens
aarongable fdb0e6e
Add metrics
aarongable 0b2e5dd
Remove unicode \in character
aarongable a462277
Change depth to latest, add metrics tests
aarongable ae5a35d
Fix metric name and description
aarongable File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -32,6 +32,7 @@ import ( | |||||||
| "google.golang.org/grpc" | ||||||||
| "google.golang.org/protobuf/types/known/emptypb" | ||||||||
|
|
||||||||
| berrors "github.com/letsencrypt/boulder/errors" | ||||||||
| noncepb "github.com/letsencrypt/boulder/nonce/proto" | ||||||||
| ) | ||||||||
|
|
||||||||
|
|
@@ -63,6 +64,7 @@ func DerivePrefix(grpcAddr string, key []byte) string { | |||||||
|
|
||||||||
| // NonceService generates, cancels, and tracks Nonces. | ||||||||
| type NonceService struct { | ||||||||
| noncepb.UnsafeNonceServiceServer | ||||||||
| mu sync.Mutex | ||||||||
| latest int64 | ||||||||
| earliest int64 | ||||||||
|
|
@@ -73,7 +75,9 @@ type NonceService struct { | |||||||
| prefix string | ||||||||
| nonceCreates prometheus.Counter | ||||||||
| nonceEarliest prometheus.Gauge | ||||||||
| nonceLatest prometheus.Gauge | ||||||||
| nonceRedeems *prometheus.CounterVec | ||||||||
| nonceAges *prometheus.HistogramVec | ||||||||
| nonceHeapLatency prometheus.Histogram | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -143,11 +147,22 @@ func NewNonceService(stats prometheus.Registerer, maxUsed int, prefix string) (* | |||||||
| Help: "A gauge with the current earliest valid nonce value", | ||||||||
| }) | ||||||||
| stats.MustRegister(nonceEarliest) | ||||||||
| nonceLatest := prometheus.NewGauge(prometheus.GaugeOpts{ | ||||||||
| Name: "nonce_latest", | ||||||||
| Help: "A gauge with the current latest valid nonce value", | ||||||||
| }) | ||||||||
| stats.MustRegister(nonceLatest) | ||||||||
| nonceRedeems := prometheus.NewCounterVec(prometheus.CounterOpts{ | ||||||||
| Name: "nonce_redeems", | ||||||||
| Help: "A counter of nonce validations labelled by result", | ||||||||
| }, []string{"result", "error"}) | ||||||||
| stats.MustRegister(nonceRedeems) | ||||||||
| nonceAges := prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||||||||
| Name: "nonce_ages", | ||||||||
| Help: "A histogram of nonce ages at the time they were (attempted to be) redeemed, expressed as fractions of the valid nonce window", | ||||||||
| Buckets: []float64{-0.01, 0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1, 1.1, 1.2, 1.5, 2, 5}, | ||||||||
| }, []string{"result"}) | ||||||||
| stats.MustRegister(nonceAges) | ||||||||
| nonceHeapLatency := prometheus.NewHistogram(prometheus.HistogramOpts{ | ||||||||
| Name: "nonce_heap_latency", | ||||||||
| Help: "A histogram of latencies of heap pop operations", | ||||||||
|
|
@@ -164,7 +179,9 @@ func NewNonceService(stats prometheus.Registerer, maxUsed int, prefix string) (* | |||||||
| prefix: prefix, | ||||||||
| nonceCreates: nonceCreates, | ||||||||
| nonceEarliest: nonceEarliest, | ||||||||
| nonceLatest: nonceLatest, | ||||||||
| nonceRedeems: nonceRedeems, | ||||||||
| nonceAges: nonceAges, | ||||||||
| nonceHeapLatency: nonceHeapLatency, | ||||||||
| }, nil | ||||||||
| } | ||||||||
|
|
@@ -232,40 +249,53 @@ func (ns *NonceService) decrypt(nonce string) (int64, error) { | |||||||
| return ctr.Int64(), nil | ||||||||
| } | ||||||||
|
|
||||||||
| // Nonce provides a new Nonce. | ||||||||
| func (ns *NonceService) Nonce() (string, error) { | ||||||||
| // nonce provides a new Nonce. | ||||||||
| func (ns *NonceService) nonce() (string, error) { | ||||||||
| ns.mu.Lock() | ||||||||
| ns.latest++ | ||||||||
| latest := ns.latest | ||||||||
| ns.mu.Unlock() | ||||||||
| defer ns.nonceCreates.Inc() | ||||||||
| ns.nonceCreates.Inc() | ||||||||
| ns.nonceLatest.Set(float64(latest)) | ||||||||
| return ns.encrypt(latest) | ||||||||
| } | ||||||||
|
|
||||||||
| // Valid determines whether the provided Nonce string is valid, returning | ||||||||
| // valid determines whether the provided Nonce string is valid, returning | ||||||||
| // true if so. | ||||||||
| func (ns *NonceService) Valid(nonce string) bool { | ||||||||
| func (ns *NonceService) valid(nonce string) error { | ||||||||
| c, err := ns.decrypt(nonce) | ||||||||
| if err != nil { | ||||||||
| ns.nonceRedeems.WithLabelValues("invalid", "decrypt").Inc() | ||||||||
| return false | ||||||||
| return berrors.BadNonceError("unable to decrypt nonce: %s", err) | ||||||||
| } | ||||||||
|
|
||||||||
| ns.mu.Lock() | ||||||||
| defer ns.mu.Unlock() | ||||||||
| if c > ns.latest { | ||||||||
|
|
||||||||
| // age represents how "far back" in the valid nonce window this nonce is. | ||||||||
| // If it is very recent, then the numerator is very small and the age is close | ||||||||
| // to zero. If it is old but still valid, the numerator is slightly smaller | ||||||||
| // than the denominator, and the age is close to one. If it is too old, then | ||||||||
| // the age is greater than one. If it is magically too new (i.e. greater than | ||||||||
| // the largest nonce we've actually handed out), then the age is negative. | ||||||||
| age := float64(ns.latest-c) / float64(ns.latest-ns.earliest) | ||||||||
|
|
||||||||
| if c > ns.latest { // i.e. age < 0 | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stylistically, we avoid inline comments.
Suggested change
|
||||||||
| ns.nonceRedeems.WithLabelValues("invalid", "too high").Inc() | ||||||||
| return false | ||||||||
| ns.nonceAges.WithLabelValues("invalid").Observe(age) | ||||||||
| return berrors.BadNonceError("nonce greater than highest dispensed nonce: %d > %d", c, ns.latest) | ||||||||
| } | ||||||||
|
|
||||||||
| if c <= ns.earliest { | ||||||||
| if c <= ns.earliest { // i.e. age >= 1 | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same style comment as above.
Suggested change
|
||||||||
| ns.nonceRedeems.WithLabelValues("invalid", "too low").Inc() | ||||||||
| return false | ||||||||
| ns.nonceAges.WithLabelValues("invalid").Observe(age) | ||||||||
| return berrors.BadNonceError("nonce less than lowest eligible nonce: %d < %d", c, ns.earliest) | ||||||||
| } | ||||||||
|
|
||||||||
| if ns.used[c] { | ||||||||
| ns.nonceRedeems.WithLabelValues("invalid", "already used").Inc() | ||||||||
| return false | ||||||||
| ns.nonceAges.WithLabelValues("invalid").Observe(age) | ||||||||
| return berrors.BadNonceError("nonce already marked as used: %d in [%d]used", c, len(ns.used)) | ||||||||
| } | ||||||||
|
|
||||||||
| ns.used[c] = true | ||||||||
|
|
@@ -279,7 +309,8 @@ func (ns *NonceService) Valid(nonce string) bool { | |||||||
| } | ||||||||
|
|
||||||||
| ns.nonceRedeems.WithLabelValues("valid", "").Inc() | ||||||||
| return true | ||||||||
| ns.nonceAges.WithLabelValues("valid").Observe(age) | ||||||||
| return nil | ||||||||
| } | ||||||||
|
|
||||||||
| // splitNonce splits a nonce into a prefix and a body. | ||||||||
|
|
@@ -290,27 +321,18 @@ func (ns *NonceService) splitNonce(nonce string) (string, string, error) { | |||||||
| return nonce[:PrefixLen], nonce[PrefixLen:], nil | ||||||||
| } | ||||||||
|
|
||||||||
| // NewServer returns a new Server, wrapping a NonceService. | ||||||||
| func NewServer(inner *NonceService) *Server { | ||||||||
| return &Server{inner: inner} | ||||||||
| } | ||||||||
|
|
||||||||
| // Server implements the gRPC nonce service. | ||||||||
| type Server struct { | ||||||||
| noncepb.UnsafeNonceServiceServer | ||||||||
| inner *NonceService | ||||||||
| } | ||||||||
|
|
||||||||
| var _ noncepb.NonceServiceServer = (*Server)(nil) | ||||||||
|
|
||||||||
| // Redeem accepts a nonce from a gRPC client and redeems it using the inner nonce service. | ||||||||
| func (ns *Server) Redeem(ctx context.Context, msg *noncepb.NonceMessage) (*noncepb.ValidMessage, error) { | ||||||||
| return &noncepb.ValidMessage{Valid: ns.inner.Valid(msg.Nonce)}, nil | ||||||||
| func (ns *NonceService) Redeem(ctx context.Context, msg *noncepb.NonceMessage) (*noncepb.ValidMessage, error) { | ||||||||
| err := ns.valid(msg.Nonce) | ||||||||
| if err != nil { | ||||||||
| return nil, err | ||||||||
| } | ||||||||
| return &noncepb.ValidMessage{Valid: true}, nil | ||||||||
| } | ||||||||
|
|
||||||||
| // Nonce generates a nonce and sends it to a gRPC client. | ||||||||
| func (ns *Server) Nonce(_ context.Context, _ *emptypb.Empty) (*noncepb.NonceMessage, error) { | ||||||||
| nonce, err := ns.inner.Nonce() | ||||||||
| func (ns *NonceService) Nonce(_ context.Context, _ *emptypb.Empty) (*noncepb.NonceMessage, error) { | ||||||||
| nonce, err := ns.nonce() | ||||||||
| if err != nil { | ||||||||
| return nil, err | ||||||||
| } | ||||||||
|
|
||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.