fleet/server/ptr/ptr.go
Magnus Jensen a8c9e261d7
speed up macOS profile delivery for initial enrollments (#41960)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #34433 

It speeds up the cron, meaning fleetd, bootstrap and now profiles should
be sent within 10 seconds of being known to fleet, compared to the
previous 1 minute.

It's heavily based on my last PR, so the structure and changes are close
to identical, with some small differences.
**I did not do the redis key part in this PR, as I think that should
come in it's own PR, to avoid overlooking logic bugs with that code, and
since this one is already quite sized since we're moving core pieces of
code around.**

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.


## Testing

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


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

* **New Features**
* Faster macOS onboarding: device profiles are delivered and installed
as part of DEP enrollment, shortening initial setup.
* Improved profile handling: per-host profile preprocessing, secret
detection, and clearer failure marking.

* **Improvements**
  * Consolidated SCEP/NDES error messaging for clearer diagnostics.
  * Cron/work scheduling tuned to prioritize Apple MDM profile delivery.

* **Tests**
* Expanded MDM unit and integration tests, including
DeclarativeManagement handling.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-03-19 14:58:10 -05:00

104 lines
2 KiB
Go

// Package ptr includes functions for creating pointers from values.
package ptr
import (
"encoding/json"
"time"
)
// String returns a pointer to the provided string.
func String(x string) *string {
val := new(string)
*val = x
return val
}
// Int returns a pointer to the provided int.
func Int(x int) *int {
return &x
}
// Uint returns a pointer to the provided uint.
func Uint(x uint) *uint {
return &x
}
// UintOrNilIfZero returns nil if the supplied value is zero, else a pointer to the provided uint.
// This is useful for cases that expect nil to be supplied for "No team" instead of zero, and allows for
// a quick way to sidestep e.g. https://github.com/fleetdm/fleet/issues/37729 (which ptr.Uint() would cause).
func UintOrNilIfZero(x uint) *uint {
if x > 0 {
return &x
}
return nil
}
// Bool returns a pointer to the provided bool.
func Bool(x bool) *bool {
return &x
}
// BoolPtr returns a double pointer to the provided bool.
func BoolPtr(x bool) **bool {
p := Bool(x)
return &p
}
func StringPtr(x string) **string {
p := String(x)
return &p
}
// Time returns a pointer to the provided time.Time.
func Time(x time.Time) *time.Time {
val := new(time.Time)
*val = x
return val
}
// TimePtr returns a *time.Time Pointer (**time.Time) for the provided time.
func TimePtr(x time.Time) **time.Time {
t := Time(x)
return &t
}
// RawMessage returns a pointer to the provided json.RawMessage.
func RawMessage(x json.RawMessage) *json.RawMessage {
return &x
}
// Float64 returns a pointer to a float64.
func Float64(x float64) *float64 {
return &x
}
// Float64Ptr returns a pointer to a *float64.
func Float64Ptr(x float64) **float64 {
p := Float64(x)
return &p
}
func Int64(x int64) *int64 {
return &x
}
func Duration(x time.Duration) *time.Duration {
return &x
}
// T is the generic version to get the pointer of any type.
func T[T any](x T) *T {
return &x
}
// ValOrZero returns the value of x if x is not nil, and the zero value
// for T otherwise.
func ValOrZero[T any](x *T) T {
var ret T
if x != nil {
return *x
}
return ret
}