Files
pmg/internal/audit/auditor.go
T
Sahil BansalGitHubClaude Opus 4.6devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>Abhisek Dattadevin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
19d735cdd2 feat: emit cloud events for dependency cooldown and host observations (#243)
* feat: emit cloud events for dependency cooldown and host observations (#237)

Wire cooldown blocks and proxy host observations through the cloud sync
pipeline so they appear as telemetry in Control Tower.

- Cooldown blocks emit PACKAGE_DECISION with COOLDOWN_BLOCKED action and
  PmgDependencyCooldown context (publish date, cooldown days, days since
  publish, days remaining)
- Proxy host observations emit HOST_OBSERVATION with PmgHostObservation
  (hostname, method)
- Session summary now includes cooldown_blocked_count
- Updated buf API dependency for new proto schema

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* format file

* fix: add explicit eventlog mapping for EventTypeDependencyCooldown

Follow the existing pattern where every audit event type has an explicit
case in mapEventType and a corresponding constant in the eventlog package.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Apply suggestion from @devin-ai-integration[bot]

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Signed-off-by: Abhisek Datta <abhisek.datta@gmail.com>

---------

Signed-off-by: Abhisek Datta <abhisek.datta@gmail.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Abhisek Datta <abhisek.datta@gmail.com>
Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-05-08 10:01:25 +05:30

140 lines
2.5 KiB
Go

package audit
import (
"context"
"sync"
"time"
"github.com/safedep/dry/log"
)
type session struct {
mu sync.Mutex
startTime time.Time
packageManager string
args []string
totalAnalyzed uint32
allowedCount uint32
blockedCount uint32
confirmedCount uint32
trustedSkipped uint32
insecureBypassed uint32
cooldownBlockedCount uint32
}
type auditor struct {
sinks []Sink
session *session
mu sync.RWMutex
}
func newAuditor(sinks ...Sink) *auditor {
return &auditor{sinks: sinks}
}
func (a *auditor) dispatch(ctx context.Context, event AuditEvent) {
if event.Timestamp.IsZero() {
event.Timestamp = time.Now()
}
for _, s := range a.sinks {
if err := s.Handle(ctx, event); err != nil {
log.Warnf("audit sink error: %v", err)
}
}
}
func (a *auditor) close() error {
var firstErr error
for _, s := range a.sinks {
if err := s.Close(); err != nil && firstErr == nil {
firstErr = err
}
}
return firstErr
}
func (a *auditor) startSession(packageManager string, args []string) {
a.mu.Lock()
defer a.mu.Unlock()
a.session = &session{
startTime: time.Now(),
packageManager: packageManager,
args: args,
}
}
func (a *auditor) getSession() *session {
a.mu.RLock()
defer a.mu.RUnlock()
return a.session
}
func (a *auditor) recordAllowed() {
s := a.getSession()
if s == nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.allowedCount++
s.totalAnalyzed++
}
func (a *auditor) recordBlocked() {
s := a.getSession()
if s == nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.blockedCount++
s.totalAnalyzed++
}
// recordConfirmed tracks that a user confirmed a suspicious package. It does not
// increment totalAnalyzed because the subsequent LogInstallAllowed call for the
// same package already does that.
func (a *auditor) recordConfirmed() {
s := a.getSession()
if s == nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.confirmedCount++
}
func (a *auditor) recordTrustedSkipped() {
s := a.getSession()
if s == nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.trustedSkipped++
s.totalAnalyzed++
}
func (a *auditor) recordInsecureBypassed() {
s := a.getSession()
if s == nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.insecureBypassed++
s.totalAnalyzed++
}
func (a *auditor) recordCooldownBlocked() {
s := a.getSession()
if s == nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.cooldownBlockedCount++
s.totalAnalyzed++
}