145 lines
4.5 KiB
Go
145 lines
4.5 KiB
Go
package bell_connector
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"regexp"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
|
|
"git.ilapage.cn/ila/yovision/Sense/server/app/sense/integration/machine_identity"
|
|
)
|
|
|
|
const MaxInboundBytes = 64 * 1024
|
|
|
|
var inboundRequestIDPattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._:-]{15,127}$`)
|
|
|
|
type IngressHandler struct {
|
|
DB *gorm.DB
|
|
Verifier machine_identity.Verifier
|
|
EvidenceOwnerID string
|
|
Now func() time.Time
|
|
}
|
|
|
|
func (h IngressHandler) Post(c *gin.Context) {
|
|
if !prepareMachineRequest(c, "/v1/events") {
|
|
return
|
|
}
|
|
body, err := io.ReadAll(http.MaxBytesReader(c.Writer, c.Request.Body, MaxInboundBytes))
|
|
if err != nil {
|
|
problem(c, http.StatusBadRequest, "invalid_event", "event payload is invalid or too large")
|
|
return
|
|
}
|
|
token, err := machine_identity.BearerToken(c.GetHeader("Authorization"))
|
|
if err != nil {
|
|
problem(c, http.StatusUnauthorized, machineCode(err), "machine identity was rejected")
|
|
return
|
|
}
|
|
if _, err = h.Verifier.Verify(token, "yovision-sense", "events:ingest", c.Request.Method, c.Request.URL.EscapedPath(), body); err != nil {
|
|
status := http.StatusUnauthorized
|
|
if code := machineCode(err); code == "machine_scope_denied" || code == "machine_audience_denied" {
|
|
status = http.StatusForbidden
|
|
}
|
|
problem(c, status, machineCode(err), "machine identity was rejected")
|
|
return
|
|
}
|
|
now := time.Now().UTC()
|
|
if h.Now != nil {
|
|
now = h.Now().UTC()
|
|
}
|
|
result, err := AcceptEvent(c.Request.Context(), h.DB, body, h.EvidenceOwnerID, now)
|
|
if errors.Is(err, ErrInboundConflict) {
|
|
problem(c, http.StatusConflict, "idempotency_conflict", "event identity is bound to another payload")
|
|
return
|
|
}
|
|
if err != nil {
|
|
problem(c, http.StatusBadRequest, "invalid_event", "event payload was rejected")
|
|
return
|
|
}
|
|
status := http.StatusAccepted
|
|
if result.Disposition == "duplicate" {
|
|
status = http.StatusOK
|
|
}
|
|
c.JSON(status, result)
|
|
}
|
|
|
|
type EvidenceHandler struct {
|
|
DB *gorm.DB
|
|
Verifier machine_identity.Verifier
|
|
Now func() time.Time
|
|
}
|
|
|
|
func (h EvidenceHandler) Get(c *gin.Context) {
|
|
path := c.Request.URL.EscapedPath()
|
|
if !prepareMachineRequest(c, path) || !safeIdentifier(c.Param("evidence_id")) {
|
|
if !c.Writer.Written() {
|
|
problem(c, http.StatusBadRequest, "evidence_not_found", "evidence reference is invalid")
|
|
}
|
|
return
|
|
}
|
|
token, err := machine_identity.BearerToken(c.GetHeader("Authorization"))
|
|
if err != nil {
|
|
problem(c, http.StatusUnauthorized, machineCode(err), "machine identity was rejected")
|
|
return
|
|
}
|
|
if _, err = h.Verifier.Verify(token, "yovision-sense", "evidence:read", c.Request.Method, path, nil); err != nil {
|
|
status := http.StatusUnauthorized
|
|
if code := machineCode(err); code == "machine_scope_denied" || code == "machine_audience_denied" {
|
|
status = http.StatusForbidden
|
|
}
|
|
problem(c, status, machineCode(err), "machine identity was rejected")
|
|
return
|
|
}
|
|
var record EvidenceRecord
|
|
if err = h.DB.WithContext(c.Request.Context()).First(&record, "evidence_id = ?", c.Param("evidence_id")).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
problem(c, http.StatusNotFound, "evidence_not_found", "evidence reference is unknown")
|
|
return
|
|
}
|
|
problem(c, http.StatusServiceUnavailable, "evidence_unavailable", "evidence metadata is temporarily unavailable")
|
|
return
|
|
}
|
|
now := time.Now().UTC()
|
|
if h.Now != nil {
|
|
now = h.Now().UTC()
|
|
}
|
|
if record.ExpiresAt != nil && !record.ExpiresAt.After(now) {
|
|
problem(c, http.StatusGone, "evidence_expired", "evidence reference has expired")
|
|
return
|
|
}
|
|
c.Data(http.StatusOK, "application/json", record.Payload)
|
|
}
|
|
|
|
func prepareMachineRequest(c *gin.Context, expectedPath string) bool {
|
|
requestID := c.GetHeader("X-Request-ID")
|
|
if requestID == "" {
|
|
requestID = uuid.NewString()
|
|
} else if !inboundRequestIDPattern.MatchString(requestID) {
|
|
problem(c, http.StatusBadRequest, "invalid_request_id", "X-Request-ID is invalid")
|
|
return false
|
|
}
|
|
c.Header("X-Request-ID", requestID)
|
|
if c.Request.URL.RawQuery != "" || c.Request.URL.Fragment != "" || c.Request.URL.EscapedPath() != expectedPath {
|
|
problem(c, http.StatusBadRequest, "invalid_request_target", "request target is invalid")
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func machineCode(err error) string {
|
|
var machineErr *machine_identity.Error
|
|
if errors.As(err, &machineErr) {
|
|
return machineErr.Code
|
|
}
|
|
return "machine_token_invalid"
|
|
}
|
|
|
|
func problem(c *gin.Context, status int, code, message string) {
|
|
c.Header("Content-Type", "application/problem+json")
|
|
c.JSON(status, Problem{Code: code, Message: message})
|
|
}
|