Skip to content

Commit d6ad84e

Browse files
authored
Merge pull request #1136 from nickysemenza/fix-null-commonname
fix(certdb): allow reading other null columns (part 2 of #1135)
2 parents 8fb5413 + e8f9337 commit d6ad84e

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

certdb/certdb.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ import (
1111
// CertificateRecord encodes a certificate and its metadata
1212
// that will be recorded in a database.
1313
type CertificateRecord struct {
14-
Serial string `db:"serial_number"`
15-
AKI string `db:"authority_key_identifier"`
16-
CALabel string `db:"ca_label"`
17-
Status string `db:"status"`
18-
Reason int `db:"reason"`
19-
Expiry time.Time `db:"expiry"`
20-
RevokedAt time.Time `db:"revoked_at"`
21-
PEM string `db:"pem"`
22-
IssuedAt time.Time `db:"issued_at"`
23-
NotBefore time.Time `db:"not_before"`
14+
Serial string `db:"serial_number"`
15+
AKI string `db:"authority_key_identifier"`
16+
CALabel string `db:"ca_label"`
17+
Status string `db:"status"`
18+
Reason int `db:"reason"`
19+
Expiry time.Time `db:"expiry"`
20+
RevokedAt time.Time `db:"revoked_at"`
21+
PEM string `db:"pem"`
22+
// the following fields will be empty for data inserted before migrate 002 has been run.
23+
IssuedAt *time.Time `db:"issued_at"`
24+
NotBefore *time.Time `db:"not_before"`
2425
MetadataJSON types.JSONText `db:"metadata"`
2526
SANsJSON types.JSONText `db:"sans"`
2627
CommonName sql.NullString `db:"common_name"`

certdb/sql/database_accessor.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ func (d *Accessor) InsertCertificate(cr certdb.CertificateRecord) error {
101101
return err
102102
}
103103

104+
var issuedAt, notBefore *time.Time
105+
if cr.IssuedAt != nil {
106+
t := cr.IssuedAt.UTC()
107+
issuedAt = &t
108+
}
109+
if cr.NotBefore != nil {
110+
t := cr.NotBefore.UTC()
111+
notBefore = &t
112+
}
104113
res, err := d.db.NamedExec(insertSQL, &certdb.CertificateRecord{
105114
Serial: cr.Serial,
106115
AKI: cr.AKI,
@@ -110,8 +119,8 @@ func (d *Accessor) InsertCertificate(cr certdb.CertificateRecord) error {
110119
Expiry: cr.Expiry.UTC(),
111120
RevokedAt: cr.RevokedAt.UTC(),
112121
PEM: cr.PEM,
113-
IssuedAt: cr.IssuedAt.UTC(),
114-
NotBefore: cr.NotBefore.UTC(),
122+
IssuedAt: issuedAt,
123+
NotBefore: notBefore,
115124
MetadataJSON: cr.MetadataJSON,
116125
SANsJSON: cr.SANsJSON,
117126
CommonName: cr.CommonName,

certdb/sql/sql_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,12 @@ func testInsertCertificateAndGetUnexpiredCertificateNullCommonName(ta TestAccess
172172
}
173173

174174
// simulate situation where there are rows before migrate 002 has been run
175-
ta.DB.MustExec("update certificates set common_name = NULL")
175+
ta.DB.MustExec(`update certificates
176+
set issued_at = NULL,
177+
not_before = NULL,
178+
metadata = NULL,
179+
sans = NULL,
180+
common_name = NULL;`)
176181

177182
rets, err := ta.Accessor.GetCertificate(want.Serial, want.AKI)
178183
if err != nil {

signer/local/local.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ func (s *Signer) Sign(req signer.SignRequest) (cert []byte, err error) {
507507
parsedCert, _ := helpers.ParseCertificatePEM(signedCert)
508508

509509
if s.dbAccessor != nil {
510+
now := time.Now()
510511
var certRecord = certdb.CertificateRecord{
511512
Serial: certTBS.SerialNumber.String(),
512513
// this relies on the specific behavior of x509.CreateCertificate
@@ -516,8 +517,8 @@ func (s *Signer) Sign(req signer.SignRequest) (cert []byte, err error) {
516517
Status: "good",
517518
Expiry: certTBS.NotAfter,
518519
PEM: string(signedCert),
519-
IssuedAt: time.Now(),
520-
NotBefore: certTBS.NotBefore,
520+
IssuedAt: &now,
521+
NotBefore: &certTBS.NotBefore,
521522
CommonName: sql.NullString{String: certTBS.Subject.CommonName, Valid: true},
522523
}
523524

0 commit comments

Comments
 (0)