Skip to content

Commit 6b1c7b7

Browse files
authored
Use context.WithoutCancel instead of context.Background (#8635)
I found a few places where we were using context.Background(), but actually had a perfectly good parent context to inherit from available. Fix those to better match best practices, and propagate the parent context's other metadata deeper into the call stack.
1 parent edbb62c commit 6b1c7b7

5 files changed

Lines changed: 15 additions & 13 deletions

File tree

ca/ca.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ func (ca *certificateAuthorityImpl) IssueCertificate(ctx context.Context, req *c
324324
// an error immediately after signing the precertificate, we have a record in the DB of what we
325325
// intended to sign, and can do revocations based on that. See #6807.
326326
// The name of the SA method ("AddPrecertificate") is a historical artifact.
327-
_, err = ca.sa.AddPrecertificate(context.Background(), &sapb.AddCertificateRequest{
327+
_, err = ca.sa.AddPrecertificate(ctx, &sapb.AddCertificateRequest{
328328
Der: lintPrecertDER,
329329
RegID: req.RegistrationID,
330330
Issued: timestamppb.New(ca.clk.Now()),

cmd/bad-key-revoker/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ func (bkr *badKeyRevoker) markRowChecked(ctx context.Context, unchecked unchecke
176176
// revokeCerts revokes all the provided certificates. It uses reason
177177
// keyCompromise and includes note indicating that they were revoked by
178178
// bad-key-revoker.
179-
func (bkr *badKeyRevoker) revokeCerts(certs []unrevokedCertificate) error {
179+
func (bkr *badKeyRevoker) revokeCerts(ctx context.Context, certs []unrevokedCertificate) error {
180180
for _, cert := range certs {
181-
_, err := bkr.raClient.AdministrativelyRevokeCertificate(context.Background(), &rapb.AdministrativelyRevokeCertificateRequest{
181+
_, err := bkr.raClient.AdministrativelyRevokeCertificate(ctx, &rapb.AdministrativelyRevokeCertificateRequest{
182182
Cert: cert.DER,
183183
Serial: cert.Serial,
184184
Code: int64(revocation.KeyCompromise),
@@ -252,7 +252,7 @@ func (bkr *badKeyRevoker) invoke(ctx context.Context) (work bool, err error) {
252252
logEvent["serials"] = serials
253253

254254
// revoke each certificate
255-
err = bkr.revokeCerts(unrevokedCerts)
255+
err = bkr.revokeCerts(ctx, unrevokedCerts)
256256
if err != nil {
257257
return false, err
258258
}

cmd/bad-key-revoker/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func TestRevokeCerts(t *testing.T) {
282282
certsRevoked: prometheus.NewCounter(prometheus.CounterOpts{}),
283283
}
284284

285-
err = bkr.revokeCerts([]unrevokedCertificate{
285+
err = bkr.revokeCerts(t.Context(), []unrevokedCertificate{
286286
{ID: 0, Serial: "ff"},
287287
{ID: 1, Serial: "ee"},
288288
})

ctpolicy/ctpolicy.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (ctp *CTPolicy) GetSCTs(ctx context.Context, cert core.CertDER, expiration
127127
go ctp.getOne(subCtx, cert, logs[nextLog], resChan)
128128
}
129129

130-
go ctp.submitPrecertInformational(cert, expiration)
130+
go ctp.submitPrecertInformational(ctx, cert, expiration)
131131

132132
// staggerTicker will be used to start a new submission each stagger interval
133133
staggerTicker := time.NewTicker(ctp.stagger)
@@ -215,7 +215,9 @@ func compliantSet(results []result) core.SCTDERs {
215215
// submitAllBestEffort submits the given certificate or precertificate to every
216216
// log ("informational" for precerts, "final" for certs) configured in the policy.
217217
// It neither waits for these submission to complete, nor tracks their success.
218-
func (ctp *CTPolicy) submitAllBestEffort(blob core.CertDER, kind pubpb.SubmissionType, expiry time.Time) {
218+
func (ctp *CTPolicy) submitAllBestEffort(ctx context.Context, blob core.CertDER, kind pubpb.SubmissionType, expiry time.Time) {
219+
ctx = context.WithoutCancel(ctx)
220+
219221
logs := ctp.finalLogs
220222
if kind == pubpb.SubmissionType_info {
221223
logs = ctp.infoLogs
@@ -228,7 +230,7 @@ func (ctp *CTPolicy) submitAllBestEffort(blob core.CertDER, kind pubpb.Submissio
228230

229231
go func(log loglist.Log) {
230232
_, err := ctp.pub.SubmitToSingleCTWithResult(
231-
context.Background(),
233+
ctx,
232234
&pubpb.Request{
233235
LogURL: log.Url,
234236
LogPublicKey: base64.StdEncoding.EncodeToString(log.Key),
@@ -245,12 +247,12 @@ func (ctp *CTPolicy) submitAllBestEffort(blob core.CertDER, kind pubpb.Submissio
245247

246248
// submitPrecertInformational submits precertificates to any configured
247249
// "informational" logs, but does not care about success or returned SCTs.
248-
func (ctp *CTPolicy) submitPrecertInformational(cert core.CertDER, expiration time.Time) {
249-
ctp.submitAllBestEffort(cert, pubpb.SubmissionType_info, expiration)
250+
func (ctp *CTPolicy) submitPrecertInformational(ctx context.Context, cert core.CertDER, expiration time.Time) {
251+
ctp.submitAllBestEffort(ctx, cert, pubpb.SubmissionType_info, expiration)
250252
}
251253

252254
// SubmitFinalCert submits finalized certificates created from precertificates
253255
// to any configured "final" logs, but does not care about success.
254-
func (ctp *CTPolicy) SubmitFinalCert(cert core.CertDER, expiration time.Time) {
255-
ctp.submitAllBestEffort(cert, pubpb.SubmissionType_final, expiration)
256+
func (ctp *CTPolicy) SubmitFinalCert(ctx context.Context, cert core.CertDER, expiration time.Time) {
257+
ctp.submitAllBestEffort(ctx, cert, pubpb.SubmissionType_final, expiration)
256258
}

ra/ra.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,7 @@ func (ra *RegistrationAuthorityImpl) issueCertificateInner(
13351335
ra.countCertificateIssued(ctx, int64(acctID), identifier.FromCert(parsedCertificate), isRenewal)
13361336

13371337
// Asynchronously submit the final certificate to any configured logs
1338-
go ra.ctpolicy.SubmitFinalCert(resp.DER, parsedCertificate.NotAfter)
1338+
go ra.ctpolicy.SubmitFinalCert(ctx, resp.DER, parsedCertificate.NotAfter)
13391339

13401340
err = ra.matchesCSR(parsedCertificate, csr)
13411341
if err != nil {

0 commit comments

Comments
 (0)