Skip to content

Commit 17e60a9

Browse files
committed
Disabled set middleware api by default
1 parent 8d85210 commit 17e60a9

File tree

9 files changed

+29
-12
lines changed

9 files changed

+29
-12
lines changed

core/admin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func getAllHandlers(hoverfly *Hoverfly) []handlers.AdminHandler {
9999
&v2.HoverflyHandler{Hoverfly: hoverfly},
100100
&v2.HoverflyDestinationHandler{Hoverfly: hoverfly},
101101
&v2.HoverflyModeHandler{Hoverfly: hoverfly},
102-
&v2.HoverflyMiddlewareHandler{Hoverfly: hoverfly},
102+
&v2.HoverflyMiddlewareHandler{Hoverfly: hoverfly, Enabled: hoverfly.Cfg.EnableMiddlewareAPI},
103103
&v2.HoverflyUsageHandler{Hoverfly: hoverfly},
104104
&v2.HoverflyVersionHandler{Hoverfly: hoverfly},
105105
&v2.HoverflyUpstreamProxyHandler{Hoverfly: hoverfly},

core/cmd/hoverfly/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ var (
121121
cors = flag.Bool("cors", false, "Enable CORS support")
122122
noImportCheck = flag.Bool("no-import-check", false, "Skip duplicate request check when importing simulations")
123123

124+
// Feature flags
125+
enableMiddlewareAPI = flag.Bool("enable-middleware-api", false, "Enable the admin API to set middleware (PUT /api/v2/hoverfly/middleware)")
126+
124127
pacFile = flag.String("pac-file", "", "Path to the pac file to be imported on startup")
125128

126129
clientAuthenticationDestination = flag.String("client-authentication-destination", "", "Regular expression of destination with client authentication")
@@ -403,6 +406,9 @@ func main() {
403406
log.Info("Import check has been disabled")
404407
}
405408

409+
// Feature flags
410+
cfg.EnableMiddlewareAPI = *enableMiddlewareAPI
411+
406412
cfg.ClientAuthenticationDestination = *clientAuthenticationDestination
407413
cfg.ClientAuthenticationClientCert = *clientAuthenticationClientCert
408414
cfg.ClientAuthenticationClientKey = *clientAuthenticationClientKey

core/handlers/v2/hoverfly_middleware_handler.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type HoverflyMiddleware interface {
1616

1717
type HoverflyMiddlewareHandler struct {
1818
Hoverfly HoverflyMiddleware
19+
Enabled bool
1920
}
2021

2122
func (this *HoverflyMiddlewareHandler) RegisterRoutes(mux *bone.Mux, am *handlers.AuthHandler) {
@@ -24,10 +25,12 @@ func (this *HoverflyMiddlewareHandler) RegisterRoutes(mux *bone.Mux, am *handler
2425
negroni.HandlerFunc(this.Get),
2526
))
2627

27-
mux.Put("/api/v2/hoverfly/middleware", negroni.New(
28-
negroni.HandlerFunc(am.RequireTokenAuthentication),
29-
negroni.HandlerFunc(this.Put),
30-
))
28+
if this.Enabled {
29+
mux.Put("/api/v2/hoverfly/middleware", negroni.New(
30+
negroni.HandlerFunc(am.RequireTokenAuthentication),
31+
negroni.HandlerFunc(this.Put),
32+
))
33+
}
3134
mux.Options("/api/v2/hoverfly/middleware", negroni.New(
3235
negroni.HandlerFunc(this.Options),
3336
))
@@ -60,6 +63,10 @@ func (this *HoverflyMiddlewareHandler) Put(w http.ResponseWriter, req *http.Requ
6063
}
6164

6265
func (this *HoverflyMiddlewareHandler) Options(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
63-
w.Header().Add("Allow", "OPTIONS, GET, PUT")
66+
allow := "OPTIONS, GET"
67+
if this.Enabled {
68+
allow += ", PUT"
69+
}
70+
w.Header().Add("Allow", allow)
6471
handlers.WriteResponse(w, []byte(""))
6572
}

core/handlers/v2/hoverfly_middleware_handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func Test_HoverflyMiddlewareHandler_Options_GetsOptions(t *testing.T) {
130130
RegisterTestingT(t)
131131

132132
var stubHoverfly HoverflyMiddlewareStub
133-
unit := HoverflyMiddlewareHandler{Hoverfly: &stubHoverfly}
133+
unit := HoverflyMiddlewareHandler{Hoverfly: &stubHoverfly, Enabled: true}
134134

135135
request, err := http.NewRequest("OPTIONS", "/api/v2/hoverfly/middleware", nil)
136136
Expect(err).To(BeNil())

core/settings.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package hoverfly
22

33
import (
4-
"github.com/SpectoLabs/hoverfly/core/cors"
54
"os"
65
"strconv"
76
"sync"
87

8+
"github.com/SpectoLabs/hoverfly/core/cors"
9+
910
"strings"
1011

1112
"github.com/SpectoLabs/hoverfly/core/middleware"
@@ -52,6 +53,9 @@ type Configuration struct {
5253
ResponsesBodyFilesPath string
5354
ResponsesBodyFilesAllowedOrigins []string
5455

56+
// Feature flags
57+
EnableMiddlewareAPI bool
58+
5559
ProxyControlWG sync.WaitGroup
5660

5761
mu sync.Mutex

functional-tests/core/api/middleware_api_v2_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var _ = Describe("/api/v2/hoverfly/middleware", func() {
1818

1919
BeforeEach(func() {
2020
hoverfly = functional_tests.NewHoverfly()
21-
hoverfly.Start()
21+
hoverfly.Start("-enable-middleware-api")
2222
})
2323

2424
AfterEach(func() {

functional-tests/core/ft_simulate_mode_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var _ = Describe("When I run Hoverfly in simulate mode", func() {
2121

2222
BeforeEach(func() {
2323
hoverfly = functional_tests.NewHoverfly()
24-
hoverfly.Start()
24+
hoverfly.Start("-enable-middleware-api")
2525
hoverfly.SetMode("simulate")
2626
})
2727

functional-tests/hoverctl/middleware_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var _ = Describe("When I use hoverctl", func() {
2323

2424
BeforeEach(func() {
2525
hoverfly = functional_tests.NewHoverfly()
26-
hoverfly.Start()
26+
hoverfly.Start("-enable-middleware-api")
2727
hoverfly.SetMiddleware("ruby", "#!/usr/bin/env ruby\n# encoding: utf-8\nwhile payload = STDIN.gets\nnext unless payload\n\nSTDOUT.puts payload\nend")
2828

2929
functional_tests.Run(hoverctlBinary, "targets", "update", "local", "--admin-port", hoverfly.GetAdminPort())

functional-tests/hoverctl/status_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var _ = Describe("when I use hoverctl status", func() {
1616

1717
BeforeEach(func() {
1818
hoverfly = functional_tests.NewHoverfly()
19-
hoverfly.Start()
19+
hoverfly.Start("-enable-middleware-api")
2020

2121
functional_tests.Run(hoverctlBinary, "targets", "update", "local", "--admin-port", hoverfly.GetAdminPort())
2222
})

0 commit comments

Comments
 (0)