forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
203 lines (173 loc) · 4.42 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package forwarding
import (
"bytes"
"crypto/tls"
"crypto/x509"
"net/http"
"net/url"
"os"
"github.com/golang/protobuf/proto"
"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.
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{
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,
}
for k, v := range req.Header {
fq.HeaderEntries[k] = &HeaderEntry{
Values: v,
}
}
buf := bytes.NewBuffer(nil)
_, err := buf.ReadFrom(req.Body)
if err != nil {
return nil, err
}
fq.Body = buf.Bytes()
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
}
}
return &fq, nil
}
// 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.
func ParseForwardedHTTPRequest(req *http.Request) (*http.Request, error) {
buf := bytes.NewBuffer(nil)
_, err := buf.ReadFrom(req.Body)
if err != nil {
return nil, err
}
fq := new(Request)
switch os.Getenv("VAULT_MESSAGE_TYPE") {
case "json", "json_compress":
err = jsonutil.DecodeJSON(buf.Bytes(), fq)
default:
err = proto.Unmarshal(buf.Bytes(), fq)
}
if err != nil {
return nil, err
}
return ParseForwardedRequest(fq)
}
func ParseForwardedRequest(fq *Request) (*http.Request, error) {
buf := bufCloser{
Buffer: bytes.NewBuffer(fq.Body),
}
ret := &http.Request{
Method: fq.Method,
Header: make(map[string][]string, len(fq.HeaderEntries)),
Body: buf,
Host: fq.Host,
RemoteAddr: fq.RemoteAddr,
}
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
}
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
}
ret.TLS.PeerCertificates[i] = cert
}
}
return ret, nil
}
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
}