Skip to content

Commit

Permalink
Make DBConfig a named field in component configuration
Browse files Browse the repository at this point in the history
Unmarshall `cmd.DBConfig` fields from `dbConfig` in component JSON
files.

Copy `cmd.DBConfig` fields recieved at the root of component
configuration struct to the `DBConfig` field.

Part of #5275
  • Loading branch information
beautifulentropy committed Feb 12, 2021
1 parent b306060 commit 1463e65
Show file tree
Hide file tree
Showing 26 changed files with 200 additions and 33 deletions.
8 changes: 7 additions & 1 deletion 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
DBConfig cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DatabaseConfig
// 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,6 +76,9 @@ 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))

// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DefaultDBConfig(&c.Revoker.DBConfig, &c.Revoker.DatabaseConfig)
dbURL, err := c.Revoker.DBConfig.URL()
cmd.FailOnError(err, "Couldn't load DB URL")
dbSettings := sa.DbSettings{
Expand Down
8 changes: 7 additions & 1 deletion 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
DBConfig cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DatabaseConfig
DebugAddr string

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

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

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
10 changes: 9 additions & 1 deletion 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
DBConfig cmd.DBConfig

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

// JobConfigs is a list of configs for individual cleanup jobs.
JobConfigs []JobConfig
Expand Down Expand Up @@ -62,6 +67,9 @@ 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 `dbconfig` field
cmd.DefaultDBConfig(&config.DBConfig, &config.DatabaseConfig)
// Create DB Map
dbURL, err := config.DBConfig.URL()
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion 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
DBConfig cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DatabaseConfig

Features map[string]bool

Expand Down Expand Up @@ -57,6 +60,11 @@ 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 `dbconfig` field
cmd.DefaultDBConfig(&saConf.DBConfig, &saConf.DatabaseConfig)

saDbSettings := sa.DbSettings{
MaxOpenConns: saConf.DBConfig.MaxOpenConns,
MaxIdleConns: saConf.DBConfig.MaxIdleConns,
Expand Down
8 changes: 7 additions & 1 deletion 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
DBConfig cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DatabaseConfig
cmd.HostnamePolicyConfig

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

// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DefaultDBConfig(&config.CertChecker.DBConfig, &config.CertChecker.DatabaseConfig)
saDbURL, err := config.CertChecker.DBConfig.URL()
cmd.FailOnError(err, "Couldn't load DB URL")
dbSettings := sa.DbSettings{
Expand Down
30 changes: 30 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type ServiceConfig struct {
// DBConfig defines how to connect to a database. The connect string may be
// stored in a file separate from the config, because it can contain a password,
// which we want to keep out of configs.
// TODO(#5275): Refactor once each component struct and all configs in
// dev, staging and prod have been updated to contain the named
// `DBConfig` field
type DBConfig struct {
DBConnect string
// A file containing a connect URL for the DB.
Expand Down Expand Up @@ -75,6 +78,15 @@ type DBConfig struct {
ConnMaxIdleTime ConfigDuration
}

// DatabaseConfig is a temporary struct 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 `dbconfig` field
type DatabaseConfig struct {
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 +98,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 `dbconfig` field
func DefaultDBConfig(dbConfig *DBConfig, databaseConfig *DatabaseConfig) {
if databaseConfig.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 @@ -15,11 +15,17 @@ func TestDBConfigURL(t *testing.T) {
}{
{
// Test with one config file that has no trailing newline
// TODO(#5275): Refactor once each component struct and all
// configs in dev, staging and prod have been updated to
// contain the named `DBConfig` field
conf: DBConfig{DBConnectFile: "testdata/test_dburl"},
expected: "test@tcp(testhost:3306)/testDB?readTimeout=800ms&writeTimeout=800ms",
},
{
// Test with a config file that *has* a trailing newline
// TODO(#5275): Refactor once each component struct and all
// configs in dev, staging and prod have been updated to
// contain the named `DBConfig` field
conf: DBConfig{DBConnectFile: "testdata/test_dburl_newline"},
expected: "test@tcp(testhost:3306)/testDB?readTimeout=800ms&writeTimeout=800ms",
},
Expand Down Expand Up @@ -98,3 +104,30 @@ func TestTLSConfigLoad(t *testing.T) {
})
}
}

func TestDefaultDBConfig(t *testing.T) {
exampleDBConfig := &DBConfig{}
exampleDatabaseConfig := &DatabaseConfig{
DBConfig{
DBConnect: "some secret",
DBConnectFile: "foo/bar",
MaxOpenConns: 100,
MaxIdleConns: 100,
ConnMaxLifetime: ConfigDuration{10000},
ConnMaxIdleTime: ConfigDuration{10000},
},
}
DefaultDBConfig(exampleDBConfig, exampleDatabaseConfig)
test.AssertEquals(t, *exampleDBConfig, exampleDatabaseConfig.DBConfig)
exampleDBConfig = &DBConfig{
DBConnect: "some secret",
DBConnectFile: "foo/bar",
MaxOpenConns: 100,
MaxIdleConns: 100,
ConnMaxLifetime: ConfigDuration{10000},
ConnMaxIdleTime: ConfigDuration{10000},
}
exampleDatabaseConfig = &DatabaseConfig{}
DefaultDBConfig(exampleDBConfig, exampleDatabaseConfig)
test.AssertEquals(t, exampleDBConfig.MaxOpenConns, 100)
}
8 changes: 7 additions & 1 deletion 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
DBConfig cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DatabaseConfig
cmd.SMTPConfig

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

// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DefaultDBConfig(&c.Mailer.DBConfig, &c.Mailer.DatabaseConfig)
// Configure DB
dbURL, err := c.Mailer.DBConfig.URL()
cmd.FailOnError(err, "Couldn't load DB URL")
Expand Down
8 changes: 7 additions & 1 deletion 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
DBConfig cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DatabaseConfig
DebugAddr string
Syslog cmd.SyslogConfig
Features map[string]bool
Expand Down Expand Up @@ -76,6 +79,9 @@ func main() {

clk := cmd.Clock()

// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DefaultDBConfig(&c.ExpiredAuthzPurger2.DBConfig, &c.ExpiredAuthzPurger2.DatabaseConfig)
dbURL, err := c.ExpiredAuthzPurger2.DBConfig.URL()
cmd.FailOnError(err, "Couldn't load DB URL")
dbSettings := sa.DbSettings{
Expand Down
8 changes: 7 additions & 1 deletion 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
DBConfig cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DatabaseConfig
cmd.PasswordConfig
Features map[string]bool
}
Expand Down Expand Up @@ -174,6 +177,9 @@ func main() {
err = features.Set(cfg.ContactExporter.Features)
cmd.FailOnError(err, "Failed to set feature flags")

// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DefaultDBConfig(&cfg.ContactExporter.DBConfig, &cfg.ContactExporter.DatabaseConfig)
dbURL, err := cfg.ContactExporter.DBConfig.URL()
cmd.FailOnError(err, "Couldn't load DB URL")
dbSettings := sa.DbSettings{
Expand Down
8 changes: 7 additions & 1 deletion 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
DBConfig cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DatabaseConfig
cmd.PasswordConfig
cmd.SMTPConfig
Features map[string]bool
Expand Down Expand Up @@ -439,6 +442,9 @@ func main() {
log := cmd.NewLogger(cfg.Syslog)
defer log.AuditPanic()

// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DefaultDBConfig(&cfg.NotifyMailer.DBConfig, &cfg.NotifyMailer.DatabaseConfig)
dbURL, err := cfg.NotifyMailer.DBConfig.URL()
cmd.FailOnError(err, "Couldn't load DB URL")
dbSettings := sa.DbSettings{
Expand Down
10 changes: 9 additions & 1 deletion cmd/ocsp-responder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,12 @@ func (src *dbSource) Response(req *ocsp.Request) ([]byte, http.Header, error) {
type config struct {
OCSPResponder struct {
cmd.ServiceConfig
cmd.DBConfig

DBConfig cmd.DBConfig

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

// Source indicates the source of pre-signed OCSP responses to be used. It
// can be a DBConnect string or a file URL. The file URL style is used
Expand Down Expand Up @@ -270,6 +275,9 @@ as generated by Boulder's ceremony command.
source, err = bocsp.NewMemorySourceFromFile(filename, logger)
cmd.FailOnError(err, fmt.Sprintf("Couldn't read file: %s", url.Path))
} else {
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DefaultDBConfig(&config.DBConfig, &config.DatabaseConfig)
// For databases, DBConfig takes precedence over Source, if present.
dbConnect, err := config.DBConfig.URL()
cmd.FailOnError(err, "Reading DB config")
Expand Down
9 changes: 8 additions & 1 deletion cmd/ocsp-updater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,11 @@ type config struct {
// for the OCSP (and SCT) updater
type OCSPUpdaterConfig struct {
cmd.ServiceConfig
cmd.DBConfig
DBConfig cmd.DBConfig

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

OldOCSPWindow cmd.ConfigDuration
OldOCSPBatchSize int
Expand Down Expand Up @@ -341,6 +345,9 @@ func main() {
defer logger.AuditPanic()
logger.Info(cmd.VersionString())

// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DefaultDBConfig(&conf.DBConfig, &conf.DatabaseConfig)
// Configure DB
dbURL, err := conf.DBConfig.URL()
cmd.FailOnError(err, "Couldn't load DB URL")
Expand Down
4 changes: 3 additions & 1 deletion test/authz-filler/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"Filler": {
"Parallelism": 20,
"dbConnectFile": "test/secrets/sa_dburl",
"dbconfig": {
"dbConnectFile": "test/secrets/sa_dburl"
}
}
}
7 changes: 6 additions & 1 deletion test/authz-filler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (

type fillerConfig struct {
Filler struct {
cmd.DBConfig
// TODO(#5275): Remove once all configs in dev, staging and prod
// have been updated to contain `dbconfig` field
cmd.DatabaseConfig
Parallelism uint
}
}
Expand All @@ -44,6 +46,9 @@ func main() {
err = json.Unmarshal(configJSON, &config)
cmd.FailOnError(err, "Failed to parse config")

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

0 comments on commit 1463e65

Please sign in to comment.