X Tutup
Skip to content

Commit ad34089

Browse files
authored
Check for duplicate certs before adding to db (letsencrypt#5497)
* Check for duplicate certs before adding to db Error at SA if the certificate or precertificate already exist in the database Fixes: letsencrypt#5468
1 parent fa30f17 commit ad34089

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

sa/precertificates.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ func (ssa *SQLStorageAuthority) AddPrecertificate(ctx context.Context, req *sapb
6161
}
6262

6363
_, overallError := db.WithTransaction(ctx, ssa.dbMap, func(txWithCtx db.Executor) (interface{}, error) {
64+
// Select to see if precert exists
65+
var row struct {
66+
Count int64
67+
}
68+
if err := txWithCtx.SelectOne(&row, "SELECT count(1) as count FROM precertificates WHERE serial=?", serialHex); err != nil {
69+
return nil, err
70+
}
71+
if row.Count > 0 {
72+
return nil, berrors.DuplicateError("cannot add a duplicate cert")
73+
}
6474
if err := txWithCtx.Insert(preCertModel); err != nil {
6575
return nil, err
6676
}

sa/precertificates_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/letsencrypt/boulder/db"
11+
berrors "github.com/letsencrypt/boulder/errors"
1112
sapb "github.com/letsencrypt/boulder/sa/proto"
1213
"github.com/letsencrypt/boulder/sa/satest"
1314
"github.com/letsencrypt/boulder/test"
@@ -87,6 +88,32 @@ func TestAddPrecertificate(t *testing.T) {
8788
addPrecert(true)
8889
}
8990

91+
func TestAddPreCertificateDuplicate(t *testing.T) {
92+
sa, clk, cleanUp := initSA(t)
93+
defer cleanUp()
94+
95+
reg := satest.CreateWorkingRegistration(t, sa)
96+
97+
_, testCert := test.ThrowAwayCert(t, 1)
98+
99+
_, err := sa.AddPrecertificate(ctx, &sapb.AddCertificateRequest{
100+
Der: testCert.Raw,
101+
Issued: clk.Now().UnixNano(),
102+
RegID: reg.ID,
103+
IssuerID: 1,
104+
})
105+
test.AssertNotError(t, err, "Couldn't add test certificate")
106+
107+
_, err = sa.AddPrecertificate(ctx, &sapb.AddCertificateRequest{
108+
Der: testCert.Raw,
109+
Issued: clk.Now().UnixNano(),
110+
RegID: reg.ID,
111+
IssuerID: 1,
112+
})
113+
test.AssertDeepEquals(t, err, berrors.DuplicateError("cannot add a duplicate cert"))
114+
115+
}
116+
90117
func TestAddPrecertificateIncomplete(t *testing.T) {
91118
sa, _, cleanUp := initSA(t)
92119
defer cleanUp()

sa/sa.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,19 @@ func (ssa *SQLStorageAuthority) AddCertificate(
417417
}
418418

419419
isRenewalRaw, overallError := db.WithTransaction(ctx, ssa.dbMap, func(txWithCtx db.Executor) (interface{}, error) {
420+
// Select to see if cert exists
421+
var row struct {
422+
Count int64
423+
}
424+
if err := txWithCtx.SelectOne(&row, "SELECT count(1) as count FROM certificates WHERE serial=?", serial); err != nil {
425+
return nil, err
426+
}
427+
if row.Count > 0 {
428+
return nil, berrors.DuplicateError("cannot add a duplicate cert")
429+
}
420430
// Save the final certificate
421431
err = txWithCtx.Insert(cert)
422432
if err != nil {
423-
if db.IsDuplicate(err) {
424-
return nil, berrors.DuplicateError("cannot add a duplicate cert")
425-
}
426433
return nil, err
427434
}
428435

sa/sa_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,23 @@ func TestAddCertificate(t *testing.T) {
235235
test.AssertNotError(t, err, "Couldn't add test-cert2.der")
236236
}
237237

238+
func TestAddCertificateDuplicate(t *testing.T) {
239+
sa, clk, cleanUp := initSA(t)
240+
defer cleanUp()
241+
242+
reg := satest.CreateWorkingRegistration(t, sa)
243+
244+
_, testCert := test.ThrowAwayCert(t, 1)
245+
246+
issuedTime := clk.Now()
247+
_, err := sa.AddCertificate(ctx, testCert.Raw, reg.ID, nil, &issuedTime)
248+
test.AssertNotError(t, err, "Couldn't add test certificate")
249+
250+
_, err = sa.AddCertificate(ctx, testCert.Raw, reg.ID, nil, &issuedTime)
251+
test.AssertDeepEquals(t, err, berrors.DuplicateError("cannot add a duplicate cert"))
252+
253+
}
254+
238255
func TestCountCertificatesByNames(t *testing.T) {
239256
sa, clk, cleanUp := initSA(t)
240257
defer cleanUp()

0 commit comments

Comments
 (0)
X Tutup