Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate the enrollment details on launcher startup #2045

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import (
"github.com/kolide/launcher/pkg/service"
"github.com/kolide/launcher/pkg/traces"
"github.com/kolide/launcher/pkg/traces/exporter"
"go.opentelemetry.io/otel/trace"

"go.etcd.io/bbolt"
)
Expand Down Expand Up @@ -195,6 +196,15 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
flagController := flags.NewFlagController(slogger, stores[storage.AgentFlagsStore], fcOpts...)
k := knapsack.New(stores, flagController, db, multiSlogger, systemMultiSlogger)

details := getEnrollmentDetails(ctx, k, startupSpan, slogger)
if err := k.SetEnrollmentDetails(details); err != nil {
slogger.Log(ctx, slog.LevelError,
"setting enrollment details",
"err", err,
)
}
startupSpan.AddEvent("enrollment_details_set")

// Generate a new run ID
newRunID := k.GetRunID()

Expand Down Expand Up @@ -638,3 +648,46 @@ func runOsqueryVersionCheckAndAddToKnapsack(ctx context.Context, slogger *slog.L
"osqueryd_path", osquerydPath,
)
}

func getEnrollmentDetails(ctx context.Context, k types.Knapsack, startupSpan trace.Span, slogger *slog.Logger) service.EnrollmentDetails {
cesarfda marked this conversation as resolved.
Show resolved Hide resolved
details := osquery.GetRuntimeEnrollDetails()

latestOsquerydPath := k.LatestOsquerydPath(ctx)
if latestOsquerydPath == "" {
slogger.Log(ctx, slog.LevelInfo,
"skipping enrollment osquery details, no osqueryd path, this is probably CI",
)
startupSpan.AddEvent("skipping_enrollment_details")
return details
}

// Add retry mechanism with backoff
if err := backoff.WaitFor(func() error {
err := osquery.GetOsqEnrollDetails(ctx, latestOsquerydPath, &details)
if err != nil {
slogger.Log(ctx, slog.LevelDebug,
"getOsqEnrollDetails failed in backoff",
"err", err,
)
}
return err
}, 30*time.Second, 5*time.Second); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can delay launcher startup for up to 30 seconds -- I think we probably don't want to do that. Maybe we should call getEnrollmentDetails in a goroutine so that it doesn't halt startup? (We would probably want to make knapsack.enrollmentDetails nullable -- var enrollmentDetails *types.EnrollmentDetails -- and then have extension.Enroll wait for those details to be available.)

// Check if details are required via env var
if os.Getenv("LAUNCHER_DEBUG_ENROLL_DETAILS_REQUIRED") == "true" {
slogger.Log(ctx, slog.LevelError,
"enrollment details required but failed to get them",
"err", err,
)
}
cesarfda marked this conversation as resolved.
Show resolved Hide resolved

slogger.Log(ctx, slog.LevelError,
"failed to get osq enrollment details with retries, moving on",
"err", err,
)
traces.SetError(startupSpan, fmt.Errorf("query osq enrollment details: %w", err))
} else {
startupSpan.AddEvent("got_enrollment_details")
}

return details
}
18 changes: 18 additions & 0 deletions ee/agent/knapsack/knapsack.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ import (
"github.com/kolide/launcher/ee/agent/types"
"github.com/kolide/launcher/ee/tuf"
"github.com/kolide/launcher/pkg/log/multislogger"

"go.etcd.io/bbolt"
)

// Package-level runID variable
var runID string

// Package-level enrollmentDetails variable
var enrollmentDetails types.EnrollmentDetails

// type alias Flags, so that we can embed it inside knapsack, as `flags` and not `Flags`
type flags types.Flags

Expand Down Expand Up @@ -239,3 +243,17 @@ func (k *knapsack) CurrentEnrollmentStatus() (types.EnrollmentStatus, error) {

return types.Enrolled, nil
}

func (k *knapsack) SetEnrollmentDetails(details types.EnrollmentDetails) error {
// Only update if there are actual changes
if details != enrollmentDetails {
k.slogger.Logger.DebugContext(context.Background(), "updating enrollment details")
cesarfda marked this conversation as resolved.
Show resolved Hide resolved
enrollmentDetails = details
return nil
}
return nil
}

func (k *knapsack) GetEnrollmentDetails() (types.EnrollmentDetails, error) {
return enrollmentDetails, nil
}
31 changes: 31 additions & 0 deletions ee/agent/types/enrollment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package types

type EnrollmentStatus string

const (
NoEnrollmentKey EnrollmentStatus = "no_enrollment_key"
Unenrolled EnrollmentStatus = "unenrolled"
Enrolled EnrollmentStatus = "enrolled"
Unknown EnrollmentStatus = "unknown"
)

// Move EnrollmentDetails from service package to types
cesarfda marked this conversation as resolved.
Show resolved Hide resolved
type EnrollmentDetails struct {
OSVersion string `json:"os_version"`
OSBuildID string `json:"os_build_id"`
OSPlatform string `json:"os_platform"`
Hostname string `json:"hostname"`
HardwareVendor string `json:"hardware_vendor"`
HardwareModel string `json:"hardware_model"`
HardwareSerial string `json:"hardware_serial"`
OsqueryVersion string `json:"osquery_version"`
LauncherHardwareKey string `json:"launcher_hardware_key"`
LauncherHardwareKeySource string `json:"launcher_hardware_key_source"`
LauncherLocalKey string `json:"launcher_local_key"`
LauncherVersion string `json:"launcher_version"`
OSName string `json:"os_name"`
OSPlatformLike string `json:"os_platform_like"`
GOOS string `json:"goos"`
GOARCH string `json:"goarch"`
HardwareUUID string `json:"hardware_uuid"`
}
4 changes: 4 additions & 0 deletions ee/agent/types/knapsack.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ type Knapsack interface {
CurrentEnrollmentStatus() (EnrollmentStatus, error)
// GetRunID returns the current launcher run ID
GetRunID() string
// GetEnrollmentDetails returns the enrollment details for the launcher installation
GetEnrollmentDetails() (EnrollmentDetails, error)
// SetEnrollmentDetails sets the enrollment details for the launcher installation
SetEnrollmentDetails(details EnrollmentDetails) error
}
90 changes: 0 additions & 90 deletions ee/agent/types/mocks/flags.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading