Merge pull request #779 from smallstep/herman/acme-cli-user-agent

This commit is contained in:
Herman Slatman 2021-12-29 20:51:09 +01:00 committed by GitHub
commit 8fee970297
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 6 deletions

View file

@ -37,15 +37,18 @@ func NewACMEClient(endpoint string, contact []string, opts ...ClientOption) (*AC
if err != nil { if err != nil {
return nil, err return nil, err
} }
ac := &ACMEClient{ ac := &ACMEClient{
client: &http.Client{ client: &http.Client{
Transport: tr, Transport: tr,
}, },
dirLoc: endpoint, dirLoc: endpoint,
} }
req, err := http.NewRequest("GET", endpoint, http.NoBody)
resp, err := ac.client.Get(endpoint) 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 { if err != nil {
return nil, errors.Wrapf(err, "client GET %s failed", endpoint) 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 // GetNonce makes a nonce request to the ACME api and returns an
// ACME directory object. // ACME directory object.
func (c *ACMEClient) GetNonce() (string, error) { func (c *ACMEClient) GetNonce() (string, error) {
resp, err := c.client.Get(c.dir.NewNonce) req, err := http.NewRequest("GET", c.dir.NewNonce, http.NoBody)
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 { if err != nil {
return "", errors.Wrapf(err, "client GET %s failed", c.dir.NewNonce) 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 { if err != nil {
return nil, err 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 { 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 return resp, nil
} }

View file

@ -109,6 +109,7 @@ func TestNewACMEClient(t *testing.T) {
i := 0 i := 0
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 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 { switch {
case i == 0: case i == 0:
api.JSONStatus(w, tc.r1, tc.rc1) api.JSONStatus(w, tc.r1, tc.rc1)
@ -203,6 +204,7 @@ func TestACMEClient_GetNonce(t *testing.T) {
tc := run(t) tc := run(t)
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 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) w.Header().Set("Replay-Nonce", expectedNonce)
api.JSONStatus(w, tc.r1, tc.rc1) api.JSONStatus(w, tc.r1, tc.rc1)
}) })
@ -309,6 +311,8 @@ func TestACMEClient_post(t *testing.T) {
i := 0 i := 0
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 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) w.Header().Set("Replay-Nonce", expectedNonce)
if i == 0 { if i == 0 {
api.JSONStatus(w, tc.r1, tc.rc1) api.JSONStatus(w, tc.r1, tc.rc1)
@ -447,6 +451,8 @@ func TestACMEClient_NewOrder(t *testing.T) {
i := 0 i := 0
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 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) w.Header().Set("Replay-Nonce", expectedNonce)
if i == 0 { if i == 0 {
api.JSONStatus(w, tc.r1, tc.rc1) api.JSONStatus(w, tc.r1, tc.rc1)
@ -567,6 +573,8 @@ func TestACMEClient_GetOrder(t *testing.T) {
i := 0 i := 0
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 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) w.Header().Set("Replay-Nonce", expectedNonce)
if i == 0 { if i == 0 {
api.JSONStatus(w, tc.r1, tc.rc1) api.JSONStatus(w, tc.r1, tc.rc1)
@ -687,6 +695,8 @@ func TestACMEClient_GetAuthz(t *testing.T) {
i := 0 i := 0
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 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) w.Header().Set("Replay-Nonce", expectedNonce)
if i == 0 { if i == 0 {
api.JSONStatus(w, tc.r1, tc.rc1) api.JSONStatus(w, tc.r1, tc.rc1)
@ -807,6 +817,8 @@ func TestACMEClient_GetChallenge(t *testing.T) {
i := 0 i := 0
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 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) w.Header().Set("Replay-Nonce", expectedNonce)
if i == 0 { if i == 0 {
api.JSONStatus(w, tc.r1, tc.rc1) api.JSONStatus(w, tc.r1, tc.rc1)
@ -928,6 +940,8 @@ func TestACMEClient_ValidateChallenge(t *testing.T) {
i := 0 i := 0
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 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) w.Header().Set("Replay-Nonce", expectedNonce)
if i == 0 { if i == 0 {
api.JSONStatus(w, tc.r1, tc.rc1) api.JSONStatus(w, tc.r1, tc.rc1)
@ -1053,6 +1067,8 @@ func TestACMEClient_FinalizeOrder(t *testing.T) {
i := 0 i := 0
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 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) w.Header().Set("Replay-Nonce", expectedNonce)
if i == 0 { if i == 0 {
api.JSONStatus(w, tc.r1, tc.rc1) api.JSONStatus(w, tc.r1, tc.rc1)
@ -1180,6 +1196,8 @@ func TestACMEClient_GetAccountOrders(t *testing.T) {
i := 0 i := 0
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 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) w.Header().Set("Replay-Nonce", expectedNonce)
if i == 0 { if i == 0 {
api.JSONStatus(w, tc.r1, tc.rc1) api.JSONStatus(w, tc.r1, tc.rc1)
@ -1309,6 +1327,8 @@ func TestACMEClient_GetCertificate(t *testing.T) {
i := 0 i := 0
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 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) w.Header().Set("Replay-Nonce", expectedNonce)
if i == 0 { if i == 0 {
api.JSONStatus(w, tc.r1, tc.rc1) api.JSONStatus(w, tc.r1, tc.rc1)