Skip to content

Commit

Permalink
Update finalise order logic, and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eggsampler committed Apr 10, 2023
1 parent c3e8167 commit 27ed5db
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
2 changes: 1 addition & 1 deletion acme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestClient_Directory(t *testing.T) {
}

func TestClient_Fetch(t *testing.T) {
_, account1order, _ := makeOrderFinalised(t, []string{ChallengeTypeDNS01}, Identifier{"dns", "example.com"})
_, account1order, _ := makeOrderFinalised(t, []string{ChallengeTypeDNS01}, Identifier{"dns", randString() + ".com"})
account2 := makeAccount(t)
err := testClient.Fetch(account2, account1order.URL, &Account{})
if err == nil {
Expand Down
67 changes: 43 additions & 24 deletions order.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,56 @@ func (c Client) FinalizeOrder(account Account, order Order, csr *x509.Certificat
}

order.URL = resp.Header.Get("Location")
retryAfter, err := parseRetryAfter(resp.Header.Get("Retry-After"))
if err != nil {
return order, fmt.Errorf("acme: error parsing retry-after header: %v", err)

updateOrder := func(resp *http.Response) (bool, error) {
if finished, err := checkFinalizedOrderStatus(order); finished {
return true, err
}

retryAfter, err := parseRetryAfter(resp.Header.Get("Retry-After"))
if err != nil {
return false, fmt.Errorf("acme: error parsing retry-after header: %v", err)
}
order.RetryAfter = retryAfter

return false, nil
}
order.RetryAfter = retryAfter

if finished, err := checkFinalizedOrderStatus(order); finished {
if finished, err := updateOrder(resp); finished || err != nil {
return order, err
}

if !c.IgnoreRetryAfter {
if retryAfter.IsZero() || retryAfter.Before(time.Now()) {
_, err := c.post(order.URL, account.URL, account.PrivateKey, "", &order, http.StatusOK)
return order, err
fetchOrder := func() (bool, error) {
resp, err := c.post(order.URL, account.URL, account.PrivateKey, "", &order, http.StatusOK)
if err != nil {
return false, nil
}
diff := time.Until(retryAfter)

return updateOrder(resp)
}

if !c.IgnoreRetryAfter && !order.RetryAfter.IsZero() {
_, pollTimeout := c.getPollingDurations()
if diff > pollTimeout {
return order, fmt.Errorf("acme: Retry-After (%v) longer than poll timeout (%v)", diff, c.PollTimeout)
end := time.Now().Add(pollTimeout)

for {
if time.Now().After(end) {
return order, errors.New("acme: finalized order timeout")
}

diff := time.Until(order.RetryAfter)
_, pollTimeout := c.getPollingDurations()
if diff > pollTimeout {
return order, fmt.Errorf("acme: Retry-After (%v) longer than poll timeout (%v)", diff, c.PollTimeout)
}
if diff > 0 {
time.Sleep(diff)
}

if finished, err := fetchOrder(); finished || err != nil {
return order, err
}
}
time.Sleep(diff)
_, err := c.post(order.URL, account.URL, account.PrivateKey, "", &order, http.StatusOK)
return order, err
}

if !c.IgnoreRetryAfter {
Expand All @@ -142,15 +169,7 @@ func (c Client) FinalizeOrder(account Account, order Order, csr *x509.Certificat
}
time.Sleep(pollInterval)

if _, err := c.post(order.URL, account.URL, account.PrivateKey, "", &order, http.StatusOK); err != nil {
// i dont think it's worth exiting the loop on this error
// it could just be connectivity issue thats resolved before the timeout duration
continue
}

order.URL = resp.Header.Get("Location")

if finished, err := checkFinalizedOrderStatus(order); finished {
if finished, err := fetchOrder(); finished || err != nil {
return order, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestProblem_Error(t *testing.T) {
{
Type: "type2",
Detail: "detail",
Identifier: Identifier{"DNS", "example.com"},
Identifier: Identifier{"DNS", randString() + ".com"},
},
},
}
Expand Down

0 comments on commit 27ed5db

Please sign in to comment.