Files
token_thief/tests/scripts/dbcheck/main.go
T

110 lines
2.7 KiB
Go

// Standalone helper for smoke.ps1. It reads CHECK_RIDS (JSON array) and verifies rows in proxy_logs.
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
"git.misaka.ren/M1saka/token_thief/db"
)
type row struct {
RequestID string
Method string
Path string
StatusCode int
RequestTruncated bool
ResponseTruncated bool
IsStream bool
LatencyMS int64
ReqBodyLen int64
RespBodyLen int64
ReqHeaders string
ErrorMsg string
}
func main() {
log.SetFlags(0)
dsn := os.Getenv("CLICKHOUSE_URL")
if dsn == "" {
log.Fatalf("CLICKHOUSE_URL not set")
}
ridsRaw := os.Getenv("CHECK_RIDS")
if ridsRaw == "" {
log.Fatalf("CHECK_RIDS not set")
}
var rids []string
if err := json.Unmarshal([]byte(ridsRaw), &rids); err != nil {
log.Fatalf("parse CHECK_RIDS: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
opts, err := db.ClickHouseOptions(dsn)
if err != nil {
log.Fatalf("connect: %v", err)
}
conn, err := clickhouse.Open(opts)
if err != nil {
log.Fatalf("connect: %v", err)
}
defer conn.Close()
const q = `
SELECT request_id, method, path, status_code,
request_truncated, response_truncated, is_stream, latency_ms,
toInt64(length(request_body)),
toInt64(length(response_body)),
request_headers,
error
FROM proxy_logs WHERE request_id = ?
ORDER BY started_at DESC LIMIT 1`
ok := 0
for _, rid := range rids {
var r row
err := conn.QueryRow(ctx, q, rid).Scan(
&r.RequestID, &r.Method, &r.Path, &r.StatusCode,
&r.RequestTruncated, &r.ResponseTruncated, &r.IsStream, &r.LatencyMS,
&r.ReqBodyLen, &r.RespBodyLen, &r.ReqHeaders, &r.ErrorMsg,
)
if err != nil {
fmt.Printf(" FAIL rid=%s: not found in db (%v)\n", rid, err)
continue
}
ok++
fmt.Printf(" OK rid=%s\n", rid)
fmt.Printf(" method=%s path=%s status=%d latency_ms=%d\n", r.Method, r.Path, r.StatusCode, r.LatencyMS)
fmt.Printf(" req_body=%d B (truncated=%v) resp_body=%d B (truncated=%v) is_stream=%v\n",
r.ReqBodyLen, r.RequestTruncated, r.RespBodyLen, r.ResponseTruncated, r.IsStream)
hasAuth := false
var hm map[string][]string
if err := json.Unmarshal([]byte(r.ReqHeaders), &hm); err == nil {
_, hasAuth = hm["Authorization"]
}
fmt.Printf(" headers has Authorization=%v\n", hasAuth)
if r.ErrorMsg != "" {
fmt.Printf(" error=%s\n", trunc(r.ErrorMsg, 200))
}
}
fmt.Printf("\n found %d / %d\n", ok, len(rids))
if ok != len(rids) {
os.Exit(1)
}
}
func trunc(s string, n int) string {
if len(s) <= n {
return s
}
return s[:n] + "..."
}