Commit Graph

12 Commits

Author SHA1 Message Date
Samantha Frank e625ff3534
sa: Store and manage rate limit overrides in the database (#8142)
Add support for managing and querying rate limit overrides in the
database.
- Add `sa.AddRateLimitOverride` to insert or update a rate limit
override. This will be used during Rate Limit Override Portal to commit
approved overrides to the database.
- Add `sa.DisableRateLimitOverride` and `sa.EnableRateLimitOverride` to
toggle override state. These will be used by the `admin` tool.
- Add `sa.GetRateLimitOverride` to retrieve a single override by limit
enum and bucket key. This will be used by the Rate Limit Portal to
prevent duplicate or downgrade requests but allow upgrade requests.
- Add `sa.GetEnabledRateLimitOverrides` to stream all currently enabled
overrides. This will be used by the rate limit consumers (`wfe` and
`ra`) to refresh the overrides in-memory.
- Implement test coverage for all new methods.
2025-05-08 14:50:30 -04:00
Jacob Hoffman-Andrews 38fc840184
sa: refactor how metrics and logging are set up (#7031)
This eliminates the need for a pair of accessors on `db.WrappedMap` that
expose the underlying `*sql.DB` and `*borp.DbMap`.

Fixes #6991
2023-08-08 09:51:23 -07:00
Jacob Hoffman-Andrews 7d66d67054
It's borpin' time! (#6982)
This change replaces [gorp] with [borp].

The changes consist of a mass renaming of the import and comments / doc
fixups, plus modifications of many call sites to provide a
context.Context everywhere, since gorp newly requires this (this was one
of the motivating factors for the borp fork).

This also refactors `github.com/letsencrypt/boulder/db.WrappedMap` and
`github.com/letsencrypt/boulder/db.Transaction` to not embed their
underlying gorp/borp objects, but to have them as plain fields. This
ensures that we can only call methods on them that are specifically
implemented in `github.com/letsencrypt/boulder/db`, so we don't miss
wrapping any. This required introducing a `NewWrappedMap` method along
with accessors `SQLDb()` and `BorpDB()` to get at the internal fields
during metrics and logging setup.

Fixes #6944
2023-07-17 14:38:29 -07:00
Aaron Gable 85b146e82a
Reland "Bump github.com/go-sql-driver/mysql from 1.5.0 to 1.7.0" (#6662)
This reverts letsencrypt/boulder#6660, and relands
letsencrypt/boulder#6625.
2023-02-21 14:52:23 -05:00
Jacob Hoffman-Andrews 0f2b47e072
Revert "Bump github.com/go-sql-driver/mysql from 1.5.0 to 1.7.0 (#6625)" (#6660)
This reverts commit fdfea0d469.

With a Go security release out this week we prefer to do a single
release on the new Go version rather than trying to deploy the new
go-sql-driver version.
2023-02-14 12:19:31 -08:00
dependabot[bot] fdfea0d469
build(deps): Bump github.com/go-sql-driver/mysql from 1.5.0 to 1.7.0 (#6625)
Update https://github.com/go-sql-driver/mysql from 1.5.0 to 1.7.0.

Also change db.IsDuplicate() and db.IsNoRows to more directly
use errors.Is(), and to be more resilient to error string message
changes such as the one that is included in this version update.

Changelog: https://github.com/go-sql-driver/mysql/compare/v1.5.0...v1.7.0
2023-02-13 13:35:21 -08:00
Aaron Gable b86e9d10da
Unify how we do streaming database selects (#6176)
Create a new type `db.MappedSelector` which exposes a new
`Query` method. This method behaves similar to gorp's
`SelectFoo` methods, in that it uses the desired result type to
look up the correct table to query and uses reflection to map
the table columns to the struct fields. It behaves similarly to
the stdlib's `sql.Query` in that it returns a `Rows` object which
can be iterated over to get one row of results at a time. And it
improves both of those by using generics, rather than `interface{}`,
to provide a nicely-typed calling interface.

Use this new type to simplify the existing streaming query in
`SerialsForIncident`. Similarly use the new type to simplify
rocsp-tool's and ocsp-updater's streams of `CertStatusMetadata`.
This new type will also be used by the crl-updater's upcoming
`GetRevokedCerts` streaming query.

Fixes #6173
2022-06-24 14:31:46 -07:00
Aaron Gable 305ef9cce9
Improve error checking paradigm (#5920)
We have decided that we don't like the if err := call(); err != nil
syntax, because it creates confusing scopes, but we have not cleaned up
all existing instances of that syntax. However, we have now found a
case where that syntax enables a bug: It caused readers to believe that
a later err = call() statement was assigning to an already-declared err
in the local scope, when in fact it was assigning to an
already-declared err in the parent scope of a closure. This caused our
ineffassign and staticcheck linters to be unable to analyze the
lifetime of the err variable, and so they did not complain when we
never checked the actual value of that error.

This change standardizes on the two-line error checking syntax
everywhere, so that we can more easily ensure that our linters are
correctly analyzing all error assignments.
2022-02-01 14:42:43 -07:00
Aaron Gable beee17c510
Janitor: refactor to be controlled by config (#5195)
Previously, configuration of the boulder-janitor was split into
two places: the actual json config file (which controlled which
jobs would be enabled, and what their rate limits should be), and
the janitor code itself (which controlled which tables and columns
those jobs should query). This resulted in significant duplicated
code, as most of the jobs were identical except for their table
and column names.

This change abstracts away the query which jobs use to find work.
Instead of having each job type parse its own config and produce
its own work query (in Go code), now each job supplies just a few
key values (the table name and two column names) in its JSON config,
and the Go code assembles the appropriate query from there. We are
able to delete all of the files defining individual job types, and
replace them with a single slightly smarter job constructor.

This enables further refactorings, namely:
* Moving all of the logic code into its own module;
* Ensuring that the exported interface of that module is safe (i.e.
  that a client cannot create and run jobs without them being valid,
  because the only exposed methods ensure validity);
* Collapsing validity checks into a single location;
* Various renamings.
2020-12-17 09:53:22 -08:00
Samantha c6c6a6bbcc
db: replacing error assertions with errors.As (#5146)
Part of #5010
2020-10-22 13:17:38 -07:00
Jacob Hoffman-Andrews 06ffb57221
Update go-gorp and run go mod tidy. (#4860)
gorp now uses go modules.

```
$ cd ~/go/src/github.com/go-gorp/gorp/
$ git checkout v3.0.1
$ go test ./...
ok      github.com/go-gorp/gorp/v3      0.002s
```
2020-06-10 16:18:37 -07:00
Daniel McCarney 1c9ece3f44
SA: use wrapped database maps/transactions. (#4585)
New types and related infrastructure are added to the `db` package to allow
wrapping gorp DbMaps and Transactions.

The wrapped versions return a special `db.ErrDatabaseOp` error type when errors
occur. The new error type includes additional information such as the operation
that failed and the related table.

Where possible we determine the table based on the types of the gorp function
arguments. Where that isn't possible (e.g. with raw SQL queries) we try to use
a simple regexp approach to find the table name. This isn't great for general
SQL but works well enough for Boulder's existing SQL queries.

To get additional confidence my regexps work for all of Boulder's queries
I temporarily changed the `db` package's `tableFromQuery` function to panic if
the table couldn't be determined. I re-ran the full unit and integration test
suites with this configuration and saw no panics.

Resolves https://github.com/letsencrypt/boulder/issues/4559
2019-12-04 13:03:09 -05:00