2025-12-19 10:41:39 -07:00
|
|
|
package agentapi
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-12-31 16:38:19 -07:00
|
|
|
"time"
|
2025-12-19 10:41:39 -07:00
|
|
|
|
2025-12-31 16:38:19 -07:00
|
|
|
"github.com/google/uuid"
|
|
|
|
|
|
2026-01-08 10:29:41 +04:00
|
|
|
"cdr.dev/slog/v3"
|
2025-12-19 10:41:39 -07:00
|
|
|
agentproto "github.com/coder/coder/v2/agent/proto"
|
2026-01-27 19:11:40 -07:00
|
|
|
"github.com/coder/coder/v2/coderd/boundaryusage"
|
2025-12-19 10:41:39 -07:00
|
|
|
)
|
|
|
|
|
|
2025-12-31 16:38:19 -07:00
|
|
|
type BoundaryLogsAPI struct {
|
2026-01-27 19:11:40 -07:00
|
|
|
Log slog.Logger
|
|
|
|
|
WorkspaceID uuid.UUID
|
|
|
|
|
OwnerID uuid.UUID
|
|
|
|
|
TemplateID uuid.UUID
|
|
|
|
|
TemplateVersionID uuid.UUID
|
|
|
|
|
BoundaryUsageTracker *boundaryusage.Tracker
|
2025-12-31 16:38:19 -07:00
|
|
|
}
|
2025-12-19 10:41:39 -07:00
|
|
|
|
2025-12-31 16:38:19 -07:00
|
|
|
func (a *BoundaryLogsAPI) ReportBoundaryLogs(ctx context.Context, req *agentproto.ReportBoundaryLogsRequest) (*agentproto.ReportBoundaryLogsResponse, error) {
|
2026-01-27 19:11:40 -07:00
|
|
|
var allowed, denied int64
|
|
|
|
|
|
2025-12-31 16:38:19 -07:00
|
|
|
for _, l := range req.Logs {
|
|
|
|
|
var logTime time.Time
|
|
|
|
|
if l.Time != nil {
|
|
|
|
|
logTime = l.Time.AsTime()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch r := l.Resource.(type) {
|
|
|
|
|
case *agentproto.BoundaryLog_HttpRequest_:
|
|
|
|
|
if r.HttpRequest == nil {
|
|
|
|
|
a.Log.Warn(ctx, "empty http request resource",
|
|
|
|
|
slog.F("workspace_id", a.WorkspaceID.String()))
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 19:11:40 -07:00
|
|
|
if l.Allowed {
|
|
|
|
|
allowed++
|
|
|
|
|
} else {
|
|
|
|
|
denied++
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-31 16:38:19 -07:00
|
|
|
fields := []slog.Field{
|
|
|
|
|
slog.F("decision", allowBoolToString(l.Allowed)),
|
|
|
|
|
slog.F("workspace_id", a.WorkspaceID.String()),
|
2026-01-22 08:37:16 -07:00
|
|
|
slog.F("template_id", a.TemplateID.String()),
|
2026-01-22 15:06:02 -07:00
|
|
|
slog.F("template_version_id", a.TemplateVersionID.String()),
|
2025-12-31 16:38:19 -07:00
|
|
|
slog.F("http_method", r.HttpRequest.Method),
|
|
|
|
|
slog.F("http_url", r.HttpRequest.Url),
|
|
|
|
|
slog.F("event_time", logTime.Format(time.RFC3339Nano)),
|
|
|
|
|
}
|
|
|
|
|
if l.Allowed {
|
|
|
|
|
fields = append(fields, slog.F("matched_rule", r.HttpRequest.MatchedRule))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a.Log.With(fields...).Info(ctx, "boundary_request")
|
|
|
|
|
default:
|
|
|
|
|
a.Log.Warn(ctx, "unknown resource type",
|
|
|
|
|
slog.F("workspace_id", a.WorkspaceID.String()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 19:11:40 -07:00
|
|
|
if a.BoundaryUsageTracker != nil && (allowed > 0 || denied > 0) {
|
|
|
|
|
a.BoundaryUsageTracker.Track(a.WorkspaceID, a.OwnerID, allowed, denied)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-31 16:38:19 -07:00
|
|
|
return &agentproto.ReportBoundaryLogsResponse{}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//nolint:revive // This stringifies the boolean argument.
|
|
|
|
|
func allowBoolToString(b bool) string {
|
|
|
|
|
if b {
|
|
|
|
|
return "allow"
|
|
|
|
|
}
|
|
|
|
|
return "deny"
|
2025-12-19 10:41:39 -07:00
|
|
|
}
|