Skip to content

Commit

Permalink
Move cmd.DBConfig fields to their own named sub-struct (#5286)
Browse files Browse the repository at this point in the history
Named field `DB`, in a each component configuration struct, acts as the
receiver for the value of `db` when component JSON files are
unmarshalled.

When `cmd.DBConfig` fields are received at the root of component
configuration struct instead of `DB` copy them to the `DB` field of the
component configuration struct.

Move existing `cmd.DBConfig` values from the root of each component's
JSON configuration in `test/config-next` to `db`

Part of #5275
  • Loading branch information
beautifulentropy committed Feb 16, 2021
1 parent f13b8db commit e2e7dad
Show file tree
Hide file tree
Showing 26 changed files with 244 additions and 81 deletions.
18 changes: 12 additions & 6 deletions cmd/admin-revoker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ args:

type config struct {
Revoker struct {
cmd.DBConfig
DB cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DeprecatedDBConfig
// Similarly, the Revoker needs a TLSConfig to set up its GRPC client certs,
// but doesn't get the TLS field from ServiceConfig, so declares its own.
TLS cmd.TLSConfig
Expand All @@ -73,13 +76,16 @@ func setupContext(c config) (core.RegistrationAuthority, blog.Logger, *db.Wrappe
cmd.FailOnError(err, "Failed to load credentials and create gRPC connection to RA")
rac := bgrpc.NewRegistrationAuthorityClient(rapb.NewRegistrationAuthorityClient(raConn))

dbURL, err := c.Revoker.DBConfig.URL()
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DefaultDBConfig(&c.Revoker.DB, &c.Revoker.DeprecatedDBConfig)
dbURL, err := c.Revoker.DB.URL()
cmd.FailOnError(err, "Couldn't load DB URL")
dbSettings := sa.DbSettings{
MaxOpenConns: c.Revoker.DBConfig.MaxOpenConns,
MaxIdleConns: c.Revoker.DBConfig.MaxIdleConns,
ConnMaxLifetime: c.Revoker.DBConfig.ConnMaxLifetime.Duration,
ConnMaxIdleTime: c.Revoker.DBConfig.ConnMaxIdleTime.Duration,
MaxOpenConns: c.Revoker.DB.MaxOpenConns,
MaxIdleConns: c.Revoker.DB.MaxIdleConns,
ConnMaxLifetime: c.Revoker.DB.ConnMaxLifetime.Duration,
ConnMaxIdleTime: c.Revoker.DB.ConnMaxIdleTime.Duration,
}
dbMap, err := sa.NewDbMap(dbURL, dbSettings)
cmd.FailOnError(err, "Couldn't setup database connection")
Expand Down
18 changes: 12 additions & 6 deletions cmd/bad-key-revoker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,10 @@ func (bkr *badKeyRevoker) invoke() (bool, error) {
func main() {
var config struct {
BadKeyRevoker struct {
cmd.DBConfig
DB cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DeprecatedDBConfig
DebugAddr string

TLS cmd.TLSConfig
Expand Down Expand Up @@ -389,14 +392,17 @@ func main() {
scope.MustRegister(certsRevoked)
scope.MustRegister(mailErrors)

dbURL, err := config.BadKeyRevoker.DBConfig.URL()
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DefaultDBConfig(&config.BadKeyRevoker.DB, &config.BadKeyRevoker.DeprecatedDBConfig)
dbURL, err := config.BadKeyRevoker.DB.URL()
cmd.FailOnError(err, "Couldn't load DB URL")

dbSettings := sa.DbSettings{
MaxOpenConns: config.BadKeyRevoker.DBConfig.MaxOpenConns,
MaxIdleConns: config.BadKeyRevoker.DBConfig.MaxIdleConns,
ConnMaxLifetime: config.BadKeyRevoker.DBConfig.ConnMaxLifetime.Duration,
ConnMaxIdleTime: config.BadKeyRevoker.DBConfig.ConnMaxIdleTime.Duration,
MaxOpenConns: config.BadKeyRevoker.DB.MaxOpenConns,
MaxIdleConns: config.BadKeyRevoker.DB.MaxIdleConns,
ConnMaxLifetime: config.BadKeyRevoker.DB.ConnMaxLifetime.Duration,
ConnMaxIdleTime: config.BadKeyRevoker.DB.ConnMaxIdleTime.Duration,
}
dbMap, err := sa.NewDbMap(dbURL, dbSettings)
cmd.FailOnError(err, "Could not connect to database")
Expand Down
4 changes: 4 additions & 0 deletions cmd/boulder-ca/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import (
type config struct {
CA struct {
cmd.ServiceConfig

// TODO(#5275): Refactor to named field once all configs in dev,
// staging and prod have been updated to contain `dbconfig`
// field
cmd.DBConfig
cmd.HostnamePolicyConfig

Expand Down
20 changes: 14 additions & 6 deletions cmd/boulder-janitor/janitor/janitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ type JanitorConfig struct {
DebugAddr string
// Features holds potential Feature flags.
Features map[string]bool

// Common database connection configuration.
cmd.DBConfig
DB cmd.DBConfig

// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DeprecatedDBConfig

// JobConfigs is a list of configs for individual cleanup jobs.
JobConfigs []JobConfig
Expand Down Expand Up @@ -62,16 +67,19 @@ func New(clk clock.Clock, config JanitorConfig) (*Janitor, error) {
defer logger.AuditPanic()
logger.Info(cmd.VersionString())

// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DefaultDBConfig(&config.DB, &config.DeprecatedDBConfig)
// Create DB Map
dbURL, err := config.DBConfig.URL()
dbURL, err := config.DB.URL()
if err != nil {
return nil, err
}
dbSettings := sa.DbSettings{
MaxOpenConns: config.DBConfig.MaxOpenConns,
MaxIdleConns: config.DBConfig.MaxIdleConns,
ConnMaxLifetime: config.DBConfig.ConnMaxLifetime.Duration,
ConnMaxIdleTime: config.DBConfig.ConnMaxIdleTime.Duration,
MaxOpenConns: config.DB.MaxOpenConns,
MaxIdleConns: config.DB.MaxIdleConns,
ConnMaxLifetime: config.DB.ConnMaxLifetime.Duration,
ConnMaxIdleTime: config.DB.ConnMaxIdleTime.Duration,
}
dbMap, err := sa.NewDbMap(dbURL, dbSettings)
if err != nil {
Expand Down
20 changes: 14 additions & 6 deletions cmd/boulder-sa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import (
type config struct {
SA struct {
cmd.ServiceConfig
cmd.DBConfig
DB cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DeprecatedDBConfig

Features map[string]bool

Expand Down Expand Up @@ -57,14 +60,19 @@ func main() {
logger.Info(cmd.VersionString())

saConf := c.SA

// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DefaultDBConfig(&saConf.DB, &saConf.DeprecatedDBConfig)

saDbSettings := sa.DbSettings{
MaxOpenConns: saConf.DBConfig.MaxOpenConns,
MaxIdleConns: saConf.DBConfig.MaxIdleConns,
ConnMaxLifetime: saConf.DBConfig.ConnMaxLifetime.Duration,
ConnMaxIdleTime: saConf.DBConfig.ConnMaxIdleTime.Duration,
MaxOpenConns: saConf.DB.MaxOpenConns,
MaxIdleConns: saConf.DB.MaxIdleConns,
ConnMaxLifetime: saConf.DB.ConnMaxLifetime.Duration,
ConnMaxIdleTime: saConf.DB.ConnMaxIdleTime.Duration,
}

dbURL, err := saConf.DBConfig.URL()
dbURL, err := saConf.DB.URL()
cmd.FailOnError(err, "Couldn't load DB URL")

dbMap, err := sa.NewDbMap(dbURL, saDbSettings)
Expand Down
18 changes: 12 additions & 6 deletions cmd/cert-checker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,10 @@ func (c *certChecker) checkCert(cert core.Certificate, ignoredLints map[string]b

type config struct {
CertChecker struct {
cmd.DBConfig
DB cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DeprecatedDBConfig
cmd.HostnamePolicyConfig

Workers int
Expand Down Expand Up @@ -371,13 +374,16 @@ func main() {
// Validate PA config and set defaults if needed
cmd.FailOnError(config.PA.CheckChallenges(), "Invalid PA configuration")

saDbURL, err := config.CertChecker.DBConfig.URL()
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DefaultDBConfig(&config.CertChecker.DB, &config.CertChecker.DeprecatedDBConfig)
saDbURL, err := config.CertChecker.DB.URL()
cmd.FailOnError(err, "Couldn't load DB URL")
dbSettings := sa.DbSettings{
MaxOpenConns: config.CertChecker.DBConfig.MaxOpenConns,
MaxIdleConns: config.CertChecker.DBConfig.MaxIdleConns,
ConnMaxLifetime: config.CertChecker.DBConfig.ConnMaxLifetime.Duration,
ConnMaxIdleTime: config.CertChecker.DBConfig.ConnMaxIdleTime.Duration,
MaxOpenConns: config.CertChecker.DB.MaxOpenConns,
MaxIdleConns: config.CertChecker.DB.MaxIdleConns,
ConnMaxLifetime: config.CertChecker.DB.ConnMaxLifetime.Duration,
ConnMaxIdleTime: config.CertChecker.DB.ConnMaxIdleTime.Duration,
}
saDbMap, err := sa.NewDbMap(saDbURL, dbSettings)
cmd.FailOnError(err, "Could not connect to database")
Expand Down
25 changes: 25 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ type DBConfig struct {
ConnMaxIdleTime ConfigDuration
}

// DeprecatedDBConfig is a temporary type that acts as a receiver for
// fields unmarshalled from the root of a component's JSON config
// (deprecated).
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
type DeprecatedDBConfig DBConfig

// URL returns the DBConnect URL represented by this DBConfig object, either
// loading it from disk or returning a default value. Leading and trailing
// whitespace is stripped.
Expand All @@ -86,6 +93,24 @@ func (d *DBConfig) URL() (string, error) {
return d.DBConnect, nil
}

// DefaultDBConfig is a temporary helper function that copies DBConfig
// fields unmarshalled from the root of a component's JSON config
// (deprecated) to the named `DBConfig` substruct of the service config.
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
func DefaultDBConfig(dbConfig *DBConfig, databaseConfig *DeprecatedDBConfig) {
if dbConfig.DBConnectFile != "" {
// dbConfig was specified properly in the JSON return early
return
}
dbConfig.DBConnect = databaseConfig.DBConnect
dbConfig.DBConnectFile = databaseConfig.DBConnectFile
dbConfig.MaxOpenConns = databaseConfig.MaxOpenConns
dbConfig.MaxIdleConns = databaseConfig.MaxIdleConns
dbConfig.ConnMaxIdleTime = databaseConfig.ConnMaxIdleTime
dbConfig.ConnMaxLifetime = databaseConfig.ConnMaxLifetime
}

type SMTPConfig struct {
PasswordConfig
Server string
Expand Down
33 changes: 33 additions & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,36 @@ func TestTLSConfigLoad(t *testing.T) {
})
}
}

func TestDefaultDBConfig(t *testing.T) {
exampleDBConfig := &DBConfig{}
exampleDatabaseConfig := &DeprecatedDBConfig{
DBConnect: "some secret",
DBConnectFile: "foo/bar",
MaxOpenConns: 100,
MaxIdleConns: 100,
ConnMaxLifetime: ConfigDuration{10000},
ConnMaxIdleTime: ConfigDuration{10000},
}
expected := &DBConfig{
DBConnect: "some secret",
DBConnectFile: "foo/bar",
MaxOpenConns: 100,
MaxIdleConns: 100,
ConnMaxLifetime: ConfigDuration{10000},
ConnMaxIdleTime: ConfigDuration{10000},
}
DefaultDBConfig(exampleDBConfig, exampleDatabaseConfig)
test.AssertDeepEquals(t, exampleDBConfig, expected)

exampleDBConfig = &DBConfig{
DBConnect: "some secret",
DBConnectFile: "foo/bar",
MaxOpenConns: 100,
MaxIdleConns: 100,
ConnMaxLifetime: ConfigDuration{10000},
ConnMaxIdleTime: ConfigDuration{10000}}
exampleDatabaseConfig = &DeprecatedDBConfig{}
DefaultDBConfig(exampleDBConfig, exampleDatabaseConfig)
test.AssertDeepEquals(t, exampleDBConfig, expected)
}
18 changes: 12 additions & 6 deletions cmd/expiration-mailer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ func (ds durationSlice) Swap(a, b int) {
type config struct {
Mailer struct {
cmd.ServiceConfig
cmd.DBConfig
DB cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DeprecatedDBConfig
cmd.SMTPConfig

From string
Expand Down Expand Up @@ -474,14 +477,17 @@ func main() {
c.Mailer.CertLimit = 100
}

// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DefaultDBConfig(&c.Mailer.DB, &c.Mailer.DeprecatedDBConfig)
// Configure DB
dbURL, err := c.Mailer.DBConfig.URL()
dbURL, err := c.Mailer.DB.URL()
cmd.FailOnError(err, "Couldn't load DB URL")
dbSettings := sa.DbSettings{
MaxOpenConns: c.Mailer.DBConfig.MaxOpenConns,
MaxIdleConns: c.Mailer.DBConfig.MaxIdleConns,
ConnMaxLifetime: c.Mailer.DBConfig.ConnMaxLifetime.Duration,
ConnMaxIdleTime: c.Mailer.DBConfig.ConnMaxIdleTime.Duration,
MaxOpenConns: c.Mailer.DB.MaxOpenConns,
MaxIdleConns: c.Mailer.DB.MaxIdleConns,
ConnMaxLifetime: c.Mailer.DB.ConnMaxLifetime.Duration,
ConnMaxIdleTime: c.Mailer.DB.ConnMaxIdleTime.Duration,
}
dbMap, err := sa.NewDbMap(dbURL, dbSettings)
cmd.FailOnError(err, "Could not connect to database")
Expand Down
18 changes: 12 additions & 6 deletions cmd/expired-authz-purger2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import (

type config struct {
ExpiredAuthzPurger2 struct {
cmd.DBConfig
DB cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DeprecatedDBConfig
DebugAddr string
Syslog cmd.SyslogConfig
Features map[string]bool
Expand Down Expand Up @@ -76,13 +79,16 @@ func main() {

clk := cmd.Clock()

dbURL, err := c.ExpiredAuthzPurger2.DBConfig.URL()
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DefaultDBConfig(&c.ExpiredAuthzPurger2.DB, &c.ExpiredAuthzPurger2.DeprecatedDBConfig)
dbURL, err := c.ExpiredAuthzPurger2.DB.URL()
cmd.FailOnError(err, "Couldn't load DB URL")
dbSettings := sa.DbSettings{
MaxOpenConns: c.ExpiredAuthzPurger2.DBConfig.MaxOpenConns,
MaxIdleConns: c.ExpiredAuthzPurger2.DBConfig.MaxIdleConns,
ConnMaxLifetime: c.ExpiredAuthzPurger2.DBConfig.ConnMaxLifetime.Duration,
ConnMaxIdleTime: c.ExpiredAuthzPurger2.DBConfig.ConnMaxIdleTime.Duration,
MaxOpenConns: c.ExpiredAuthzPurger2.DB.MaxOpenConns,
MaxIdleConns: c.ExpiredAuthzPurger2.DB.MaxIdleConns,
ConnMaxLifetime: c.ExpiredAuthzPurger2.DB.ConnMaxLifetime.Duration,
ConnMaxIdleTime: c.ExpiredAuthzPurger2.DB.ConnMaxIdleTime.Duration,
}
dbMap, err := sa.NewDbMap(dbURL, dbSettings)
cmd.FailOnError(err, "Could not connect to database")
Expand Down
10 changes: 8 additions & 2 deletions cmd/id-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ func main() {
domainsFile := flag.String("domains", "", "If provided only output contacts for certificates that contain at least one of the domains in the provided file. Provided file should contain one domain per line")
type config struct {
ContactExporter struct {
cmd.DBConfig
DB cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DeprecatedDBConfig
cmd.PasswordConfig
Features map[string]bool
}
Expand Down Expand Up @@ -174,7 +177,10 @@ func main() {
err = features.Set(cfg.ContactExporter.Features)
cmd.FailOnError(err, "Failed to set feature flags")

dbURL, err := cfg.ContactExporter.DBConfig.URL()
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DefaultDBConfig(&cfg.ContactExporter.DB, &cfg.ContactExporter.DeprecatedDBConfig)
dbURL, err := cfg.ContactExporter.DB.URL()
cmd.FailOnError(err, "Couldn't load DB URL")
dbSettings := sa.DbSettings{
MaxOpenConns: 10,
Expand Down
10 changes: 8 additions & 2 deletions cmd/notify-mailer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,10 @@ func main() {
reconnMax := flag.Duration("reconnectMax", 5*60*time.Second, "Max sleep duration between reconnect attempts after exponential backoff")
type config struct {
NotifyMailer struct {
cmd.DBConfig
DB cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DeprecatedDBConfig
cmd.PasswordConfig
cmd.SMTPConfig
Features map[string]bool
Expand Down Expand Up @@ -439,7 +442,10 @@ func main() {
log := cmd.NewLogger(cfg.Syslog)
defer log.AuditPanic()

dbURL, err := cfg.NotifyMailer.DBConfig.URL()
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain the `dbconfig` field
cmd.DefaultDBConfig(&cfg.NotifyMailer.DB, &cfg.NotifyMailer.DeprecatedDBConfig)
dbURL, err := cfg.NotifyMailer.DB.URL()
cmd.FailOnError(err, "Couldn't load DB URL")
dbSettings := sa.DbSettings{
MaxOpenConns: 10,
Expand Down

0 comments on commit e2e7dad

Please sign in to comment.