Skip to content

Commit

Permalink
feat: Error if purge request made with dev mode disabled (#3295)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves #3140

## Description

The issue was that if a purge request was made when the database was not
in development mode, it would fail, and an error would be output on the
node side of things. However, on the client side nothing indicated that
the process had failed. Whether the purge was successful, or it failed,
there would be no output.

I have created a new variable in the http package called `IsDevMode`,
which is checked in the `http/handler_extras.go/Purge` function. If dev
mode is enabled then `htttp.StatusOK` is written to the response header.
If it is not enabled, then `http.StatusBadRequest` is written to the
header instead, with a message indicating what happened.

## Tasks

- [x] I made sure the code is well commented, particularly
hard-to-understand areas.
- [x] I made sure the repository-held documentation is changed
accordingly.
- [x] I made sure the pull request title adheres to the conventional
commit style (the subset used in the project can be found in
[tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)).
- [x] I made sure to discuss its limitations such as threats to
validity, vulnerability to mistake and misuse, robustness to
invalidation of assumptions, resource requirements, ...

## How has this been tested?

The platform(s) on which this was tested:
- Windows
  • Loading branch information
ChrisBQu authored Dec 9, 2024
1 parent be3f381 commit d6900b7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
1 change: 1 addition & 0 deletions cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func MakeStartCommand() *cobra.Command {
}

isDevMode := cfg.GetBool("development")
http.IsDevMode = isDevMode
if isDevMode {
cmd.Printf(devModeBanner)
if cfg.GetBool("keyring.disabled") {
Expand Down
7 changes: 4 additions & 3 deletions http/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import (
)

const (
errFailedToLoadKeys string = "failed to load given keys"
errMethodIsNotImplemented string = "the method is not implemented"
errFailedToGetContext string = "failed to get context"
errFailedToLoadKeys string = "failed to load given keys"
errMethodIsNotImplemented string = "the method is not implemented"
errFailedToGetContext string = "failed to get context"
errPurgeRequestNonDeveloperMode string = "cannot purge database when development mode is disabled"
)

// Errors returnable from this package.
Expand Down
4 changes: 4 additions & 0 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
"github.com/go-chi/chi/v5"
)

// Global variable for the development mode flag
// This is checked by the http/handler_extras.go/Purge function to determine which response to send
var IsDevMode bool = false

// Version is the identifier for the current API version.
var Version string = "v0"

Expand Down
9 changes: 8 additions & 1 deletion http/handler_extras.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ type extrasHandler struct{}

func (s *extrasHandler) Purge(rw http.ResponseWriter, req *http.Request) {
db := mustGetContextClientDB(req)
rw.WriteHeader(http.StatusOK) // write the response before we restart to purge

// Send either 200 or 400 response based on whether the server is in dev mode
if IsDevMode {
rw.WriteHeader(http.StatusOK)
} else {
responseJSON(rw, http.StatusBadRequest, errPurgeRequestNonDeveloperMode)
}

db.Events().Publish(event.NewMessage(event.PurgeName, nil))
}

Expand Down
29 changes: 28 additions & 1 deletion http/handler_extras_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ import (
"github.com/stretchr/testify/require"
)

func TestPurge(t *testing.T) {
func TestPurgeDevModeTrue(t *testing.T) {
cdb := setupDatabase(t)

IsDevMode = true

url := "http://localhost:9181/api/v0/purge"

req := httptest.NewRequest(http.MethodPost, url, nil)
Expand All @@ -40,3 +43,27 @@ func TestPurge(t *testing.T) {
// test will timeout if purge never received
<-purgeSub.Message()
}

func TestPurgeDevModeFalse(t *testing.T) {
cdb := setupDatabase(t)

IsDevMode = false

url := "http://localhost:9181/api/v0/purge"

req := httptest.NewRequest(http.MethodPost, url, nil)
rec := httptest.NewRecorder()

purgeSub, err := cdb.Events().Subscribe(event.PurgeName)
require.NoError(t, err)

handler, err := NewHandler(cdb)
require.NoError(t, err)
handler.ServeHTTP(rec, req)

res := rec.Result()
require.Equal(t, 400, res.StatusCode)

// test will timeout if purge never received
<-purgeSub.Message()
}

0 comments on commit d6900b7

Please sign in to comment.