fleet/server/contexts/host/host.go
Victor Lyuboslavsky c88cc953fb
Refactor endpoint_utils for modularization (#36484)
Resolves #37192

Separating generic endpoint_utils middleware logic from domain-specific
business logic. New bounded contexts would share the generic logic and
implement their own domain-specific logic. The two approaches used in
this PR are:
- Use common `platform` types
- Use interfaces

In the next PR we will move `endpointer_utils`, `authzcheck` and
`ratelimit` into `platform` directory.

# Checklist for submitter

- [x] Added changes file

## Testing

- [x] Added/updated tests
- [x] QA'd all new/changed functionality manually



<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Refactor**
* Restructured internal error handling and context management to support
bounded context architecture.
* Improved error context collection and telemetry observability through
a provider-based mechanism.
* Decoupled licensing and authentication concerns into interfaces for
better modularity.

* **Chores**
* Updated internal package dependencies to align with new architectural
boundaries.

<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-12-31 09:12:00 -06:00

54 lines
1.3 KiB
Go

// Package host enables setting and reading
// the current host from context
package host
import (
"context"
"github.com/fleetdm/fleet/v4/server/fleet"
)
type key int
const hostKey key = 0
// NewContext returns a new context carrying the current osquery host.
func NewContext(ctx context.Context, host *fleet.Host) context.Context {
return context.WithValue(ctx, hostKey, host)
}
// FromContext extracts the osquery host from context if present.
func FromContext(ctx context.Context) (*fleet.Host, bool) {
host, ok := ctx.Value(hostKey).(*fleet.Host)
return host, ok
}
// HostAttributeProvider wraps a fleet.Host to provide error context.
// It implements ctxerr.ErrorContextProvider.
type HostAttributeProvider struct {
Host *fleet.Host
}
// GetDiagnosticContext implements ctxerr.ErrorContextProvider
func (p *HostAttributeProvider) GetDiagnosticContext() map[string]any {
if p.Host == nil {
return nil
}
return map[string]any{
"host": map[string]any{
"platform": p.Host.Platform,
"osquery_version": p.Host.OsqueryVersion,
},
}
}
// GetTelemetryContext implements ctxerr.ErrorContextProvider
func (p *HostAttributeProvider) GetTelemetryContext() map[string]any {
if p.Host == nil {
return nil
}
return map[string]any{
"host.hostname": p.Host.Hostname,
"host.id": p.Host.ID,
}
}