Files
warmbly/internal/api/handler/internal_dek_test.go
T
Matthew Meszaros 99226338c9 infra(encryptedkeys): pluggable DEK store with HTTP proxy for workers
New encryptedkeys.Store interface with three impls:

  postgres  - backend default, durable via PG
  dynamodb  - existing AWS path, also covers Scylla Alternator via
              AWS_ENDPOINT_URL_DYNAMODB
  http      - worker-side adapter that talks to the backend's new
              /api/v1/internal/dek/:userID endpoint, so workers never
              connect directly to Postgres

The HTTP endpoint sits behind a new InternalAuthMiddleware that does
constant-time bearer-token compare against INTERNAL_API_TOKEN. Fail-
closed if the env var is unset.

cipher.Service now takes an encryptedkeys.Store instead of a Dynamo
repository. The old internal/repository/dynamo_user_encrypted_keys.go
is deleted (the file also had a pre-existing copy-paste bug using
EmailMessageMapTable in Get/Del that's gone with it).

New migration 38 adds user_encrypted_keys (user_id PK, encrypted_data_key,
created_at, updated_at).

20 tests cover HTTP round-trip, conflict semantics, factory selection,
middleware auth (fail-closed / wrong-scheme / timing-safe / happy path),
and DEK handler responses through gin's test harness.
2026-05-27 14:42:11 +00:00

176 lines
4.8 KiB
Go

package handler
import (
"bytes"
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/warmbly/warmbly/internal/infrastructure/encryptedkeys"
)
func init() { gin.SetMode(gin.TestMode) }
type mockEKStore struct {
put func(ctx context.Context, userID uuid.UUID, dek string) error
get func(ctx context.Context, userID uuid.UUID) (string, error)
delete func(ctx context.Context, userID uuid.UUID) error
}
func (m *mockEKStore) Put(ctx context.Context, userID uuid.UUID, dek string) error {
return m.put(ctx, userID, dek)
}
func (m *mockEKStore) Get(ctx context.Context, userID uuid.UUID) (string, error) {
return m.get(ctx, userID)
}
func (m *mockEKStore) Delete(ctx context.Context, userID uuid.UUID) error {
return m.delete(ctx, userID)
}
func (m *mockEKStore) Name() string { return "mock" }
func newDEKRouter(t *testing.T, store encryptedkeys.Store) *gin.Engine {
t.Helper()
h := &Handler{EncryptedKeys: store}
r := gin.New()
r.GET("/dek/:userID", h.InternalGetDEK)
r.PUT("/dek/:userID", h.InternalPutDEK)
r.DELETE("/dek/:userID", h.InternalDeleteDEK)
return r
}
func TestInternalGetDEK_Found(t *testing.T) {
id := uuid.New()
store := &mockEKStore{
get: func(_ context.Context, u uuid.UUID) (string, error) {
if u != id {
t.Fatalf("wrong userID: got %s want %s", u, id)
}
return "encrypted-blob", nil
},
}
r := newDEKRouter(t, store)
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/dek/"+id.String(), nil)
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", w.Code, w.Body.String())
}
var p dekPayload
if err := json.Unmarshal(w.Body.Bytes(), &p); err != nil {
t.Fatal(err)
}
if p.EncryptedDataKey != "encrypted-blob" {
t.Fatalf("payload mismatch: %q", p.EncryptedDataKey)
}
}
func TestInternalGetDEK_NotFoundReturns404(t *testing.T) {
store := &mockEKStore{
get: func(_ context.Context, _ uuid.UUID) (string, error) {
return "", nil // empty = not found per Store contract
},
}
r := newDEKRouter(t, store)
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/dek/"+uuid.New().String(), nil)
r.ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Fatalf("expected 404, got %d", w.Code)
}
}
func TestInternalGetDEK_BadUUID(t *testing.T) {
r := newDEKRouter(t, &mockEKStore{})
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/dek/not-a-uuid", nil)
r.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d", w.Code)
}
}
func TestInternalGetDEK_StoreError(t *testing.T) {
store := &mockEKStore{
get: func(_ context.Context, _ uuid.UUID) (string, error) {
return "", errors.New("kaboom")
},
}
r := newDEKRouter(t, store)
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/dek/"+uuid.New().String(), nil)
r.ServeHTTP(w, req)
if w.Code != http.StatusInternalServerError {
t.Fatalf("expected 500, got %d", w.Code)
}
}
func TestInternalPutDEK_Created(t *testing.T) {
id := uuid.New()
store := &mockEKStore{
put: func(_ context.Context, u uuid.UUID, dek string) error {
if u != id {
t.Fatalf("userID mismatch")
}
if dek != "blob" {
t.Fatalf("dek mismatch: %q", dek)
}
return nil
},
}
r := newDEKRouter(t, store)
body, _ := json.Marshal(dekPayload{EncryptedDataKey: "blob"})
w := httptest.NewRecorder()
req := httptest.NewRequest("PUT", "/dek/"+id.String(), bytes.NewReader(body))
r.ServeHTTP(w, req)
if w.Code != http.StatusCreated {
t.Fatalf("expected 201, got %d", w.Code)
}
}
func TestInternalPutDEK_ConflictReturns409(t *testing.T) {
store := &mockEKStore{
put: func(_ context.Context, _ uuid.UUID, _ string) error {
return encryptedkeys.ErrAlreadyExists
},
}
r := newDEKRouter(t, store)
body, _ := json.Marshal(dekPayload{EncryptedDataKey: "blob"})
w := httptest.NewRecorder()
req := httptest.NewRequest("PUT", "/dek/"+uuid.New().String(), bytes.NewReader(body))
r.ServeHTTP(w, req)
if w.Code != http.StatusConflict {
t.Fatalf("expected 409, got %d", w.Code)
}
}
func TestInternalPutDEK_RejectsEmptyBody(t *testing.T) {
r := newDEKRouter(t, &mockEKStore{})
w := httptest.NewRecorder()
req := httptest.NewRequest("PUT", "/dek/"+uuid.New().String(), strings.NewReader(`{}`))
r.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("expected 400 for empty key, got %d", w.Code)
}
}
func TestInternalDeleteDEK_NoContent(t *testing.T) {
store := &mockEKStore{
delete: func(_ context.Context, _ uuid.UUID) error { return nil },
}
r := newDEKRouter(t, store)
w := httptest.NewRecorder()
req := httptest.NewRequest("DELETE", "/dek/"+uuid.New().String(), nil)
r.ServeHTTP(w, req)
if w.Code != http.StatusNoContent {
t.Fatalf("expected 204, got %d", w.Code)
}
}