package httpapi import ( "crypto/sha256" "database/sql" "encoding/base64" "net/http" "time" "github.com/gin-gonic/gin" ) // Instance credentials authorize only this instance, never an entire customer account. func (a *BusinessAPI) portalSnapshot(c *gin.Context) { c.Header("Cache-Control", "private, no-store") c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 4096) var in struct { InstanceID string `json:"instance_id"` Secret string `json:"secret"` } if c.Query("customer_id") != "" || c.ShouldBindJSON(&in) != nil || len(in.InstanceID) != 36 || len(in.Secret) > 128 { apiError(c, 400, "invalid_request", "Invalid portal request") return } raw, err := base64.RawURLEncoding.DecodeString(in.Secret) if err != nil || len(raw) != 32 { apiError(c, 403, "forbidden", "Invalid credentials") return } sum := sha256.Sum256(raw) tx, err := a.db.BeginTx(c, &sql.TxOptions{Isolation: sql.LevelRepeatableRead}) if err != nil { apiError(c, 503, "portal_unavailable", "Portal unavailable") return } defer tx.Rollback() var rowID, actor, owner, productID, licenseID uint64 var stored []byte var customerName, customerState, productName, productCode, instanceState, freeze, licenseState, observed string var active bool var starts, expires, now time.Time var seen, lease sql.NullTime err = tx.QueryRowContext(c, `SELECT i.id,i.created_by,i.customer_id,i.product_id,i.license_id,i.secret_hash,c.name,c.status,p.name,p.code,p.active,i.status,i.freeze_state,l.status,l.starts_at,l.expires_at,i.observed_state,i.last_seen_at,i.lease_expires_at FROM license_instances i JOIN licenses l ON l.id=i.license_id AND l.customer_id=i.customer_id AND l.product_id=i.product_id JOIN customers c ON c.id=i.customer_id JOIN products p ON p.id=i.product_id WHERE i.instance_id=?`, in.InstanceID).Scan(&rowID, &actor, &owner, &productID, &licenseID, &stored, &customerName, &customerState, &productName, &productCode, &active, &instanceState, &freeze, &licenseState, &starts, &expires, &observed, &seen, &lease) if err == sql.ErrNoRows || (err == nil && !bytesEqual(stored, sum[:])) { apiError(c, 403, "forbidden", "Invalid credentials") return } if err != nil { apiError(c, 503, "portal_unavailable", "Portal unavailable") return } if err = tx.QueryRowContext(c, "SELECT UTC_TIMESTAMP(6)").Scan(&now); err != nil { apiError(c, 503, "portal_unavailable", "Portal unavailable") return } state := leaseState(instanceState, freeze, licenseState, customerState, active, starts, expires, now) p, size := page(c) // Bound offset independently of integer overflow and expensive arbitrary scans. if p > 10000 { apiError(c, 400, "invalid_page", "Page out of range") return } var total int if err = tx.QueryRowContext(c, "SELECT COUNT(*) FROM product_releases WHERE product_id=? AND published=1", productID).Scan(&total); err != nil { apiError(c, 503, "portal_unavailable", "Portal unavailable") return } rows, err := tx.QueryContext(c, "SELECT id,version,sha256,size_bytes FROM product_releases WHERE product_id=? AND published=1 ORDER BY id DESC LIMIT ? OFFSET ?", productID, size, (p-1)*size) if err != nil { apiError(c, 503, "portal_unavailable", "Portal unavailable") return } releases := []gin.H{} for rows.Next() { var id, sizeBytes uint64 var version, digest string if err = rows.Scan(&id, &version, &digest, &sizeBytes); err != nil { break } releases = append(releases, gin.H{"id": id, "version": version, "sha256": digest, "size_bytes": sizeBytes, "download_allowed": state == "active"}) } scanErr := rows.Err() rows.Close() if err != nil || scanErr != nil { apiError(c, 503, "portal_unavailable", "Portal unavailable") return } // Audit attributes the client to an instance, not to the provisioning administrator. if _, err = tx.ExecContext(c, `INSERT INTO audit_events(actor_admin_id,action,resource_type,resource_id,details) VALUES(?,'portal.viewed','license_instance',?,JSON_OBJECT('client_actor','instance','customer_id',?))`, actor, rowID, owner); err != nil { apiError(c, 503, "portal_unavailable", "Portal unavailable") return } if err = tx.Commit(); err != nil { apiError(c, 503, "portal_unavailable", "Portal unavailable") return } nullable := func(v sql.NullTime) any { if v.Valid { return v.Time } return nil } c.JSON(200, gin.H{"data": gin.H{"scope": "instance", "customer": gin.H{"id": owner, "name": customerName}, "product": gin.H{"id": productID, "code": productCode, "name": productName}, "license": gin.H{"id": licenseID, "status": licenseState, "starts_at": starts, "expires_at": expires}, "instance": gin.H{"instance_id": in.InstanceID, "status": instanceState, "freeze_state": freeze}, "lease": gin.H{"observed_state": observed, "last_seen_at": nullable(seen), "expires_at": nullable(lease), "current_state": state}, "releases": releases}, "meta": gin.H{"page": p, "page_size": size, "total": total}}) }