-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79a07dd
commit 62feece
Showing
27 changed files
with
3,275 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package cache | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io/ioutil" | ||
|
||
hclog "github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/vault/api" | ||
) | ||
|
||
// APIProxy is an implementation of the proxier interface that is used to | ||
// forward the request to Vault and get the response. | ||
type APIProxy struct { | ||
logger hclog.Logger | ||
} | ||
|
||
type APIProxyConfig struct { | ||
Logger hclog.Logger | ||
} | ||
|
||
func NewAPIProxy(config *APIProxyConfig) Proxier { | ||
return &APIProxy{ | ||
logger: config.Logger, | ||
} | ||
} | ||
|
||
func (ap *APIProxy) Send(ctx context.Context, req *SendRequest) (*SendResponse, error) { | ||
client, err := api.NewClient(api.DefaultConfig()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
client.SetToken(req.Token) | ||
client.SetHeaders(req.Request.Header) | ||
|
||
fwReq := client.NewRequest(req.Request.Method, req.Request.URL.Path) | ||
fwReq.BodyBytes = req.RequestBody | ||
|
||
// Make the request to Vault and get the response | ||
ap.logger.Info("forwarding request", "path", req.Request.URL.Path) | ||
resp, err := client.RawRequestWithContext(ctx, fwReq) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Parse and reset response body | ||
respBody, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
ap.logger.Error("failed to read request body", "error", err) | ||
return nil, err | ||
} | ||
resp.Body = ioutil.NopCloser(bytes.NewBuffer(respBody)) | ||
|
||
return &SendResponse{ | ||
Response: resp, | ||
ResponseBody: respBody, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cache | ||
|
||
import ( | ||
"testing" | ||
|
||
hclog "github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/vault/api" | ||
"github.com/hashicorp/vault/helper/jsonutil" | ||
"github.com/hashicorp/vault/helper/logging" | ||
"github.com/hashicorp/vault/helper/namespace" | ||
) | ||
|
||
func TestCache_APIProxy(t *testing.T) { | ||
cleanup, client, _ := setupClusterAndAgent(t, nil) | ||
defer cleanup() | ||
|
||
proxier := NewAPIProxy(&APIProxyConfig{ | ||
Logger: logging.NewVaultLogger(hclog.Trace), | ||
}) | ||
|
||
r := client.NewRequest("GET", "/v1/sys/health") | ||
req, err := r.ToRetryableHTTP() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
resp, err := proxier.Send(namespace.RootContext(nil), &SendRequest{ | ||
Request: req.Request, | ||
}) | ||
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") | ||
} | ||
} |
Oops, something went wrong.