Skip to content

Commit

Permalink
Merge pull request #1195 from BowonY/bowon/avoid-select-all
Browse files Browse the repository at this point in the history
  • Loading branch information
nickysemenza committed Jun 17, 2021
2 parents 1f29b04 + 19c09ff commit f4208c6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions certdb/certdb.go
Expand Up @@ -77,6 +77,7 @@ type Accessor interface {
GetUnexpiredCertificates() ([]CertificateRecord, error)
GetRevokedAndUnexpiredCertificates() ([]CertificateRecord, error)
GetRevokedAndUnexpiredCertificatesByLabel(label string) ([]CertificateRecord, error)
GetRevokedAndUnexpiredCertificatesByLabelSelectColumns(label string) ([]CertificateRecord, error)
RevokeCertificate(serial, aki string, reasonCode int) error
InsertOCSP(rr OCSPRecord) error
GetOCSP(serial, aki string) ([]OCSPRecord, error)
Expand Down
19 changes: 19 additions & 0 deletions certdb/sql/database_accessor.go
Expand Up @@ -36,6 +36,10 @@ SELECT %s FROM certificates
SELECT %s FROM certificates
WHERE CURRENT_TIMESTAMP < expiry AND status='revoked' AND ca_label= ?;`

selectRevokedAndUnexpiredWithLabelSQL = `
SELECT serial_number, revoked_at FROM certificates
WHERE CURRENT_TIMESTAMP < expiry AND status='revoked' AND ca_label= ?;`

selectAllRevokedAndUnexpiredSQL = `
SELECT %s FROM certificates
WHERE CURRENT_TIMESTAMP < expiry AND status='revoked';`
Expand Down Expand Up @@ -202,6 +206,21 @@ func (d *Accessor) GetRevokedAndUnexpiredCertificatesByLabel(label string) (crs
return crs, nil
}

// GetRevokedAndUnexpiredCertificatesSelectColumnsByLabel gets serial_number and revoed_at from all revoked and unexpired certificate from db (for CRLs) with specified ca_label.
func (d *Accessor) GetRevokedAndUnexpiredCertificatesByLabelSelectColumns(label string) (crs []certdb.CertificateRecord, err error) {
err = d.checkDB()
if err != nil {
return nil, err
}

err = d.db.Select(&crs, d.db.Rebind(selectRevokedAndUnexpiredWithLabelSQL), label)
if err != nil {
return nil, wrapSQLError(err)
}

return crs, nil
}

// RevokeCertificate updates a certificate with a given serial number and marks it revoked.
func (d *Accessor) RevokeCertificate(serial, aki string, reasonCode int) error {
err := d.checkDB()
Expand Down
11 changes: 11 additions & 0 deletions certdb/sql/sql_test.go
Expand Up @@ -280,6 +280,17 @@ func testUpdateCertificateAndGetCertificate(ta TestAccessor, t *testing.T) {
want.PEM != got.PEM {
t.Errorf("want Certificate %+v, got %+v", want, got)
}

rets, err = ta.Accessor.GetRevokedAndUnexpiredCertificatesByLabelSelectColumns("")
if err != nil {
t.Fatal(err)
}

got = rets[0]
// reflection comparison with zero time objects are not stable as it seems
if want.Serial != got.Serial || got.RevokedAt.IsZero() {
t.Errorf("want Certificate %+v, got %+v", want, got)
}
}

func testInsertOCSPAndGetOCSP(ta TestAccessor, t *testing.T) {
Expand Down

0 comments on commit f4208c6

Please sign in to comment.