/
test_tools.go
66 lines (51 loc) · 1.78 KB
/
test_tools.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package server
import (
"net/http"
"os"
"github.com/Shopify/goose/srvutil"
"github.com/covid-tracing-mongolia/backend-server/pkg/keyclaim"
"github.com/covid-tracing-mongolia/backend-server/pkg/persistence"
"github.com/gorilla/mux"
)
func NewTestToolsServlet(db persistence.Conn, auth keyclaim.Authenticator) srvutil.Servlet {
log(nil, nil).Info("registering admin servlet")
return &testToolsServlet{db: db, auth: auth}
}
func (t testToolsServlet) RegisterRouting(r *mux.Router) {
// This should never happen but I want to be extra sure this isn't in production
if os.Getenv("ENABLE_TEST_TOOLS") != "true" {
panic("test tools must be enabled to register these routes")
}
if os.Getenv("ENV") == "production" {
panic("attempting to enable test tools in production")
}
log(nil, nil).Info("registering admin routes")
r.HandleFunc("/clear-diagnosis-keys", t.clearDiagnosisKeys)
}
type testToolsServlet struct {
db persistence.Conn
auth keyclaim.Authenticator
}
func (t *testToolsServlet) clearDiagnosisKeys(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if r.Method != "POST" {
log(ctx, nil).WithField("method", r.Method).Info("disallowed method")
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
hdr := r.Header.Get("Authorization")
_, _, ok := t.auth.RegionFromAuthHeader(hdr)
if !ok {
log(ctx, nil).WithField("header", hdr).Info("bad auth header")
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
if err := t.db.ClearDiagnosisKeys(ctx); err != nil {
log(ctx, err).Error("unable to clear diagnosis_keys")
http.Error(w, "unable to clear diagnosis_keys", http.StatusInternalServerError)
return
}
log(ctx, nil).Info("cleared diagnosis_keys")
w.WriteHeader(http.StatusOK)
w.Write([]byte("cleared diagnosis_keys"))
}