mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-11-11 11:57:57 +00:00
Follow-up to #2306 / #2319. With personal observer logins, a migrated observer still is not safe from the shared login: the hook answers `ignore` for the shared login, so EMQX's built-in database lets it publish under any observer's topic, including one whose owner has moved to a personal login. This PR lets an operator close that per migrated observer, before the shared login is removed altogether. ## Changes **users.db v8.** `observer_bindings.confirmed_at`: the moment the owner's personal login first published under that pubkey. - An auto-bind (the owner's first publish) is confirmed at once. - An admin assignment stays unconfirmed until the owner's login publishes under that pubkey; that publish sets it. - Reassigning to another user or unbinding clears it; reassigning to the same owner keeps it. - Migration: existing `auto` rows get `confirmed_at = bound_at`, `admin` rows stay NULL. **Config.** `userManagement.observerLogins.sharedUsernames` (default empty): the broker's shared login names. A name starting with `obs_` is refused at startup. **Authz hook.** For a login in `sharedUsernames`, a publish under `meshcore/<x>/<pubkey>/...` is denied when that pubkey's binding is confirmed, whatever the number of topic segments (the ingestor attributes `meshcore/<x>/<pubkey>` and longer topics to that pubkey). Logged as `publish denied: user=<name> observer=<8 hex>: confirmed owner`. Every other request from a shared login keeps `ignore`, so the built-in database decides as before. Personal logins are unchanged. An orphaned binding (owner account deleted) does not count as confirmed. **Admin.** The observers table of the Observer logins tab gets a Confirmed column; the bindings JSON gets `confirmedAt`. **Docs.** `docs/observer-mqtt-logins.md`: "Locking the shared login out of migrated observers" (what it denies, how to see `confirmedAt`, the risk below). `config.example.json` has the key. ## Risk for operators An observer that is confirmed but still runs a second uploader on the shared login loses that second uploader once its name is in `sharedUsernames`. The deny log line names the observer. ## Tests - `internal/users`: v7 to v8 migration on a real v7 database, auto confirm, admin assignment then owner confirm, non-owner and repeated confirm, reassign clears, orphan reassign. - `cmd/server`: the decision table (confirmed, unconfirmed, unbound, non-publish), topics with 3, 4 and 5+ segments, trailing slash, empty and `client` iata, lowercase key; owner publish confirms, confirm failure still allows and retries; reassign, unbind and user removal clear the flag; the index load against a real store; config validation; the hook and admin endpoints end to end. - `tests/e2e/test-user-management-e2e.js`: the Confirmed column for an auto-bound observer. - Local: `internal/users`, `cmd/server` (two Windows-only tests skipped, as on master) and `cmd/ingestor` green. The E2E assertion was not run locally; CI runs it. ## Not in this PR - Using `confirmed_at` for verified `/neighbors` reports: a separate PR, built on #2300. - Removing the shared login: still an operator step after migration. --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
190 lines
5.3 KiB
Go
190 lines
5.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/meshcore-analyzer/users"
|
|
)
|
|
|
|
const (
|
|
hookSecretHeader = "X-CoreScope-Hook-Secret"
|
|
|
|
// mqttAuthTouchEvery bounds last-auth writes: the broker authenticates on
|
|
// every (re)connect, and users.db has one writer.
|
|
mqttAuthTouchEvery = 10 * time.Minute
|
|
|
|
observerBoundMailPurpose = "observer-bound"
|
|
|
|
// hookBodyLimit caps the broker request body.
|
|
hookBodyLimit = 16 << 10
|
|
)
|
|
|
|
// emqxAuthnReq is the body EMQX's HTTP authentication sends.
|
|
type emqxAuthnReq struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
ClientID string `json:"clientid"`
|
|
}
|
|
|
|
// emqxAuthzReq is the body EMQX's HTTP authorization sends. Action is
|
|
// "publish" or "subscribe".
|
|
type emqxAuthzReq struct {
|
|
Username string `json:"username"`
|
|
Topic string `json:"topic"`
|
|
Action string `json:"action"`
|
|
}
|
|
|
|
// emqxHookResp is the answer: allow, deny or ignore.
|
|
type emqxHookResp struct {
|
|
Result hookResult `json:"result"`
|
|
}
|
|
|
|
// authTouches throttles TouchMQTTAuth per user. It holds at most one entry
|
|
// per login.
|
|
type authTouches struct {
|
|
mu sync.Mutex
|
|
last map[int64]time.Time
|
|
}
|
|
|
|
// due reports whether uid should be written at now, and records it if so.
|
|
func (t *authTouches) due(uid int64, now time.Time) bool {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
if prev, ok := t.last[uid]; ok && now.Sub(prev) < mqttAuthTouchEvery {
|
|
return false
|
|
}
|
|
if t.last == nil {
|
|
t.last = map[int64]time.Time{}
|
|
}
|
|
t.last[uid] = now
|
|
return true
|
|
}
|
|
|
|
// hookAuthed checks the shared secret and decodes the body into v. It has
|
|
// answered the request when it returns false.
|
|
func (s *Server) hookAuthed(w http.ResponseWriter, r *http.Request, v any) bool {
|
|
if !constantTimeEqual(r.Header.Get(hookSecretHeader), s.auth.set.observerLogins.hookSecret) {
|
|
writeError(w, http.StatusUnauthorized, "unauthorized")
|
|
return false
|
|
}
|
|
if s.auth.obsIdx == nil {
|
|
writeError(w, http.StatusServiceUnavailable, "observer logins unavailable")
|
|
return false
|
|
}
|
|
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, hookBodyLimit)).Decode(v); err != nil {
|
|
writeError(w, http.StatusBadRequest, "bad request")
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (s *Server) handleEMQXAuthn(w http.ResponseWriter, r *http.Request) {
|
|
var req emqxAuthnReq
|
|
if !s.hookAuthed(w, r, &req) {
|
|
return
|
|
}
|
|
a := s.auth
|
|
res := a.obsIdx.authenticate(req.Username, req.Password)
|
|
if res == hookAllow {
|
|
if uid, ok := a.obsIdx.userIDFor(req.Username); ok && a.touches.due(uid, time.Now()) {
|
|
a.auditWG.Add(1)
|
|
go func() {
|
|
defer a.auditWG.Done()
|
|
if err := a.st.TouchMQTTAuth(uid, time.Now()); err != nil {
|
|
log.Printf("[observer-logins] touch last auth: user #%d: %v", uid, err)
|
|
}
|
|
}()
|
|
}
|
|
}
|
|
writeJSON(w, emqxHookResp{Result: res})
|
|
}
|
|
|
|
func (s *Server) handleEMQXAuthz(w http.ResponseWriter, r *http.Request) {
|
|
var req emqxAuthzReq
|
|
if !s.hookAuthed(w, r, &req) {
|
|
return
|
|
}
|
|
a := s.auth
|
|
res, pk, created := a.obsIdx.authorize(req.Username, req.Action, req.Topic, func(pk string, uid int64) (*int64, bool, error) {
|
|
return a.st.BindObserver(pk, uid, "auto")
|
|
}, a.st.ConfirmObserver)
|
|
writeJSON(w, emqxHookResp{Result: res})
|
|
if res == hookAllow && created {
|
|
uid, _ := a.obsIdx.userIDFor(req.Username)
|
|
a.auditWG.Add(1)
|
|
go func() {
|
|
defer a.auditWG.Done()
|
|
s.mailAdminsIfKnownObserver(pk, uid)
|
|
}()
|
|
}
|
|
}
|
|
|
|
// observerKnown reports whether the analyzer already has an observer with
|
|
// this pubkey, through the read-only connection.
|
|
func (s *Server) observerKnown(pk string) (bool, error) {
|
|
if s.db == nil {
|
|
return false, nil
|
|
}
|
|
var one int
|
|
err := s.db.conn.QueryRow(`SELECT 1 FROM observers WHERE UPPER(id) = ?`, strings.ToUpper(pk)).Scan(&one)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// mailAdminsIfKnownObserver tells the admins that a user just claimed an
|
|
// observer that was already reporting, so they can check the claim.
|
|
func (s *Server) mailAdminsIfKnownObserver(pk string, uid int64) {
|
|
a := s.auth
|
|
known, err := s.observerKnown(pk)
|
|
if err != nil {
|
|
log.Printf("[observer-logins] observer lookup %s: %v", pk[:8], err)
|
|
return
|
|
}
|
|
if !known {
|
|
return
|
|
}
|
|
claimer, err := a.st.GetByID(uid)
|
|
if err != nil {
|
|
log.Printf("[observer-logins] claimer #%d: %v", uid, err)
|
|
return
|
|
}
|
|
admins, err := a.st.List(users.ListFilter{Role: users.RoleAdmin, Status: users.StatusActive})
|
|
if err != nil {
|
|
log.Printf("[observer-logins] list admins: %v", err)
|
|
return
|
|
}
|
|
for i := range admins {
|
|
adm := &admins[i]
|
|
if adm.EmailBouncing {
|
|
continue
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), notifySendTimeout)
|
|
msg := a.render(adm.Email, adm.DisplayName, observerBoundMailPurpose, mailContent{
|
|
subject: "An existing observer was claimed",
|
|
greeting: "Hello " + adm.DisplayName + ",",
|
|
paragraphs: []string{
|
|
"Observer " + pk[:12] + " was already reporting to " + a.set.baseURL.Host + ". " +
|
|
mailSafeText(claimer.DisplayName) + " (user #" + strconv.FormatInt(uid, 10) + ") has just bound it to their personal MQTT login by publishing as it.",
|
|
"If that is not the owner, reassign the observer.",
|
|
},
|
|
actionLabel: "Users", actionURL: a.set.baseURL.String() + "/#/admin",
|
|
})
|
|
_ = a.sendMail(ctx, adm, observerBoundMailPurpose, msg) // failure logged by sendMail
|
|
cancel()
|
|
}
|
|
}
|