-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): Add readonly flag to api (#963)
- Loading branch information
1 parent
5fc5a29
commit 138341f
Showing
10 changed files
with
273 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package handler | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/moira-alert/moira/api" | ||
"github.com/moira-alert/moira/api/dto" | ||
"github.com/moira-alert/moira/logging/zerolog_adapter" | ||
mock_moira_alert "github.com/moira-alert/moira/mock/moira-alert" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestReadonlyMode(t *testing.T) { | ||
Convey("Test readonly mode enabled", t, func() { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
responseWriter := httptest.NewRecorder() | ||
mockDb := mock_moira_alert.NewMockDatabase(mockCtrl) | ||
database = mockDb | ||
|
||
logger, _ := zerolog_adapter.GetLogger("Test") | ||
config := &api.Config{Flags: api.FeatureFlags{IsReadonlyEnabled: true}} | ||
expectedConfig := []byte("Expected config") | ||
handler := NewHandler(mockDb, logger, nil, config, nil, expectedConfig) | ||
|
||
Convey("Get notifier health", func() { | ||
mockDb.EXPECT().GetNotifierState().Return("OK", nil).Times(1) | ||
|
||
expected := &dto.NotifierState{ | ||
State: "OK", | ||
} | ||
|
||
testRequest := httptest.NewRequest(http.MethodGet, "/api/health/notifier", nil) | ||
|
||
handler.ServeHTTP(responseWriter, testRequest) | ||
|
||
response := responseWriter.Result() | ||
defer response.Body.Close() | ||
content, _ := io.ReadAll(response.Body) | ||
actual := &dto.NotifierState{} | ||
err := json.Unmarshal(content, actual) | ||
So(err, ShouldBeNil) | ||
|
||
So(actual, ShouldResemble, expected) | ||
So(response.StatusCode, ShouldEqual, http.StatusOK) | ||
}) | ||
|
||
Convey("Put notifier health", func() { | ||
mockDb.EXPECT().SetNotifierState("OK").Return(nil).Times(1) | ||
|
||
state := &dto.NotifierState{ | ||
State: "OK", | ||
} | ||
|
||
stateBytes, err := json.Marshal(state) | ||
So(err, ShouldBeNil) | ||
|
||
testRequest := httptest.NewRequest(http.MethodPut, "/api/health/notifier", bytes.NewReader(stateBytes)) | ||
|
||
handler.ServeHTTP(responseWriter, testRequest) | ||
|
||
response := responseWriter.Result() | ||
defer response.Body.Close() | ||
So(response.StatusCode, ShouldEqual, http.StatusOK) | ||
}) | ||
|
||
Convey("Put new trigger", func() { | ||
trigger := &dto.Trigger{} | ||
triggerBytes, err := json.Marshal(trigger) | ||
So(err, ShouldBeNil) | ||
|
||
testRequest := httptest.NewRequest(http.MethodPut, "/api/trigger", bytes.NewReader(triggerBytes)) | ||
|
||
handler.ServeHTTP(responseWriter, testRequest) | ||
|
||
response := responseWriter.Result() | ||
defer response.Body.Close() | ||
So(response.StatusCode, ShouldEqual, http.StatusForbidden) | ||
}) | ||
|
||
Convey("Get contact", func() { | ||
testRequest := httptest.NewRequest(http.MethodGet, "/api/config", nil) | ||
|
||
handler.ServeHTTP(responseWriter, testRequest) | ||
|
||
response := responseWriter.Result() | ||
defer response.Body.Close() | ||
actual, _ := io.ReadAll(response.Body) | ||
|
||
So(response.StatusCode, ShouldEqual, http.StatusOK) | ||
So(actual, ShouldResemble, expectedConfig) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-chi/render" | ||
"github.com/moira-alert/moira/api" | ||
) | ||
|
||
// ReadOnlyMiddleware returns 403 for mutating queries if readonly mode is enabled | ||
func ReadOnlyMiddleware(config *api.Config) func(next http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
fn := func(w http.ResponseWriter, r *http.Request) { | ||
if config.Flags.IsReadonlyEnabled && isMutatingMethod(r.Method) { | ||
render.Render(w, r, api.ErrorForbidden("Moira is currently in read-only mode")) //nolint:errcheck | ||
return | ||
} | ||
next.ServeHTTP(w, r) | ||
} | ||
return http.HandlerFunc(fn) | ||
} | ||
} | ||
|
||
func isMutatingMethod(method string) bool { | ||
return method == http.MethodPut || | ||
method == http.MethodPost || | ||
method == http.MethodPatch || | ||
method == http.MethodDelete | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/moira-alert/moira/api" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestReadonlyModeMiddleware(t *testing.T) { | ||
Convey("Given readonly mode is disabled", t, func() { | ||
config := &api.Config{Flags: api.FeatureFlags{IsReadonlyEnabled: false}} | ||
|
||
Convey("Performing get request", func() { | ||
actual := PerformRequestWithReadonlyModeMiddleware(config, http.MethodGet) | ||
|
||
So(actual, ShouldEqual, http.StatusOK) | ||
}) | ||
Convey("Performing put request", func() { | ||
actual := PerformRequestWithReadonlyModeMiddleware(config, http.MethodPut) | ||
|
||
So(actual, ShouldEqual, http.StatusOK) | ||
}) | ||
}) | ||
|
||
Convey("Given readonly mode is enabled", t, func() { | ||
config := &api.Config{Flags: api.FeatureFlags{IsReadonlyEnabled: true}} | ||
|
||
Convey("Performing get request", func() { | ||
actual := PerformRequestWithReadonlyModeMiddleware(config, http.MethodGet) | ||
|
||
So(actual, ShouldEqual, http.StatusOK) | ||
}) | ||
Convey("Performing put request", func() { | ||
actual := PerformRequestWithReadonlyModeMiddleware(config, http.MethodPut) | ||
|
||
So(actual, ShouldEqual, http.StatusForbidden) | ||
}) | ||
Convey("Performing post request", func() { | ||
actual := PerformRequestWithReadonlyModeMiddleware(config, http.MethodPost) | ||
|
||
So(actual, ShouldEqual, http.StatusForbidden) | ||
}) | ||
Convey("Performing patch request", func() { | ||
actual := PerformRequestWithReadonlyModeMiddleware(config, http.MethodPatch) | ||
|
||
So(actual, ShouldEqual, http.StatusForbidden) | ||
}) | ||
Convey("Performing delete request", func() { | ||
actual := PerformRequestWithReadonlyModeMiddleware(config, http.MethodDelete) | ||
|
||
So(actual, ShouldEqual, http.StatusForbidden) | ||
}) | ||
}) | ||
} | ||
|
||
func PerformRequestWithReadonlyModeMiddleware(config *api.Config, method string) int { | ||
responseWriter := httptest.NewRecorder() | ||
|
||
testRequest := httptest.NewRequest(method, "/test", nil) | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) {} | ||
middlewareFunc := ReadOnlyMiddleware(config) | ||
wrappedHandler := middlewareFunc(http.HandlerFunc(handler)) | ||
|
||
wrappedHandler.ServeHTTP(responseWriter, testRequest) | ||
response := responseWriter.Result() | ||
defer response.Body.Close() | ||
|
||
return response.StatusCode | ||
} |
Oops, something went wrong.