Skip to content
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

require db for contentsignaturepki gnight #1065

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
7 changes: 7 additions & 0 deletions database/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,10 @@ func GetTestDBHost() string {
}
return host
}

// DeleteAllIn removes all the data from the given database. It's meant only
// for use in the tests.
func DeleteAllIn(db *Handler) error {
_, err := db.DB.Exec("truncate table endentities;")
return err
}
16 changes: 8 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,13 @@ func run(conf configuration, listen string, debug bool) {
ag = newAutographer(conf.Server.NonceCacheSize)
ag.heartbeatConf = &conf.Heartbeat

if conf.Database.Name != "" {
if conf.Database.Name != "" || os.Getenv("AUTOGRAPH_DB_DSN") != "" {
// ignore the monitor close chan since it will stop
// when the app is stopped
_ = ag.addDB(conf.Database)
err = ag.addDB(conf.Database)
if err != nil {
log.Fatalf("main.run failed to add database: %s", err)
}
}

// initialize the hsm if a configuration is defined
Expand Down Expand Up @@ -361,21 +364,18 @@ func (a *autographer) startCleanupHandler() {

// addDB connects to the DB and starts a gorountine to monitor DB
// connectivity
func (a *autographer) addDB(dbConf database.Config) chan bool {
func (a *autographer) addDB(dbConf database.Config) error {
var err error
a.db, err = database.Connect(dbConf)
if err != nil {
log.Fatal(err)
}
if a.db == nil {
log.Fatal("failed to initialize database connection, unknown error")
return fmt.Errorf("failed to connect to database: %w", err)
}
// start a monitoring function that errors if the db
// becomes inaccessible
closeDBMonitor := make(chan bool, 1)
go a.db.Monitor(dbConf.MonitorPollInterval, closeDBMonitor)
log.Print("database connection established")
return closeDBMonitor
return nil
}

// initHSM sets up the HSM and notifies signers it is available
Expand Down
18 changes: 17 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ func newTestAutographer(t *testing.T) (*autographer, configuration) {
log.Fatal(err)
}
ag := newAutographer(1)
// FIXME refactor helper
host := database.GetTestDBHost()
err = ag.addDB(database.Config{
Name: "autograph",
User: "myautographdbuser",
Password: "myautographdbpassword",
Host: host + ":5432",
MonitorPollInterval: 10 * time.Second,
})
if err != nil {
log.Fatalf("newTestAutographer: failed to connect to db: %s", err)
}
err = database.DeleteAllIn(ag.db)
if err != nil {
log.Fatalf("newTestAutographer: failed to delete all in db %s", err)
}
err = ag.addSigners(conf.Signers)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -60,7 +76,7 @@ func newTestAutographer(t *testing.T) (*autographer, configuration) {
MonitorPollInterval: 10 * time.Second,
})
if err == nil {
db.Exec("truncate table endentities;")
database.DeleteAllIn(db)
}
close(ag.exit)
})
Expand Down
3 changes: 3 additions & 0 deletions signer/contentsignaturepki/contentsignature.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func New(conf signer.Configuration) (s *ContentSigner, err error) {
s.chainUploadLocation = conf.ChainUploadLocation
s.caCert = conf.CaCert
s.db = conf.DB
if s.db == nil {
return nil, fmt.Errorf("contentsignaturepki %q: a database is required by the contentsignaturepki signer type but none have been configured", s.ID)
}
s.subdomainOverride = conf.SubdomainOverride

if conf.Type != Type {
Expand Down
Loading
Loading