mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-14 18:47:01 +02:00
* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License. Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUS-1.1 * Fix test that expected exact offset on hcl file --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Sarah Thompson <sthompson@hashicorp.com> Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
130 lines
3.5 KiB
Go
130 lines
3.5 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package forwarding
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"net/http"
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func Test_ForwardedRequest_GenerateParse(t *testing.T) {
|
|
testForwardedRequestGenerateParse(t)
|
|
}
|
|
|
|
func Benchmark_ForwardedRequest_GenerateParse_JSON(b *testing.B) {
|
|
os.Setenv("VAULT_MESSAGE_TYPE", "json")
|
|
var totalSize int64
|
|
var numRuns int64
|
|
for i := 0; i < b.N; i++ {
|
|
totalSize += testForwardedRequestGenerateParse(b)
|
|
numRuns++
|
|
}
|
|
b.Logf("message size per op: %d", totalSize/numRuns)
|
|
}
|
|
|
|
func Benchmark_ForwardedRequest_GenerateParse_JSON_Compressed(b *testing.B) {
|
|
os.Setenv("VAULT_MESSAGE_TYPE", "json_compress")
|
|
var totalSize int64
|
|
var numRuns int64
|
|
for i := 0; i < b.N; i++ {
|
|
totalSize += testForwardedRequestGenerateParse(b)
|
|
numRuns++
|
|
}
|
|
b.Logf("message size per op: %d", totalSize/numRuns)
|
|
}
|
|
|
|
func Benchmark_ForwardedRequest_GenerateParse_Proto3(b *testing.B) {
|
|
os.Setenv("VAULT_MESSAGE_TYPE", "proto3")
|
|
var totalSize int64
|
|
var numRuns int64
|
|
for i := 0; i < b.N; i++ {
|
|
totalSize += testForwardedRequestGenerateParse(b)
|
|
numRuns++
|
|
}
|
|
b.Logf("message size per op: %d", totalSize/numRuns)
|
|
}
|
|
|
|
func testForwardedRequestGenerateParse(t testing.TB) int64 {
|
|
bodBuf := bytes.NewReader([]byte(`{ "foo": "bar", "zip": { "argle": "bargle", neet: 0 } }`))
|
|
req, err := http.NewRequest("FOOBAR", "https://pushit.real.good:9281/snicketysnack?furbleburble=bloopetybloop", bodBuf)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// We want to get the fields we would expect from an incoming request, so
|
|
// we write it out and then read it again
|
|
buf1 := bytes.NewBuffer(nil)
|
|
err = req.Write(buf1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Read it back in, parsing like a server
|
|
bufr1 := bufio.NewReader(buf1)
|
|
initialReq, err := http.ReadRequest(bufr1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Generate the request with the forwarded request in the body
|
|
req, err = GenerateForwardedHTTPRequest(initialReq, "https://bloopety.bloop:8201")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Perform another "round trip"
|
|
buf2 := bytes.NewBuffer(nil)
|
|
err = req.Write(buf2)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
size := int64(buf2.Len())
|
|
bufr2 := bufio.NewReader(buf2)
|
|
intreq, err := http.ReadRequest(bufr2)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Now extract the forwarded request to generate a final request for processing
|
|
finalReq, err := ParseForwardedHTTPRequest(intreq)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
switch {
|
|
case initialReq.Method != finalReq.Method:
|
|
t.Fatalf("bad method:\ninitialReq:\n%#v\nfinalReq:\n%#v\n", *initialReq, *finalReq)
|
|
case initialReq.RemoteAddr != finalReq.RemoteAddr:
|
|
t.Fatalf("bad remoteaddr:\ninitialReq:\n%#v\nfinalReq:\n%#v\n", *initialReq, *finalReq)
|
|
case initialReq.Host != finalReq.Host:
|
|
t.Fatalf("bad host:\ninitialReq:\n%#v\nfinalReq:\n%#v\n", *initialReq, *finalReq)
|
|
case !reflect.DeepEqual(initialReq.URL, finalReq.URL):
|
|
t.Fatalf("bad url:\ninitialReq:\n%#v\nfinalReq:\n%#v\n", *initialReq.URL, *finalReq.URL)
|
|
case !reflect.DeepEqual(initialReq.Header, finalReq.Header):
|
|
t.Fatalf("bad header:\ninitialReq:\n%#v\nfinalReq:\n%#v\n", *initialReq, *finalReq)
|
|
default:
|
|
// Compare bodies
|
|
bodBuf.Seek(0, 0)
|
|
initBuf := bytes.NewBuffer(nil)
|
|
_, err = initBuf.ReadFrom(bodBuf)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
finBuf := bytes.NewBuffer(nil)
|
|
_, err = finBuf.ReadFrom(finalReq.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(initBuf.Bytes(), finBuf.Bytes()) {
|
|
t.Fatalf("badbody :\ninitialReq:\n%#v\nfinalReq:\n%#v\n", initBuf.Bytes(), finBuf.Bytes())
|
|
}
|
|
}
|
|
|
|
return size
|
|
}
|