Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

agent/caching: support proxying request query parameters #6772

Merged
merged 4 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion command/agent/cache/api_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ func (ap *APIProxy) Send(ctx context.Context, req *SendRequest) (*SendResponse,
fwReq := client.NewRequest(req.Request.Method, req.Request.URL.Path)
fwReq.BodyBytes = req.RequestBody

query := req.Request.URL.Query()
if len(query) != 0 {
fwReq.Params = query
}

// Make the request to Vault and get the response
ap.logger.Info("forwarding request", "path", req.Request.URL.Path, "method", req.Request.Method)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I swapped the log message ordering on "method" and "path" since it looks nicer IMO (similar to the browser's inspection console log messages), but can revert it back if we don't want this change.

ap.logger.Info("forwarding request", "method", req.Request.Method, "path", req.Request.URL.Path)

resp, err := client.RawRequestWithContext(ctx, fwReq)
if resp == nil && err != nil {
Expand Down
52 changes: 50 additions & 2 deletions command/agent/cache/api_proxy_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cache

import (
"net/http"
"testing"

hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/helper/namespace"
"github.com/hashicorp/vault/sdk/helper/jsonutil"
Expand Down Expand Up @@ -42,6 +43,53 @@ func TestAPIProxy(t *testing.T) {
}

if !result.Initialized || result.Sealed || result.Standby {
t.Fatalf("bad sys/health response")
t.Fatalf("bad sys/health response: %#v", result)
}
}

func TestAPIProxy_queryParams(t *testing.T) {
// Set up a caching agent that points to a standby node for our particular test
// that needs to query sys/health?standbyok=true on a standby
cleanup, client, _, _ := setupClusterAndAgentOnStandby(namespace.RootContext(nil), t, nil)
defer cleanup()

proxier, err := NewAPIProxy(&APIProxyConfig{
Client: client,
Logger: logging.NewVaultLogger(hclog.Trace),
})
if err != nil {
t.Fatal(err)
}

r := client.NewRequest("GET", "/v1/sys/health")
req, err := r.ToHTTP()
if err != nil {
t.Fatal(err)
}

// Add a query parameter for testing
q := req.URL.Query()
q.Add("standbyok", "true")
req.URL.RawQuery = q.Encode()

resp, err := proxier.Send(namespace.RootContext(nil), &SendRequest{
Request: req,
})
if err != nil {
t.Fatal(err)
}

var result api.HealthResponse
err = jsonutil.DecodeJSONFromReader(resp.Response.Body, &result)
if err != nil {
t.Fatal(err)
}

if !result.Initialized || result.Sealed || !result.Standby {
t.Fatalf("bad sys/health response: %#v", result)
}

if resp.Response.StatusCode != http.StatusOK {
t.Fatalf("exptected standby to return 200, got: %v", resp.Response.StatusCode)
}
}
47 changes: 34 additions & 13 deletions command/agent/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,22 @@ path "*" {
`

// setupClusterAndAgent is a helper func used to set up a test cluster and
// caching agent. It returns a cleanup func that should be deferred immediately
// along with two clients, one for direct cluster communication and another to
// talk to the caching agent.
// caching agent against the active node. It returns a cleanup func that should
// be deferred immediately along with two clients, one for direct cluster
// communication and another to talk to the caching agent.
func setupClusterAndAgent(ctx context.Context, t *testing.T, coreConfig *vault.CoreConfig) (func(), *api.Client, *api.Client, *LeaseCache) {
return setupClusterAndAgentCommon(ctx, t, coreConfig, false)
}

// setupClusterAndAgentOnStandby is a helper func used to set up a test cluster
// and caching agent against a standby node. It returns a cleanup func that
// should be deferred immediately along with two clients, one for direct cluster
// communication and another to talk to the caching agent.
func setupClusterAndAgentOnStandby(ctx context.Context, t *testing.T, coreConfig *vault.CoreConfig) (func(), *api.Client, *api.Client, *LeaseCache) {
return setupClusterAndAgentCommon(ctx, t, coreConfig, true)
}

func setupClusterAndAgentCommon(ctx context.Context, t *testing.T, coreConfig *vault.CoreConfig, onStandby bool) (func(), *api.Client, *api.Client, *LeaseCache) {
t.Helper()

if ctx == nil {
Expand Down Expand Up @@ -70,21 +82,30 @@ func setupClusterAndAgent(ctx context.Context, t *testing.T, coreConfig *vault.C
cores := cluster.Cores
vault.TestWaitActive(t, cores[0].Core)

// clusterClient is the client that is used to talk directly to the cluster.
clusterClient := cores[0].Client
activeClient := cores[0].Client
standbyClient := cores[1].Client

// clienToUse is the client for the agent to point to.
clienToUse := activeClient
if onStandby {
clienToUse = standbyClient
}

// Add an admin policy
if err := clusterClient.Sys().PutPolicy("admin", policyAdmin); err != nil {
if err := activeClient.Sys().PutPolicy("admin", policyAdmin); err != nil {
t.Fatal(err)
}

// Set up the userpass auth backend and an admin user. Used for getting a token
// for the agent later down in this func.
clusterClient.Sys().EnableAuthWithOptions("userpass", &api.EnableAuthOptions{
err := activeClient.Sys().EnableAuthWithOptions("userpass", &api.EnableAuthOptions{
Type: "userpass",
})
if err != nil {
t.Fatal(err)
}

_, err := clusterClient.Logical().Write("auth/userpass/users/foo", map[string]interface{}{
_, err = activeClient.Logical().Write("auth/userpass/users/foo", map[string]interface{}{
"password": "bar",
"policies": []string{"admin"},
})
Expand All @@ -94,7 +115,7 @@ func setupClusterAndAgent(ctx context.Context, t *testing.T, coreConfig *vault.C

// Set up env vars for agent consumption
origEnvVaultAddress := os.Getenv(api.EnvVaultAddress)
os.Setenv(api.EnvVaultAddress, clusterClient.Address())
os.Setenv(api.EnvVaultAddress, clienToUse.Address())

origEnvVaultCACert := os.Getenv(api.EnvVaultCACert)
os.Setenv(api.EnvVaultCACert, fmt.Sprintf("%s/ca_cert.pem", cluster.TempDir))
Expand All @@ -108,7 +129,7 @@ func setupClusterAndAgent(ctx context.Context, t *testing.T, coreConfig *vault.C

// Create the API proxier
apiProxy, err := NewAPIProxy(&APIProxyConfig{
Client: clusterClient,
Client: clienToUse,
Logger: cacheLogger.Named("apiproxy"),
})
if err != nil {
Expand All @@ -118,7 +139,7 @@ func setupClusterAndAgent(ctx context.Context, t *testing.T, coreConfig *vault.C
// Create the lease cache proxier and set its underlying proxier to
// the API proxier.
leaseCache, err := NewLeaseCache(&LeaseCacheConfig{
Client: clusterClient,
Client: clienToUse,
BaseContext: ctx,
Proxier: apiProxy,
Logger: cacheLogger.Named("leasecache"),
Expand All @@ -142,7 +163,7 @@ func setupClusterAndAgent(ctx context.Context, t *testing.T, coreConfig *vault.C
go server.Serve(listener)

// testClient is the client that is used to talk to the agent for proxying/caching behavior.
testClient, err := clusterClient.Clone()
testClient, err := activeClient.Clone()
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -171,7 +192,7 @@ func setupClusterAndAgent(ctx context.Context, t *testing.T, coreConfig *vault.C
listener.Close()
}

return cleanup, clusterClient, testClient, leaseCache
return cleanup, clienToUse, testClient, leaseCache
}

func tokenRevocationValidation(t *testing.T, sampleSpace map[string]string, expected map[string]string, leaseCache *LeaseCache) {
Expand Down