diff --git a/ca/acmeClient.go b/ca/acmeClient.go index 28451a45..6c73b2fd 100644 --- a/ca/acmeClient.go +++ b/ca/acmeClient.go @@ -37,15 +37,18 @@ func NewACMEClient(endpoint string, contact []string, opts ...ClientOption) (*AC if err != nil { return nil, err } - ac := &ACMEClient{ client: &http.Client{ Transport: tr, }, dirLoc: endpoint, } - - resp, err := ac.client.Get(endpoint) + req, err := http.NewRequest("GET", endpoint, nil) + if err != nil { + return nil, errors.Wrapf(err, "creating GET request %s failed", endpoint) + } + req.Header.Set("User-Agent", UserAgent) + resp, err := ac.client.Do(req) if err != nil { return nil, errors.Wrapf(err, "client GET %s failed", endpoint) } @@ -99,7 +102,12 @@ func (c *ACMEClient) GetDirectory() (*acmeAPI.Directory, error) { // GetNonce makes a nonce request to the ACME api and returns an // ACME directory object. func (c *ACMEClient) GetNonce() (string, error) { - resp, err := c.client.Get(c.dir.NewNonce) + req, err := http.NewRequest("GET", c.dir.NewNonce, nil) + if err != nil { + return "", errors.Wrapf(err, "creating GET request %s failed", c.dir.NewNonce) + } + req.Header.Set("User-Agent", UserAgent) + resp, err := c.client.Do(req) if err != nil { return "", errors.Wrapf(err, "client GET %s failed", c.dir.NewNonce) } @@ -171,9 +179,15 @@ func (c *ACMEClient) post(payload []byte, url string, headerOps ...withHeaderOpt if err != nil { return nil, err } - resp, err := c.client.Post(url, "application/jose+json", strings.NewReader(raw)) + req, err := http.NewRequest("POST", url, strings.NewReader(raw)) if err != nil { - return nil, errors.Wrapf(err, "client GET %s failed", c.dir.NewOrder) + return nil, errors.Wrapf(err, "creating POST request %s failed", url) + } + req.Header.Set("Content-Type", "application/jose+json") + req.Header.Set("User-Agent", UserAgent) + resp, err := c.client.Do(req) + if err != nil { + return nil, errors.Wrapf(err, "client POST %s failed", c.dir.NewOrder) } return resp, nil } diff --git a/ca/acmeClient_test.go b/ca/acmeClient_test.go index d22c4972..ad5f2116 100644 --- a/ca/acmeClient_test.go +++ b/ca/acmeClient_test.go @@ -109,6 +109,7 @@ func TestNewACMEClient(t *testing.T) { i := 0 srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + assert.Equals(t, "step-http-client/1.0", req.Header.Get("User-Agent")) // check default User-Agent header switch { case i == 0: api.JSONStatus(w, tc.r1, tc.rc1) @@ -203,6 +204,7 @@ func TestACMEClient_GetNonce(t *testing.T) { tc := run(t) srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + assert.Equals(t, "step-http-client/1.0", req.Header.Get("User-Agent")) // check default User-Agent header w.Header().Set("Replay-Nonce", expectedNonce) api.JSONStatus(w, tc.r1, tc.rc1) }) @@ -309,6 +311,8 @@ func TestACMEClient_post(t *testing.T) { i := 0 srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + assert.Equals(t, "step-http-client/1.0", req.Header.Get("User-Agent")) // check default User-Agent header + w.Header().Set("Replay-Nonce", expectedNonce) if i == 0 { api.JSONStatus(w, tc.r1, tc.rc1) @@ -447,6 +451,8 @@ func TestACMEClient_NewOrder(t *testing.T) { i := 0 srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + assert.Equals(t, "step-http-client/1.0", req.Header.Get("User-Agent")) // check default User-Agent header + w.Header().Set("Replay-Nonce", expectedNonce) if i == 0 { api.JSONStatus(w, tc.r1, tc.rc1) @@ -567,6 +573,8 @@ func TestACMEClient_GetOrder(t *testing.T) { i := 0 srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + assert.Equals(t, "step-http-client/1.0", req.Header.Get("User-Agent")) // check default User-Agent header + w.Header().Set("Replay-Nonce", expectedNonce) if i == 0 { api.JSONStatus(w, tc.r1, tc.rc1) @@ -687,6 +695,8 @@ func TestACMEClient_GetAuthz(t *testing.T) { i := 0 srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + assert.Equals(t, "step-http-client/1.0", req.Header.Get("User-Agent")) // check default User-Agent header + w.Header().Set("Replay-Nonce", expectedNonce) if i == 0 { api.JSONStatus(w, tc.r1, tc.rc1) @@ -807,6 +817,8 @@ func TestACMEClient_GetChallenge(t *testing.T) { i := 0 srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + assert.Equals(t, "step-http-client/1.0", req.Header.Get("User-Agent")) // check default User-Agent header + w.Header().Set("Replay-Nonce", expectedNonce) if i == 0 { api.JSONStatus(w, tc.r1, tc.rc1) @@ -928,6 +940,8 @@ func TestACMEClient_ValidateChallenge(t *testing.T) { i := 0 srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + assert.Equals(t, "step-http-client/1.0", req.Header.Get("User-Agent")) // check default User-Agent header + w.Header().Set("Replay-Nonce", expectedNonce) if i == 0 { api.JSONStatus(w, tc.r1, tc.rc1) @@ -1053,6 +1067,8 @@ func TestACMEClient_FinalizeOrder(t *testing.T) { i := 0 srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + assert.Equals(t, "step-http-client/1.0", req.Header.Get("User-Agent")) // check default User-Agent header + w.Header().Set("Replay-Nonce", expectedNonce) if i == 0 { api.JSONStatus(w, tc.r1, tc.rc1) @@ -1180,6 +1196,8 @@ func TestACMEClient_GetAccountOrders(t *testing.T) { i := 0 srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + assert.Equals(t, "step-http-client/1.0", req.Header.Get("User-Agent")) // check default User-Agent header + w.Header().Set("Replay-Nonce", expectedNonce) if i == 0 { api.JSONStatus(w, tc.r1, tc.rc1) @@ -1309,6 +1327,8 @@ func TestACMEClient_GetCertificate(t *testing.T) { i := 0 srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + assert.Equals(t, "step-http-client/1.0", req.Header.Get("User-Agent")) // check default User-Agent header + w.Header().Set("Replay-Nonce", expectedNonce) if i == 0 { api.JSONStatus(w, tc.r1, tc.rc1)