Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Error if purge request made with dev mode disabled #3295

Merged
merged 19 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}

isDevMode := cfg.GetBool("development")
http.IsDevMode = isDevMode

Check warning on line 138 in cli/start.go

View check run for this annotation

Codecov / codecov/patch

cli/start.go#L138

Added line #L138 was not covered by tests
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()
}
Loading