S3-вложения и сверка Postgresql с MinIO

This commit is contained in:
2026-11-13 19:15:04 +03:00
parent ec540a1a72
commit 79e94f9d2a
60 changed files with 6702 additions and 50 deletions
+277
View File
@@ -0,0 +1,277 @@
package api
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"errors"
"io"
"net/http"
"strconv"
"strings"
"unicode/utf8"
"messenger/internal/blob"
"messenger/internal/imagecheck"
"messenger/internal/store"
)
type attachmentJSON struct {
ID string `json:"id"`
ChatID string `json:"chat_id"`
OwnerID string `json:"owner_id"`
Filename string `json:"filename"`
ContentType string `json:"content_type"`
SizeBytes int64 `json:"size_bytes"`
SHA256 string `json:"sha256"`
Status string `json:"status"`
Variants []struct{} `json:"variants"`
ErrorCode *string `json:"error_code"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
func (a *API) routeAttachment(w http.ResponseWriter, r *http.Request, rest string) {
if _, ok := a.authenticate(w, r); !ok {
return
}
rest = strings.Trim(rest, "/")
if rest == "" || strings.Count(rest, "/") > 1 {
a.fail(w, r, http.StatusNotFound, "not_found", "Объект не найден", 0)
return
}
id, content, hasContent := strings.Cut(rest, "/")
if hasContent && content != "content" {
a.fail(w, r, http.StatusNotFound, "not_found", "Объект не найден", 0)
return
}
if _, ok := canonicalUUID(id); !ok {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный идентификатор", 0)
return
}
if r.Method != http.MethodGet {
a.fail(w, r, http.StatusMethodNotAllowed, "invalid_request", "Метод не поддерживается", 0)
return
}
if hasContent {
a.downloadAttachment(w, r, id)
return
}
a.getAttachment(w, r, id)
}
func (a *API) uploadAttachment(w http.ResponseWriter, r *http.Request, rawChatID string) {
user, ok := a.authenticate(w, r)
if !ok {
return
}
if a.blobs == nil {
a.fail(w, r, http.StatusServiceUnavailable, "unavailable", "Сервис временно недоступен", 1)
return
}
chatID, ok := a.chatID(w, r, rawChatID)
if !ok {
return
}
if _, err := a.store.ChatFor(r.Context(), chatID, user.ID); a.writeStoreErr(w, r, err) {
return
}
filename := r.URL.Query().Get("filename")
if !validFilename(filename) {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректное имя файла", 0)
return
}
media := ""
if ct := r.Header.Get("Content-Type"); ct != "" {
media = strings.ToLower(strings.TrimSpace(strings.Split(ct, ";")[0]))
}
if media != "image/png" && media != "image/jpeg" {
a.fail(w, r, http.StatusUnsupportedMediaType, "unsupported_media_type", "Нужен PNG или JPEG", 0)
return
}
// Не отвечаем до чтения: иначе клиент ещё шлёт тело и видит обрыв вместо 413.
// Читаем не больше лимита плюс один байт. Лишний байт - отказ, без записи в bucket.
body, err := io.ReadAll(io.LimitReader(r.Body, a.maxUpload+1))
if err != nil {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Загрузка прервана", 0)
return
}
if int64(len(body)) > a.maxUpload {
a.fail(w, r, http.StatusRequestEntityTooLarge, "payload_too_large", "Файл больше 10 МиБ", 0)
return
}
if len(body) == 0 {
a.fail(w, r, http.StatusUnsupportedMediaType, "unsupported_media_type", "Пустое изображение", 0)
return
}
width, height, err := imagecheck.Inspect(media, body, a.maxPixels)
if err != nil {
if errors.Is(err, imagecheck.ErrPixels) {
a.fail(w, r, http.StatusRequestEntityTooLarge, "payload_too_large", "Слишком много пикселей", 0)
return
}
a.fail(w, r, http.StatusUnsupportedMediaType, "unsupported_media_type", "Файл не является PNG или JPEG", 0)
return
}
sum := sha256.Sum256(body)
key, err := newObjectKey()
if err != nil {
a.fail(w, r, http.StatusInternalServerError, "internal_error", "Внутренняя ошибка", 0)
return
}
if err := a.blobs.Put(r.Context(), key, media, body); err != nil {
a.fail(w, r, http.StatusServiceUnavailable, "unavailable", "Сервис временно недоступен", 1)
return
}
row, err := a.store.InsertAttachment(r.Context(), store.Attachment{
ChatID: chatID,
OwnerID: user.ID,
Filename: filename,
ContentType: media,
SizeBytes: int64(len(body)),
SHA256: hex.EncodeToString(sum[:]),
ObjectKey: key,
Width: width,
Height: height,
})
if err != nil {
// PUT уже прошёл, COMMIT нет: сразу убираем объект. Если удаление не удалось,
// ключ останется сиротой до сверки и не попадёт в ответ 201.
if delErr := a.blobs.Delete(r.Context(), key); delErr != nil {
a.log.Error("orphan object", "request_id", requestIDFrom(r))
}
if a.writeStoreErr(w, r, err) {
return
}
}
writeJSON(w, http.StatusCreated, asAttachment(row))
}
func (a *API) getAttachment(w http.ResponseWriter, r *http.Request, rawID string) {
row, ok := a.visibleAttachment(w, r, rawID)
if !ok {
return
}
writeJSON(w, http.StatusOK, asAttachment(row))
}
func (a *API) downloadAttachment(w http.ResponseWriter, r *http.Request, rawID string) {
row, ok := a.visibleAttachment(w, r, rawID)
if !ok {
return
}
variant := "original"
if r.URL.Query().Has("variant") {
variant = r.URL.Query().Get("variant")
}
switch variant {
case "original":
case "thumbnail", "crop", "optimized":
a.fail(w, r, http.StatusNotFound, "not_found", "Объект не найден", 0)
return
default:
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный вариант", 0)
return
}
if a.blobs == nil {
a.fail(w, r, http.StatusServiceUnavailable, "unavailable", "Сервис временно недоступен", 1)
return
}
rc, err := a.blobs.Get(r.Context(), row.ObjectKey)
if err != nil {
if errors.Is(err, blob.ErrNotFound) {
a.fail(w, r, http.StatusNotFound, "not_found", "Объект не найден", 0)
return
}
a.fail(w, r, http.StatusServiceUnavailable, "unavailable", "Сервис временно недоступен", 1)
return
}
defer rc.Close()
w.Header().Set("Content-Type", row.ContentType)
w.Header().Set("Content-Length", strconv.FormatInt(row.SizeBytes, 10))
w.WriteHeader(http.StatusOK)
if _, err := io.Copy(w, rc); err != nil {
a.log.Error("download", "request_id", requestIDFrom(r))
}
}
func (a *API) visibleAttachment(w http.ResponseWriter, r *http.Request, rawID string) (store.Attachment, bool) {
user, ok := a.authenticate(w, r)
if !ok {
return store.Attachment{}, false
}
id, ok := canonicalUUID(rawID)
if !ok {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный идентификатор", 0)
return store.Attachment{}, false
}
row, err := a.store.AttachmentVisible(r.Context(), id, user.ID)
if a.writeStoreErr(w, r, err) {
return store.Attachment{}, false
}
return row, true
}
func (a *API) attachmentIDList(w http.ResponseWriter, r *http.Request, raw []string) ([]string, bool) {
if len(raw) > 10 {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Слишком много вложений", 0)
return nil, false
}
seen := make(map[string]struct{}, len(raw))
out := make([]string, 0, len(raw))
for _, item := range raw {
id, ok := canonicalUUID(item)
if !ok {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный идентификатор вложения", 0)
return nil, false
}
if _, dup := seen[id]; dup {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Повторяющееся вложение", 0)
return nil, false
}
seen[id] = struct{}{}
out = append(out, id)
}
return out, true
}
func asAttachment(row store.Attachment) attachmentJSON {
return attachmentJSON{
ID: row.ID,
ChatID: row.ChatID,
OwnerID: row.OwnerID,
Filename: row.Filename,
ContentType: row.ContentType,
SizeBytes: row.SizeBytes,
SHA256: row.SHA256,
Status: row.Status,
Variants: []struct{}{},
ErrorCode: nil,
CreatedAt: formatTime(row.CreatedAt),
UpdatedAt: formatTime(row.UpdatedAt),
}
}
func validFilename(name string) bool {
if !utf8.ValidString(name) {
return false
}
n := utf8.RuneCountInString(name)
if n < 1 || n > 255 {
return false
}
for _, r := range name {
if r < 0x20 || r == 0x7f || r == '/' || r == '\\' {
return false
}
}
return true
}
func newObjectKey() (string, error) {
var b [32]byte
if _, err := rand.Read(b[:]); err != nil {
return "", err
}
return "o/" + hex.EncodeToString(b[:]), nil
}
+182
View File
@@ -0,0 +1,182 @@
package api
import (
"crypto/rand"
"encoding/base64"
"encoding/hex"
"errors"
"io"
"net/http"
"strings"
"time"
"unicode/utf8"
"messenger/internal/password"
"messenger/internal/session"
"messenger/internal/store"
)
const (
basicChallenge = `Basic realm="messenger", charset="UTF-8"`
bearerChallenge = `Bearer realm="messenger"`
)
func (a *API) createSession(w http.ResponseWriter, r *http.Request) {
if !emptyBody(r) {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "У входа не должно быть тела", 0)
return
}
username, plain, ok := parseBasic(r)
if !ok || !usernameRe.MatchString(username) || !validPassword(plain) {
a.unauthorized(w, r, basicChallenge)
return
}
user, hash, err := a.store.UserByUsername(r.Context(), username)
if errors.Is(err, store.ErrNotFound) {
password.Verify(plain, a.dummyHash)
a.unauthorized(w, r, basicChallenge)
return
}
if a.writeStoreErr(w, r, err) {
return
}
if !password.Verify(plain, hash) {
a.unauthorized(w, r, basicChallenge)
return
}
token, err := newToken()
if err != nil {
a.fail(w, r, http.StatusInternalServerError, "internal_error", "Внутренняя ошибка", 0)
return
}
expires := time.Now().UTC().Add(a.sessionTTL).Truncate(time.Microsecond)
if err := a.sessions.Create(r.Context(), token, user.ID, expires); a.writeStoreErr(w, r, err) {
return
}
writeJSON(w, http.StatusCreated, map[string]any{
"token": token,
"token_type": "Bearer",
"expires_at": formatTime(expires),
"user": asUser(user),
})
}
func (a *API) revokeSession(w http.ResponseWriter, r *http.Request) {
token, ok := a.bearerToken(w, r)
if !ok {
return
}
if _, _, err := a.sessions.Lookup(r.Context(), token); err != nil {
if errors.Is(err, session.ErrNotFound) {
a.unauthorized(w, r, bearerChallenge)
return
}
a.writeStoreErr(w, r, err)
return
}
if err := a.sessions.Revoke(r.Context(), token); err != nil {
if errors.Is(err, session.ErrNotFound) {
a.unauthorized(w, r, bearerChallenge)
return
}
a.writeStoreErr(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (a *API) authenticate(w http.ResponseWriter, r *http.Request) (store.User, bool) {
token, ok := a.bearerToken(w, r)
if !ok {
return store.User{}, false
}
userID, expires, err := a.sessions.Lookup(r.Context(), token)
if errors.Is(err, session.ErrNotFound) || (err == nil && !time.Now().Before(expires)) {
if err == nil {
_ = a.sessions.Revoke(r.Context(), token)
}
a.unauthorized(w, r, bearerChallenge)
return store.User{}, false
}
if err != nil {
a.writeStoreErr(w, r, err)
return store.User{}, false
}
user, err := a.store.User(r.Context(), userID)
if errors.Is(err, store.ErrNotFound) {
a.unauthorized(w, r, bearerChallenge)
return store.User{}, false
}
if a.writeStoreErr(w, r, err) {
return store.User{}, false
}
return user, true
}
func (a *API) bearerToken(w http.ResponseWriter, r *http.Request) (string, bool) {
scheme, param, ok := splitAuth(r.Header.Get("Authorization"))
if !ok || !strings.EqualFold(scheme, "Bearer") || param == "" || len(param) > 512 || strings.Contains(param, " ") {
a.unauthorized(w, r, bearerChallenge)
return "", false
}
return param, true
}
func (a *API) unauthorized(w http.ResponseWriter, r *http.Request, challenge string) {
w.Header().Set("WWW-Authenticate", challenge)
a.fail(w, r, http.StatusUnauthorized, "unauthorized", "Нет действительной идентификации", 0)
}
func parseBasic(r *http.Request) (string, string, bool) {
scheme, param, ok := splitAuth(r.Header.Get("Authorization"))
if !ok || !strings.EqualFold(scheme, "Basic") || param == "" {
return "", "", false
}
raw, err := base64.StdEncoding.DecodeString(param)
if err != nil {
raw, err = base64.RawStdEncoding.DecodeString(param)
if err != nil {
return "", "", false
}
}
if !utf8.Valid(raw) {
return "", "", false
}
username, plain, ok := strings.Cut(string(raw), ":")
if !ok || username == "" {
return "", "", false
}
return username, plain, true
}
func splitAuth(header string) (string, string, bool) {
scheme, param, ok := strings.Cut(strings.TrimSpace(header), " ")
if !ok {
return "", "", false
}
param = strings.TrimSpace(param)
if scheme == "" || param == "" {
return "", "", false
}
return scheme, param, true
}
func emptyBody(r *http.Request) bool {
if r.Body == nil || r.ContentLength == 0 {
return true
}
buf := make([]byte, 1)
n, err := r.Body.Read(buf)
if n > 0 {
return false
}
return err == io.EOF || err == nil
}
func newToken() (string, error) {
var b [32]byte
if _, err := rand.Read(b[:]); err != nil {
return "", err
}
return hex.EncodeToString(b[:]), nil
}
+365
View File
@@ -0,0 +1,365 @@
package api
import (
"context"
"errors"
"net/http"
"strings"
"time"
"messenger/internal/session"
"messenger/internal/store"
)
func (a *API) live(w http.ResponseWriter, _ *http.Request) {
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
}
func (a *API) ready(w http.ResponseWriter, r *http.Request) {
if a.shutting.Load() {
a.fail(w, r, http.StatusServiceUnavailable, "unavailable", "Сервер останавливается", 1)
return
}
ctx, cancel := context.WithTimeout(r.Context(), 800*time.Millisecond)
defer cancel()
if err := a.store.Ping(ctx); err != nil || a.sessions.Ping(ctx) != nil {
a.fail(w, r, http.StatusServiceUnavailable, "unavailable", "Сервис временно недоступен", 1)
return
}
if a.blobs != nil {
if err := a.blobs.Ping(ctx); err != nil {
a.fail(w, r, http.StatusServiceUnavailable, "unavailable", "Сервис временно недоступен", 1)
return
}
}
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
}
func (a *API) route(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
switch {
case path == "/api/v1/users" && r.Method == http.MethodPost:
a.createUser(w, r)
case path == "/api/v1/users/me" && r.Method == http.MethodGet:
a.currentUser(w, r)
case path == "/api/v1/auth/sessions" && r.Method == http.MethodPost:
a.createSession(w, r)
case path == "/api/v1/auth/sessions/current" && r.Method == http.MethodDelete:
a.revokeSession(w, r)
case path == "/api/v1/chats" && r.Method == http.MethodPost:
a.createChat(w, r)
case path == "/api/v1/chats" && r.Method == http.MethodGet:
a.listChats(w, r)
default:
if strings.HasPrefix(path, "/api/v1/attachments/") {
a.routeAttachment(w, r, strings.TrimPrefix(path, "/api/v1/attachments/"))
return
}
chatID, kind, ok := splitChatPath(path)
if !ok {
if knownWrongMethod(path) {
a.fail(w, r, http.StatusMethodNotAllowed, "invalid_request", "Метод не поддерживается", 0)
return
}
a.fail(w, r, http.StatusNotFound, "not_found", "Объект не найден", 0)
return
}
switch kind {
case "":
if r.Method == http.MethodGet {
a.getChat(w, r, chatID)
return
}
case "messages":
switch r.Method {
case http.MethodPost:
a.createMessage(w, r, chatID)
return
case http.MethodGet:
a.listMessages(w, r, chatID)
return
}
case "attachments":
if r.Method == http.MethodPost {
a.uploadAttachment(w, r, chatID)
return
}
}
a.fail(w, r, http.StatusMethodNotAllowed, "invalid_request", "Метод не поддерживается", 0)
}
}
func knownWrongMethod(path string) bool {
switch path {
case "/api/v1/users", "/api/v1/users/me", "/api/v1/chats",
"/api/v1/auth/sessions", "/api/v1/auth/sessions/current":
return true
default:
return false
}
}
func (a *API) createUser(w http.ResponseWriter, r *http.Request) {
var body struct {
Username string `json:"username"`
DisplayName string `json:"display_name"`
Password string `json:"password"`
}
if !a.readJSON(w, r, &body) {
return
}
if !usernameRe.MatchString(body.Username) || !validText(body.DisplayName, 1, 100) || !validPassword(body.Password) {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректные поля пользователя", 0)
return
}
hash, err := a.hasher.Hash(body.Password)
if err != nil {
a.fail(w, r, http.StatusInternalServerError, "internal_error", "Внутренняя ошибка", 0)
return
}
user, err := a.store.CreateUser(r.Context(), body.Username, body.DisplayName, hash)
if a.writeStoreErr(w, r, err) {
return
}
writeJSON(w, http.StatusCreated, asUser(user))
}
func (a *API) currentUser(w http.ResponseWriter, r *http.Request) {
user, ok := a.authenticate(w, r)
if !ok {
return
}
writeJSON(w, http.StatusOK, asUser(user))
}
func (a *API) createChat(w http.ResponseWriter, r *http.Request) {
caller, ok := a.authenticate(w, r)
if !ok {
return
}
var body struct {
Title string `json:"title"`
MemberIDs []string `json:"member_ids"`
}
if !a.readJSON(w, r, &body) {
return
}
if !validText(body.Title, 1, 100) || len(body.MemberIDs) < 1 || len(body.MemberIDs) > 100 {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректные поля чата", 0)
return
}
ids := make([]string, len(body.MemberIDs))
seen := make(map[string]struct{}, len(body.MemberIDs))
callerIn := false
for i, raw := range body.MemberIDs {
id, ok := canonicalUUID(raw)
if !ok {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный список участников", 0)
return
}
if _, dup := seen[id]; dup {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный список участников", 0)
return
}
seen[id] = struct{}{}
_, err := a.store.User(r.Context(), id)
if errors.Is(err, store.ErrNotFound) {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный список участников", 0)
return
}
if a.writeStoreErr(w, r, err) {
return
}
if id == caller.ID {
callerIn = true
}
ids[i] = id
}
if !callerIn {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный список участников", 0)
return
}
chat, err := a.store.CreateChat(r.Context(), body.Title, ids, caller.ID)
if errors.Is(err, store.ErrInvalid) {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный список участников", 0)
return
}
if a.writeStoreErr(w, r, err) {
return
}
writeJSON(w, http.StatusCreated, asChat(chat))
}
func (a *API) listChats(w http.ResponseWriter, r *http.Request) {
user, ok := a.authenticate(w, r)
if !ok {
return
}
limit, cur, ok := a.pageQuery(w, r, "chats:"+user.ID)
if !ok {
return
}
items, hasMore, err := a.store.ListChats(r.Context(), user.ID, cur, limit)
if a.writeStoreErr(w, r, err) {
return
}
out := make([]chatJSON, 0, len(items))
for _, c := range items {
out = append(out, asChat(c))
}
var next *string
if hasMore && len(items) > 0 {
last := items[len(items)-1]
encoded := encodeCursor(cursorPayload{Scope: "chats:" + user.ID, ID: last.ID, At: formatTime(last.CreatedAt)})
next = &encoded
}
writeJSON(w, http.StatusOK, chatPage{Items: out, NextCursor: next})
}
func (a *API) getChat(w http.ResponseWriter, r *http.Request, rawID string) {
user, ok := a.authenticate(w, r)
if !ok {
return
}
id, ok := a.chatID(w, r, rawID)
if !ok {
return
}
chat, err := a.store.ChatFor(r.Context(), id, user.ID)
if a.writeStoreErr(w, r, err) {
return
}
writeJSON(w, http.StatusOK, asChat(chat))
}
func (a *API) createMessage(w http.ResponseWriter, r *http.Request, rawID string) {
user, ok := a.authenticate(w, r)
if !ok {
return
}
id, ok := a.chatID(w, r, rawID)
if !ok {
return
}
if _, err := a.store.ChatFor(r.Context(), id, user.ID); a.writeStoreErr(w, r, err) {
return
}
idemKey := r.Header.Get("Idempotency-Key")
if !idemKeyRe.MatchString(idemKey) {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Нужен заголовок Idempotency-Key", 0)
return
}
var body struct {
Text string `json:"text"`
AttachmentIDs []string `json:"attachment_ids"`
}
if !a.readJSON(w, r, &body) {
return
}
ids, ok := a.attachmentIDList(w, r, body.AttachmentIDs)
if !ok {
return
}
if !validMessageText(body.Text, len(ids)) {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный текст сообщения", 0)
return
}
// Отпечаток — разобранные поля, не сырые байты: порядок ключей JSON и отсутствующий attachment_ids равны пустому списку.
msg, err := a.store.AddMessage(r.Context(), id, user.ID, body.Text, ids, idemKey, messageFingerprint(body.Text, ids))
if a.writeStoreErr(w, r, err) {
return
}
writeJSON(w, http.StatusCreated, asMessage(msg))
}
func (a *API) listMessages(w http.ResponseWriter, r *http.Request, rawID string) {
user, ok := a.authenticate(w, r)
if !ok {
return
}
id, ok := a.chatID(w, r, rawID)
if !ok {
return
}
if _, err := a.store.ChatFor(r.Context(), id, user.ID); a.writeStoreErr(w, r, err) {
return
}
limit, cur, ok := a.pageQuery(w, r, "msg:"+id)
if !ok {
return
}
items, hasMore, err := a.store.ListMessages(r.Context(), id, user.ID, cur, limit)
if a.writeStoreErr(w, r, err) {
return
}
out := make([]messageJSON, 0, len(items))
for _, m := range items {
out = append(out, asMessage(m))
}
var next *string
if hasMore && len(items) > 0 {
last := items[len(items)-1]
encoded := encodeCursor(cursorPayload{Scope: "msg:" + id, ID: last.ID, At: formatTime(last.CreatedAt)})
next = &encoded
}
writeJSON(w, http.StatusOK, messagePage{Items: out, NextCursor: next})
}
func (a *API) chatID(w http.ResponseWriter, r *http.Request, raw string) (string, bool) {
id, ok := canonicalUUID(raw)
if !ok {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный идентификатор", 0)
return "", false
}
return id, true
}
func (a *API) writeStoreErr(w http.ResponseWriter, r *http.Request, err error) bool {
if err == nil {
return false
}
switch {
case errors.Is(err, store.ErrUnavailable), errors.Is(err, session.ErrUnavailable):
a.fail(w, r, http.StatusServiceUnavailable, "unavailable", "Сервис временно недоступен", 1)
case errors.Is(err, store.ErrIdempotencyMismatch):
a.fail(w, r, http.StatusConflict, "conflict", "Этот ключ уже использован для другого сообщения", 0)
case errors.Is(err, store.ErrConflict):
a.fail(w, r, http.StatusConflict, "conflict", "Имя пользователя уже занято", 0)
case errors.Is(err, store.ErrNotFound):
a.fail(w, r, http.StatusNotFound, "not_found", "Объект не найден", 0)
case errors.Is(err, store.ErrNotReady):
a.fail(w, r, http.StatusConflict, "conflict", "Вложение нельзя прикрепить", 0)
case errors.Is(err, store.ErrInvalid):
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректные данные", 0)
case errors.Is(err, store.ErrBadCursor):
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный cursor", 0)
default:
a.log.Error("store", "request_id", requestIDFrom(r))
a.fail(w, r, http.StatusInternalServerError, "internal_error", "Внутренняя ошибка", 0)
}
return true
}
func splitChatPath(path string) (id, kind string, ok bool) {
const prefix = "/api/v1/chats/"
if !strings.HasPrefix(path, prefix) {
return "", "", false
}
rest := strings.TrimPrefix(path, prefix)
if rest == "" {
return "", "", false
}
for _, suffix := range []string{"/messages", "/attachments"} {
if strings.HasSuffix(rest, suffix) {
id = strings.TrimSuffix(rest, suffix)
if id == "" || strings.Contains(id, "/") {
return "", "", false
}
return id, strings.TrimPrefix(suffix, "/"), true
}
}
if strings.Contains(rest, "/") {
return "", "", false
}
return rest, "", true
}
+338
View File
@@ -0,0 +1,338 @@
package api
import (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"encoding/json"
"io"
"net/http"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"unicode/utf8"
"messenger/internal/store"
)
var usernameRe = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
var uuidRe = regexp.MustCompile(`(?i)^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`)
var limitRe = regexp.MustCompile(`^[0-9]{1,3}$`)
var requestIDRe = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,64}$`)
var idemKeyRe = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
type errorEnvelope struct {
Error errorObject `json:"error"`
}
type errorObject struct {
Code string `json:"code"`
Message string `json:"message"`
RequestID string `json:"request_id"`
}
type userJSON struct {
ID string `json:"id"`
Username string `json:"username"`
DisplayName string `json:"display_name"`
CreatedAt string `json:"created_at"`
}
type chatJSON struct {
ID string `json:"id"`
Title string `json:"title"`
MemberIDs []string `json:"member_ids"`
CreatedAt string `json:"created_at"`
}
type messageJSON struct {
ID string `json:"id"`
ChatID string `json:"chat_id"`
SenderID string `json:"sender_id"`
Text string `json:"text"`
CreatedAt string `json:"created_at"`
AttachmentIDs []string `json:"attachment_ids"`
}
type chatPage struct {
Items []chatJSON `json:"items"`
NextCursor *string `json:"next_cursor"`
}
type messagePage struct {
Items []messageJSON `json:"items"`
NextCursor *string `json:"next_cursor"`
}
type cursorPayload struct {
Scope string `json:"scope"`
ID string `json:"id"`
At string `json:"at"`
}
type statusWriter struct {
http.ResponseWriter
status int
wrote bool
}
func (s *statusWriter) WriteHeader(code int) {
if s.wrote {
return
}
s.status = code
s.wrote = true
s.ResponseWriter.WriteHeader(code)
}
func (s *statusWriter) Write(b []byte) (int, error) {
if !s.wrote {
s.WriteHeader(http.StatusOK)
}
return s.ResponseWriter.Write(b)
}
func (a *API) fail(w http.ResponseWriter, r *http.Request, status int, code, message string, retryAfter int) {
if retryAfter > 0 {
w.Header().Set("Retry-After", strconv.Itoa(retryAfter))
}
writeJSON(w, status, errorEnvelope{Error: errorObject{
Code: code,
Message: message,
RequestID: requestIDFrom(r),
}})
}
func writeJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(status)
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)
_ = enc.Encode(v)
}
func (a *API) readJSON(w http.ResponseWriter, r *http.Request, dst any) bool {
media := ""
if ct := r.Header.Get("Content-Type"); ct != "" {
media = strings.TrimSpace(strings.Split(ct, ";")[0])
}
if !strings.EqualFold(media, "application/json") {
a.fail(w, r, http.StatusUnsupportedMediaType, "unsupported_media_type", "Нужен Content-Type application/json", 0)
return false
}
body, err := io.ReadAll(r.Body)
if err != nil || !utf8.Valid(body) || !decodeExactJSON(body, dst) {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный JSON", 0)
return false
}
return true
}
// decodeExactJSON принимает один JSON-объект: точные имена полей, без хвоста и без замены битого UTF-8.
func decodeExactJSON(body []byte, dst any) bool {
dec := json.NewDecoder(bytes.NewReader(body))
var fields map[string]json.RawMessage
if err := dec.Decode(&fields); err != nil || fields == nil {
return false
}
var trailing json.RawMessage
if err := dec.Decode(&trailing); err != io.EOF {
return false
}
allowed, ok := jsonFieldNames(dst)
if !ok {
return false
}
for name := range fields {
if _, ok := allowed[name]; !ok {
return false
}
}
return json.Unmarshal(body, dst) == nil
}
func jsonFieldNames(dst any) (map[string]struct{}, bool) {
t := reflect.TypeOf(dst)
if t == nil || t.Kind() != reflect.Pointer {
return nil, false
}
t = t.Elem()
if t.Kind() != reflect.Struct {
return nil, false
}
names := make(map[string]struct{}, t.NumField())
for i := 0; i < t.NumField(); i++ {
name, _, _ := strings.Cut(t.Field(i).Tag.Get("json"), ",")
if name == "" || name == "-" {
return nil, false
}
names[name] = struct{}{}
}
return names, true
}
func validPassword(s string) bool {
if !utf8.ValidString(s) {
return false
}
n := utf8.RuneCountInString(s)
return n >= 12 && n <= 128 && strings.TrimSpace(s) != ""
}
func (a *API) pageQuery(w http.ResponseWriter, r *http.Request, scope string) (int, *store.Cursor, bool) {
q := r.URL.Query()
limit := 50
if q.Has("limit") {
raw := q.Get("limit")
n, err := strconv.Atoi(raw)
if err != nil || !limitRe.MatchString(raw) || n < 1 || n > 100 {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный limit", 0)
return 0, nil, false
}
limit = n
}
if !q.Has("cursor") {
return limit, nil, true
}
raw := q.Get("cursor")
if raw == "" || len(raw) > 512 {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный cursor", 0)
return 0, nil, false
}
payload, err := decodeCursor(raw)
if err != nil || payload.Scope != scope || payload.ID == "" || payload.At == "" {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный cursor", 0)
return 0, nil, false
}
at, err := time.Parse("2006-01-02T15:04:05.000000Z", payload.At)
if err != nil {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный cursor", 0)
return 0, nil, false
}
id, ok := canonicalUUID(payload.ID)
if !ok {
a.fail(w, r, http.StatusBadRequest, "invalid_request", "Некорректный cursor", 0)
return 0, nil, false
}
return limit, &store.Cursor{ID: id, At: at}, true
}
func asUser(u store.User) userJSON {
return userJSON{
ID: u.ID,
Username: u.Username,
DisplayName: u.DisplayName,
CreatedAt: formatTime(u.CreatedAt),
}
}
func asChat(c store.Chat) chatJSON {
ids := c.MemberIDs
if ids == nil {
ids = []string{}
}
return chatJSON{
ID: c.ID,
Title: c.Title,
MemberIDs: ids,
CreatedAt: formatTime(c.CreatedAt),
}
}
func asMessage(m store.Message) messageJSON {
ids := m.AttachmentIDs
if ids == nil {
ids = []string{}
}
return messageJSON{
ID: m.ID,
ChatID: m.ChatID,
SenderID: m.SenderID,
Text: m.Text,
CreatedAt: formatTime(m.CreatedAt),
AttachmentIDs: ids,
}
}
func messageFingerprint(text string, ids []string) string {
if len(ids) == 0 {
return text + "\n"
}
return text + "\n" + strings.Join(ids, ",")
}
func validMessageText(text string, attachments int) bool {
if utf8.RuneCountInString(text) > 4000 {
return false
}
if strings.TrimSpace(text) == "" {
return text == "" && attachments > 0
}
return true
}
func formatTime(t time.Time) string {
return t.UTC().Format("2006-01-02T15:04:05.000000Z")
}
func canonicalUUID(s string) (string, bool) {
if !uuidRe.MatchString(s) {
return "", false
}
return strings.ToLower(s), true
}
func validText(s string, min, max int) bool {
if strings.TrimSpace(s) == "" {
return false
}
n := utf8.RuneCountInString(s)
return n >= min && n <= max
}
func requestID(r *http.Request) string {
incoming := strings.TrimSpace(r.Header.Get("X-Request-Id"))
if requestIDRe.MatchString(incoming) {
return incoming
}
var b [16]byte
if _, err := rand.Read(b[:]); err != nil {
return strconv.FormatInt(time.Now().UnixNano(), 36)
}
return hex.EncodeToString(b[:])
}
func encodeCursor(p cursorPayload) string {
raw, err := json.Marshal(p)
if err != nil {
return ""
}
return base64.RawURLEncoding.EncodeToString(raw)
}
func decodeCursor(s string) (cursorPayload, error) {
b, err := base64.RawURLEncoding.DecodeString(s)
if err != nil {
b, err = base64.URLEncoding.DecodeString(s)
if err != nil {
return cursorPayload{}, err
}
}
dec := json.NewDecoder(bytes.NewReader(b))
dec.DisallowUnknownFields()
var p cursorPayload
if err := dec.Decode(&p); err != nil {
return cursorPayload{}, err
}
var trailing json.RawMessage
if err := dec.Decode(&trailing); err != io.EOF {
if err == nil {
err = io.ErrUnexpectedEOF
}
return cursorPayload{}, err
}
return p, nil
}
+130
View File
@@ -0,0 +1,130 @@
package api
import (
"context"
"log/slog"
"net/http"
"sync/atomic"
"time"
"messenger/internal/blob"
"messenger/internal/password"
"messenger/internal/session"
"messenger/internal/store"
)
type ctxKey struct{}
type API struct {
store store.Repository
sessions session.Store
hasher password.Params
dummyHash string
sessionTTL time.Duration
instanceID string
log *slog.Logger
shutting atomic.Bool
blobs blob.Store
maxUpload int64
maxPixels int
}
// New связывает HTTP с хранилищем и сессиями.
// dummyHash нужен, чтобы несуществующий username тратил столько же KDF, сколько существующий.
func New(st store.Repository, sessions session.Store, hasher password.Params, sessionTTL time.Duration, instanceID string, log *slog.Logger) *API {
if log == nil {
log = slog.Default()
}
if sessionTTL <= 0 {
sessionTTL = time.Hour
}
if instanceID == "" {
instanceID = "api"
}
dummy, err := hasher.Hash("dummy-password-not-a-login")
if err != nil {
// Параметры KDF проверяются на старте. Пустая строка заставит Verify сразу вернуть false.
dummy = ""
}
return &API{
store: st,
sessions: sessions,
hasher: hasher,
dummyHash: dummy,
sessionTTL: sessionTTL,
instanceID: instanceID,
log: log,
}
}
// EnableAttachments подключает приватный bucket и жёсткие лимиты загрузки.
// maxUpload не может превысить 10 МиБ контракта.
func (a *API) EnableAttachments(objects blob.Store, maxUpload int64, maxPixels int) {
const contractMax = 10 * 1024 * 1024
if maxUpload <= 0 || maxUpload > contractMax {
maxUpload = contractMax
}
if maxPixels <= 0 || maxPixels > 40_000_000 {
maxPixels = 40_000_000
}
a.blobs = objects
a.maxUpload = maxUpload
a.maxPixels = maxPixels
}
// BeginShutdown переводит /health/ready в 503 и говорит новым бизнес-запросам
// отказать до закрытия слушателя. Уже принятые обработчики дорабатывают.
func (a *API) BeginShutdown() {
a.shutting.Store(true)
a.log.Info("shutdown started")
}
// Точка входа
func (a *API) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sw := &statusWriter{ResponseWriter: w, status: http.StatusOK}
start := time.Now()
id := requestID(r)
sw.Header().Set("X-Request-Id", id)
sw.Header().Set("X-Instance-Id", a.instanceID)
r = r.WithContext(context.WithValue(r.Context(), ctxKey{}, id))
defer func() {
if p := recover(); p != nil {
a.log.Error("panic", "request_id", id)
if !sw.wrote {
a.fail(sw, r, http.StatusInternalServerError, "internal_error", "Внутренняя ошибка", 0)
}
}
a.log.Info("request",
"request_id", id,
"method", r.Method,
"path", r.URL.Path,
"status", sw.status,
"duration_ms", time.Since(start).Milliseconds(),
"bytes_in", r.ContentLength,
)
}()
switch {
case r.Method == http.MethodGet && r.URL.Path == "/health/live":
a.live(sw, r)
return
case r.Method == http.MethodGet && r.URL.Path == "/health/ready":
a.ready(sw, r)
return
}
if a.shutting.Load() {
a.fail(sw, r, http.StatusServiceUnavailable, "unavailable", "Сервер останавливается", 1)
return
}
a.route(sw, r)
}
func requestIDFrom(r *http.Request) string {
id, _ := r.Context().Value(ctxKey{}).(string)
if id == "" {
return "unknown"
}
return id
}
+842
View File
@@ -0,0 +1,842 @@
package api
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"image"
"image/png"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"messenger/internal/blob"
"messenger/internal/password"
"messenger/internal/session"
"messenger/internal/store"
)
const testPassword = "correct-horse-1"
type labServer struct {
URL string
tokens map[string]string
api *API
store store.Repository
sess session.Store
}
func newServer(t *testing.T) *labServer {
return newServerTTL(t, time.Hour)
}
func newServerTTL(t *testing.T, ttl time.Duration) *labServer {
t.Helper()
st := store.New()
sess := session.NewMemory()
application := New(st, sess, password.Params{Time: 1, Memory: 8, Threads: 1}, ttl, "test-instance", slog.New(slog.NewJSONHandler(io.Discard, nil)))
srv := httptest.NewServer(application)
t.Cleanup(srv.Close)
return &labServer{URL: srv.URL, tokens: map[string]string{}, api: application, store: st, sess: sess}
}
func (e *labServer) bearer(user string) string {
if user == "" {
return ""
}
if tok, ok := e.tokens[user]; ok {
return tok
}
return "unknown-session-token"
}
var keySeq atomic.Uint64
func nextIdemKey() string {
return "k" + strconv.FormatUint(keySeq.Add(1), 36)
}
func do(t *testing.T, env *labServer, method, url, user string, body any, raw []byte, contentType string, extra ...map[string]string) (int, http.Header, []byte) {
t.Helper()
var rdr io.Reader
if raw != nil {
rdr = bytes.NewReader(raw)
} else if body != nil {
b, err := json.Marshal(body)
if err != nil {
t.Fatal(err)
}
rdr = bytes.NewReader(b)
}
req, err := http.NewRequest(method, url, rdr)
if err != nil {
t.Fatal(err)
}
if body != nil || raw != nil {
if contentType == "" {
contentType = "application/json"
}
req.Header.Set("Content-Type", contentType)
}
if tok := env.bearer(user); tok != "" {
req.Header.Set("Authorization", "Bearer "+tok)
}
if len(extra) > 0 {
for k, v := range extra[0] {
req.Header.Set(k, v)
}
}
if method == http.MethodPost && strings.Contains(url, "/messages") && req.Header.Get("Idempotency-Key") == "" {
req.Header.Set("Idempotency-Key", nextIdemKey())
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if resp.Header.Get("X-Request-Id") == "" {
t.Fatalf("%s %s: missing X-Request-Id", method, url)
}
if resp.Header.Get("X-Instance-Id") != "test-instance" {
t.Fatalf("%s %s: X-Instance-Id = %q", method, url, resp.Header.Get("X-Instance-Id"))
}
if resp.StatusCode >= 400 {
var env errorEnvelope
if err := json.Unmarshal(data, &env); err != nil {
t.Fatalf("error json: %v body %s", err, data)
}
if env.Error.RequestID != resp.Header.Get("X-Request-Id") || env.Error.Code == "" || env.Error.Message == "" {
t.Fatalf("bad error envelope: %+v", env)
}
}
return resp.StatusCode, resp.Header, data
}
func createUser(t *testing.T, env *labServer, username, display string) userJSON {
t.Helper()
status, _, body := do(t, env, http.MethodPost, env.URL+"/api/v1/users", "", map[string]string{
"username": username, "display_name": display, "password": testPassword,
}, nil, "")
if status != http.StatusCreated {
t.Fatalf("create user %s: %d %s", username, status, body)
}
if bytes.Contains(body, []byte("password")) || bytes.Contains(body, []byte("argon2")) {
t.Fatalf("user response leaked a secret: %s", body)
}
var u userJSON
if err := json.Unmarshal(body, &u); err != nil {
t.Fatal(err)
}
req, err := http.NewRequest(http.MethodPost, env.URL+"/api/v1/auth/sessions", nil)
if err != nil {
t.Fatal(err)
}
req.SetBasicAuth(username, testPassword)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
raw, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusCreated {
t.Fatalf("login %s: %d %s", username, resp.StatusCode, raw)
}
var sess struct {
Token string `json:"token"`
}
if err := json.Unmarshal(raw, &sess); err != nil || len(sess.Token) < 32 {
t.Fatalf("session: %s", raw)
}
env.tokens[u.ID] = sess.Token
return u
}
func createChat(t *testing.T, env *labServer, caller, title string, members ...string) chatJSON {
t.Helper()
status, _, body := do(t, env, http.MethodPost, env.URL+"/api/v1/chats", caller, map[string]any{
"title": title, "member_ids": members,
}, nil, "")
if status != http.StatusCreated {
t.Fatalf("create chat: %d %s", status, body)
}
var c chatJSON
if err := json.Unmarshal(body, &c); err != nil {
t.Fatal(err)
}
return c
}
func TestInvalidJSONExtraFieldAndTypes(t *testing.T) {
env := newServer(t)
cases := [][]byte{
[]byte(`{`),
[]byte(`{"username":"abc","display_name":"Алиса","extra":1}`),
[]byte(`{"username":1,"display_name":"Алиса"}`),
[]byte(`null`),
}
for _, raw := range cases {
status, _, body := do(t, env, http.MethodPost, env.URL+"/api/v1/users", "", nil, raw, "application/json")
if status != http.StatusBadRequest || !bytes.Contains(body, []byte(`"invalid_request"`)) {
t.Fatalf("body %s: %d %s", raw, status, body)
}
}
status, _, _ := do(t, env, http.MethodPost, env.URL+"/api/v1/users", "", nil, []byte(`{"username":"abc","display_name":"Алиса"}`), "text/plain")
if status != http.StatusUnsupportedMediaType {
t.Fatalf("content type: %d", status)
}
}
func TestStrictJSONNamesUTF8AndCursorTail(t *testing.T) {
env := newServer(t)
bad := [][]byte{
[]byte(`{"username":"bad_json","display_name":"Я"}]`),
[]byte(`{"username":"bad_json","display_name":"Я"}}`),
[]byte(`{"Username":"bad_case","display_name":"Я"}`),
append([]byte(`{"username":"bad_utf","display_name":"`), 0xff, '"', '}'),
}
for _, raw := range bad {
status, _, body := do(t, env, http.MethodPost, env.URL+"/api/v1/users", "", nil, raw, "application/json")
if status != http.StatusBadRequest {
t.Fatalf("user body %q: %d %s", raw, status, body)
}
}
alice := createUser(t, env, "alice", "Алиса")
status, _, body := do(t, env, http.MethodPost, env.URL+"/api/v1/chats", alice.ID, nil, []byte(`{"Title":"чат","member_ids":["`+alice.ID+`"]}`), "application/json")
if status != http.StatusBadRequest {
t.Fatalf("Title: %d %s", status, body)
}
chat := createChat(t, env, alice.ID, "чат", alice.ID)
status, _, body = do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/messages", alice.ID, nil, []byte(`{"TEXT":"hi"}`), "application/json")
if status != http.StatusBadRequest {
t.Fatalf("TEXT: %d %s", status, body)
}
rawText := append([]byte(`{"text":"`), 0xff, '"', '}')
status, _, body = do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/messages", alice.ID, nil, rawText, "application/json")
if status != http.StatusBadRequest {
t.Fatalf("message utf-8: %d %s", status, body)
}
status, _, body = do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/messages", alice.ID, map[string]string{"text": "one"}, nil, "")
if status != http.StatusCreated {
t.Fatalf("seed: %d %s", status, body)
}
status, _, body = do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/messages", alice.ID, map[string]string{"text": "two"}, nil, "")
if status != http.StatusCreated {
t.Fatalf("seed2: %d %s", status, body)
}
status, _, body = do(t, env, http.MethodGet, env.URL+"/api/v1/chats/"+chat.ID+"/messages?limit=1", alice.ID, nil, nil, "")
if status != http.StatusOK {
t.Fatalf("page: %d %s", status, body)
}
var page messagePage
if err := json.Unmarshal(body, &page); err != nil || page.NextCursor == nil {
t.Fatalf("cursor: %v %s", err, body)
}
decoded, err := base64.RawURLEncoding.DecodeString(*page.NextCursor)
if err != nil {
t.Fatal(err)
}
tampered := base64.RawURLEncoding.EncodeToString(append(decoded, []byte("garbage")...))
status, _, body = do(t, env, http.MethodGet, env.URL+"/api/v1/chats/"+chat.ID+"/messages?limit=1&cursor="+url.QueryEscape(tampered), alice.ID, nil, nil, "")
if status != http.StatusBadRequest {
t.Fatalf("tampered cursor: %d %s", status, body)
}
status, _, _ = do(t, env, http.MethodGet, env.URL+"/api/v1/chats/"+chat.ID+"/messages?limit=1&cursor="+url.QueryEscape(*page.NextCursor), alice.ID, nil, nil, "")
if status != http.StatusOK {
t.Fatalf("valid cursor: %d", status)
}
}
func TestDuplicateUsername(t *testing.T) {
env := newServer(t)
createUser(t, env, "alice", "Алиса")
status, _, body := do(t, env, http.MethodPost, env.URL+"/api/v1/users", "", map[string]string{
"username": "alice", "display_name": "Другая", "password": testPassword,
}, nil, "")
if status != http.StatusConflict || !bytes.Contains(body, []byte(`"conflict"`)) {
t.Fatalf("duplicate: %d %s", status, body)
}
}
func TestEmptyMessageForeignChatAndAuth(t *testing.T) {
env := newServer(t)
alice := createUser(t, env, "alice", "Алиса")
bob := createUser(t, env, "bob", "Боб")
eve := createUser(t, env, "eve", "Ева")
chat := createChat(t, env, alice.ID, "Контрактный тест", alice.ID, bob.ID)
status, _, _ := do(t, env, http.MethodGet, env.URL+"/api/v1/users/me", "", nil, nil, "")
if status != http.StatusUnauthorized {
t.Fatalf("missing header: %d", status)
}
status, _, _ = do(t, env, http.MethodGet, env.URL+"/api/v1/users/me", "11111111-1111-4111-8111-111111111111", nil, nil, "")
if status != http.StatusUnauthorized {
t.Fatalf("unknown user: %d", status)
}
meStatus, _, meBody := do(t, env, http.MethodGet, env.URL+"/api/v1/users/me", alice.ID, nil, nil, "")
if meStatus != http.StatusOK || !bytes.Contains(meBody, []byte(alice.ID)) {
t.Fatalf("me: %d %s", meStatus, meBody)
}
status, _, _ = do(t, env, http.MethodGet, env.URL+"/api/v1/chats/"+chat.ID, eve.ID, nil, nil, "")
if status != http.StatusNotFound {
t.Fatalf("foreign chat: %d", status)
}
status, _, _ = do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/messages", eve.ID, map[string]string{"text": "чужое"}, nil, "")
if status != http.StatusNotFound {
t.Fatalf("foreign post: %d", status)
}
for _, text := range []string{"", " ", "\n\t"} {
status, _, body := do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/messages", alice.ID, map[string]string{"text": text}, nil, "")
if status != http.StatusBadRequest {
t.Fatalf("empty %q: %d %s", text, status, body)
}
}
status, _, _ = do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/messages", alice.ID, map[string]string{"text": "привет", "sender_id": eve.ID}, nil, "")
if status != http.StatusBadRequest {
t.Fatalf("sender_id must be rejected: %d", status)
}
text := "Привет, мир 👋"
status, _, body := do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/messages", alice.ID, map[string]string{"text": text}, nil, "")
if status != http.StatusCreated {
t.Fatalf("unicode: %d %s", status, body)
}
var msg messageJSON
if err := json.Unmarshal(body, &msg); err != nil {
t.Fatal(err)
}
if msg.Text != text || msg.SenderID != alice.ID || msg.ChatID != chat.ID {
t.Fatalf("message mutated: %+v", msg)
}
if !strings.HasSuffix(msg.CreatedAt, "Z") {
t.Fatalf("timestamp: %s", msg.CreatedAt)
}
}
func TestPaginationSeveralPages(t *testing.T) {
env := newServer(t)
alice := createUser(t, env, "alice", "Алиса")
bob := createUser(t, env, "bob", "Боб")
chat := createChat(t, env, alice.ID, "страницы", alice.ID, bob.ID)
other := createChat(t, env, alice.ID, "другой", alice.ID)
const n = 25
ids := make([]string, 0, n)
for i := 0; i < n; i++ {
sender := alice.ID
if i%2 == 1 {
sender = bob.ID
}
status, _, body := do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/messages", sender, map[string]string{
"text": "сообщение " + strings.Repeat("я", i%3) + string(rune('a'+i%26)),
}, nil, "")
if status != http.StatusCreated {
t.Fatalf("post %d: %d %s", i, status, body)
}
var msg messageJSON
if err := json.Unmarshal(body, &msg); err != nil {
t.Fatal(err)
}
ids = append(ids, msg.ID)
}
var got []string
cursor := ""
pages := 0
for {
pages++
if pages > 10 {
t.Fatal("pagination did not finish")
}
url := env.URL + "/api/v1/chats/" + chat.ID + "/messages?limit=10"
if cursor != "" {
url += "&cursor=" + cursor
}
status, _, body := do(t, env, http.MethodGet, url, bob.ID, nil, nil, "")
if status != http.StatusOK {
t.Fatalf("page: %d %s", status, body)
}
var page messagePage
if err := json.Unmarshal(body, &page); err != nil {
t.Fatal(err)
}
if len(page.Items) == 0 || len(page.Items) > 10 {
t.Fatalf("page size %d", len(page.Items))
}
for _, item := range page.Items {
got = append(got, item.ID)
}
if page.NextCursor == nil {
break
}
cursor = *page.NextCursor
}
if pages < 3 {
t.Fatalf("expected several pages, got %d", pages)
}
if strings.Join(got, ",") != strings.Join(ids, ",") {
t.Fatalf("order/duplication mismatch\nwant %v\ngot %v", ids, got)
}
status, _, _ := do(t, env, http.MethodGet, env.URL+"/api/v1/chats/"+other.ID+"/messages?limit=10&cursor="+cursor, alice.ID, nil, nil, "")
if status != http.StatusBadRequest {
t.Fatalf("foreign cursor: %d", status)
}
status, _, _ = do(t, env, http.MethodGet, env.URL+"/api/v1/chats/"+chat.ID+"/messages?limit=0", alice.ID, nil, nil, "")
if status != http.StatusBadRequest {
t.Fatalf("limit 0: %d", status)
}
}
func TestConcurrentHundredMessages(t *testing.T) {
env := newServer(t)
users := make([]userJSON, 10)
ids := make([]string, 10)
for i := 0; i < 10; i++ {
users[i] = createUser(t, env, "u"+string(rune('a'+i))+"x", "Участник")
ids[i] = users[i].ID
}
chat := createChat(t, env, ids[0], "гонка", ids...)
var wg sync.WaitGroup
errCh := make(chan error, 100)
for i := 0; i < 10; i++ {
wg.Add(1)
go func(user string) {
defer wg.Done()
for j := 0; j < 10; j++ {
req, err := http.NewRequest(http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/messages", strings.NewReader(`{"text":"n"}`))
if err != nil {
errCh <- err
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+env.tokens[user])
req.Header.Set("Idempotency-Key", user+"-"+strconv.Itoa(j))
resp, err := http.DefaultClient.Do(req)
if err != nil {
errCh <- err
return
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
errCh <- &statusErr{resp.StatusCode, body}
}
}
}(ids[i])
}
wg.Wait()
close(errCh)
for err := range errCh {
t.Fatal(err)
}
seen := map[string]struct{}{}
cursor := ""
for {
url := env.URL + "/api/v1/chats/" + chat.ID + "/messages?limit=17"
if cursor != "" {
url += "&cursor=" + cursor
}
status, _, body := do(t, env, http.MethodGet, url, ids[3], nil, nil, "")
if status != http.StatusOK {
t.Fatalf("list: %d %s", status, body)
}
var page messagePage
if err := json.Unmarshal(body, &page); err != nil {
t.Fatal(err)
}
for _, item := range page.Items {
if _, ok := seen[item.ID]; ok {
t.Fatalf("duplicate %s", item.ID)
}
seen[item.ID] = struct{}{}
}
if page.NextCursor == nil {
break
}
cursor = *page.NextCursor
}
if len(seen) != 100 {
t.Fatalf("unique messages = %d, want 100", len(seen))
}
}
func TestSessionWrongPasswordLogoutAndIgnoredUserHeader(t *testing.T) {
env := newServer(t)
alice := createUser(t, env, "alice", "Алиса")
eve := createUser(t, env, "eve", "Ева")
req, err := http.NewRequest(http.MethodPost, env.URL+"/api/v1/auth/sessions", nil)
if err != nil {
t.Fatal(err)
}
req.SetBasicAuth("alice", "wrong-password-1")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusUnauthorized || !strings.HasPrefix(resp.Header.Get("WWW-Authenticate"), "Basic ") {
t.Fatalf("wrong password: %d %s %q", resp.StatusCode, body, resp.Header.Get("WWW-Authenticate"))
}
status, hdr, _ := do(t, env, http.MethodGet, env.URL+"/api/v1/users/me", "", nil, nil, "")
if status != http.StatusUnauthorized || !strings.HasPrefix(hdr.Get("WWW-Authenticate"), "Bearer ") {
t.Fatalf("missing bearer: %d %q", status, hdr.Get("WWW-Authenticate"))
}
bare, err := http.NewRequest(http.MethodGet, env.URL+"/api/v1/users/me", nil)
if err != nil {
t.Fatal(err)
}
bare.Header.Set("X-User-Id", alice.ID)
resp, err = http.DefaultClient.Do(bare)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusUnauthorized {
t.Fatalf("X-User-Id alone: %d", resp.StatusCode)
}
over, err := http.NewRequest(http.MethodGet, env.URL+"/api/v1/users/me", nil)
if err != nil {
t.Fatal(err)
}
over.Header.Set("Authorization", "Bearer "+env.tokens[alice.ID])
over.Header.Set("X-User-Id", eve.ID)
resp, err = http.DefaultClient.Do(over)
if err != nil {
t.Fatal(err)
}
raw, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusOK || !bytes.Contains(raw, []byte(alice.ID)) || bytes.Contains(raw, []byte(eve.ID)) {
t.Fatalf("X-User-Id overrode bearer: %d %s", resp.StatusCode, raw)
}
basic, err := http.NewRequest(http.MethodGet, env.URL+"/api/v1/users/me", nil)
if err != nil {
t.Fatal(err)
}
basic.SetBasicAuth("alice", testPassword)
resp, err = http.DefaultClient.Do(basic)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusUnauthorized || !strings.HasPrefix(resp.Header.Get("WWW-Authenticate"), "Bearer ") {
t.Fatalf("basic on business endpoint: %d", resp.StatusCode)
}
status, _, body = do(t, env, http.MethodDelete, env.URL+"/api/v1/auth/sessions/current", alice.ID, nil, nil, "")
if status != http.StatusNoContent || len(body) != 0 {
t.Fatalf("logout: %d %q", status, body)
}
status, _, _ = do(t, env, http.MethodGet, env.URL+"/api/v1/users/me", alice.ID, nil, nil, "")
if status != http.StatusUnauthorized {
t.Fatalf("revoked token: %d", status)
}
status, _, _ = do(t, env, http.MethodDelete, env.URL+"/api/v1/auth/sessions/current", alice.ID, nil, nil, "")
if status != http.StatusUnauthorized {
t.Fatalf("second logout: %d", status)
}
}
func TestSessionExpires(t *testing.T) {
env := newServerTTL(t, 2*time.Second)
alice := createUser(t, env, "alice", "Алиса")
time.Sleep(2100 * time.Millisecond)
status, _, _ := do(t, env, http.MethodGet, env.URL+"/api/v1/users/me", alice.ID, nil, nil, "")
if status != http.StatusUnauthorized {
t.Fatalf("expired session: %d", status)
}
}
type downSessions struct{ session.Store }
func (downSessions) Lookup(context.Context, string) (string, time.Time, error) {
return "", time.Time{}, session.ErrUnavailable
}
func (downSessions) Ping(context.Context) error { return session.ErrUnavailable }
type downStore struct{ store.Repository }
func (downStore) Ping(context.Context) error { return store.ErrUnavailable }
func (downStore) User(context.Context, string) (store.User, error) {
return store.User{}, store.ErrUnavailable
}
func TestDependencyDownDoesNotTrustAnonymous(t *testing.T) {
env := newServer(t)
alice := createUser(t, env, "alice", "Алиса")
broken := New(downStore{env.store}, downSessions{env.sess}, password.Params{Time: 1, Memory: 8, Threads: 1}, time.Hour, "test-instance", slog.New(slog.NewJSONHandler(io.Discard, nil)))
srv := httptest.NewServer(broken)
defer srv.Close()
live, err := http.Get(srv.URL + "/health/live")
if err != nil {
t.Fatal(err)
}
live.Body.Close()
if live.StatusCode != http.StatusOK {
t.Fatalf("live: %d", live.StatusCode)
}
ready, err := http.Get(srv.URL + "/health/ready")
if err != nil {
t.Fatal(err)
}
readyBody, _ := io.ReadAll(ready.Body)
ready.Body.Close()
if ready.StatusCode != http.StatusServiceUnavailable || !bytes.Contains(readyBody, []byte(`"unavailable"`)) {
t.Fatalf("ready: %d %s", ready.StatusCode, readyBody)
}
anon, err := http.Get(srv.URL + "/api/v1/users/me")
if err != nil {
t.Fatal(err)
}
anon.Body.Close()
if anon.StatusCode != http.StatusUnauthorized {
t.Fatalf("anonymous while down: %d", anon.StatusCode)
}
authed, err := http.NewRequest(http.MethodGet, srv.URL+"/api/v1/users/me", nil)
if err != nil {
t.Fatal(err)
}
authed.Header.Set("Authorization", "Bearer "+env.tokens[alice.ID])
resp, err := http.DefaultClient.Do(authed)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("bearer while session store is down: %d", resp.StatusCode)
}
}
func TestIdempotencyReplayConflictAndRace(t *testing.T) {
env := newServer(t)
alice := createUser(t, env, "alice", "Алиса")
bob := createUser(t, env, "bob", "Боб")
chat := createChat(t, env, alice.ID, "ключ", alice.ID, bob.ID)
path := env.URL + "/api/v1/chats/" + chat.ID + "/messages"
key := "same-key-1"
status, _, body := do(t, env, http.MethodPost, path, alice.ID, map[string]string{"text": "одно"}, nil, "", map[string]string{"Idempotency-Key": key})
if status != http.StatusCreated {
t.Fatalf("first: %d %s", status, body)
}
var first messageJSON
if err := json.Unmarshal(body, &first); err != nil {
t.Fatal(err)
}
status, _, body = do(t, env, http.MethodPost, path, alice.ID, nil, []byte(`{"text":"одно"}`), "application/json", map[string]string{"Idempotency-Key": key})
if status != http.StatusCreated {
t.Fatalf("replay: %d %s", status, body)
}
var replay messageJSON
if err := json.Unmarshal(body, &replay); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(replay, first) {
t.Fatalf("replay changed message: %+v vs %+v", replay, first)
}
status, _, _ = do(t, env, http.MethodPost, path, alice.ID, map[string]string{"text": "другое"}, nil, "", map[string]string{"Idempotency-Key": key})
if status != http.StatusConflict {
t.Fatalf("mismatch: %d", status)
}
req, err := http.NewRequest(http.MethodPost, path, strings.NewReader(`{"text":"без ключа"}`))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+env.tokens[alice.ID])
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("missing key: %d", resp.StatusCode)
}
status, _, body = do(t, env, http.MethodPost, path, bob.ID, map[string]string{"text": "одно"}, nil, "", map[string]string{"Idempotency-Key": key})
if status != http.StatusCreated {
t.Fatalf("other user: %d %s", status, body)
}
var other messageJSON
if err := json.Unmarshal(body, &other); err != nil {
t.Fatal(err)
}
if other.ID == first.ID {
t.Fatal("same key for another user reused the message")
}
raceKey := "race-key"
var wg sync.WaitGroup
ids := make(chan string, 16)
errs := make(chan error, 16)
for i := 0; i < 16; i++ {
wg.Add(1)
go func() {
defer wg.Done()
req, err := http.NewRequest(http.MethodPost, path, strings.NewReader(`{"text":"гонка"}`))
if err != nil {
errs <- err
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+env.tokens[alice.ID])
req.Header.Set("Idempotency-Key", raceKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
errs <- err
return
}
defer resp.Body.Close()
raw, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusCreated {
errs <- &statusErr{resp.StatusCode, raw}
return
}
var msg messageJSON
if err := json.Unmarshal(raw, &msg); err != nil {
errs <- err
return
}
ids <- msg.ID
}()
}
wg.Wait()
close(ids)
close(errs)
for err := range errs {
t.Fatal(err)
}
seen := map[string]struct{}{}
for id := range ids {
seen[id] = struct{}{}
}
if len(seen) != 1 {
t.Fatalf("concurrent same key created %d messages", len(seen))
}
}
func pngBytes(t *testing.T, w, h int) []byte {
t.Helper()
img := image.NewNRGBA(image.Rect(0, 0, w, h))
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
t.Fatal(err)
}
return buf.Bytes()
}
func TestAttachmentRoundTripAndRejections(t *testing.T) {
env := newServer(t)
env.api.EnableAttachments(blob.NewMemory(), 10*1024*1024, 40_000_000)
alice := createUser(t, env, "alice", "Алиса")
bob := createUser(t, env, "bob", "Боб")
eve := createUser(t, env, "eve", "Ева")
chat := createChat(t, env, alice.ID, "файлы", alice.ID, bob.ID)
other := createChat(t, env, alice.ID, "другой", alice.ID)
body := pngBytes(t, 4, 3)
status, _, raw := do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/attachments?filename=a/b.png", alice.ID, nil, body, "image/png")
if status != http.StatusBadRequest {
t.Fatalf("slash filename: %d %s", status, raw)
}
status, _, raw = do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/attachments?filename=note.png", alice.ID, nil, []byte("hello"), "image/png")
if status != http.StatusUnsupportedMediaType {
t.Fatalf("text as png: %d %s", status, raw)
}
jpegHeader := []byte{0xff, 0xd8, 0xff, 0xd9}
status, _, raw = do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/attachments?filename=x.png", alice.ID, nil, jpegHeader, "image/png")
if status != http.StatusUnsupportedMediaType {
t.Fatalf("mime swap: %d %s", status, raw)
}
status, _, raw = do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/attachments?filename=pic.png", alice.ID, nil, body, "image/png")
if status != http.StatusCreated {
t.Fatalf("upload: %d %s", status, raw)
}
var att attachmentJSON
if err := json.Unmarshal(raw, &att); err != nil {
t.Fatal(err)
}
if att.Status != "ready" || att.ErrorCode != nil || att.ChatID != chat.ID || att.OwnerID != alice.ID || len(att.Variants) != 0 {
t.Fatalf("metadata: %+v", att)
}
if bytes.Contains(raw, []byte("object_key")) || bytes.Contains(raw, []byte("\"o/")) {
t.Fatalf("key leaked: %s", raw)
}
status, hdr, got := do(t, env, http.MethodGet, env.URL+"/api/v1/attachments/"+att.ID+"/content", bob.ID, nil, nil, "")
if status != http.StatusOK || hdr.Get("Content-Type") != "image/png" || !bytes.Equal(got, body) {
t.Fatalf("download: %d %s %d", status, hdr.Get("Content-Type"), len(got))
}
status, _, _ = do(t, env, http.MethodGet, env.URL+"/api/v1/attachments/"+att.ID, eve.ID, nil, nil, "")
if status != http.StatusNotFound {
t.Fatalf("outsider meta: %d", status)
}
status, _, _ = do(t, env, http.MethodGet, env.URL+"/api/v1/attachments/"+att.ID+"/content", eve.ID, nil, nil, "")
if status != http.StatusNotFound {
t.Fatalf("outsider bytes: %d", status)
}
status, _, raw = do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+other.ID+"/attachments?filename=other.png", alice.ID, nil, body, "image/png")
if status != http.StatusCreated {
t.Fatalf("other upload: %d %s", status, raw)
}
var foreign attachmentJSON
if err := json.Unmarshal(raw, &foreign); err != nil {
t.Fatal(err)
}
status, _, raw = do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/messages", alice.ID, map[string]any{
"text": "", "attachment_ids": []string{foreign.ID},
}, nil, "")
if status != http.StatusNotFound {
t.Fatalf("foreign attachment: %d %s", status, raw)
}
status, _, raw = do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/messages", alice.ID, map[string]any{
"text": "", "attachment_ids": []string{att.ID},
}, nil, "")
if status != http.StatusCreated {
t.Fatalf("attach: %d %s", status, raw)
}
var msg messageJSON
if err := json.Unmarshal(raw, &msg); err != nil {
t.Fatal(err)
}
if len(msg.AttachmentIDs) != 1 || msg.AttachmentIDs[0] != att.ID || msg.Text != "" {
t.Fatalf("message: %+v", msg)
}
env.api.EnableAttachments(blob.NewMemory(), 8, 40_000_000)
status, _, raw = do(t, env, http.MethodPost, env.URL+"/api/v1/chats/"+chat.ID+"/attachments?filename=big.png", alice.ID, nil, body, "image/png")
if status != http.StatusRequestEntityTooLarge {
t.Fatalf("small cap: %d %s", status, raw)
}
}
type statusErr struct {
code int
body []byte
}
func (e *statusErr) Error() string {
return http.StatusText(e.code) + " " + string(e.body)
}
+32
View File
@@ -0,0 +1,32 @@
// Хранилище байтов вложения. Реализация — приватный S3-совместимый bucket.
// Ключ задаёт вызывающий код; имя файла пользователя сюда не попадает.
package blob
import (
"context"
"errors"
"io"
"time"
)
var (
ErrNotFound = errors.New("blob not found")
ErrUnavailable = errors.New("blob unavailable")
)
// Object — запись листинга. LastModified нужен сверке, чтобы не удалять свежий объект.
type Object struct {
Key string
Size int64
Modified time.Time
}
// Store — операции, которые нужны загрузке, скачиванию и сверке.
type Store interface {
Ping(ctx context.Context) error
Put(ctx context.Context, key, contentType string, body []byte) error
Get(ctx context.Context, key string) (io.ReadCloser, error)
Delete(ctx context.Context, key string) error
// List обходит bucket одним листингом. pages — сколько страниц S3 прочитано.
List(ctx context.Context) (objects []Object, pages int, err error)
}
+86
View File
@@ -0,0 +1,86 @@
package blob
import (
"bytes"
"context"
"io"
"sync"
"time"
)
type memObj struct {
body []byte
modified time.Time
}
// Memory - двойник bucket для тестов без MinIO.
type Memory struct {
mu sync.Mutex
m map[string]memObj
}
func NewMemory() *Memory {
return &Memory{m: map[string]memObj{}}
}
func (m *Memory) Ping(ctx context.Context) error {
if err := ctx.Err(); err != nil {
return ErrUnavailable
}
return nil
}
func (m *Memory) Put(ctx context.Context, key, _ string, body []byte) error {
return m.PutAt(ctx, key, body, time.Now().UTC())
}
// PutAt записывает объект с заданным временем.
func (m *Memory) PutAt(ctx context.Context, key string, body []byte, modified time.Time) error {
if err := ctx.Err(); err != nil {
return ErrUnavailable
}
m.mu.Lock()
defer m.mu.Unlock()
m.m[key] = memObj{body: append([]byte(nil), body...), modified: modified.UTC()}
return nil
}
func (m *Memory) Get(ctx context.Context, key string) (io.ReadCloser, error) {
if err := ctx.Err(); err != nil {
return nil, ErrUnavailable
}
m.mu.Lock()
defer m.mu.Unlock()
obj, ok := m.m[key]
if !ok {
return nil, ErrNotFound
}
return io.NopCloser(bytes.NewReader(obj.body)), nil
}
func (m *Memory) Delete(ctx context.Context, key string) error {
if err := ctx.Err(); err != nil {
return ErrUnavailable
}
m.mu.Lock()
defer m.mu.Unlock()
delete(m.m, key)
return nil
}
func (m *Memory) List(ctx context.Context) ([]Object, int, error) {
if err := ctx.Err(); err != nil {
return nil, 0, ErrUnavailable
}
m.mu.Lock()
defer m.mu.Unlock()
out := make([]Object, 0, len(m.m))
for key, obj := range m.m {
out = append(out, Object{Key: key, Size: int64(len(obj.body)), Modified: obj.modified})
}
pages := 1
if len(out) == 0 {
pages = 1
}
return out, pages, nil
}
+104
View File
@@ -0,0 +1,104 @@
package blob
import (
"bytes"
"context"
"errors"
"io"
"net/url"
"strings"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
// S3 - path-style клиент одного bucket. Учётные данные - отдельный пользователь.
type S3 struct {
client *minio.Client
bucket string
}
func Open(endpoint, region, bucket, accessKey, secretKey string) (*S3, error) {
u, err := url.Parse(endpoint)
if err != nil || u.Host == "" || bucket == "" || accessKey == "" || secretKey == "" {
return nil, errors.New("s3 config")
}
secure := u.Scheme == "https"
client, err := minio.New(u.Host, &minio.Options{
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
Secure: secure,
Region: region,
BucketLookup: minio.BucketLookupPath,
})
if err != nil {
return nil, err
}
return &S3{client: client, bucket: bucket}, nil
}
func (s *S3) Ping(ctx context.Context) error {
ok, err := s.client.BucketExists(ctx, s.bucket)
if err != nil || !ok {
return ErrUnavailable
}
return nil
}
func (s *S3) Put(ctx context.Context, key, contentType string, body []byte) error {
_, err := s.client.PutObject(ctx, s.bucket, key, bytes.NewReader(body), int64(len(body)), minio.PutObjectOptions{
ContentType: contentType,
})
if err != nil {
return ErrUnavailable
}
return nil
}
func (s *S3) Get(ctx context.Context, key string) (io.ReadCloser, error) {
_, err := s.client.StatObject(ctx, s.bucket, key, minio.StatObjectOptions{})
if err != nil {
if isMissing(err) {
return nil, ErrNotFound
}
return nil, ErrUnavailable
}
obj, err := s.client.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{})
if err != nil {
return nil, ErrUnavailable
}
return obj, nil
}
func (s *S3) Delete(ctx context.Context, key string) error {
err := s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{})
if err != nil && !isMissing(err) {
return ErrUnavailable
}
return nil
}
func (s *S3) List(ctx context.Context) ([]Object, int, error) {
ch := s.client.ListObjects(ctx, s.bucket, minio.ListObjectsOptions{Recursive: true})
out := make([]Object, 0)
for obj := range ch {
if obj.Err != nil {
return nil, 0, ErrUnavailable
}
out = append(out, Object{Key: obj.Key, Size: obj.Size, Modified: obj.LastModified.UTC()})
}
pages := 1
if n := len(out); n > 0 {
pages = (n + 999) / 1000
}
return out, pages, nil
}
func isMissing(err error) bool {
resp := minio.ToErrorResponse(err)
switch resp.Code {
case "NoSuchKey", "NoSuchBucket", "NotFound":
return true
default:
return strings.Contains(resp.Code, "NoSuch")
}
}
+118
View File
@@ -0,0 +1,118 @@
// Одноразовый и повторно безопасный bootstrap bucket.
// Root нужен только здесь. Процесс API ходит отдельным ключом.
package bootstrap
import (
"context"
"fmt"
"net/url"
"strings"
"time"
"github.com/minio/madmin-go/v3"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
const policyName = "messenger-app"
// Bucket создаёт приватный bucket, политику и пользователя API, если их ещё нет.
func Bucket(ctx context.Context, endpoint, region, bucket, rootKey, rootSecret, appKey, appSecret string) error {
u, err := url.Parse(endpoint)
if err != nil || u.Host == "" || bucket == "" || rootKey == "" || rootSecret == "" || appKey == "" || appSecret == "" {
return fmt.Errorf("s3 bootstrap config")
}
if region == "" {
region = "us-east-1"
}
secure := u.Scheme == "https"
root, err := minio.New(u.Host, &minio.Options{
Creds: credentials.NewStaticV4(rootKey, rootSecret, ""),
Secure: secure,
Region: region,
BucketLookup: minio.BucketLookupPath,
})
if err != nil {
return err
}
var last error
for attempt := 0; attempt < 30; attempt++ {
exists, err := root.BucketExists(ctx, bucket)
if err == nil {
if !exists {
if err := root.MakeBucket(ctx, bucket, minio.MakeBucketOptions{Region: region}); err != nil {
exists, again := root.BucketExists(ctx, bucket)
if again != nil || !exists {
return err
}
}
}
last = nil
break
}
last = err
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Second):
}
}
if last != nil {
return last
}
adm, err := madmin.New(u.Host, rootKey, rootSecret, secure)
if err != nil {
return err
}
if _, err := adm.InfoCannedPolicy(ctx, policyName); err != nil {
if err := adm.AddCannedPolicy(ctx, policyName, policyJSON(bucket)); err != nil {
return fmt.Errorf("add policy: %w", err)
}
}
if err := adm.AddUser(ctx, appKey, appSecret); err != nil {
return fmt.Errorf("add user: %w", err)
}
if _, err := adm.AttachPolicy(ctx, madmin.PolicyAssociationReq{
Policies: []string{policyName},
User: appKey,
}); err != nil {
info, infoErr := adm.GetUserInfo(ctx, appKey)
if infoErr != nil || !strings.Contains(info.PolicyName, policyName) {
return fmt.Errorf("attach policy: %w", err)
}
}
app, err := minio.New(u.Host, &minio.Options{
Creds: credentials.NewStaticV4(appKey, appSecret, ""),
Secure: secure,
Region: region,
BucketLookup: minio.BucketLookupPath,
})
if err != nil {
return err
}
ok, err := app.BucketExists(ctx, bucket)
if err != nil || !ok {
return fmt.Errorf("app user cannot see bucket")
}
return nil
}
func policyJSON(bucket string) []byte {
return []byte(`{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:GetBucketLocation", "s3:ListBucket"],
"Resource": ["arn:aws:s3:::` + bucket + `"]
},
{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
"Resource": ["arn:aws:s3:::` + bucket + `/*"]
}
]
}`)
}
+169
View File
@@ -0,0 +1,169 @@
package config
import (
"fmt"
"net"
"os"
"strconv"
"time"
)
// Config — параметры процесса. Секреты приходят из окружения, не из образа.
type Config struct {
Host string
Port string
Env string
ShutdownTimeout time.Duration
DatabaseURL string
RedisURL string
SessionTTL time.Duration
DBMaxConns int32
Argon2Time uint32
Argon2Memory uint32
Argon2Threads uint8
InstanceID string
IdempotencyTTL time.Duration
S3Endpoint string
S3Region string
S3Bucket string
S3AccessKey string
S3SecretKey string
MaxUploadBytes int64
MaxImagePixels int
ReconcileGrace time.Duration
}
// Addr — адрес прослушивания. В контейнере это 0.0.0.0:8080,
// иначе published port Docker не попадёт в процесс.
func (c Config) Addr() string {
return net.JoinHostPort(c.Host, c.Port)
}
// Load завершает запуск ошибкой, если значение вне допустимого диапазона.
func Load() (Config, error) {
var c Config
c.Host = getenv("APP_HOST", "0.0.0.0")
c.Port = getenv("APP_PORT", "8080")
c.Env = getenv("APP_ENV", "lab")
if c.Host == "" {
return Config{}, fmt.Errorf("APP_HOST is empty")
}
port, err := strconv.Atoi(c.Port)
if err != nil || port < 1 || port > 65535 {
return Config{}, fmt.Errorf("APP_PORT must be 1..65535")
}
sec, err := nonNeg("SHUTDOWN_TIMEOUT_SECONDS", 8)
if err != nil {
return Config{}, err
}
if sec < 1 || sec > 10 {
return Config{}, fmt.Errorf("SHUTDOWN_TIMEOUT_SECONDS must be 1..10")
}
c.ShutdownTimeout = time.Duration(sec) * time.Second
c.DatabaseURL = getenv("DATABASE_URL", "")
c.RedisURL = getenv("REDIS_URL", "")
if c.DatabaseURL == "" || c.RedisURL == "" {
return Config{}, fmt.Errorf("DATABASE_URL and REDIS_URL are required")
}
ttl, err := nonNeg("SESSION_TTL_SECONDS", 3600)
if err != nil {
return Config{}, err
}
if ttl < 1 || ttl > 7*24*3600 {
return Config{}, fmt.Errorf("SESSION_TTL_SECONDS must be 1..604800")
}
c.SessionTTL = time.Duration(ttl) * time.Second
maxConns, err := nonNeg("DB_MAX_CONNS", 10)
if err != nil {
return Config{}, err
}
if maxConns < 1 || maxConns > 50 {
return Config{}, fmt.Errorf("DB_MAX_CONNS must be 1..50")
}
c.DBMaxConns = int32(maxConns)
argonTime, err := nonNeg("ARGON2_TIME", 2)
if err != nil {
return Config{}, err
}
argonMemory, err := nonNeg("ARGON2_MEMORY_KIB", 19456)
if err != nil {
return Config{}, err
}
argonThreads, err := nonNeg("ARGON2_THREADS", 1)
if err != nil {
return Config{}, err
}
if argonTime < 1 || argonTime > 10 || argonMemory < 8 || argonMemory > 1<<20 || argonThreads < 1 || argonThreads > 8 {
return Config{}, fmt.Errorf("argon2 params are out of range")
}
c.Argon2Time = uint32(argonTime)
c.Argon2Memory = uint32(argonMemory)
c.Argon2Threads = uint8(argonThreads)
c.InstanceID = getenv("INSTANCE_ID", "")
idemTTL, err := nonNeg("IDEMPOTENCY_TTL_SECONDS", 86400)
if err != nil {
return Config{}, err
}
if idemTTL < 86400 || idemTTL > 7*24*3600 {
return Config{}, fmt.Errorf("IDEMPOTENCY_TTL_SECONDS must be 86400..604800")
}
c.IdempotencyTTL = time.Duration(idemTTL) * time.Second
c.S3Endpoint = getenv("S3_ENDPOINT", "")
c.S3Region = getenv("S3_REGION", "us-east-1")
c.S3Bucket = getenv("S3_BUCKET", "")
c.S3AccessKey = getenv("S3_ACCESS_KEY", "")
c.S3SecretKey = getenv("S3_SECRET_KEY", "")
if c.S3Endpoint == "" || c.S3Bucket == "" || c.S3AccessKey == "" || c.S3SecretKey == "" {
return Config{}, fmt.Errorf("S3_ENDPOINT, S3_BUCKET, S3_ACCESS_KEY and S3_SECRET_KEY are required")
}
upload, err := nonNeg("MAX_UPLOAD_BYTES", 10*1024*1024)
if err != nil {
return Config{}, err
}
if upload < 1 || upload > 10*1024*1024 {
return Config{}, fmt.Errorf("MAX_UPLOAD_BYTES must be 1..10485760")
}
c.MaxUploadBytes = int64(upload)
pixels, err := nonNeg("MAX_IMAGE_PIXELS", 40_000_000)
if err != nil {
return Config{}, err
}
if pixels < 1 || pixels > 40_000_000 {
return Config{}, fmt.Errorf("MAX_IMAGE_PIXELS must be 1..40000000")
}
c.MaxImagePixels = pixels
grace, err := nonNeg("RECONCILE_GRACE_SECONDS", 900)
if err != nil {
return Config{}, err
}
if grace > 24*3600 {
return Config{}, fmt.Errorf("RECONCILE_GRACE_SECONDS must be 0..86400")
}
c.ReconcileGrace = time.Duration(grace) * time.Second
return c, nil
}
func getenv(key, def string) string {
if v, ok := os.LookupEnv(key); ok && v != "" {
return v
}
return def
}
func nonNeg(key string, def int) (int, error) {
v, ok := os.LookupEnv(key)
if !ok || v == "" {
return def, nil
}
n, err := strconv.Atoi(v)
if err != nil || n < 0 {
return 0, fmt.Errorf("%s must be a non-negative integer", key)
}
return n, nil
}
+190
View File
@@ -0,0 +1,190 @@
// Проверка PNG/JPEG без распаковки всего растра в память.
// Размеры читаются из заголовка. Полный Decode выполняется только для небольших
// картинок, чтобы поймать битый поток.
package imagecheck
import (
"bytes"
"encoding/binary"
"errors"
"hash/crc32"
"image"
_ "image/jpeg"
_ "image/png"
)
const decodePixelLimit = 1_000_000
var (
ErrFormat = errors.New("image format")
ErrPixels = errors.New("image pixels")
)
var pngSig = []byte{0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'}
// Inspect сверяет заявленный MIME с байтами, проверяет структуру и лимит пикселей.
// declared - ровно image/png или image/jpeg, без параметров.
func Inspect(declared string, data []byte, maxPixels int) (width, height int, err error) {
if maxPixels < 1 {
return 0, 0, ErrFormat
}
detected := Detect(data)
if detected == "" || detected != declared {
return 0, 0, ErrFormat
}
switch detected {
case "image/png":
width, height, err = parsePNG(data)
case "image/jpeg":
width, height, err = parseJPEG(data)
default:
return 0, 0, ErrFormat
}
if err != nil {
return 0, 0, ErrFormat
}
if uint64(width)*uint64(height) > uint64(maxPixels) {
return 0, 0, ErrPixels
}
// Маленький файл декодируем целиком: битый IDAT/скан не должен стать ready.
// Выше порога остаётся проверка заголовка и CRC/маркеров, без буфера width*height*4.
if uint64(width)*uint64(height) <= decodePixelLimit {
if _, _, decErr := image.Decode(bytes.NewReader(data)); decErr != nil {
return 0, 0, ErrFormat
}
}
return width, height, nil
}
// Detect смотрит только на сигнатуру, не на заголовок Content-Type.
func Detect(data []byte) string {
if len(data) >= len(pngSig) && bytes.Equal(data[:len(pngSig)], pngSig) {
return "image/png"
}
if len(data) >= 3 && data[0] == 0xff && data[1] == 0xd8 && data[2] == 0xff {
return "image/jpeg"
}
return ""
}
func parsePNG(data []byte) (int, int, error) {
if len(data) < len(pngSig) || !bytes.Equal(data[:len(pngSig)], pngSig) {
return 0, 0, ErrFormat
}
i := len(pngSig)
first := true
var width, height int
for i < len(data) {
if i+12 > len(data) {
return 0, 0, ErrFormat
}
n64 := uint64(binary.BigEndian.Uint32(data[i : i+4]))
if n64 > uint64(len(data)-i-12) {
return 0, 0, ErrFormat
}
n := int(n64)
chunk := data[i+4 : i+8+n]
got := binary.BigEndian.Uint32(data[i+8+n : i+12+n])
if crc32.ChecksumIEEE(chunk) != got {
return 0, 0, ErrFormat
}
kind := string(data[i+4 : i+8])
if first {
if kind != "IHDR" || n != 13 {
return 0, 0, ErrFormat
}
width = int(binary.BigEndian.Uint32(data[i+8 : i+12]))
height = int(binary.BigEndian.Uint32(data[i+12 : i+16]))
if width < 1 || height < 1 {
return 0, 0, ErrFormat
}
first = false
}
i += 12 + n
if kind == "IEND" {
if n != 0 || i != len(data) {
return 0, 0, ErrFormat
}
return width, height, nil
}
}
return 0, 0, ErrFormat
}
func parseJPEG(data []byte) (int, int, error) {
if len(data) < 4 || data[0] != 0xff || data[1] != 0xd8 {
return 0, 0, ErrFormat
}
i := 2
var width, height int
sawSOF := false
sawEOI := false
for i < len(data) {
if data[i] != 0xff {
return 0, 0, ErrFormat
}
for i < len(data) && data[i] == 0xff {
i++
}
if i >= len(data) {
return 0, 0, ErrFormat
}
marker := data[i]
i++
switch {
case marker == 0xd9:
sawEOI = true
case marker == 0x01 || (marker >= 0xd0 && marker <= 0xd7):
continue
case marker == 0xda:
// Энтропийные данные до следующего маркера.
for i < len(data) {
if data[i] != 0xff {
i++
continue
}
if i+1 >= len(data) {
return 0, 0, ErrFormat
}
if data[i+1] == 0x00 {
i += 2
continue
}
break
}
continue
default:
if i+2 > len(data) {
return 0, 0, ErrFormat
}
seglen := int(data[i])<<8 | int(data[i+1])
if seglen < 2 || i+seglen > len(data) {
return 0, 0, ErrFormat
}
if isSOF(marker) {
if seglen < 7 {
return 0, 0, ErrFormat
}
height = int(data[i+3])<<8 | int(data[i+4])
width = int(data[i+5])<<8 | int(data[i+6])
sawSOF = true
}
i += seglen
continue
}
break
}
if !sawSOF || !sawEOI || width < 1 || height < 1 {
return 0, 0, ErrFormat
}
return width, height, nil
}
func isSOF(marker byte) bool {
switch marker {
case 0xc0, 0xc1, 0xc2:
return true
default:
return false
}
}
+114
View File
@@ -0,0 +1,114 @@
package imagecheck
import (
"bytes"
"hash/crc32"
"image"
"image/color"
"image/jpeg"
"image/png"
"os"
"testing"
)
func tinyPNG(t *testing.T, w, h int) []byte {
t.Helper()
img := image.NewNRGBA(image.Rect(0, 0, w, h))
img.Set(0, 0, color.NRGBA{R: 10, G: 20, B: 30, A: 255})
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
t.Fatal(err)
}
return buf.Bytes()
}
func tinyJPEG(t *testing.T) []byte {
t.Helper()
img := image.NewRGBA(image.Rect(0, 0, 8, 4))
var buf bytes.Buffer
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: 80}); err != nil {
t.Fatal(err)
}
return buf.Bytes()
}
func TestInspectAcceptsEncodedImages(t *testing.T) {
pngBytes := tinyPNG(t, 3, 2)
w, h, err := Inspect("image/png", pngBytes, 40_000_000)
if err != nil || w != 3 || h != 2 {
t.Fatalf("png: %d %d %v", w, h, err)
}
jpg := tinyJPEG(t)
w, h, err = Inspect("image/jpeg", jpg, 40_000_000)
if err != nil || w != 8 || h != 4 {
t.Fatalf("jpeg: %d %d %v", w, h, err)
}
}
func TestInspectRejectsMIMESwapAndDamage(t *testing.T) {
pngBytes := tinyPNG(t, 2, 2)
if _, _, err := Inspect("image/jpeg", pngBytes, 40_000_000); err != ErrFormat {
t.Fatalf("png declared as jpeg: %v", err)
}
if _, _, err := Inspect("image/png", tinyJPEG(t), 40_000_000); err != ErrFormat {
t.Fatalf("jpeg declared as png: %v", err)
}
if _, _, err := Inspect("image/png", []byte("not an image"), 40_000_000); err != ErrFormat {
t.Fatalf("text: %v", err)
}
cut := pngBytes[:len(pngBytes)/2]
if _, _, err := Inspect("image/png", cut, 40_000_000); err != ErrFormat {
t.Fatalf("truncated: %v", err)
}
flipped := append([]byte(nil), pngBytes...)
flipped[len(flipped)-20] ^= 0xff
if _, _, err := Inspect("image/png", flipped, 40_000_000); err != ErrFormat {
t.Fatalf("flipped byte: %v", err)
}
}
func TestInspectPixelLimitBeforeDecode(t *testing.T) {
// 20000×2001 = 40_020_000 > 40_000_000. Файл маленький: лимит смотрит IHDR, не растр.
huge := pngWithSize(t, 20000, 2001)
if _, _, err := Inspect("image/png", huge, 40_000_000); err != ErrPixels {
t.Fatalf("pixels: %v", err)
}
}
func pngWithSize(t *testing.T, w, h int) []byte {
t.Helper()
// Валидный минимальный PNG с подменёнными размерами и пересчитанным CRC IHDR
// не обязателен: достаточно корректного CRC, Decode для огромного размера не вызывается.
base := tinyPNG(t, 1, 1)
// IHDR width/height лежат сразу после сигнатуры и длины/типа: смещение 16 и 20.
out := append([]byte(nil), base...)
out[16] = byte(w >> 24)
out[17] = byte(w >> 16)
out[18] = byte(w >> 8)
out[19] = byte(w)
out[20] = byte(h >> 24)
out[21] = byte(h >> 16)
out[22] = byte(h >> 8)
out[23] = byte(h)
// CRC IHDR: байты 12..28 type+data, CRC по смещению 29.
sum := crc32.ChecksumIEEE(out[12:29])
out[29] = byte(sum >> 24)
out[30] = byte(sum >> 16)
out[31] = byte(sum >> 8)
out[32] = byte(sum)
return out
}
func TestFixtureSample(t *testing.T) {
b, err := os.ReadFile("../../tests/fixtures/sample.png")
if err != nil {
t.Fatal(err)
}
w, h, err := Inspect("image/png", b, 40_000_000)
if err != nil || w != 320 || h != 200 {
t.Fatalf("fixture: %d %d %v", w, h, err)
}
if _, _, err := Inspect("image/jpeg", b, 40_000_000); err != ErrFormat {
t.Fatalf("fixture mime: %v", err)
}
}
+82
View File
@@ -0,0 +1,82 @@
// применяет SQL из бинарника ровно один раз на файл.
package migrate
import (
"context"
"fmt"
"io/fs"
"sort"
"strings"
"github.com/jackc/pgx/v5/pgxpool"
"messenger/migrations"
)
// advisoryLock сериализует миграции между процессами. Номер постоянный для этого приложения.
const advisoryLock int64 = 748392016
// Apply создаёт schema_migrations и выполняет ещё не записанные файлы.
// Замок держится на одном соединении: пул сам по себе не привязывает блокировку к сессии.
func Apply(ctx context.Context, pool *pgxpool.Pool) error {
conn, err := pool.Acquire(ctx)
if err != nil {
return err
}
defer conn.Release()
if _, err := conn.Exec(ctx, `SELECT pg_advisory_lock($1)`, advisoryLock); err != nil {
return fmt.Errorf("advisory lock: %w", err)
}
defer func() {
_, _ = conn.Exec(context.Background(), `SELECT pg_advisory_unlock($1)`, advisoryLock)
}()
if _, err := conn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS schema_migrations (
version text PRIMARY KEY,
applied_at timestamptz NOT NULL DEFAULT now()
)`); err != nil {
return fmt.Errorf("schema_migrations: %w", err)
}
entries, err := fs.ReadDir(migrations.FS, ".")
if err != nil {
return err
}
names := make([]string, 0, len(entries))
for _, entry := range entries {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sql") {
continue
}
names = append(names, entry.Name())
}
sort.Strings(names)
for _, name := range names {
var applied bool
if err := conn.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM schema_migrations WHERE version = $1)`, name).Scan(&applied); err != nil {
return err
}
if applied {
continue
}
body, err := fs.ReadFile(migrations.FS, name)
if err != nil {
return err
}
tx, err := conn.Begin(ctx)
if err != nil {
return err
}
if _, err := tx.Exec(ctx, string(body)); err != nil {
_ = tx.Rollback(ctx)
return fmt.Errorf("%s: %w", name, err)
}
if _, err := tx.Exec(ctx, `INSERT INTO schema_migrations (version) VALUES ($1)`, name); err != nil {
_ = tx.Rollback(ctx)
return err
}
if err := tx.Commit(ctx); err != nil {
return err
}
}
return nil
}
+63
View File
@@ -0,0 +1,63 @@
// Package password хеширует пароли Argon2id.
package password
import (
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"fmt"
"strings"
"golang.org/x/crypto/argon2"
)
// Params — стоимость KDF. Значения пишутся в саму строку хеша,
// поэтому старые пароли проверяются даже после смены настроек.
type Params struct {
Time uint32
Memory uint32
Threads uint8
}
// Hash возвращает PHC-строку.
func (p Params) Hash(plain string) (string, error) {
if p.Time < 1 || p.Threads < 1 || p.Memory < 8*uint32(p.Threads) {
return "", fmt.Errorf("argon2 params are too small")
}
salt := make([]byte, 16)
if _, err := rand.Read(salt); err != nil {
return "", err
}
sum := argon2.IDKey([]byte(plain), salt, p.Time, p.Memory, p.Threads, 32)
return fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
argon2.Version,
p.Memory, p.Time, p.Threads,
base64.RawStdEncoding.EncodeToString(salt),
base64.RawStdEncoding.EncodeToString(sum),
), nil
}
// Verify сравнивает пароль с PHC-строкой. Параметры берутся из строки, не из текущего конфига.
func Verify(plain, encoded string) bool {
parts := strings.Split(encoded, "$")
if len(parts) != 6 || parts[1] != "argon2id" || !strings.HasPrefix(parts[2], "v=") {
return false
}
var memory, timeCost, threads int
if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &timeCost, &threads); err != nil {
return false
}
if memory < 8 || timeCost < 1 || threads < 1 || threads > 255 {
return false
}
salt, err := base64.RawStdEncoding.DecodeString(parts[4])
if err != nil || len(salt) == 0 {
return false
}
want, err := base64.RawStdEncoding.DecodeString(parts[5])
if err != nil || len(want) == 0 {
return false
}
got := argon2.IDKey([]byte(plain), salt, uint32(timeCost), uint32(memory), uint8(threads), uint32(len(want)))
return subtle.ConstantTimeCompare(got, want) == 1
}
+23
View File
@@ -0,0 +1,23 @@
package password
import "testing"
func TestArgon2idRoundTrip(t *testing.T) {
p := Params{Time: 1, Memory: 8, Threads: 1}
encoded, err := p.Hash("correct-horse-1")
if err != nil {
t.Fatal(err)
}
if encoded == "correct-horse-1" || len(encoded) < 20 {
t.Fatalf("hash is not a PHC string: %s", encoded)
}
if !Verify("correct-horse-1", encoded) {
t.Fatal("matching password rejected")
}
if Verify("other-password-1", encoded) {
t.Fatal("wrong password accepted")
}
if Verify("correct-horse-1", "sha256:deadbeef") {
t.Fatal("non-argon string accepted")
}
}
+115
View File
@@ -0,0 +1,115 @@
// Сверка PostgreSQL и bucket.
// Удаляются только старые расхождения: объект без строки и строка без объекта,
package reconcile
import (
"context"
"time"
"messenger/internal/blob"
"messenger/internal/store"
)
// Meta - чтение и безопасное удаление метаданных. Удаление само проверяет ссылки и возраст.
type Meta interface {
AttachmentRefs(ctx context.Context) ([]store.AttachmentRef, error)
DeleteUnreferencedAttachment(ctx context.Context, id string, olderThan time.Time) (bool, error)
}
// Summary - наблюдаемый результат одного прохода.
type Summary struct {
Mode string `json:"mode"`
GraceSeconds int `json:"grace_seconds"`
Cutoff string `json:"cutoff"`
DBRows int `json:"db_rows"`
S3Objects int `json:"s3_objects"`
ListPages int `json:"list_pages"`
Pairs int `json:"pairs"`
OrphanFresh int `json:"orphan_fresh"`
OrphanStale int `json:"orphan_stale"`
MissingFresh int `json:"missing_fresh"`
MissingReferenced int `json:"missing_referenced"`
MissingStaleUnreferenced int `json:"missing_stale_unreferenced"`
DeletedObjects int `json:"deleted_objects"`
DeletedRows int `json:"deleted_rows"`
DurationMs int64 `json:"duration_ms"`
}
// Run сравнивает множество ключей. Стоимость - один листинг bucket и один запрос метаданных,
// без HEAD на каждый объект.
func Run(ctx context.Context, meta Meta, objects blob.Store, grace time.Duration, apply bool) (Summary, error) {
start := time.Now()
if grace < 0 {
grace = 0
}
cutoff := time.Now().UTC().Add(-grace)
rows, err := meta.AttachmentRefs(ctx)
if err != nil {
return Summary{}, err
}
listed, pages, err := objects.List(ctx)
if err != nil {
return Summary{}, err
}
byKey := make(map[string]store.AttachmentRef, len(rows))
for _, row := range rows {
byKey[row.ObjectKey] = row
}
seen := make(map[string]struct{}, len(listed))
sum := Summary{
Mode: "dry-run",
GraceSeconds: int(grace / time.Second),
Cutoff: cutoff.Format(time.RFC3339),
DBRows: len(rows),
S3Objects: len(listed),
ListPages: pages,
}
if apply {
sum.Mode = "apply"
}
for _, obj := range listed {
seen[obj.Key] = struct{}{}
if _, ok := byKey[obj.Key]; ok {
sum.Pairs++
continue
}
if !obj.Modified.UTC().Before(cutoff) {
sum.OrphanFresh++
continue
}
sum.OrphanStale++
if !apply {
continue
}
if err := objects.Delete(ctx, obj.Key); err != nil {
return Summary{}, err
}
sum.DeletedObjects++
}
for _, row := range rows {
if _, ok := seen[row.ObjectKey]; ok {
continue
}
if row.Referenced {
sum.MissingReferenced++
continue
}
if !row.CreatedAt.UTC().Before(cutoff) {
sum.MissingFresh++
continue
}
sum.MissingStaleUnreferenced++
if !apply {
continue
}
deleted, err := meta.DeleteUnreferencedAttachment(ctx, row.ID, cutoff)
if err != nil {
return Summary{}, err
}
if deleted {
sum.DeletedRows++
}
}
sum.DurationMs = time.Since(start).Milliseconds()
return sum, nil
}
+98
View File
@@ -0,0 +1,98 @@
package reconcile
import (
"context"
"testing"
"time"
"messenger/internal/blob"
"messenger/internal/store"
)
func TestReconcileKeepsFreshAndReferenced(t *testing.T) {
ctx := context.Background()
meta := store.New()
objects := blob.NewMemory()
old := time.Now().UTC().Add(-time.Hour)
fresh := time.Now().UTC()
mustPut := func(key string, at time.Time) {
t.Helper()
if err := objects.PutAt(ctx, key, []byte("x"), at); err != nil {
t.Fatal(err)
}
}
mustRow := func(key string, at time.Time, referenced bool) string {
t.Helper()
row, err := meta.InsertAttachment(ctx, store.Attachment{
ChatID: "chat", OwnerID: "user", Filename: "a.png", ContentType: "image/png",
SizeBytes: 1, SHA256: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
ObjectKey: key, Width: 1, Height: 1, CreatedAt: at,
})
if err != nil {
t.Fatal(err)
}
if referenced {
meta.MarkReferenced(row.ID)
}
return row.ID
}
mustPut("orphan-old", old)
mustPut("orphan-fresh", fresh)
mustPut("pair-fresh", fresh)
refID := mustRow("missing-ref", old, true)
staleID := mustRow("missing-stale", old, false)
freshID := mustRow("missing-fresh", fresh, false)
pairID := mustRow("pair-fresh", fresh, false)
grace := 15 * time.Minute
dry, err := Run(ctx, meta, objects, grace, false)
if err != nil {
t.Fatal(err)
}
if dry.Mode != "dry-run" || dry.DeletedObjects != 0 || dry.DeletedRows != 0 {
t.Fatalf("dry-run mutated: %+v", dry)
}
if dry.OrphanStale != 1 || dry.OrphanFresh != 1 || dry.MissingReferenced != 1 || dry.MissingStaleUnreferenced != 1 || dry.MissingFresh != 1 || dry.Pairs != 1 {
t.Fatalf("classification: %+v", dry)
}
again, err := Run(ctx, meta, objects, grace, false)
if err != nil || again.OrphanStale != dry.OrphanStale || again.MissingStaleUnreferenced != dry.MissingStaleUnreferenced {
t.Fatalf("second dry-run: %+v %v", again, err)
}
applied, err := Run(ctx, meta, objects, grace, true)
if err != nil {
t.Fatal(err)
}
if applied.DeletedObjects != 1 || applied.DeletedRows != 1 {
t.Fatalf("apply: %+v", applied)
}
if _, err := objects.Get(ctx, "orphan-old"); err != blob.ErrNotFound {
t.Fatalf("old orphan still present: %v", err)
}
if _, err := objects.Get(ctx, "orphan-fresh"); err != nil {
t.Fatalf("fresh orphan deleted: %v", err)
}
if _, err := meta.AttachmentByID(ctx, refID); err != nil {
t.Fatalf("referenced row deleted: %v", err)
}
if _, err := meta.AttachmentByID(ctx, freshID); err != nil {
t.Fatalf("fresh row deleted: %v", err)
}
if _, err := meta.AttachmentByID(ctx, pairID); err != nil {
t.Fatalf("pair deleted: %v", err)
}
if _, err := meta.AttachmentByID(ctx, staleID); err != store.ErrNotFound {
t.Fatalf("stale row remains: %v", err)
}
clean, err := Run(ctx, meta, objects, grace, true)
if err != nil {
t.Fatal(err)
}
if clean.DeletedObjects != 0 || clean.DeletedRows != 0 || clean.OrphanStale != 0 || clean.MissingStaleUnreferenced != 0 {
t.Fatalf("rerun not idempotent: %+v", clean)
}
}
+77
View File
@@ -0,0 +1,77 @@
package session
import (
"context"
"errors"
"os"
"testing"
"time"
)
func TestMemoryExpiryAndRevoke(t *testing.T) {
s := NewMemory()
ctx := context.Background()
exp := time.Now().Add(30 * time.Millisecond)
if err := s.Create(ctx, "token-value-with-enough-entropy-xx", "user-1", exp); err != nil {
t.Fatal(err)
}
if _, _, err := s.Lookup(ctx, "token-value-with-enough-entropy-xx"); err != nil {
t.Fatal(err)
}
time.Sleep(40 * time.Millisecond)
if _, _, err := s.Lookup(ctx, "token-value-with-enough-entropy-xx"); !errors.Is(err, ErrNotFound) {
t.Fatalf("expired: %v", err)
}
exp = time.Now().Add(time.Minute)
if err := s.Create(ctx, "token-value-with-enough-entropy-yy", "user-1", exp); err != nil {
t.Fatal(err)
}
if err := s.Revoke(ctx, "token-value-with-enough-entropy-yy"); err != nil {
t.Fatal(err)
}
if _, _, err := s.Lookup(ctx, "token-value-with-enough-entropy-yy"); !errors.Is(err, ErrNotFound) {
t.Fatalf("revoked: %v", err)
}
}
func TestRedisDown(t *testing.T) {
s, err := OpenRedis("redis://127.0.0.1:1/0")
if err != nil {
t.Fatal(err)
}
defer s.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err := s.Ping(ctx); !errors.Is(err, ErrUnavailable) {
t.Fatalf("ping: %v", err)
}
if err := s.Create(ctx, "token", "user", time.Now().Add(time.Minute)); !errors.Is(err, ErrUnavailable) {
t.Fatalf("create: %v", err)
}
}
func TestIntegrationRedisRoundTrip(t *testing.T) {
raw := os.Getenv("REDIS_URL")
if raw == "" {
t.Skip("REDIS_URL is not set")
}
s, err := OpenRedis(raw)
if err != nil {
t.Fatal(err)
}
defer s.Close()
ctx := context.Background()
token := "integration-token-" + time.Now().UTC().Format("150405.000000")
exp := time.Now().UTC().Add(2 * time.Second).Truncate(time.Microsecond)
if err := s.Create(ctx, token, "11111111-1111-4111-8111-111111111111", exp); err != nil {
t.Fatal(err)
}
id, got, err := s.Lookup(ctx, token)
if err != nil || id != "11111111-1111-4111-8111-111111111111" || !got.Equal(exp) {
t.Fatalf("lookup id=%s exp=%s err=%v", id, got, err)
}
time.Sleep(2200 * time.Millisecond)
if _, _, err := s.Lookup(ctx, token); !errors.Is(err, ErrNotFound) {
t.Fatalf("ttl did not expire: %v", err)
}
}
+173
View File
@@ -0,0 +1,173 @@
// Package session хранит непрозрачные Bearer-сессии.
// В стенде это Redis: ключ живёт ровно до expires_at, рестарт TTL не сдвигает.
package session
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"sync"
"time"
"github.com/redis/go-redis/v9"
)
var (
ErrNotFound = errors.New("session not found")
ErrUnavailable = errors.New("session store unavailable")
)
// Store — срок жизни сессии задаёт вызывающий, хранилище его не продлевает.
type Store interface {
Create(ctx context.Context, token, userID string, expiresAt time.Time) error
Lookup(ctx context.Context, token string) (userID string, expiresAt time.Time, err error)
Revoke(ctx context.Context, token string) error
Ping(ctx context.Context) error
Close() error
}
type record struct {
UserID string `json:"user_id"`
ExpUnix int64 `json:"exp_unix"`
}
// key — хеш токена. Сам токен в Redis не пишется: утечка дампа не равна утечке Bearer.
func key(token string) string {
sum := sha256.Sum256([]byte(token))
return "sess:" + hex.EncodeToString(sum[:])
}
// Memory — сессии процесса. Нужна юнит-тестам без Docker.
type Memory struct {
mu sync.Mutex
items map[string]record
}
func NewMemory() *Memory {
return &Memory{items: make(map[string]record)}
}
func (m *Memory) Create(_ context.Context, token, userID string, expiresAt time.Time) error {
if token == "" || userID == "" {
return ErrNotFound
}
m.mu.Lock()
defer m.mu.Unlock()
m.items[key(token)] = record{UserID: userID, ExpUnix: expiresAt.UTC().UnixMicro()}
return nil
}
func (m *Memory) Lookup(_ context.Context, token string) (string, time.Time, error) {
m.mu.Lock()
defer m.mu.Unlock()
rec, ok := m.items[key(token)]
if !ok {
return "", time.Time{}, ErrNotFound
}
exp := time.UnixMicro(rec.ExpUnix).UTC()
if !time.Now().Before(exp) {
delete(m.items, key(token))
return "", time.Time{}, ErrNotFound
}
return rec.UserID, exp, nil
}
func (m *Memory) Revoke(_ context.Context, token string) error {
m.mu.Lock()
defer m.mu.Unlock()
k := key(token)
if _, ok := m.items[k]; !ok {
return ErrNotFound
}
delete(m.items, k)
return nil
}
func (m *Memory) Ping(ctx context.Context) error {
if err := ctx.Err(); err != nil {
return ErrUnavailable
}
return nil
}
func (m *Memory) Close() error { return nil }
// Redis — единственное хранилище сессий стенда.
type Redis struct {
client *redis.Client
}
func OpenRedis(rawURL string) (*Redis, error) {
opt, err := redis.ParseURL(rawURL)
if err != nil {
return nil, err
}
opt.DialTimeout = 500 * time.Millisecond
opt.ReadTimeout = 500 * time.Millisecond
opt.WriteTimeout = 500 * time.Millisecond
opt.PoolSize = 10
opt.MinIdleConns = 1
return &Redis{client: redis.NewClient(opt)}, nil
}
func (r *Redis) Create(ctx context.Context, token, userID string, expiresAt time.Time) error {
if token == "" || userID == "" {
return ErrNotFound
}
ttl := time.Until(expiresAt)
if ttl < time.Millisecond {
return ErrNotFound
}
body, err := json.Marshal(record{UserID: userID, ExpUnix: expiresAt.UTC().UnixMicro()})
if err != nil {
return err
}
if err := r.client.Set(ctx, key(token), body, ttl).Err(); err != nil {
return ErrUnavailable
}
return nil
}
func (r *Redis) Lookup(ctx context.Context, token string) (string, time.Time, error) {
raw, err := r.client.Get(ctx, key(token)).Bytes()
if errors.Is(err, redis.Nil) {
return "", time.Time{}, ErrNotFound
}
if err != nil {
return "", time.Time{}, ErrUnavailable
}
var rec record
if err := json.Unmarshal(raw, &rec); err != nil || rec.UserID == "" {
return "", time.Time{}, ErrNotFound
}
exp := time.UnixMicro(rec.ExpUnix).UTC()
if !time.Now().Before(exp) {
_ = r.client.Del(ctx, key(token)).Err()
return "", time.Time{}, ErrNotFound
}
return rec.UserID, exp, nil
}
func (r *Redis) Revoke(ctx context.Context, token string) error {
n, err := r.client.Del(ctx, key(token)).Result()
if err != nil {
return ErrUnavailable
}
if n == 0 {
return ErrNotFound
}
return nil
}
func (r *Redis) Ping(ctx context.Context) error {
if err := r.client.Ping(ctx).Err(); err != nil {
return ErrUnavailable
}
return nil
}
func (r *Redis) Close() error {
return r.client.Close()
}
+286
View File
@@ -0,0 +1,286 @@
package store
import (
"context"
"time"
"github.com/jackc/pgx/v5"
)
func (p *PG) InsertAttachment(ctx context.Context, row Attachment) (Attachment, error) {
if row.ID == "" {
row.ID = newID()
}
now := time.Now().UTC().Truncate(time.Microsecond)
if row.CreatedAt.IsZero() {
row.CreatedAt = now
} else {
row.CreatedAt = row.CreatedAt.UTC().Truncate(time.Microsecond)
}
row.UpdatedAt = row.CreatedAt
row.Status = "ready"
_, err := p.pool.Exec(ctx, `
INSERT INTO attachments (
id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
object_key, width, height, status, created_at, updated_at
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
)`,
row.ID, row.ChatID, row.OwnerID, row.Filename, row.ContentType, row.SizeBytes, row.SHA256,
row.ObjectKey, row.Width, row.Height, row.Status, row.CreatedAt, row.UpdatedAt)
if err != nil {
return Attachment{}, mapDB(err)
}
return row, nil
}
func (p *PG) AttachmentVisible(ctx context.Context, id, userID string) (Attachment, error) {
var row Attachment
err := p.pool.QueryRow(ctx, `
SELECT a.id::text, a.chat_id::text, a.owner_id::text, a.filename, a.content_type,
a.size_bytes, a.sha256, a.object_key, a.width, a.height, a.status, a.created_at, a.updated_at
FROM attachments a
JOIN chat_members m ON m.chat_id = a.chat_id AND m.user_id = $2
WHERE a.id = $1`, id, userID).Scan(
&row.ID, &row.ChatID, &row.OwnerID, &row.Filename, &row.ContentType,
&row.SizeBytes, &row.SHA256, &row.ObjectKey, &row.Width, &row.Height, &row.Status,
&row.CreatedAt, &row.UpdatedAt)
if err != nil {
return Attachment{}, mapDB(err)
}
row.CreatedAt = row.CreatedAt.UTC()
row.UpdatedAt = row.UpdatedAt.UTC()
return row, nil
}
func (p *PG) AttachmentRefs(ctx context.Context) ([]AttachmentRef, error) {
rows, err := p.pool.Query(ctx, `
SELECT a.id::text, a.object_key, a.created_at,
EXISTS(SELECT 1 FROM message_attachments m WHERE m.attachment_id = a.id)
FROM attachments a`)
if err != nil {
return nil, mapDB(err)
}
defer rows.Close()
out := make([]AttachmentRef, 0)
for rows.Next() {
var row AttachmentRef
if err := rows.Scan(&row.ID, &row.ObjectKey, &row.CreatedAt, &row.Referenced); err != nil {
return nil, mapDB(err)
}
row.CreatedAt = row.CreatedAt.UTC()
out = append(out, row)
}
if err := rows.Err(); err != nil {
return nil, mapDB(err)
}
return out, nil
}
func (p *PG) DeleteUnreferencedAttachment(ctx context.Context, id string, olderThan time.Time) (bool, error) {
tag, err := p.pool.Exec(ctx, `
DELETE FROM attachments a
WHERE a.id = $1
AND a.created_at < $2
AND NOT EXISTS (SELECT 1 FROM message_attachments m WHERE m.attachment_id = a.id)`, id, olderThan)
if err != nil {
return false, mapDB(err)
}
return tag.RowsAffected() == 1, nil
}
func bindAttachments(ctx context.Context, tx pgx.Tx, messageID, chatID string, ids []string) error {
for i, id := range ids {
var gotChat, status string
err := tx.QueryRow(ctx, `
SELECT chat_id::text, status FROM attachments WHERE id = $1`, id).Scan(&gotChat, &status)
if err != nil {
if mapDB(err) == ErrNotFound {
return ErrNotFound
}
return err
}
if gotChat != chatID {
return ErrNotFound
}
if status != "ready" {
return ErrNotReady
}
if _, err := tx.Exec(ctx, `
INSERT INTO message_attachments (message_id, attachment_id, position)
VALUES ($1, $2, $3)`, messageID, id, i); err != nil {
return err
}
}
return nil
}
func attachmentIDsOf(ctx context.Context, q queryer, messageID string) ([]string, error) {
rows, err := q.Query(ctx, `
SELECT attachment_id::text
FROM message_attachments
WHERE message_id = $1
ORDER BY position`, messageID)
if err != nil {
return nil, mapDB(err)
}
defer rows.Close()
ids := make([]string, 0)
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
return nil, mapDB(err)
}
ids = append(ids, id)
}
if err := rows.Err(); err != nil {
return nil, mapDB(err)
}
return ids, nil
}
func fillAttachmentIDs(ctx context.Context, q queryer, items []Message) error {
if len(items) == 0 {
return nil
}
ids := make([]string, len(items))
for i := range items {
ids[i] = items[i].ID
items[i].AttachmentIDs = []string{}
}
rows, err := q.Query(ctx, `
SELECT message_id::text, attachment_id::text
FROM message_attachments
WHERE message_id::text = ANY($1::text[])
ORDER BY message_id, position`, ids)
if err != nil {
return mapDB(err)
}
defer rows.Close()
grouped := map[string][]string{}
for rows.Next() {
var messageID, attachmentID string
if err := rows.Scan(&messageID, &attachmentID); err != nil {
return mapDB(err)
}
grouped[messageID] = append(grouped[messageID], attachmentID)
}
if err := rows.Err(); err != nil {
return mapDB(err)
}
for i := range items {
if got, ok := grouped[items[i].ID]; ok {
items[i].AttachmentIDs = got
}
}
return nil
}
func (s *Memory) InsertAttachment(ctx context.Context, row Attachment) (Attachment, error) {
if err := ctx.Err(); err != nil {
return Attachment{}, ErrUnavailable
}
s.mu.Lock()
defer s.mu.Unlock()
if row.ID == "" {
row.ID = newID()
}
now := s.nextTimeLocked()
if row.CreatedAt.IsZero() {
row.CreatedAt = now
} else {
row.CreatedAt = row.CreatedAt.UTC()
}
row.UpdatedAt = row.CreatedAt
if row.Status == "" {
row.Status = "ready"
}
s.attachments[row.ID] = row
return row, nil
}
func (s *Memory) AttachmentVisible(ctx context.Context, id, userID string) (Attachment, error) {
if err := ctx.Err(); err != nil {
return Attachment{}, ErrUnavailable
}
s.mu.RLock()
defer s.mu.RUnlock()
row, ok := s.attachments[id]
if !ok {
return Attachment{}, ErrNotFound
}
rec, ok := s.chats[row.ChatID]
if !ok {
return Attachment{}, ErrNotFound
}
if _, member := rec.members[userID]; !member {
return Attachment{}, ErrNotFound
}
return row, nil
}
func (s *Memory) AttachmentRefs(ctx context.Context) ([]AttachmentRef, error) {
if err := ctx.Err(); err != nil {
return nil, ErrUnavailable
}
s.mu.RLock()
defer s.mu.RUnlock()
out := make([]AttachmentRef, 0, len(s.attachments))
for _, row := range s.attachments {
out = append(out, AttachmentRef{
ID: row.ID, ObjectKey: row.ObjectKey, CreatedAt: row.CreatedAt, Referenced: s.referencedLocked(row.ID),
})
}
return out, nil
}
func (s *Memory) DeleteUnreferencedAttachment(ctx context.Context, id string, olderThan time.Time) (bool, error) {
if err := ctx.Err(); err != nil {
return false, ErrUnavailable
}
s.mu.Lock()
defer s.mu.Unlock()
row, ok := s.attachments[id]
if !ok || s.referencedLocked(row.ID) || !row.CreatedAt.Before(olderThan) {
return false, nil
}
delete(s.attachments, id)
return true, nil
}
// MarkReferenced нужен тесту сверки: строка считается используемой без сообщения.
func (s *Memory) MarkReferenced(id string) {
s.mu.Lock()
s.referenced[id] = struct{}{}
s.mu.Unlock()
}
// AttachmentByID читает строку без проверки участника. Только для тестов сверки.
func (s *Memory) AttachmentByID(ctx context.Context, id string) (Attachment, error) {
if err := ctx.Err(); err != nil {
return Attachment{}, ErrUnavailable
}
s.mu.RLock()
defer s.mu.RUnlock()
row, ok := s.attachments[id]
if !ok {
return Attachment{}, ErrNotFound
}
return row, nil
}
func (s *Memory) referencedLocked(id string) bool {
if _, ok := s.referenced[id]; ok {
return true
}
for _, msgs := range s.messages {
for _, m := range msgs {
for _, aid := range m.AttachmentIDs {
if aid == id {
return true
}
}
}
}
return false
}
+473
View File
@@ -0,0 +1,473 @@
package store
import (
"context"
"errors"
"net"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
)
// PG - источник истины для пользователей, чатов и сообщений.
// Запросы только с параметрами. Пул ограничен, у сессии соединения есть statement_timeout.
type PG struct {
pool *pgxpool.Pool
idemTTL time.Duration
}
func Open(ctx context.Context, databaseURL string, maxConns int32) (*PG, error) {
cfg, err := pgxpool.ParseConfig(databaseURL)
if err != nil {
return nil, err
}
if maxConns < 1 {
maxConns = 10
}
cfg.MaxConns = maxConns
cfg.MinConns = 1
cfg.MaxConnLifetime = 30 * time.Minute
cfg.MaxConnIdleTime = 5 * time.Minute
cfg.HealthCheckPeriod = 30 * time.Second
cfg.ConnConfig.ConnectTimeout = 3 * time.Second
cfg.ConnConfig.RuntimeParams["application_name"] = "messenger"
cfg.ConnConfig.RuntimeParams["statement_timeout"] = "5000"
cfg.ConnConfig.RuntimeParams["lock_timeout"] = "3000"
cfg.ConnConfig.RuntimeParams["idle_in_transaction_session_timeout"] = "10000"
pool, err := pgxpool.NewWithConfig(ctx, cfg)
if err != nil {
return nil, err
}
pingCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
if err := pool.Ping(pingCtx); err != nil {
pool.Close()
return nil, err
}
return &PG{pool: pool, idemTTL: 24 * time.Hour}, nil
}
// SetIdempotencyTTL задаёт, сколько хранить результат POST сообщения.
func (p *PG) SetIdempotencyTTL(ttl time.Duration) {
if ttl > 0 {
p.idemTTL = ttl
}
}
func (p *PG) Close() { p.pool.Close() }
func (p *PG) Pool() *pgxpool.Pool { return p.pool }
func (p *PG) Ping(ctx context.Context) error {
return mapDB(p.pool.Ping(ctx))
}
func (p *PG) CreateUser(ctx context.Context, username, displayName, passwordHash string) (User, error) {
u := User{
ID: newID(),
Username: username,
DisplayName: displayName,
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
}
_, err := p.pool.Exec(ctx, `
INSERT INTO users (id, username, display_name, password_hash, created_at)
VALUES ($1, $2, $3, $4, $5)`,
u.ID, u.Username, u.DisplayName, passwordHash, u.CreatedAt)
if err != nil {
return User{}, mapDB(err)
}
return u, nil
}
func (p *PG) User(ctx context.Context, id string) (User, error) {
row := p.pool.QueryRow(ctx, `
SELECT id::text, username, display_name, created_at
FROM users WHERE id = $1`, id)
return scanUser(row)
}
func (p *PG) UserByUsername(ctx context.Context, username string) (User, string, error) {
var u User
var hash string
err := p.pool.QueryRow(ctx, `
SELECT id::text, username, display_name, created_at, password_hash
FROM users WHERE username = $1`, username).Scan(&u.ID, &u.Username, &u.DisplayName, &u.CreatedAt, &hash)
if err != nil {
return User{}, "", mapDB(err)
}
u.CreatedAt = u.CreatedAt.UTC()
return u, hash, nil
}
// CreateChat пишет чат и всех участников в одной транзакции.
// Если кого-то из списка нет, COMMIT не случается и строка чата не остаётся.
func (p *PG) CreateChat(ctx context.Context, title string, memberIDs []string, caller string) (Chat, error) {
seen := make(map[string]struct{}, len(memberIDs))
for _, id := range memberIDs {
if _, dup := seen[id]; dup {
return Chat{}, ErrInvalid
}
seen[id] = struct{}{}
}
if _, ok := seen[caller]; !ok {
return Chat{}, ErrInvalid
}
tx, err := p.pool.Begin(ctx)
if err != nil {
return Chat{}, mapDB(err)
}
defer func() { _ = tx.Rollback(ctx) }()
c := Chat{
ID: newID(),
Title: title,
MemberIDs: append([]string(nil), memberIDs...),
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
}
if _, err := tx.Exec(ctx, `INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)`, c.ID, c.Title, c.CreatedAt); err != nil {
return Chat{}, mapDB(err)
}
var n int
if err := tx.QueryRow(ctx, `SELECT count(*) FROM users WHERE id::text = ANY($1::text[])`, memberIDs).Scan(&n); err != nil {
return Chat{}, mapDB(err)
}
if n != len(memberIDs) {
return Chat{}, ErrInvalid
}
for i, id := range memberIDs {
if _, err := tx.Exec(ctx, `
INSERT INTO chat_members (chat_id, user_id, position) VALUES ($1, $2, $3)`,
c.ID, id, i); err != nil {
return Chat{}, mapDB(err)
}
}
if err := tx.Commit(ctx); err != nil {
return Chat{}, mapDB(err)
}
return c, nil
}
func (p *PG) ChatFor(ctx context.Context, chatID, userID string) (Chat, error) {
var c Chat
err := p.pool.QueryRow(ctx, `
SELECT c.id::text, c.title, c.created_at
FROM chats c
JOIN chat_members cm ON cm.chat_id = c.id AND cm.user_id = $2
WHERE c.id = $1`, chatID, userID).Scan(&c.ID, &c.Title, &c.CreatedAt)
if err != nil {
return Chat{}, mapDB(err)
}
c.CreatedAt = c.CreatedAt.UTC()
ids, err := p.memberIDs(ctx, p.pool, []string{c.ID})
if err != nil {
return Chat{}, err
}
c.MemberIDs = ids[c.ID]
if c.MemberIDs == nil {
c.MemberIDs = []string{}
}
return c, nil
}
func (p *PG) ListChats(ctx context.Context, userID string, cur *Cursor, limit int) ([]Chat, bool, error) {
hasCursor := cur != nil
at := time.Time{}
var id any
if hasCursor {
at = cur.At
id = cur.ID
}
rows, err := p.pool.Query(ctx, `
SELECT c.id::text, c.title, c.created_at
FROM chats c
JOIN chat_members cm ON cm.chat_id = c.id AND cm.user_id = $1
WHERE NOT $2::bool OR (c.created_at, c.id) > ($3::timestamptz, $4::uuid)
ORDER BY c.created_at, c.id
LIMIT $5`, userID, hasCursor, at, id, limit+1)
if err != nil {
return nil, false, mapDB(err)
}
defer rows.Close()
items := make([]Chat, 0)
ids := make([]string, 0)
for rows.Next() {
var c Chat
if err := rows.Scan(&c.ID, &c.Title, &c.CreatedAt); err != nil {
return nil, false, mapDB(err)
}
c.CreatedAt = c.CreatedAt.UTC()
items = append(items, c)
ids = append(ids, c.ID)
}
if err := rows.Err(); err != nil {
return nil, false, mapDB(err)
}
hasMore := len(items) > limit
if hasMore {
items = items[:limit]
ids = ids[:limit]
}
members, err := p.memberIDs(ctx, p.pool, ids)
if err != nil {
return nil, false, err
}
for i := range items {
items[i].MemberIDs = members[items[i].ID]
if items[i].MemberIDs == nil {
items[i].MemberIDs = []string{}
}
}
return items, hasMore, nil
}
func (p *PG) AddMessage(ctx context.Context, chatID, senderID, text string, attachmentIDs []string, idemKey, fingerprint string) (Message, error) {
if idemKey == "" || fingerprint == "" {
return Message{}, ErrInvalid
}
var last error
for attempt := 0; attempt < 5; attempt++ {
msg, retry, err := p.addMessageOnce(ctx, chatID, senderID, text, attachmentIDs, idemKey, fingerprint)
if !retry {
return msg, err
}
last = err
}
if last == nil {
last = ErrUnavailable
}
return Message{}, ErrUnavailable
}
// addMessageOnce пишет сообщение и ключ одной транзакцией.
// retry=true значит, что параллельная реплика заняла тот же ключ раньше: свою вставку откатываем и читаем её результат.
func (p *PG) addMessageOnce(ctx context.Context, chatID, senderID, text string, attachmentIDs []string, idemKey, fingerprint string) (Message, bool, error) {
tx, err := p.pool.Begin(ctx)
if err != nil {
return Message{}, false, mapDB(err)
}
defer func() { _ = tx.Rollback(ctx) }()
var member int
if err := tx.QueryRow(ctx, `
SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2`, chatID, senderID).Scan(&member); err != nil {
return Message{}, false, mapDB(err)
}
var storedFP, messageID string
var expires time.Time
err = tx.QueryRow(ctx, `
SELECT fingerprint, message_id::text, expires_at
FROM message_idempotency
WHERE user_id = $1 AND chat_id = $2 AND idem_key = $3
FOR UPDATE`, senderID, chatID, idemKey).Scan(&storedFP, &messageID, &expires)
if err == nil {
if expires.After(time.Now()) {
if storedFP != fingerprint {
return Message{}, false, ErrIdempotencyMismatch
}
msg, err := scanMessage(tx.QueryRow(ctx, `
SELECT id::text, chat_id::text, sender_id::text, text, created_at
FROM messages WHERE id = $1`, messageID))
if err != nil {
return Message{}, false, err
}
msg.AttachmentIDs, err = attachmentIDsOf(ctx, tx, msg.ID)
if err != nil {
return Message{}, false, err
}
if err := tx.Commit(ctx); err != nil {
return Message{}, false, mapDB(err)
}
return msg, false, nil
}
if _, err := tx.Exec(ctx, `
DELETE FROM message_idempotency
WHERE user_id = $1 AND chat_id = $2 AND idem_key = $3`, senderID, chatID, idemKey); err != nil {
return Message{}, false, mapDB(err)
}
} else if !errors.Is(err, pgx.ErrNoRows) {
return Message{}, false, mapDB(err)
}
now := time.Now().UTC().Truncate(time.Microsecond)
m := Message{
ID: newID(),
ChatID: chatID,
SenderID: senderID,
Text: text,
CreatedAt: now,
AttachmentIDs: append([]string(nil), attachmentIDs...),
}
if m.AttachmentIDs == nil {
m.AttachmentIDs = []string{}
}
if _, err := tx.Exec(ctx, `
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
VALUES ($1, $2, $3, $4, $5)`, m.ID, m.ChatID, m.SenderID, m.Text, m.CreatedAt); err != nil {
return Message{}, retryable(err), mapDB(err)
}
if err := bindAttachments(ctx, tx, m.ID, chatID, m.AttachmentIDs); err != nil {
return Message{}, retryable(err), mapDB(err)
}
recorded := time.Now().UTC().Truncate(time.Microsecond)
if _, err := tx.Exec(ctx, `
INSERT INTO message_idempotency
(user_id, chat_id, idem_key, fingerprint, message_id, created_at, expires_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
senderID, chatID, idemKey, fingerprint, m.ID, recorded, recorded.Add(p.idemTTL)); err != nil {
return Message{}, retryable(err), mapDB(err)
}
if err := tx.Commit(ctx); err != nil {
return Message{}, retryable(err), mapDB(err)
}
return m, false, nil
}
func retryable(err error) bool {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
switch pgErr.Code {
case "23505", "40001", "40P01":
return true
}
}
return false
}
func (p *PG) ListMessages(ctx context.Context, chatID, userID string, cur *Cursor, limit int) ([]Message, bool, error) {
var member int
err := p.pool.QueryRow(ctx, `
SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2`, chatID, userID).Scan(&member)
if err != nil {
return nil, false, mapDB(err)
}
if cur != nil {
var one int
err := p.pool.QueryRow(ctx, `
SELECT 1 FROM messages
WHERE chat_id = $1 AND id = $2 AND created_at = $3`, chatID, cur.ID, cur.At).Scan(&one)
if errors.Is(err, pgx.ErrNoRows) {
return nil, false, ErrBadCursor
}
if err != nil {
return nil, false, mapDB(err)
}
}
hasCursor := cur != nil
at := time.Time{}
var id any
if hasCursor {
at = cur.At
id = cur.ID
}
rows, err := p.pool.Query(ctx, `
SELECT id::text, chat_id::text, sender_id::text, text, created_at
FROM messages
WHERE chat_id = $1
AND (NOT $2::bool OR (created_at, id) > ($3::timestamptz, $4::uuid))
ORDER BY created_at, id
LIMIT $5`, chatID, hasCursor, at, id, limit+1)
if err != nil {
return nil, false, mapDB(err)
}
defer rows.Close()
items := make([]Message, 0)
for rows.Next() {
m, err := scanMessage(rows)
if err != nil {
return nil, false, err
}
items = append(items, m)
}
if err := rows.Err(); err != nil {
return nil, false, mapDB(err)
}
hasMore := len(items) > limit
if hasMore {
items = items[:limit]
}
if err := fillAttachmentIDs(ctx, p.pool, items); err != nil {
return nil, false, err
}
return items, hasMore, nil
}
type queryer interface {
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
}
func (p *PG) memberIDs(ctx context.Context, q queryer, chatIDs []string) (map[string][]string, error) {
out := make(map[string][]string, len(chatIDs))
if len(chatIDs) == 0 {
return out, nil
}
rows, err := q.Query(ctx, `
SELECT chat_id::text, user_id::text
FROM chat_members
WHERE chat_id::text = ANY($1::text[])
ORDER BY chat_id, position`, chatIDs)
if err != nil {
return nil, mapDB(err)
}
defer rows.Close()
for rows.Next() {
var chatID, userID string
if err := rows.Scan(&chatID, &userID); err != nil {
return nil, mapDB(err)
}
out[chatID] = append(out[chatID], userID)
}
if err := rows.Err(); err != nil {
return nil, mapDB(err)
}
return out, nil
}
func scanMessage(row pgx.Row) (Message, error) {
var m Message
if err := row.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt); err != nil {
return Message{}, mapDB(err)
}
m.CreatedAt = m.CreatedAt.UTC()
return m, nil
}
func scanUser(row pgx.Row) (User, error) {
var u User
err := row.Scan(&u.ID, &u.Username, &u.DisplayName, &u.CreatedAt)
if err != nil {
return User{}, mapDB(err)
}
u.CreatedAt = u.CreatedAt.UTC()
return u, nil
}
func mapDB(err error) error {
if err == nil {
return nil
}
if errors.Is(err, pgx.ErrNoRows) {
return ErrNotFound
}
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
return ErrUnavailable
}
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
switch pgErr.Code {
case "23505":
return ErrConflict
case "23503", "23514", "22P02":
return ErrInvalid
case "57014", "55P03":
return ErrUnavailable
}
}
var netErr net.Error
if errors.As(err, &netErr) {
return ErrUnavailable
}
return err
}
+183
View File
@@ -0,0 +1,183 @@
package store
import (
"context"
"errors"
"os"
"sync"
"testing"
"time"
"messenger/internal/migrate"
)
func openIntegration(t *testing.T) *PG {
t.Helper()
raw := os.Getenv("DATABASE_URL")
if raw == "" {
t.Skip("DATABASE_URL is not set")
}
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
pg, err := Open(ctx, raw, 10)
if err != nil {
t.Fatal(err)
}
t.Cleanup(pg.Close)
if err := migrate.Apply(ctx, pg.Pool()); err != nil {
t.Fatal(err)
}
return pg
}
func TestIntegrationMigrationRerunKeepsRows(t *testing.T) {
pg := openIntegration(t)
ctx := context.Background()
name := "mig_" + newID()[:8]
if _, err := pg.CreateUser(ctx, name, "Миграция", "hash"); err != nil {
t.Fatal(err)
}
var before int
if err := pg.Pool().QueryRow(ctx, `SELECT count(*) FROM users`).Scan(&before); err != nil {
t.Fatal(err)
}
if err := migrate.Apply(ctx, pg.Pool()); err != nil {
t.Fatal(err)
}
var after int
if err := pg.Pool().QueryRow(ctx, `SELECT count(*) FROM users`).Scan(&after); err != nil {
t.Fatal(err)
}
if before != after {
t.Fatalf("migration rerun changed users %d -> %d", before, after)
}
}
func TestIntegrationUsernameRace(t *testing.T) {
pg := openIntegration(t)
ctx := context.Background()
name := "race_" + newID()[:8]
const n = 32
var wg sync.WaitGroup
wins := make(chan struct{}, n)
fails := make(chan error, n)
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, err := pg.CreateUser(ctx, name, "Гонка", "hash")
if err == nil {
wins <- struct{}{}
return
}
fails <- err
}()
}
wg.Wait()
close(wins)
close(fails)
got := 0
for range wins {
got++
}
if got != 1 {
t.Fatalf("winners = %d, want 1", got)
}
for err := range fails {
if !errors.Is(err, ErrConflict) {
t.Fatal(err)
}
}
}
func TestIntegrationChatRollback(t *testing.T) {
pg := openIntegration(t)
ctx := context.Background()
u, err := pg.CreateUser(ctx, "rb_"+newID()[:8], "Откат", "hash")
if err != nil {
t.Fatal(err)
}
var before int
if err := pg.Pool().QueryRow(ctx, `SELECT count(*) FROM chats`).Scan(&before); err != nil {
t.Fatal(err)
}
_, err = pg.CreateChat(ctx, "не должен сохраниться", []string{u.ID, "99999999-9999-4999-8999-999999999999"}, u.ID)
if !errors.Is(err, ErrInvalid) {
t.Fatalf("want invalid, got %v", err)
}
var after int
if err := pg.Pool().QueryRow(ctx, `SELECT count(*) FROM chats`).Scan(&after); err != nil {
t.Fatal(err)
}
if before != after {
t.Fatalf("rolled-back chat stayed: %d -> %d", before, after)
}
var members int
if err := pg.Pool().QueryRow(ctx, `SELECT count(*) FROM chat_members WHERE user_id = $1`, u.ID).Scan(&members); err != nil {
t.Fatal(err)
}
if members != 0 {
t.Fatalf("rolled-back membership stayed: %d", members)
}
}
func TestIntegrationIdempotencyRace(t *testing.T) {
pg := openIntegration(t)
ctx := context.Background()
u, err := pg.CreateUser(ctx, "idm_"+newID()[:8], "Ключ", "hash")
if err != nil {
t.Fatal(err)
}
chat, err := pg.CreateChat(ctx, "идемпотентность", []string{u.ID}, u.ID)
if err != nil {
t.Fatal(err)
}
const n = 16
var wg sync.WaitGroup
ids := make(chan string, n)
errs := make(chan error, n)
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
defer wg.Done()
msg, err := pg.AddMessage(ctx, chat.ID, u.ID, "одно", nil, "key-1", "одно")
if err != nil {
errs <- err
return
}
ids <- msg.ID
}()
}
wg.Wait()
close(ids)
close(errs)
for err := range errs {
t.Fatal(err)
}
seen := map[string]struct{}{}
for id := range ids {
seen[id] = struct{}{}
}
if len(seen) != 1 {
t.Fatalf("concurrent key created %d messages", len(seen))
}
if _, err := pg.AddMessage(ctx, chat.ID, u.ID, "другое", nil, "key-1", "другое"); !errors.Is(err, ErrIdempotencyMismatch) {
t.Fatalf("mismatch: %v", err)
}
var count int
if err := pg.Pool().QueryRow(ctx, `SELECT count(*) FROM messages WHERE chat_id = $1`, chat.ID).Scan(&count); err != nil {
t.Fatal(err)
}
if count != 1 {
t.Fatalf("messages = %d, want 1", count)
}
}
func TestPostgresUnreachable(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
_, err := Open(ctx, "postgres://messenger:secret@127.0.0.1:1/messenger?sslmode=disable&connect_timeout=1", 1)
if err == nil {
t.Fatal("closed port was accepted")
}
}
+415
View File
@@ -0,0 +1,415 @@
package store
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"sync"
"time"
)
var (
ErrConflict = errors.New("conflict")
ErrIdempotencyMismatch = errors.New("idempotency mismatch")
ErrNotFound = errors.New("not found")
ErrInvalid = errors.New("invalid")
ErrBadCursor = errors.New("bad cursor")
ErrUnavailable = errors.New("unavailable")
ErrNotReady = errors.New("attachment not ready")
)
// User — публичная карточка. Хеш пароля в неё не входит.
type User struct {
ID string
Username string
DisplayName string
CreatedAt time.Time
}
// Chat — неизменяемый состав участников.
type Chat struct {
ID string
Title string
MemberIDs []string
CreatedAt time.Time
}
// Message — подтверждённое сообщение. Текст после записи не меняется.
type Message struct {
ID string
ChatID string
SenderID string
Text string
CreatedAt time.Time
AttachmentIDs []string
}
// Attachment — метаданные файла. ObjectKey наружу API не отдаётся.
type Attachment struct {
ID string
ChatID string
OwnerID string
Filename string
ContentType string
SizeBytes int64
SHA256 string
ObjectKey string
Width int
Height int
Status string
CreatedAt time.Time
UpdatedAt time.Time
}
// AttachmentRef — строка для сверки с bucket.
type AttachmentRef struct {
ID string
ObjectKey string
CreatedAt time.Time
Referenced bool
}
// Cursor — позиция keyset (created_at, id). Пустые поля означают первую страницу.
type Cursor struct {
ID string
At time.Time
}
// Repository — пользователи, чаты и сообщения. Сессии сюда не входят.
type Repository interface {
Ping(ctx context.Context) error
CreateUser(ctx context.Context, username, displayName, passwordHash string) (User, error)
User(ctx context.Context, id string) (User, error)
UserByUsername(ctx context.Context, username string) (User, string, error)
CreateChat(ctx context.Context, title string, memberIDs []string, caller string) (Chat, error)
ChatFor(ctx context.Context, chatID, userID string) (Chat, error)
ListChats(ctx context.Context, userID string, cur *Cursor, limit int) ([]Chat, bool, error)
// AddMessage создаёт сообщение или возвращает уже записанное для того же ключа.
// fingerprint — каноническое тело. Другой fingerprint при живом ключе даёт ErrIdempotencyMismatch.
// Ключ живёт idempotencyTTL. Область: отправитель + чат + ключ (метод POST и путь чата).
// Чужое или неизвестное вложение — ErrNotFound. Неготовое — ErrNotReady.
AddMessage(ctx context.Context, chatID, senderID, text string, attachmentIDs []string, idemKey, fingerprint string) (Message, error)
ListMessages(ctx context.Context, chatID, userID string, cur *Cursor, limit int) ([]Message, bool, error)
InsertAttachment(ctx context.Context, row Attachment) (Attachment, error)
AttachmentVisible(ctx context.Context, id, userID string) (Attachment, error)
AttachmentRefs(ctx context.Context) ([]AttachmentRef, error)
DeleteUnreferencedAttachment(ctx context.Context, id string, olderThan time.Time) (bool, error)
}
type chatRec struct {
chat Chat
members map[string]struct{}
}
type idemRec struct {
fingerprint string
messageID string
expires time.Time
}
// Memory сериализует записи мьютексом. Это двойник контракта для тестов без PostgreSQL.
type Memory struct {
mu sync.RWMutex
users map[string]User
byName map[string]string
hashes map[string]string
chats map[string]*chatRec
chatOrder []string
messages map[string][]Message
msgIndex map[string]map[string]int
attachments map[string]Attachment
referenced map[string]struct{}
idem map[string]idemRec
idemTTL time.Duration
last time.Time
}
func New() *Memory {
return &Memory{
users: make(map[string]User),
byName: make(map[string]string),
hashes: make(map[string]string),
chats: make(map[string]*chatRec),
messages: make(map[string][]Message),
msgIndex: make(map[string]map[string]int),
attachments: make(map[string]Attachment),
referenced: make(map[string]struct{}),
idem: make(map[string]idemRec),
idemTTL: 24 * time.Hour,
}
}
// SetIdempotencyTTL меняет срок ключа. Нужен тестам; в PostgreSQL тот же смысл у IDEMPOTENCY_TTL_SECONDS.
func (s *Memory) SetIdempotencyTTL(d time.Duration) {
if d <= 0 {
return
}
s.mu.Lock()
s.idemTTL = d
s.mu.Unlock()
}
func (s *Memory) Ping(ctx context.Context) error {
if err := ctx.Err(); err != nil {
return ErrUnavailable
}
return nil
}
func (s *Memory) CreateUser(ctx context.Context, username, displayName, passwordHash string) (User, error) {
if err := ctx.Err(); err != nil {
return User{}, ErrUnavailable
}
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.byName[username]; ok {
return User{}, ErrConflict
}
u := User{
ID: newID(),
Username: username,
DisplayName: displayName,
CreatedAt: s.nextTimeLocked(),
}
s.users[u.ID] = u
s.byName[username] = u.ID
s.hashes[u.ID] = passwordHash
return u, nil
}
func (s *Memory) User(ctx context.Context, id string) (User, error) {
if err := ctx.Err(); err != nil {
return User{}, ErrUnavailable
}
s.mu.RLock()
defer s.mu.RUnlock()
u, ok := s.users[id]
if !ok {
return User{}, ErrNotFound
}
return u, nil
}
func (s *Memory) UserByUsername(ctx context.Context, username string) (User, string, error) {
if err := ctx.Err(); err != nil {
return User{}, "", ErrUnavailable
}
s.mu.RLock()
defer s.mu.RUnlock()
id, ok := s.byName[username]
if !ok {
return User{}, "", ErrNotFound
}
return s.users[id], s.hashes[id], nil
}
func (s *Memory) CreateChat(ctx context.Context, title string, memberIDs []string, caller string) (Chat, error) {
if err := ctx.Err(); err != nil {
return Chat{}, ErrUnavailable
}
s.mu.Lock()
defer s.mu.Unlock()
seen := make(map[string]struct{}, len(memberIDs))
for _, id := range memberIDs {
if _, dup := seen[id]; dup {
return Chat{}, ErrInvalid
}
seen[id] = struct{}{}
if _, ok := s.users[id]; !ok {
return Chat{}, ErrInvalid
}
}
if _, ok := seen[caller]; !ok {
return Chat{}, ErrInvalid
}
c := Chat{
ID: newID(),
Title: title,
MemberIDs: append([]string(nil), memberIDs...),
CreatedAt: s.nextTimeLocked(),
}
s.chats[c.ID] = &chatRec{chat: c, members: seen}
s.chatOrder = append(s.chatOrder, c.ID)
s.messages[c.ID] = nil
s.msgIndex[c.ID] = make(map[string]int)
return c, nil
}
func (s *Memory) ChatFor(ctx context.Context, chatID, userID string) (Chat, error) {
if err := ctx.Err(); err != nil {
return Chat{}, ErrUnavailable
}
s.mu.RLock()
defer s.mu.RUnlock()
rec, ok := s.chats[chatID]
if !ok {
return Chat{}, ErrNotFound
}
if _, member := rec.members[userID]; !member {
return Chat{}, ErrNotFound
}
return rec.chat.clone(), nil
}
func (s *Memory) ListChats(ctx context.Context, userID string, cur *Cursor, limit int) ([]Chat, bool, error) {
if err := ctx.Err(); err != nil {
return nil, false, ErrUnavailable
}
s.mu.RLock()
defer s.mu.RUnlock()
filtered := make([]Chat, 0)
for _, id := range s.chatOrder {
rec := s.chats[id]
if _, ok := rec.members[userID]; ok {
filtered = append(filtered, rec.chat.clone())
}
}
start, err := indexAfter(filtered, cur, func(c Chat) (string, time.Time) {
return c.ID, c.CreatedAt
})
if err != nil {
return nil, false, err
}
end := start + limit
if end > len(filtered) {
end = len(filtered)
}
return append([]Chat(nil), filtered[start:end]...), end < len(filtered), nil
}
func (s *Memory) AddMessage(ctx context.Context, chatID, senderID, text string, attachmentIDs []string, idemKey, fingerprint string) (Message, error) {
if err := ctx.Err(); err != nil {
return Message{}, ErrUnavailable
}
if idemKey == "" || fingerprint == "" {
return Message{}, ErrInvalid
}
s.mu.Lock()
defer s.mu.Unlock()
rec, ok := s.chats[chatID]
if !ok {
return Message{}, ErrNotFound
}
if _, member := rec.members[senderID]; !member {
return Message{}, ErrNotFound
}
slot := senderID + "\x00" + chatID + "\x00" + idemKey
if prev, ok := s.idem[slot]; ok && prev.expires.After(time.Now()) {
if prev.fingerprint != fingerprint {
return Message{}, ErrIdempotencyMismatch
}
idx, ok := s.msgIndex[chatID][prev.messageID]
if !ok {
return Message{}, ErrNotFound
}
return s.messages[chatID][idx], nil
}
ids := append([]string(nil), attachmentIDs...)
for _, id := range ids {
row, ok := s.attachments[id]
if !ok || row.ChatID != chatID {
return Message{}, ErrNotFound
}
if row.Status != "ready" {
return Message{}, ErrNotReady
}
}
m := Message{
ID: newID(),
ChatID: chatID,
SenderID: senderID,
Text: text,
CreatedAt: s.nextTimeLocked(),
AttachmentIDs: ids,
}
if m.AttachmentIDs == nil {
m.AttachmentIDs = []string{}
}
idx := len(s.messages[chatID])
s.messages[chatID] = append(s.messages[chatID], m)
s.msgIndex[chatID][m.ID] = idx
s.idem[slot] = idemRec{fingerprint: fingerprint, messageID: m.ID, expires: time.Now().Add(s.idemTTL)}
return m, nil
}
func (s *Memory) ListMessages(ctx context.Context, chatID, userID string, cur *Cursor, limit int) ([]Message, bool, error) {
if err := ctx.Err(); err != nil {
return nil, false, ErrUnavailable
}
s.mu.RLock()
defer s.mu.RUnlock()
rec, ok := s.chats[chatID]
if !ok {
return nil, false, ErrNotFound
}
if _, member := rec.members[userID]; !member {
return nil, false, ErrNotFound
}
all := s.messages[chatID]
start := 0
if cur != nil {
idx, ok := s.msgIndex[chatID][cur.ID]
if !ok || !all[idx].CreatedAt.Equal(cur.At) {
return nil, false, ErrBadCursor
}
start = idx + 1
}
end := start + limit
if end > len(all) {
end = len(all)
}
out := append([]Message(nil), all[start:end]...)
return out, end < len(all), nil
}
// nextTimeLocked выдаёт неубывающее время фиксации.
func (s *Memory) nextTimeLocked() time.Time {
now := time.Now().UTC().Truncate(time.Microsecond)
if !now.After(s.last) {
now = s.last.Add(time.Microsecond)
}
s.last = now
return now
}
func indexAfter[T any](items []T, cur *Cursor, key func(T) (string, time.Time)) (int, error) {
if cur == nil {
return 0, nil
}
for i, item := range items {
id, at := key(item)
if id == cur.ID {
if !at.Equal(cur.At) {
return 0, ErrBadCursor
}
return i + 1, nil
}
}
return 0, ErrBadCursor
}
func (c Chat) clone() Chat {
c.MemberIDs = append([]string(nil), c.MemberIDs...)
return c
}
func newID() string {
var b [16]byte
if _, err := rand.Read(b[:]); err != nil {
panic("crypto/rand: " + err.Error())
}
b[6] = (b[6] & 0x0f) | 0x40
b[8] = (b[8] & 0x3f) | 0x80
var buf [36]byte
hex.Encode(buf[0:8], b[0:4])
buf[8] = '-'
hex.Encode(buf[9:13], b[4:6])
buf[13] = '-'
hex.Encode(buf[14:18], b[6:8])
buf[18] = '-'
hex.Encode(buf[19:23], b[8:10])
buf[23] = '-'
hex.Encode(buf[24:36], b[10:16])
return string(buf[:])
}