@@ -11,24 +11,17 @@ A reasonably complete and well-tested golang port of [Kenneth Reitz][kr]'s
11
11
12
12
## Usage
13
13
14
+ ### Docker
14
15
15
- ### Configuration
16
-
17
- go-httpbin can be configured via either command line arguments or environment
18
- variables (or a combination of the two):
19
-
20
- | Argument| Env var | Documentation | Default |
21
- | - | - | - | - |
22
- | ` -host ` | ` HOST ` | Host to listen on | "0.0.0.0" |
23
- | ` -https-cert-file ` | ` HTTPS_CERT_FILE ` | HTTPS Server certificate file | |
24
- | ` -https-key-file ` | ` HTTPS_KEY_FILE ` | HTTPS Server private key file | |
25
- | ` -max-body-size ` | ` MAX_BODY_SIZE ` | Maximum size of request or response, in bytes | 1048576 |
26
- | ` -max-duration ` | ` MAX_DURATION ` | Maximum duration a response may take | 10s |
27
- | ` -port ` | ` PORT ` | Port to listen on | 8080 |
28
- | ` -use-real-hostname ` | ` USE_REAL_HOSTNAME ` | Expose real hostname as reported by os.Hostname() in the /hostname endpoint | false |
16
+ Docker images are published to [ Docker Hub] [ docker-hub ] :
29
17
30
- ** Note:** Command line arguments take precedence over environment variables.
18
+ ``` bash
19
+ # Run http server
20
+ $ docker run -P mccutchen/go-httpbin
31
21
22
+ # Run https server
23
+ $ docker run -e HTTPS_CERT_FILE=' /tmp/server.crt' -e HTTPS_KEY_FILE=' /tmp/server.key' -p 8080:8080 -v /tmp:/tmp mccutchen/go-httpbin
24
+ ```
32
25
33
26
### Standalone binary
34
27
@@ -48,18 +41,6 @@ $ openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
48
41
$ go-httpbin -host 127.0.0.1 -port 8081 -https-cert-file ./server.crt -https-key-file ./server.key
49
42
```
50
43
51
- ### Docker
52
-
53
- Docker images are published to [ Docker Hub] [ docker-hub ] :
54
-
55
- ``` bash
56
- # Run http server
57
- $ docker run -P mccutchen/go-httpbin
58
-
59
- # Run https server
60
- $ docker run -e HTTPS_CERT_FILE=' /tmp/server.crt' -e HTTPS_KEY_FILE=' /tmp/server.key' -p 8080:8080 -v /tmp:/tmp mccutchen/go-httpbin
61
- ```
62
-
63
44
### Unit testing helper library
64
45
65
46
The ` github.com/mccutchen/go-httpbin/httpbin/v2 ` package can also be used as a
@@ -95,16 +76,26 @@ func TestSlowResponse(t *testing.T) {
95
76
}
96
77
```
97
78
79
+ ### Configuration
98
80
99
- ## Custom instrumentation
81
+ go-httpbin can be configured via either command line arguments or environment
82
+ variables (or a combination of the two):
100
83
101
- If you're running go-httpbin in your own infrastructure and would like custom
102
- instrumentation (metrics, structured logging, request tracing, etc), you'll
103
- need to wrap this package in your own code and use the included
104
- [ Observer] [ observer ] mechanism to instrument requests as necessary.
84
+ | Argument| Env var | Documentation | Default |
85
+ | - | - | - | - |
86
+ | ` -allowed-redirect-domains ` | ` ALLOWED_REDIRECT_DOMAINS ` | Comma-separated list of domains the /redirect-to endpoint will allow | |
87
+ | ` -host ` | ` HOST ` | Host to listen on | "0.0.0.0" |
88
+ | ` -https-cert-file ` | ` HTTPS_CERT_FILE ` | HTTPS Server certificate file | |
89
+ | ` -https-key-file ` | ` HTTPS_KEY_FILE ` | HTTPS Server private key file | |
90
+ | ` -max-body-size ` | ` MAX_BODY_SIZE ` | Maximum size of request or response, in bytes | 1048576 |
91
+ | ` -max-duration ` | ` MAX_DURATION ` | Maximum duration a response may take | 10s |
92
+ | ` -port ` | ` PORT ` | Port to listen on | 8080 |
93
+ | ` -use-real-hostname ` | ` USE_REAL_HOSTNAME ` | Expose real hostname as reported by os.Hostname() in the /hostname endpoint | false |
105
94
106
- See [ examples/custom-instrumentation] [ custom-instrumentation ] for an example
107
- that instruments every request using DataDog.
95
+ ** Notes:**
96
+ - Command line arguments take precedence over environment variables.
97
+ - See [ Production considerations] for recommendations around safe configuration
98
+ of public instances of go-httpbin
108
99
109
100
110
101
## Installation
@@ -122,6 +113,66 @@ go install github.com/mccutchen/go-httpbin/v2/cmd/go-httpbin
122
113
```
123
114
124
115
116
+ ## Production considerations
117
+
118
+ Before deploying an instance of go-httpbin on your own infrastructure on the
119
+ public internet, consider tuning it appropriately:
120
+
121
+ 1 . ** Restrict the domains to which the ` /redirect-to ` endpoint will send
122
+ traffic to avoid the security issues of an open redirect**
123
+
124
+ Use the ` -allowed-redirect-domains ` CLI argument or the
125
+ ` ALLOWED_REDIRECT_DOMAINS ` env var to configure an appropriate allowlist.
126
+
127
+ 2 . ** Tune per-request limits**
128
+
129
+ Because go-httpbin allows clients send arbitrary data in request bodies and
130
+ control the duration some requests (e.g. ` /delay/60s ` ), it's important to
131
+ properly tune limits to prevent misbehaving or malicious clients from taking
132
+ too many resources.
133
+
134
+ Use the ` -max-body-size ` /` MAX_BODY_SIZE ` and ` -max-duration ` /` MAX_DURATION `
135
+ CLI arguments or env vars to enforce appropriate limits on each request.
136
+
137
+ 3 . ** Decide whether to expose real hostnames in the ` /hostname ` endpoint**
138
+
139
+ By default, the ` /hostname ` endpoint serves a dummy hostname value, but it
140
+ can be configured to serve the real underlying hostname (according to
141
+ ` os.Hostname() ` ) using the ` -use-real-hostname ` CLI argument or the
142
+ ` USE_REAL_HOSTNAME ` env var to enable this functionality.
143
+
144
+ Before enabling this, ensure that your hostnames do not reveal too much
145
+ about your underlying infrastructure.
146
+
147
+ 4 . ** Add custom instrumentation**
148
+
149
+ By default, go-httpbin logs basic information about each request. To add
150
+ more detailed instrumentation (metrics, structured logging, request
151
+ tracing), you'll need to wrap this package in your own code, which you can
152
+ then instrument as you would any net/http server. Some examples:
153
+
154
+ - [ examples/custom-instrumentation] instruments every request using DataDog,
155
+ based on the built-in [ Observer] mechanism.
156
+
157
+ - [ mccutchen/httpbingo.org] is the code that powers the public instance of
158
+ go-httpbin deployed to [ httpbingo.org] , which adds customized structured
159
+ logging using [ zerolog] and further hardens the HTTP server against
160
+ malicious clients by tuning lower-level timeouts and limits.
161
+
162
+ ## Development
163
+
164
+ ``` bash
165
+ # local development
166
+ make
167
+ make test
168
+ make testcover
169
+ make run
170
+
171
+ # building & pushing docker images
172
+ make image
173
+ make imagepush
174
+ ```
175
+
125
176
## Motivation & prior art
126
177
127
178
I've been a longtime user of [ Kenneith Reitz] [ kr ] 's original
@@ -148,24 +199,14 @@ Compared to [ahmetb/go-httpbin][ahmet]:
148
199
- More complete implementation of endpoints
149
200
150
201
151
- ## Development
152
-
153
- ``` bash
154
- # local development
155
- make
156
- make test
157
- make testcover
158
- make run
159
-
160
- # building & pushing docker images
161
- make image
162
- make imagepush
163
- ```
164
-
165
- [ kr ] : https://github.com/kennethreitz
166
- [ httpbin-org ] : https://httpbin.org/
167
- [ httpbin-repo ] : https://github.com/kennethreitz/httpbin
168
202
[ ahmet ] : https://github.com/ahmetb/go-httpbin
169
203
[ docker-hub ] : https://hub.docker.com/r/mccutchen/go-httpbin/
170
- [ observer ] : https://pkg.go.dev/github.com/mccutchen/go-httpbin/v2/httpbin#Observer
171
- [ custom-instrumentation ] : ./examples/custom-instrumentation/
204
+ [ examples/custom-instrumentation ] : ./examples/custom-instrumentation/
205
+ [ httpbin-org ] : https://httpbin.org/
206
+ [ httpbin-repo ] : https://github.com/kennethreitz/httpbin
207
+ [ httpbingo.org ] : https://httpbingo.org/
208
+ [ kr ] : https://github.com/kennethreitz
209
+ [ mccutchen/httpbingo.org ] : https://github.com/mccutchen/httpbingo.org
210
+ [ Observer ] : https://pkg.go.dev/github.com/mccutchen/go-httpbin/v2/httpbin#Observer
211
+ [ Production considerations ] : #production-considerations
212
+ [ zerolog ] : https://github.com/rs/zerolog
0 commit comments