Skip to content

Commit

Permalink
Improve error messages returned by SigningCert (#388)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Stromberg <t+github@stromberg.org>
  • Loading branch information
tstromberg committed Feb 6, 2022
1 parent 0c51e0a commit fa4236e
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -81,12 +82,12 @@ func (c *client) SigningCert(cr CertificateRequest, token string) (*CertificateR

b, err := json.Marshal(cr)
if err != nil {
return nil, err
return nil, fmt.Errorf("marshal: %w", err)
}

req, err := http.NewRequest(http.MethodPost, endpoint.String(), bytes.NewBuffer(b))
if err != nil {
return nil, err
return nil, fmt.Errorf("request: %w", err)
}
// Set the authorization header to our OIDC bearer token.
req.Header.Set("Authorization", "Bearer "+token)
Expand All @@ -95,25 +96,25 @@ func (c *client) SigningCert(cr CertificateRequest, token string) (*CertificateR

resp, err := c.client.Do(req)
if err != nil {
return nil, err
return nil, fmt.Errorf("client: %w", err)
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
return nil, fmt.Errorf("%s read: %w", endpoint.String(), err)
}

// The API should return a 201 Created on success. If we see anything else,
// then turn the response body into an error.
if resp.StatusCode != http.StatusCreated {
return nil, errors.New(string(body))
return nil, fmt.Errorf("%s %s returned %s: %q", http.MethodPost, endpoint.String(), resp.Status, body)
}

// Extract the SCT from the response header.
sct, err := base64.StdEncoding.DecodeString(resp.Header.Get("SCT"))
if err != nil {
return nil, err
return nil, fmt.Errorf("decode: %w", err)
}

// Split the cert and the chain
Expand Down

0 comments on commit fa4236e

Please sign in to comment.