Skip to content

Commit 2ca1d56

Browse files
authored
rpc: add a /health endpoint verifying KMS (#28)
1 parent 8645216 commit 2ca1d56

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

rpc/rpc.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,13 @@ func (s *RPC) Handler() http.Handler {
191191
r.Use(middleware.PageRoute("/status", http.HandlerFunc(s.statusHandler)))
192192
r.Use(middleware.PageRoute("/favicon.ico", http.HandlerFunc(emptyHandler)))
193193

194-
userRouter := r.Group(func(r chi.Router) {
195-
// Generate attestation document
196-
r.Use(attestation.Middleware(s.Enclave))
194+
// Generate attestation document
195+
r.Use(attestation.Middleware(s.Enclave))
196+
197+
// Healthcheck
198+
r.Use(middleware.PageRoute("/health", http.HandlerFunc(s.healthHandler)))
197199

200+
userRouter := r.Group(func(r chi.Router) {
198201
// Find and decrypt tenant data
199202
r.Use(tenant.Middleware(s.Tenants, s.Config.KMS.TenantKeys))
200203
})
@@ -203,9 +206,6 @@ func (s *RPC) Handler() http.Handler {
203206
adminRouter := r.Group(func(r chi.Router) {
204207
// Validate admin JWTs
205208
r.Use(access.JWTAuthMiddleware(s.Config.Admin))
206-
207-
// Generate attestation document
208-
r.Use(attestation.Middleware(s.Enclave))
209209
})
210210
adminRouter.Handle("/rpc/WaasAuthenticatorAdmin/*", proto.NewWaasAuthenticatorAdminServer(s))
211211

rpc/status.go

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
waasauthenticator "github.com/0xsequence/waas-authenticator"
1010
"github.com/0xsequence/waas-authenticator/proto"
11+
"github.com/0xsequence/waas-authenticator/rpc/attestation"
1112
)
1213

1314
func (s *RPC) Version(ctx context.Context) (*proto.Version, error) {
@@ -46,3 +47,13 @@ func (s *RPC) statusHandler(w http.ResponseWriter, r *http.Request) {
4647
w.WriteHeader(http.StatusOK)
4748
_ = json.NewEncoder(w).Encode(status)
4849
}
50+
51+
func (s *RPC) healthHandler(w http.ResponseWriter, r *http.Request) {
52+
ctx := r.Context()
53+
att := attestation.FromContext(ctx)
54+
if _, err := att.GenerateDataKey(ctx, s.Config.KMS.TenantKeys[0]); err != nil {
55+
w.WriteHeader(http.StatusServiceUnavailable)
56+
return
57+
}
58+
w.WriteHeader(http.StatusOK)
59+
}

0 commit comments

Comments
 (0)