Fix race condition in signer

このコミットが含まれているのは:
Cory Slep 2019-05-11 11:33:55 +02:00
コミット f339304ce4
1個のファイルの変更24行の追加16行の削除

ファイルの表示

@ -60,14 +60,16 @@ var _ Transport = &HttpSigTransport{}
// //
// Only one request is tried per call. // Only one request is tried per call.
type HttpSigTransport struct { type HttpSigTransport struct {
client HttpClient client HttpClient
appAgent string appAgent string
gofedAgent string gofedAgent string
clock Clock clock Clock
getSigner httpsig.Signer getSigner httpsig.Signer
postSigner httpsig.Signer getSignerMu *sync.Mutex
pubKeyId string postSigner httpsig.Signer
privKey crypto.PrivateKey postSignerMu *sync.Mutex
pubKeyId string
privKey crypto.PrivateKey
} }
// NewHttpSigTransport returns a new Transport. // NewHttpSigTransport returns a new Transport.
@ -93,14 +95,16 @@ func NewHttpSigTransport(
pubKeyId string, pubKeyId string,
privKey crypto.PrivateKey) *HttpSigTransport { privKey crypto.PrivateKey) *HttpSigTransport {
return &HttpSigTransport{ return &HttpSigTransport{
client: client, client: client,
appAgent: appAgent, appAgent: appAgent,
gofedAgent: goFedUserAgent(), gofedAgent: goFedUserAgent(),
clock: clock, clock: clock,
getSigner: getSigner, getSigner: getSigner,
postSigner: postSigner, getSignerMu: &sync.Mutex{},
pubKeyId: pubKeyId, postSigner: postSigner,
privKey: privKey, postSignerMu: &sync.Mutex{},
pubKeyId: pubKeyId,
privKey: privKey,
} }
} }
@ -116,7 +120,9 @@ func (h HttpSigTransport) Dereference(c context.Context, iri *url.URL) ([]byte,
req.Header.Add("Accept-Charset", "utf-8") req.Header.Add("Accept-Charset", "utf-8")
req.Header.Add("Date", h.clock.Now().UTC().Format("Mon, 02 Jan 2006 15:04:05")+" GMT") req.Header.Add("Date", h.clock.Now().UTC().Format("Mon, 02 Jan 2006 15:04:05")+" GMT")
req.Header.Add("User-Agent", fmt.Sprintf("%s %s", h.appAgent, h.gofedAgent)) req.Header.Add("User-Agent", fmt.Sprintf("%s %s", h.appAgent, h.gofedAgent))
h.getSignerMu.Lock()
err = h.getSigner.SignRequest(h.privKey, h.pubKeyId, req) err = h.getSigner.SignRequest(h.privKey, h.pubKeyId, req)
h.getSignerMu.Unlock()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -149,7 +155,9 @@ func (h HttpSigTransport) Deliver(c context.Context, b []byte, to *url.URL) erro
req.Header.Add("Digest", req.Header.Add("Digest",
fmt.Sprintf("SHA-256=%s", fmt.Sprintf("SHA-256=%s",
base64.StdEncoding.EncodeToString(sum[:]))) base64.StdEncoding.EncodeToString(sum[:])))
h.postSignerMu.Lock()
err = h.postSigner.SignRequest(h.privKey, h.pubKeyId, req) err = h.postSigner.SignRequest(h.privKey, h.pubKeyId, req)
h.postSignerMu.Unlock()
if err != nil { if err != nil {
return err return err
} }