2016-08-19 15:03:53 +00:00
|
|
|
package forwarding
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/tls"
|
2016-08-17 14:29:53 +00:00
|
|
|
"crypto/x509"
|
2016-08-15 13:42:42 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-08-17 20:15:15 +00:00
|
|
|
"os"
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2016-08-17 20:15:15 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2016-08-15 13:42:42 +00:00
|
|
|
"github.com/hashicorp/vault/helper/compressutil"
|
|
|
|
"github.com/hashicorp/vault/helper/jsonutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type bufCloser struct {
|
|
|
|
*bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b bufCloser) Close() error {
|
|
|
|
b.Reset()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateForwardedRequest generates a new http.Request that contains the
|
|
|
|
// original requests's information in the new request's body.
|
2016-08-19 15:03:53 +00:00
|
|
|
func GenerateForwardedHTTPRequest(req *http.Request, addr string) (*http.Request, error) {
|
|
|
|
fq, err := GenerateForwardedRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var newBody []byte
|
|
|
|
switch os.Getenv("VAULT_MESSAGE_TYPE") {
|
|
|
|
case "json":
|
|
|
|
newBody, err = jsonutil.EncodeJSON(fq)
|
|
|
|
case "json_compress":
|
|
|
|
newBody, err = jsonutil.EncodeJSONAndCompress(fq, &compressutil.CompressionConfig{
|
|
|
|
Type: compressutil.CompressionTypeLzw,
|
|
|
|
})
|
|
|
|
case "proto3":
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
newBody, err = proto.Marshal(fq)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err := http.NewRequest("POST", addr, bytes.NewBuffer(newBody))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateForwardedRequest(req *http.Request) (*Request, error) {
|
|
|
|
fq := Request{
|
2016-08-17 20:15:15 +00:00
|
|
|
Method: req.Method,
|
|
|
|
HeaderEntries: make(map[string]*HeaderEntry, len(req.Header)),
|
|
|
|
Host: req.Host,
|
|
|
|
RemoteAddr: req.RemoteAddr,
|
|
|
|
}
|
|
|
|
|
|
|
|
reqURL := req.URL
|
|
|
|
fq.Url = &URL{
|
|
|
|
Scheme: reqURL.Scheme,
|
|
|
|
Opaque: reqURL.Opaque,
|
|
|
|
Host: reqURL.Host,
|
|
|
|
Path: reqURL.Path,
|
|
|
|
RawPath: reqURL.RawPath,
|
|
|
|
RawQuery: reqURL.RawQuery,
|
|
|
|
Fragment: reqURL.Fragment,
|
2016-08-17 14:29:53 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 20:15:15 +00:00
|
|
|
for k, v := range req.Header {
|
|
|
|
fq.HeaderEntries[k] = &HeaderEntry{
|
|
|
|
Values: v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
_, err := buf.ReadFrom(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fq.Body = buf.Bytes()
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
if req.TLS != nil && req.TLS.PeerCertificates != nil && len(req.TLS.PeerCertificates) > 0 {
|
|
|
|
fq.PeerCertificates = make([][]byte, len(req.TLS.PeerCertificates))
|
|
|
|
for i, cert := range req.TLS.PeerCertificates {
|
|
|
|
fq.PeerCertificates[i] = cert.Raw
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
return &fq, nil
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseForwardedRequest generates a new http.Request that is comprised of the
|
|
|
|
// values in the given request's body, assuming it correctly parses into a
|
|
|
|
// ForwardedRequest.
|
2016-08-19 15:03:53 +00:00
|
|
|
func ParseForwardedHTTPRequest(req *http.Request) (*http.Request, error) {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
2016-08-15 13:42:42 +00:00
|
|
|
_, err := buf.ReadFrom(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
fq := new(Request)
|
2016-08-17 20:15:15 +00:00
|
|
|
switch os.Getenv("VAULT_MESSAGE_TYPE") {
|
|
|
|
case "json", "json_compress":
|
|
|
|
err = jsonutil.DecodeJSON(buf.Bytes(), fq)
|
|
|
|
default:
|
|
|
|
err = proto.Unmarshal(buf.Bytes(), fq)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
return ParseForwardedRequest(fq)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseForwardedRequest(fq *Request) (*http.Request, error) {
|
|
|
|
buf := bufCloser{
|
|
|
|
Buffer: bytes.NewBuffer(fq.Body),
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret := &http.Request{
|
|
|
|
Method: fq.Method,
|
2016-08-17 20:15:15 +00:00
|
|
|
Header: make(map[string][]string, len(fq.HeaderEntries)),
|
2016-08-15 13:42:42 +00:00
|
|
|
Body: buf,
|
|
|
|
Host: fq.Host,
|
|
|
|
RemoteAddr: fq.RemoteAddr,
|
2016-08-17 14:29:53 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 20:15:15 +00:00
|
|
|
ret.URL = &url.URL{
|
|
|
|
Scheme: fq.Url.Scheme,
|
|
|
|
Opaque: fq.Url.Opaque,
|
|
|
|
Host: fq.Url.Host,
|
|
|
|
Path: fq.Url.Path,
|
|
|
|
RawPath: fq.Url.RawPath,
|
|
|
|
RawQuery: fq.Url.RawQuery,
|
|
|
|
Fragment: fq.Url.Fragment,
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range fq.HeaderEntries {
|
|
|
|
ret.Header[k] = v.Values
|
|
|
|
}
|
|
|
|
|
2016-08-17 14:29:53 +00:00
|
|
|
if fq.PeerCertificates != nil && len(fq.PeerCertificates) > 0 {
|
|
|
|
ret.TLS = &tls.ConnectionState{
|
|
|
|
PeerCertificates: make([]*x509.Certificate, len(fq.PeerCertificates)),
|
|
|
|
}
|
|
|
|
for i, certBytes := range fq.PeerCertificates {
|
|
|
|
cert, err := x509.ParseCertificate(certBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-17 20:15:15 +00:00
|
|
|
ret.TLS.PeerCertificates[i] = cert
|
2016-08-17 14:29:53 +00:00
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2016-08-19 15:03:53 +00:00
|
|
|
|
|
|
|
type RPCResponseWriter struct {
|
|
|
|
statusCode int
|
|
|
|
header http.Header
|
|
|
|
body *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRPCResponseWriter returns an initialized RPCResponseWriter
|
|
|
|
func NewRPCResponseWriter() *RPCResponseWriter {
|
|
|
|
w := &RPCResponseWriter{
|
|
|
|
header: make(http.Header),
|
|
|
|
body: new(bytes.Buffer),
|
|
|
|
statusCode: 200,
|
|
|
|
}
|
|
|
|
//w.header.Set("Content-Type", "application/octet-stream")
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *RPCResponseWriter) Header() http.Header {
|
|
|
|
return w.header
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *RPCResponseWriter) Write(buf []byte) (int, error) {
|
|
|
|
w.body.Write(buf)
|
|
|
|
return len(buf), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *RPCResponseWriter) WriteHeader(code int) {
|
|
|
|
w.statusCode = code
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *RPCResponseWriter) StatusCode() int {
|
|
|
|
return w.statusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *RPCResponseWriter) Body() *bytes.Buffer {
|
|
|
|
return w.body
|
|
|
|
}
|