Fix non-nil error value when there is no error

This commit is contained in:
Matthew Holt 2018-04-19 18:02:18 -06:00
parent 823a03a417
commit fad2257e11

View file

@ -294,24 +294,24 @@ DNSNames:
if err != nil { if err != nil {
return CertificateResource{}, err return CertificateResource{}, err
} }
authz, failures := c.getAuthzForOrder(order) authz, err := c.getAuthzForOrder(order)
// If any challenge fails - return. Do not generate partial SAN certificates. if err != nil {
if len(failures) > 0 { // If any challenge fails, return. Do not generate partial SAN certificates.
/*for _, auth := range authz { /*for _, auth := range authz {
c.disableAuthz(auth) c.disableAuthz(auth)
}*/ }*/
return CertificateResource{}, err
return CertificateResource{}, failures
} }
errs := c.solveChallengeForAuthz(authz) err = c.solveChallengeForAuthz(authz)
// If any challenge fails - return. Do not generate partial SAN certificates. if err != nil {
if len(errs) > 0 { // If any challenge fails, return. Do not generate partial SAN certificates.
return CertificateResource{}, errs return CertificateResource{}, err
} }
logf("[INFO][%s] acme: Validations succeeded; requesting certificates", strings.Join(domains, ", ")) logf("[INFO][%s] acme: Validations succeeded; requesting certificates", strings.Join(domains, ", "))
failures := make(ObtainError)
cert, err := c.requestCertificateForCsr(order, bundle, csr.Raw, nil) cert, err := c.requestCertificateForCsr(order, bundle, csr.Raw, nil)
if err != nil { if err != nil {
for _, chln := range authz { for _, chln := range authz {
@ -322,8 +322,13 @@ DNSNames:
// Add the CSR to the certificate so that it can be used for renewals. // Add the CSR to the certificate so that it can be used for renewals.
cert.CSR = pemEncode(&csr) cert.CSR = pemEncode(&csr)
// do not return an empty failures map, because
// it would still be a non-nil error value
if len(failures) > 0 {
return cert, failures return cert, failures
} }
return cert, nil
}
// ObtainCertificate tries to obtain a single certificate using all domains passed into it. // ObtainCertificate tries to obtain a single certificate using all domains passed into it.
// The first domain in domains is used for the CommonName field of the certificate, all other // The first domain in domains is used for the CommonName field of the certificate, all other
@ -336,7 +341,7 @@ DNSNames:
// the whole certificate will fail. // the whole certificate will fail.
func (c *Client) ObtainCertificate(domains []string, bundle bool, privKey crypto.PrivateKey, mustStaple bool) (CertificateResource, error) { func (c *Client) ObtainCertificate(domains []string, bundle bool, privKey crypto.PrivateKey, mustStaple bool) (CertificateResource, error) {
if len(domains) == 0 { if len(domains) == 0 {
return CertificateResource{}, errors.New("Passed no domains into ObtainCertificate") return CertificateResource{}, errors.New("No domains to obtain a certificate for")
} }
if bundle { if bundle {
@ -349,24 +354,24 @@ func (c *Client) ObtainCertificate(domains []string, bundle bool, privKey crypto
if err != nil { if err != nil {
return CertificateResource{}, err return CertificateResource{}, err
} }
authz, failures := c.getAuthzForOrder(order) authz, err := c.getAuthzForOrder(order)
// If any challenge fails - return. Do not generate partial SAN certificates. if err != nil {
if len(failures) > 0 { // If any challenge fails, return. Do not generate partial SAN certificates.
/*for _, auth := range authz { /*for _, auth := range authz {
c.disableAuthz(auth) c.disableAuthz(auth)
}*/ }*/
return CertificateResource{}, err
return CertificateResource{}, failures
} }
errs := c.solveChallengeForAuthz(authz) err = c.solveChallengeForAuthz(authz)
// If any challenge fails - return. Do not generate partial SAN certificates. if err != nil {
if len(errs) > 0 { // If any challenge fails, return. Do not generate partial SAN certificates.
return CertificateResource{}, errs return CertificateResource{}, err
} }
logf("[INFO][%s] acme: Validations succeeded; requesting certificates", strings.Join(domains, ", ")) logf("[INFO][%s] acme: Validations succeeded; requesting certificates", strings.Join(domains, ", "))
failures := make(ObtainError)
cert, err := c.requestCertificateForOrder(order, bundle, privKey, mustStaple) cert, err := c.requestCertificateForOrder(order, bundle, privKey, mustStaple)
if err != nil { if err != nil {
for _, auth := range authz { for _, auth := range authz {
@ -374,8 +379,13 @@ func (c *Client) ObtainCertificate(domains []string, bundle bool, privKey crypto
} }
} }
// do not return an empty failures map, because
// it would still be a non-nil error value
if len(failures) > 0 {
return cert, failures return cert, failures
} }
return cert, nil
}
// RevokeCertificate takes a PEM encoded certificate or bundle and tries to revoke it at the CA. // RevokeCertificate takes a PEM encoded certificate or bundle and tries to revoke it at the CA.
func (c *Client) RevokeCertificate(certificate []byte) error { func (c *Client) RevokeCertificate(certificate []byte) error {
@ -485,9 +495,10 @@ func (c *Client) createOrderForIdentifiers(domains []string) (orderResource, err
// Looks through the challenge combinations to find a solvable match. // Looks through the challenge combinations to find a solvable match.
// Then solves the challenges in series and returns. // Then solves the challenges in series and returns.
func (c *Client) solveChallengeForAuthz(authorizations []authorization) ObtainError { func (c *Client) solveChallengeForAuthz(authorizations []authorization) error {
// loop through the resources, basically through the domains.
failures := make(ObtainError) failures := make(ObtainError)
// loop through the resources, basically through the domains.
for _, authz := range authorizations { for _, authz := range authorizations {
if authz.Status == "valid" { if authz.Status == "valid" {
// Boulder might recycle recent validated authz (see issue #267) // Boulder might recycle recent validated authz (see issue #267)
@ -508,8 +519,13 @@ func (c *Client) solveChallengeForAuthz(authorizations []authorization) ObtainEr
} }
} }
// be careful not to return an empty failures map, for
// even an empty ObtainError is a non-nil error value
if len(failures) > 0 {
return failures return failures
} }
return nil
}
// Checks all challenges from the server in order and returns the first matching solver. // Checks all challenges from the server in order and returns the first matching solver.
func (c *Client) chooseSolver(auth authorization, domain string) (int, solver) { func (c *Client) chooseSolver(auth authorization, domain string) (int, solver) {
@ -523,7 +539,7 @@ func (c *Client) chooseSolver(auth authorization, domain string) (int, solver) {
} }
// Get the challenges needed to proof our identifier to the ACME server. // Get the challenges needed to proof our identifier to the ACME server.
func (c *Client) getAuthzForOrder(order orderResource) ([]authorization, ObtainError) { func (c *Client) getAuthzForOrder(order orderResource) ([]authorization, error) {
resc, errc := make(chan authorization), make(chan domainError) resc, errc := make(chan authorization), make(chan domainError)
delay := time.Second / overallRequestLimit delay := time.Second / overallRequestLimit
@ -544,7 +560,7 @@ func (c *Client) getAuthzForOrder(order orderResource) ([]authorization, ObtainE
} }
var responses []authorization var responses []authorization
failures := make(map[string]error) failures := make(ObtainError)
for i := 0; i < len(order.Authorizations); i++ { for i := 0; i < len(order.Authorizations); i++ {
select { select {
case res := <-resc: case res := <-resc:
@ -559,8 +575,13 @@ func (c *Client) getAuthzForOrder(order orderResource) ([]authorization, ObtainE
close(resc) close(resc)
close(errc) close(errc)
// be careful to not return an empty failures map;
// even if empty, they become non-nil error values
if len(failures) > 0 {
return responses, failures return responses, failures
} }
return responses, nil
}
func logAuthz(order orderResource) { func logAuthz(order orderResource) {
for i, auth := range order.Authorizations { for i, auth := range order.Authorizations {