Skip to content

Commit

Permalink
Replace http method strings with net/http constants (#14677)
Browse files Browse the repository at this point in the history
  • Loading branch information
averche authored Mar 24, 2022
1 parent 7f36a29 commit 0dd4cda
Show file tree
Hide file tree
Showing 26 changed files with 142 additions and 109 deletions.
29 changes: 15 additions & 14 deletions api/auth_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"context"
"net/http"
)

// TokenAuth is used to perform token backend operations on Vault
Expand All @@ -22,7 +23,7 @@ func (c *TokenAuth) CreateWithContext(ctx context.Context, opts *TokenCreateRequ
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("POST", "/v1/auth/token/create")
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/create")
if err := r.SetJSONBody(opts); err != nil {
return nil, err
}
Expand All @@ -44,7 +45,7 @@ func (c *TokenAuth) CreateOrphanWithContext(ctx context.Context, opts *TokenCrea
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("POST", "/v1/auth/token/create-orphan")
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/create-orphan")
if err := r.SetJSONBody(opts); err != nil {
return nil, err
}
Expand All @@ -66,7 +67,7 @@ func (c *TokenAuth) CreateWithRoleWithContext(ctx context.Context, opts *TokenCr
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("POST", "/v1/auth/token/create/"+roleName)
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/create/"+roleName)
if err := r.SetJSONBody(opts); err != nil {
return nil, err
}
Expand All @@ -88,7 +89,7 @@ func (c *TokenAuth) LookupWithContext(ctx context.Context, token string) (*Secre
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("POST", "/v1/auth/token/lookup")
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/lookup")
if err := r.SetJSONBody(map[string]interface{}{
"token": token,
}); err != nil {
Expand All @@ -112,7 +113,7 @@ func (c *TokenAuth) LookupAccessorWithContext(ctx context.Context, accessor stri
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("POST", "/v1/auth/token/lookup-accessor")
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/lookup-accessor")
if err := r.SetJSONBody(map[string]interface{}{
"accessor": accessor,
}); err != nil {
Expand All @@ -136,7 +137,7 @@ func (c *TokenAuth) LookupSelfWithContext(ctx context.Context) (*Secret, error)
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("GET", "/v1/auth/token/lookup-self")
r := c.c.NewRequest(http.MethodGet, "/v1/auth/token/lookup-self")

resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
Expand All @@ -155,7 +156,7 @@ func (c *TokenAuth) RenewAccessorWithContext(ctx context.Context, accessor strin
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("POST", "/v1/auth/token/renew-accessor")
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/renew-accessor")
if err := r.SetJSONBody(map[string]interface{}{
"accessor": accessor,
"increment": increment,
Expand All @@ -180,7 +181,7 @@ func (c *TokenAuth) RenewWithContext(ctx context.Context, token string, incremen
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("PUT", "/v1/auth/token/renew")
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/renew")
if err := r.SetJSONBody(map[string]interface{}{
"token": token,
"increment": increment,
Expand All @@ -205,7 +206,7 @@ func (c *TokenAuth) RenewSelfWithContext(ctx context.Context, increment int) (*S
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("PUT", "/v1/auth/token/renew-self")
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/renew-self")

body := map[string]interface{}{"increment": increment}
if err := r.SetJSONBody(body); err != nil {
Expand All @@ -232,7 +233,7 @@ func (c *TokenAuth) RenewTokenAsSelfWithContext(ctx context.Context, token strin
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("PUT", "/v1/auth/token/renew-self")
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/renew-self")
r.ClientToken = token

body := map[string]interface{}{"increment": increment}
Expand Down Expand Up @@ -260,7 +261,7 @@ func (c *TokenAuth) RevokeAccessorWithContext(ctx context.Context, accessor stri
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("POST", "/v1/auth/token/revoke-accessor")
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/revoke-accessor")
if err := r.SetJSONBody(map[string]interface{}{
"accessor": accessor,
}); err != nil {
Expand All @@ -287,7 +288,7 @@ func (c *TokenAuth) RevokeOrphanWithContext(ctx context.Context, token string) e
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("PUT", "/v1/auth/token/revoke-orphan")
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/revoke-orphan")
if err := r.SetJSONBody(map[string]interface{}{
"token": token,
}); err != nil {
Expand Down Expand Up @@ -315,7 +316,7 @@ func (c *TokenAuth) RevokeSelfWithContext(ctx context.Context, token string) err
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("PUT", "/v1/auth/token/revoke-self")
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/revoke-self")

resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
Expand All @@ -338,7 +339,7 @@ func (c *TokenAuth) RevokeTreeWithContext(ctx context.Context, token string) err
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("PUT", "/v1/auth/token/revoke")
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/revoke")
if err := r.SetJSONBody(map[string]interface{}{
"token": token,
}); err != nil {
Expand Down
12 changes: 6 additions & 6 deletions api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestClientHostHeader(t *testing.T) {
// Set the token manually
client.SetToken("foo")

resp, err := client.RawRequest(client.NewRequest("PUT", "/"))
resp, err := client.RawRequest(client.NewRequest(http.MethodPut, "/"))
if err != nil {
t.Fatal(err)
}
Expand All @@ -152,13 +152,13 @@ func TestClientBadToken(t *testing.T) {
}

client.SetToken("foo")
_, err = client.RawRequest(client.NewRequest("PUT", "/"))
_, err = client.RawRequest(client.NewRequest(http.MethodPut, "/"))
if err != nil {
t.Fatal(err)
}

client.SetToken("foo\u007f")
_, err = client.RawRequest(client.NewRequest("PUT", "/"))
_, err = client.RawRequest(client.NewRequest(http.MethodPut, "/"))
if err == nil || !strings.Contains(err.Error(), "printable") {
t.Fatalf("expected error due to bad token")
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestClientRedirect(t *testing.T) {
client.SetToken("foo")

// Do a raw "/" request
resp, err := client.RawRequest(client.NewRequest("PUT", "/"))
resp, err := client.RawRequest(client.NewRequest(http.MethodPut, "/"))
if err != nil {
t.Fatalf("err: %s", err)
}
Expand Down Expand Up @@ -331,7 +331,7 @@ func TestClientEnvNamespace(t *testing.T) {
t.Fatalf("err: %s", err)
}

_, err = client.RawRequest(client.NewRequest("GET", "/"))
_, err = client.RawRequest(client.NewRequest(http.MethodGet, "/"))
if err != nil {
t.Fatalf("err: %s", err)
}
Expand Down Expand Up @@ -962,7 +962,7 @@ func TestClient_ReadYourWrites(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testRequest := func(client *Client, val string) {
req := client.NewRequest("GET", "/"+val)
req := client.NewRequest(http.MethodGet, "/"+val)
req.Headers.Set(HeaderIndex, val)
resp, err := client.RawRequestWithContext(context.Background(), req)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion api/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"fmt"
"net/http"
)

// Help wraps HelpWithContext using context.Background.
Expand All @@ -15,7 +16,7 @@ func (c *Client) HelpWithContext(ctx context.Context, path string) (*Help, error
ctx, cancelFunc := c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.NewRequest("GET", fmt.Sprintf("/v1/%s", path))
r := c.NewRequest(http.MethodGet, fmt.Sprintf("/v1/%s", path))
r.Params.Add("help", "1")

resp, err := c.rawRequestWithContext(ctx, r)
Expand Down
17 changes: 9 additions & 8 deletions api/logical.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
Expand All @@ -30,7 +31,7 @@ var (
return os.Getenv(EnvVaultWrapTTL)
}

if (operation == "PUT" || operation == "POST") && path == "sys/wrapping/wrap" {
if (operation == http.MethodPut || operation == http.MethodPost) && path == "sys/wrapping/wrap" {
return DefaultWrappingTTL
}

Expand Down Expand Up @@ -64,7 +65,7 @@ func (c *Logical) ReadWithDataWithContext(ctx context.Context, path string, data
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("GET", "/v1/"+path)
r := c.c.NewRequest(http.MethodGet, "/v1/"+path)

var values url.Values
for k, v := range data {
Expand Down Expand Up @@ -116,7 +117,7 @@ func (c *Logical) ListWithContext(ctx context.Context, path string) (*Secret, er
r := c.c.NewRequest("LIST", "/v1/"+path)
// Set this for broader compatibility, but we use LIST above to be able to
// handle the wrapping lookup function
r.Method = "GET"
r.Method = http.MethodGet
r.Params.Set("list", "true")

resp, err := c.c.rawRequestWithContext(ctx, r)
Expand Down Expand Up @@ -149,7 +150,7 @@ func (c *Logical) Write(path string, data map[string]interface{}) (*Secret, erro
}

func (c *Logical) WriteWithContext(ctx context.Context, path string, data map[string]interface{}) (*Secret, error) {
r := c.c.NewRequest("PUT", "/v1/"+path)
r := c.c.NewRequest(http.MethodPut, "/v1/"+path)
if err := r.SetJSONBody(data); err != nil {
return nil, err
}
Expand All @@ -158,7 +159,7 @@ func (c *Logical) WriteWithContext(ctx context.Context, path string, data map[st
}

func (c *Logical) JSONMergePatch(ctx context.Context, path string, data map[string]interface{}) (*Secret, error) {
r := c.c.NewRequest("PATCH", "/v1/"+path)
r := c.c.NewRequest(http.MethodPatch, "/v1/"+path)
r.Headers.Set("Content-Type", "application/merge-patch+json")
if err := r.SetJSONBody(data); err != nil {
return nil, err
Expand All @@ -172,7 +173,7 @@ func (c *Logical) WriteBytes(path string, data []byte) (*Secret, error) {
}

func (c *Logical) WriteBytesWithContext(ctx context.Context, path string, data []byte) (*Secret, error) {
r := c.c.NewRequest("PUT", "/v1/"+path)
r := c.c.NewRequest(http.MethodPut, "/v1/"+path)
r.BodyBytes = data

return c.write(ctx, path, r)
Expand Down Expand Up @@ -222,7 +223,7 @@ func (c *Logical) DeleteWithDataWithContext(ctx context.Context, path string, da
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("DELETE", "/v1/"+path)
r := c.c.NewRequest(http.MethodDelete, "/v1/"+path)

var values url.Values
for k, v := range data {
Expand Down Expand Up @@ -282,7 +283,7 @@ func (c *Logical) UnwrapWithContext(ctx context.Context, wrappingToken string) (
}
}

r := c.c.NewRequest("PUT", "/v1/sys/wrapping/unwrap")
r := c.c.NewRequest(http.MethodPut, "/v1/sys/wrapping/unwrap")
if err := r.SetJSONBody(data); err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion api/output_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"fmt"
"net/http"
"strings"

retryablehttp "github.com/hashicorp/go-retryablehttp"
Expand Down Expand Up @@ -45,7 +46,7 @@ func (d *OutputStringError) parseRequest() {
if d.TLSSkipVerify {
d.parsedCurlString += "--insecure "
}
if d.Request.Method != "GET" {
if d.Request.Method != http.MethodGet {
d.parsedCurlString = fmt.Sprintf("%s-X %s ", d.parsedCurlString, d.Request.Method)
}
if d.ClientCACert != "" {
Expand Down
5 changes: 3 additions & 2 deletions api/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"fmt"
"net/http"
)

// SSH is used to return a client to invoke operations on SSH backend.
Expand Down Expand Up @@ -34,7 +35,7 @@ func (c *SSH) CredentialWithContext(ctx context.Context, role string, data map[s
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("PUT", fmt.Sprintf("/v1/%s/creds/%s", c.MountPoint, role))
r := c.c.NewRequest(http.MethodPut, fmt.Sprintf("/v1/%s/creds/%s", c.MountPoint, role))
if err := r.SetJSONBody(data); err != nil {
return nil, err
}
Expand All @@ -59,7 +60,7 @@ func (c *SSH) SignKeyWithContext(ctx context.Context, role string, data map[stri
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("PUT", fmt.Sprintf("/v1/%s/sign/%s", c.MountPoint, role))
r := c.c.NewRequest(http.MethodPut, fmt.Sprintf("/v1/%s/sign/%s", c.MountPoint, role))
if err := r.SetJSONBody(data); err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion api/ssh_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
"os"

"github.com/hashicorp/errwrap"
Expand Down Expand Up @@ -218,7 +219,7 @@ func (c *SSHHelper) VerifyWithContext(ctx context.Context, otp string) (*SSHVeri
"otp": otp,
}
verifyPath := fmt.Sprintf("/v1/%s/verify", c.MountPoint)
r := c.c.NewRequest("PUT", verifyPath)
r := c.c.NewRequest(http.MethodPut, verifyPath)
if err := r.SetJSONBody(data); err != nil {
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions api/sys_audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net/http"

"github.com/mitchellh/mapstructure"
)
Expand All @@ -20,7 +21,7 @@ func (c *Sys) AuditHashWithContext(ctx context.Context, path string, input strin
"input": input,
}

r := c.c.NewRequest("PUT", fmt.Sprintf("/v1/sys/audit-hash/%s", path))
r := c.c.NewRequest(http.MethodPut, fmt.Sprintf("/v1/sys/audit-hash/%s", path))
if err := r.SetJSONBody(body); err != nil {
return "", err
}
Expand Down Expand Up @@ -59,7 +60,7 @@ func (c *Sys) ListAuditWithContext(ctx context.Context) (map[string]*Audit, erro
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("GET", "/v1/sys/audit")
r := c.c.NewRequest(http.MethodGet, "/v1/sys/audit")

resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
Expand Down Expand Up @@ -102,7 +103,7 @@ func (c *Sys) EnableAuditWithOptionsWithContext(ctx context.Context, path string
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("PUT", fmt.Sprintf("/v1/sys/audit/%s", path))
r := c.c.NewRequest(http.MethodPut, fmt.Sprintf("/v1/sys/audit/%s", path))
if err := r.SetJSONBody(options); err != nil {
return err
}
Expand All @@ -124,7 +125,7 @@ func (c *Sys) DisableAuditWithContext(ctx context.Context, path string) error {
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest("DELETE", fmt.Sprintf("/v1/sys/audit/%s", path))
r := c.c.NewRequest(http.MethodDelete, fmt.Sprintf("/v1/sys/audit/%s", path))

resp, err := c.c.rawRequestWithContext(ctx, r)

Expand Down
Loading

0 comments on commit 0dd4cda

Please sign in to comment.