Skip to content

Commit 4518f0b

Browse files
author
J.C. Jones
committed
Migrate CADB to using GORP.
1 parent 1008bd8 commit 4518f0b

File tree

10 files changed

+45
-59
lines changed

10 files changed

+45
-59
lines changed

ca/certificate-authority-data.go

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,61 @@
66
package ca
77

88
import (
9-
"database/sql"
109
"errors"
1110
"time"
1211

1312
"github.com/letsencrypt/boulder/core"
1413
blog "github.com/letsencrypt/boulder/log"
14+
"github.com/letsencrypt/boulder/sa"
15+
16+
gorp "github.com/letsencrypt/boulder/Godeps/_workspace/src/gopkg.in/gorp.v1"
1517
)
1618

1719
// CertificateAuthorityDatabaseImpl represents a database used by the CA; it
1820
// enforces transaction semantics, and is effectively single-threaded.
1921
type CertificateAuthorityDatabaseImpl struct {
2022
log *blog.AuditLogger
21-
db *sql.DB
22-
activeTx *sql.Tx
23+
dbMap *gorp.DbMap
24+
activeTx *gorp.Transaction
25+
}
26+
27+
type SerialNumber struct {
28+
ID int `db:"id"`
29+
Number int64 `db:"number"`
30+
LastUpdated time.Time `db:"lastUpdated"`
2331
}
2432

2533
// NewCertificateAuthorityDatabaseImpl constructs a Database for the
2634
// Certificate Authority.
2735
func NewCertificateAuthorityDatabaseImpl(driver string, name string) (cadb core.CertificateAuthorityDatabase, err error) {
2836
logger := blog.GetAuditLogger()
2937

30-
db, err := sql.Open(driver, name)
38+
dbMap, err := sa.NewDbMap(driver, name)
3139
if err != nil {
32-
return
33-
}
34-
if err = db.Ping(); err != nil {
35-
return
40+
return nil, err
3641
}
3742

43+
dbMap.AddTableWithName(SerialNumber{}, "serialNumber").SetKeys(true, "ID")
44+
3845
cadb = &CertificateAuthorityDatabaseImpl{
39-
db: db,
40-
log: logger,
46+
dbMap: dbMap,
47+
log: logger,
4148
}
42-
return
49+
return cadb, nil
4350
}
4451

4552
// createTablesIfNotExist builds the database tables and inserts the initial
4653
// state, if the tables do not already exist. It is not an error for the tables
4754
// to already exist.
4855
func (cadb *CertificateAuthorityDatabaseImpl) CreateTablesIfNotExists() (err error) {
49-
tx, err := cadb.db.Begin()
50-
if err != nil {
51-
return
52-
}
53-
5456
// Create serial number table
55-
_, err = tx.Exec("CREATE TABLE serialNumber (id INTEGER, number INTEGER, lastUpdated DATETIME);")
57+
err = cadb.dbMap.CreateTablesIfNotExists()
5658
if err != nil {
57-
// If the table exists, exit early
58-
tx.Rollback()
59-
return nil
60-
}
61-
62-
// Initialize the serial number
63-
_, err = tx.Exec("INSERT INTO serialNumber (id, number, lastUpdated) VALUES (1, 1, ?);", time.Now())
64-
if err != nil {
65-
tx.Rollback()
6659
return
6760
}
6861

69-
err = tx.Commit()
62+
// Initialize the serial number
63+
err = cadb.dbMap.Insert(&SerialNumber{ID: 1, Number: 1, LastUpdated: time.Now()})
7064
return
7165
}
7266

@@ -77,7 +71,7 @@ func (cadb *CertificateAuthorityDatabaseImpl) Begin() (err error) {
7771
err = errors.New("Transaction already open")
7872
return
7973
}
80-
cadb.activeTx, err = cadb.db.Begin()
74+
cadb.activeTx, err = cadb.dbMap.Begin()
8175
return
8276
}
8377

@@ -109,21 +103,23 @@ func (cadb *CertificateAuthorityDatabaseImpl) Rollback() (err error) {
109103
// it in the database before returning. There must be an active transaction to
110104
// call this method. Callers should Begin the transaction, call this method,
111105
// perform any other work, and Commit at the end once the certificate is issued.
112-
func (cadb *CertificateAuthorityDatabaseImpl) IncrementAndGetSerial() (val int, err error) {
106+
func (cadb *CertificateAuthorityDatabaseImpl) IncrementAndGetSerial() (val int64, err error) {
113107
if cadb.activeTx == nil {
114108
err = errors.New("No transaction open")
115109
return
116110
}
117111

118-
row := cadb.activeTx.QueryRow("SELECT number FROM serialNumber LIMIT 1;")
119-
120-
err = row.Scan(&val)
112+
rowObj, err := cadb.activeTx.Get(SerialNumber{}, 1)
121113
if err != nil {
122114
cadb.activeTx.Rollback()
123115
return
124116
}
125117

126-
_, err = cadb.activeTx.Exec("UPDATE serialNumber SET number=?, lastUpdated=? WHERE id=1", val+1, time.Now())
118+
row := rowObj.(*SerialNumber)
119+
val = row.Number
120+
row.Number = val + 1
121+
122+
_, err = cadb.activeTx.Update(row)
127123
if err != nil {
128124
cadb.activeTx.Rollback()
129125
return

ca/certificate-authority_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func (cadb *MockCADatabase) Rollback() error {
318318
return nil
319319
}
320320

321-
func (cadb *MockCADatabase) IncrementAndGetSerial() (int, error) {
321+
func (cadb *MockCADatabase) IncrementAndGetSerial() (int64, error) {
322322
return 1, nil
323323
}
324324

cmd/boulder-ca/main.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ package main
88
import (
99
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/cactus/go-statsd-client/statsd"
1010
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/streadway/amqp"
11-
// Load both drivers to allow configuring either
12-
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/go-sql-driver/mysql"
13-
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/mattn/go-sqlite3"
1411

1512
"github.com/letsencrypt/boulder/ca"
1613
"github.com/letsencrypt/boulder/cmd"

cmd/boulder-sa/main.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import (
99
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/cactus/go-statsd-client/statsd"
1010
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/streadway/amqp"
1111

12-
// Load both drivers to allow configuring either
13-
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/go-sql-driver/mysql"
14-
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/mattn/go-sqlite3"
1512
"github.com/letsencrypt/boulder/cmd"
1613
blog "github.com/letsencrypt/boulder/log"
1714
"github.com/letsencrypt/boulder/rpc"

cmd/boulder/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/cactus/go-statsd-client/statsd"
15-
// Load both drivers to allow configuring either
16-
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/go-sql-driver/mysql"
17-
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/mattn/go-sqlite3"
15+
1816
"github.com/letsencrypt/boulder/ca"
1917
"github.com/letsencrypt/boulder/cmd"
2018
blog "github.com/letsencrypt/boulder/log"

cmd/ocsp-responder/main.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@ import (
1414
"net/http"
1515
"time"
1616

17-
// Load both drivers to allow configuring either
18-
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/go-sql-driver/mysql"
19-
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/mattn/go-sqlite3"
17+
gorp "github.com/letsencrypt/boulder/Godeps/_workspace/src/gopkg.in/gorp.v1"
2018

2119
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/cactus/go-statsd-client/statsd"
2220
cfocsp "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/cloudflare/cfssl/ocsp"
2321
"golang.org/x/crypto/ocsp"
2422

2523
"github.com/letsencrypt/boulder/cmd"
26-
"github.com/letsencrypt/boulder/core"
2724
blog "github.com/letsencrypt/boulder/log"
25+
"github.com/letsencrypt/boulder/sa"
2826
)
2927

3028
type timedHandler struct {
@@ -76,8 +74,8 @@ type DBSource struct {
7674
caKeyHash []byte
7775
}
7876

79-
func NewSourceFromDatabase(db *sql.DB, caKeyHash []byte) (src *DBSource, err error) {
80-
src = &DBSource{db: db, caKeyHash: caKeyHash}
77+
func NewSourceFromDatabase(dbMap *gorp.DbMap, caKeyHash []byte) (src *DBSource, err error) {
78+
src = &DBSource{db: dbMap.Db, caKeyHash: caKeyHash}
8179
return
8280
}
8381

@@ -118,10 +116,9 @@ func main() {
118116

119117
go cmd.ProfileCmd("OCSP", stats)
120118

121-
// Connect to the DB
122-
db, err := sql.Open(c.OCSPResponder.DBDriver, c.OCSPResponder.DBName)
119+
// Configure DB
120+
dbMap, err := sa.NewDbMap(c.OCSPResponder.DBDriver, c.OCSPResponder.DBName)
123121
cmd.FailOnError(err, "Could not connect to database")
124-
defer db.Close()
125122

126123
// Load the CA's key and hash it
127124
caCertDER, err := cmd.LoadCert(c.CA.IssuerCert)
@@ -133,7 +130,7 @@ func main() {
133130
caKeyHash := h.Sum(nil)
134131

135132
// Construct source from DB
136-
src, err := NewSourceFromDatabase(db, caKeyHash)
133+
src, err := NewSourceFromDatabase(dbMap, caKeyHash)
137134
cmd.FailOnError(err, "Could not connect to OCSP database")
138135

139136
// Configure HTTP

cmd/ocsp-updater/main.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ import (
1212
"math"
1313
"time"
1414

15-
// Load both drivers to allow configuring either
16-
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/go-sql-driver/mysql"
17-
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/mattn/go-sqlite3"
18-
1915
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/cactus/go-statsd-client/statsd"
2016
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/codegangsta/cli"
2117
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/streadway/amqp"

core/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,5 @@ type CertificateAuthorityDatabase interface {
124124
Begin() error
125125
Commit() error
126126
Rollback() error
127-
IncrementAndGetSerial() (int, error)
127+
IncrementAndGetSerial() (int64, error)
128128
}

ra/registration-authority_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (cadb *MockCADatabase) Rollback() error {
5858
return nil
5959
}
6060

61-
func (cadb *MockCADatabase) IncrementAndGetSerial() (int, error) {
61+
func (cadb *MockCADatabase) IncrementAndGetSerial() (int64, error) {
6262
return 1, nil
6363
}
6464

sa/database.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ package sa
88
import (
99
"database/sql"
1010
"fmt"
11+
12+
// Load both drivers to allow configuring either
13+
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/go-sql-driver/mysql"
14+
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/mattn/go-sqlite3"
15+
1116
gorp "github.com/letsencrypt/boulder/Godeps/_workspace/src/gopkg.in/gorp.v1"
1217

1318
"github.com/letsencrypt/boulder/core"

0 commit comments

Comments
 (0)