Files
ai/object/record.go
zeekay 6952e90db8 one way for a controller test to be somebody
Four harnesses each built a request, a recorder and a fake session store, then
seeded whatever principal the test wanted to be true. Nothing reads that store
any more, and an identity a test can assert is not an authorisation test — it is
a test of the fake. They are replaced by visit plus one verb, presenting, which
takes the credential a caller actually arrives with: authtest.Bearer for one that
should be accepted, or the literal string a test wants refused.

authtest gains Signing, so the verifier's certificate has ONE installer. Two of
them raced over the same global, and the loser signed against a certificate that
was no longer installed.

principalUser and credentialUser were the same read: one asked for a session
first, the other deliberately bypassed the session because a stale guest in the
process-local store could shadow the real identity. That store is gone, so both
were reading one token and the order between them meant nothing. One function.

principalIsOwnBrand no longer answers yes for any verified token. It short-cut on
"there is a session", which was sound while a session could only exist on its own
brand's host, and became wrong when a session became the token itself — a sibling
brand's bearer took that branch and reached this brand's all-customer view. The
issuer is the only thing that ever named the minting brand, so it is the only
thing asked.
2026-08-17 09:56:36 -07:00

439 lines
13 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
// Portions Copyright 2023 The OpenAgent Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package object
import (
"fmt"
"net/http"
"strings"
"github.com/google/uuid"
"github.com/hanzoai/ai/conf"
"github.com/hanzoai/ai/i18n"
"github.com/hanzoai/ai/util"
"github.com/hanzoai/dbx"
"github.com/zap-proto/zip"
)
var logPostOnly bool
func init() {
logPostOnly = conf.GetConfigBool("logPostOnly")
}
// Recorded reports whether a request with this method is kept. It is the one place
// the logPostOnly rule is read, so a caller can ask before building a record rather
// than after: under logPostOnly a GET is discarded, and composing one costs a geo-IP
// lookup and a provider query that nothing then reads.
//
// It also answers no when there is nowhere to keep one. A record is written to the
// store and the store is opened by boot, so a request served before boot — or in a
// deployment configured without a database, which is a supported mode that answers
// fail-closed — has nothing to write to. AddRecord queries the providers through
// adapter.db on its way, so asking afterwards is a nil dereference on the way to
// discovering there was no point: the request dies in a filter, after it was served,
// over a log row.
func Recorded(method string) bool {
if adapter == nil || adapter.db == nil {
return false
}
return !(logPostOnly && method == "GET")
}
type Record struct {
Id int `db:"pk" json:"id"`
Owner string `json:"owner"`
Name string `json:"name"`
CreatedTime string `json:"createdTime"`
Organization string `json:"organization"`
ClientIp string `json:"clientIp"`
UserAgent string `json:"userAgent"`
User string `json:"user"`
Method string `json:"method"`
RequestUri string `json:"requestUri"`
Action string `json:"action"`
Language string `json:"language"`
Query string `json:"query"`
Region string `json:"region"`
City string `json:"city"`
Unit string `json:"unit"`
Section string `json:"section"`
Object string `json:"object"`
Response string `json:"response"`
ErrorText string `json:"errorText"`
// ExtendedUser *User `db:"-" json:"extendedUser"`
Provider string `json:"provider"`
Block string `json:"block"`
BlockHash string `json:"blockHash"`
Transaction string `json:"transaction"`
Provider2 string `json:"provider2"`
Block2 string `json:"block2"`
BlockHash2 string `json:"blockHash2"`
Transaction2 string `json:"transaction2"`
// For cross-chain records
Count int `json:"count"`
IsTriggered bool `json:"isTriggered"`
// NO `db:"index"`. dbx's `db` tag is the COLUMN NAME, not a directive —
// parseTag (dbx struct.go:229) special-cases only "pk" and "pk,<name>" and
// returns everything else verbatim as the name. So `db:"index"` did not
// index anything; it named this column `index` (a reserved SQL word), while
// ScanNeedCommitRecords queries `need_commit`. Live effect: every 5 minutes,
// forever, `no such column: need_commit` — the record-chain commit task has
// never committed a record. Untagged, the name is DefaultFieldMapFunc's
// snake_case, which is the `need_commit` the query already asks for.
NeedCommit bool `json:"needCommit"`
}
type Response struct {
Status string `json:"status"`
Msg string `json:"msg"`
}
func GetRecordCount(owner, field, value string) (int64, error) {
session := GetDbQuery(owner, -1, -1, field, value, "", "")
return queryCount(session, "record")
}
func GetRecords(owner string) ([]*Record, error) {
records := []*Record{}
err := findAll(adapter.db, "record", &records, dbx.HashExp{"owner": owner}, "id DESC")
if err != nil {
return records, err
}
return records, nil
}
func getAllRecords() ([]*Record, error) {
records := []*Record{}
err := findAll(adapter.db, "record", &records, dbx.HashExp{}, "id DESC")
if err != nil {
return records, err
}
return records, nil
}
func getValidAndNeedCommitRecords(records []*Record) ([]*Record, []int, []interface{}, error) {
providerFirst, providerSecond, err := GetTwoActiveBlockchainProvider("admin")
if err != nil {
return nil, nil, nil, err
}
var validRecords []*Record
var needCommitIdx []int
var data []interface{}
recordTime := util.GetCurrentTimeWithMilli()
for i, record := range records {
ok, err := prepareRecord(record, providerFirst, providerSecond)
if err != nil {
return nil, nil, nil, err
}
if !ok {
continue
}
record.CreatedTime = util.GetCurrentTimeBasedOnLastMilli(recordTime)
recordTime = record.CreatedTime
validRecords = append(validRecords, record)
data = append(data, map[string]interface{}{"name": record.Name})
if record.NeedCommit {
needCommitIdx = append(needCommitIdx, i)
}
}
return validRecords, needCommitIdx, data, nil
}
func GetPaginationRecords(owner string, offset, limit int, field, value, sortField, sortOrder string) ([]*Record, error) {
records := []*Record{}
session := GetDbQuery(owner, offset, limit, field, value, sortField, sortOrder)
err := queryFind(session, "record", &records)
if err != nil {
return records, err
}
return records, nil
}
// GetRecord retrieves a record by its ID or owner/name format.
func GetRecord(id string, lang string) (*Record, error) {
record := &Record{}
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, fmt.Errorf("%s", fmt.Sprintf(i18n.Translate(lang, "object:failed to parse record identifier '%s': neither a valid owner/[id|name] format"), id))
}
// Try to parse as integer ID first
if recordId, err := util.ParseIntWithError(name); err == nil && recordId > 0 {
// Valid integer ID
record.Id = recordId
} else {
record.Owner = owner
record.Name = name
}
var existed bool
if record.Id != 0 {
existed, err = getOne(adapter.db, "record", record, pkID(record.Id))
} else {
existed, err = getOne(adapter.db, "record", record, pk2(record.Owner, record.Name))
}
if err != nil {
return nil, fmt.Errorf("%s", fmt.Sprintf(i18n.Translate(lang, "object:failed to get record with id '%s': %w"), id, err))
}
if existed {
return record, nil
}
return nil, nil
}
func prepareRecord(record *Record, providerFirst, providerSecond *Provider) (bool, error) {
if !Recorded(record.Method) {
return false, nil
}
if strings.HasSuffix(record.Action, "-record") {
return false, nil
}
if strings.HasSuffix(record.Action, "-record-second") {
return false, nil
}
if strings.HasSuffix(record.Action, "-records") {
return false, nil
}
if record.Provider == "" {
if providerFirst != nil {
record.Provider = providerFirst.Name
}
if providerSecond != nil {
record.Provider2 = providerSecond.Name
}
}
record.Id = 0
record.Name = uuid.NewString()
record.Owner = record.Organization
// Set default count to 1 if not set
if record.Count == 0 {
record.Count = 1
}
return true, nil
}
func UpdateRecord(id string, record *Record, lang string) (bool, error) {
p, err := GetRecord(id, lang)
if err != nil {
return false, err
} else if p == nil {
return false, nil
}
// Update provider
if record.Provider != p.Provider {
record.Block = ""
record.BlockHash = ""
record.Transaction = ""
}
if record.Provider2 != p.Provider2 {
record.Block2 = ""
record.BlockHash2 = ""
record.Transaction2 = ""
}
record.Id = int(p.Id)
err = adapter.db.Model(record).Update()
affected := int64(1)
if err != nil {
affected = 0
}
if err != nil {
return false, err
}
return affected != 0, nil
}
func UpdateRecordInternal(id int, record Record) error {
err := adapter.db.Model(&record).Update()
if err != nil {
return err
}
return nil
}
func UpdateRecordFields(id string, fields map[string]interface{}, lang string) (bool, error) {
p, err := GetRecord(id, lang)
if err != nil {
return false, err
} else if p == nil {
return false, nil
}
affected, err := updateByPK(adapter.db, "record", pkID(int(p.Id)), dbx.Params(fields))
if err != nil {
return false, err
}
return affected != 0, nil
}
func NewRecord(ctx *zip.Ctx) (*Record, error) {
ip := strings.Replace(util.GetIPInfo(ctx.Fiber().IP()), ": ", "", -1)
action := strings.TrimPrefix(ctx.Path(), "/v1/")
requestUri := util.FilterQuery(string(ctx.Fiber().Request().RequestURI()), []string{"accessToken"})
if len(requestUri) > 1000 {
requestUri = requestUri[0:1000]
}
object := ""
if body := ctx.Body(); len(body) != 0 {
object = string(body)
}
status, msg := "ok", ""
if code := ctx.Fiber().Response().StatusCode(); code >= 400 {
status, msg = "error", http.StatusText(code)
}
language := ctx.Header("Accept-Language")
if len(language) > 2 {
language = language[0:2]
}
languageCode := conf.GetLanguage(language)
// get location info from client ip
locationInfo, err := util.GetInfoFromIP(ip)
if err != nil {
return nil, err
}
region := locationInfo.Country
city := locationInfo.City
record := Record{
Name: uuid.NewString(),
CreatedTime: util.GetCurrentTimeWithMilli(),
ClientIp: ip,
User: "",
Method: ctx.Method(),
RequestUri: requestUri,
Action: action,
Language: languageCode,
Region: region,
City: city,
Object: object,
Response: fmt.Sprintf("{\"status\":\"%s\",\"msg\":\"%s\"}", status, msg),
Count: 1,
IsTriggered: false,
}
return &record, nil
}
func AddRecord(record *Record, lang string) (bool, interface{}, error) {
providerFirst, providerSecond, err := GetTwoActiveBlockchainProvider(record.Owner)
if err != nil {
return false, nil, err
}
ok, err := prepareRecord(record, providerFirst, providerSecond)
if err != nil {
return false, nil, err
}
if !ok {
return false, nil, nil
}
record.CreatedTime = util.GetCurrentTimeWithMilli()
// Set default count to 1 if not set
if record.Count == 0 {
record.Count = 1
}
err = insertRow(adapter.db, record)
affected := int64(1)
if err != nil {
affected = 0
}
if err != nil {
return false, nil, err
}
data := map[string]interface{}{"name": record.Name}
if record.NeedCommit {
_, commitResult, err := CommitRecord(record, lang)
if err != nil {
data["error_text"] = err.Error()
} else {
data = commitResult
}
}
return affected != 0, data, nil
}
func AddRecords(records []*Record, syncEnabled bool, lang string) (bool, interface{}, error) {
if len(records) == 0 {
return false, nil, nil
}
validRecords, needCommitRecordsIdx, data, err := getValidAndNeedCommitRecords(records)
if err != nil {
return false, nil, err
}
if len(validRecords) == 0 {
return false, nil, nil
}
totalAffected := int64(0)
err = adapter.db.Transactional(func(tx *dbx.Tx) error {
batchSize := 150
for i := 0; i < len(validRecords); i += batchSize {
end := min(i+batchSize, len(validRecords))
batch := validRecords[i:end]
for _, r := range batch {
if err := tx.Model(r).Insert(); err != nil {
return err
}
totalAffected++
}
}
return nil
})
if err != nil {
return false, nil, err
}
// Send commit event for records that need to be committed
if len(needCommitRecordsIdx) > 0 {
if syncEnabled {
var needCommitRecords []*Record
for _, idx := range needCommitRecordsIdx {
needCommitRecords = append(needCommitRecords, records[idx])
}
_, commitResults := CommitRecords(needCommitRecords, lang)
for i, idx := range needCommitRecordsIdx {
data[idx] = commitResults[i]
}
} else {
go ScanNeedCommitRecords()
}
}
return totalAffected != 0, data, nil
}
func DeleteRecord(record *Record) (bool, error) {
affected, err := deleteByPK(adapter.db, "record", pkID(int(record.Id)))
if err != nil {
return false, err
}
return affected != 0, nil
}
func (record *Record) getUniqueId() string {
return fmt.Sprintf("%s/%d", record.Owner, record.Id)
}
func (record *Record) getId() string {
return fmt.Sprintf("%s/%s", record.Owner, record.Name)
}
func (r *Record) updateErrorText(errText string, lang string) (bool, error) {
r.ErrorText = errText
if r.Id != 0 {
affected, err := updateCols(adapter.db, "record", pk2(r.Owner, r.Name), dbx.Params{"error_text": r.ErrorText})
if err != nil {
return affected > 0, fmt.Errorf("%s", fmt.Sprintf(i18n.Translate(lang, "object:failed to update error text for record %s: %s"), r.getId(), err))
}
return affected > 0, nil
} else {
affected, err := updateCols(adapter.db, "record", pkID(r.Id), dbx.Params{"error_text": r.ErrorText})
if err != nil {
return affected > 0, fmt.Errorf("%s", fmt.Sprintf(i18n.Translate(lang, "object:failed to update error text for record %s: %s"), r.getUniqueId(), err))
}
return affected > 0, nil
}
}