renewal.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package autocert
  5. import (
  6. "context"
  7. "crypto"
  8. "sync"
  9. "time"
  10. )
  11. // renewJitter is the maximum deviation from Manager.RenewBefore.
  12. const renewJitter = time.Hour
  13. // domainRenewal tracks the state used by the periodic timers
  14. // renewing a single domain's cert.
  15. type domainRenewal struct {
  16. m *Manager
  17. ck certKey
  18. key crypto.Signer
  19. timerMu sync.Mutex
  20. timer *time.Timer
  21. timerClose chan struct{} // if non-nil, renew closes this channel (and nils out the timer fields) instead of running
  22. }
  23. // start starts a cert renewal timer at the time
  24. // defined by the certificate expiration time exp.
  25. //
  26. // If the timer is already started, calling start is a noop.
  27. func (dr *domainRenewal) start(exp time.Time) {
  28. dr.timerMu.Lock()
  29. defer dr.timerMu.Unlock()
  30. if dr.timer != nil {
  31. return
  32. }
  33. dr.timer = time.AfterFunc(dr.next(exp), dr.renew)
  34. }
  35. // stop stops the cert renewal timer and waits for any in-flight calls to renew
  36. // to complete. If the timer is already stopped, calling stop is a noop.
  37. func (dr *domainRenewal) stop() {
  38. dr.timerMu.Lock()
  39. defer dr.timerMu.Unlock()
  40. for {
  41. if dr.timer == nil {
  42. return
  43. }
  44. if dr.timer.Stop() {
  45. dr.timer = nil
  46. return
  47. } else {
  48. // dr.timer fired, and we acquired dr.timerMu before the renew callback did.
  49. // (We know this because otherwise the renew callback would have reset dr.timer!)
  50. timerClose := make(chan struct{})
  51. dr.timerClose = timerClose
  52. dr.timerMu.Unlock()
  53. <-timerClose
  54. dr.timerMu.Lock()
  55. }
  56. }
  57. }
  58. // renew is called periodically by a timer.
  59. // The first renew call is kicked off by dr.start.
  60. func (dr *domainRenewal) renew() {
  61. dr.timerMu.Lock()
  62. defer dr.timerMu.Unlock()
  63. if dr.timerClose != nil {
  64. close(dr.timerClose)
  65. dr.timer, dr.timerClose = nil, nil
  66. return
  67. }
  68. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
  69. defer cancel()
  70. // TODO: rotate dr.key at some point?
  71. next, err := dr.do(ctx)
  72. if err != nil {
  73. next = renewJitter / 2
  74. next += time.Duration(pseudoRand.int63n(int64(next)))
  75. }
  76. testDidRenewLoop(next, err)
  77. dr.timer = time.AfterFunc(next, dr.renew)
  78. }
  79. // updateState locks and replaces the relevant Manager.state item with the given
  80. // state. It additionally updates dr.key with the given state's key.
  81. func (dr *domainRenewal) updateState(state *certState) {
  82. dr.m.stateMu.Lock()
  83. defer dr.m.stateMu.Unlock()
  84. dr.key = state.key
  85. dr.m.state[dr.ck] = state
  86. }
  87. // do is similar to Manager.createCert but it doesn't lock a Manager.state item.
  88. // Instead, it requests a new certificate independently and, upon success,
  89. // replaces dr.m.state item with a new one and updates cache for the given domain.
  90. //
  91. // It may lock and update the Manager.state if the expiration date of the currently
  92. // cached cert is far enough in the future.
  93. //
  94. // The returned value is a time interval after which the renewal should occur again.
  95. func (dr *domainRenewal) do(ctx context.Context) (time.Duration, error) {
  96. // a race is likely unavoidable in a distributed environment
  97. // but we try nonetheless
  98. if tlscert, err := dr.m.cacheGet(ctx, dr.ck); err == nil {
  99. next := dr.next(tlscert.Leaf.NotAfter)
  100. if next > dr.m.renewBefore()+renewJitter {
  101. signer, ok := tlscert.PrivateKey.(crypto.Signer)
  102. if ok {
  103. state := &certState{
  104. key: signer,
  105. cert: tlscert.Certificate,
  106. leaf: tlscert.Leaf,
  107. }
  108. dr.updateState(state)
  109. return next, nil
  110. }
  111. }
  112. }
  113. der, leaf, err := dr.m.authorizedCert(ctx, dr.key, dr.ck)
  114. if err != nil {
  115. return 0, err
  116. }
  117. state := &certState{
  118. key: dr.key,
  119. cert: der,
  120. leaf: leaf,
  121. }
  122. tlscert, err := state.tlscert()
  123. if err != nil {
  124. return 0, err
  125. }
  126. if err := dr.m.cachePut(ctx, dr.ck, tlscert); err != nil {
  127. return 0, err
  128. }
  129. dr.updateState(state)
  130. return dr.next(leaf.NotAfter), nil
  131. }
  132. func (dr *domainRenewal) next(expiry time.Time) time.Duration {
  133. d := expiry.Sub(dr.m.now()) - dr.m.renewBefore()
  134. // add a bit of randomness to renew deadline
  135. n := pseudoRand.int63n(int64(renewJitter))
  136. d -= time.Duration(n)
  137. if d < 0 {
  138. return 0
  139. }
  140. return d
  141. }
  142. var testDidRenewLoop = func(next time.Duration, err error) {}