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

added new store for uptime history and uptime tracking with knapsack #1923

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ 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)

// start counting uptime
processStartTime := time.Now().UTC()

k.LauncherHistoryStore().Set([]byte("process_start_time"), []byte(processStartTime.Format(time.RFC3339)))

go runOsqueryVersionCheckAndAddToKnapsack(ctx, slogger, k, k.LatestOsquerydPath(ctx))
go timemachine.AddExclusions(ctx, k)

Expand Down
4 changes: 4 additions & 0 deletions ee/agent/knapsack/knapsack.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ func (k *knapsack) TokenStore() types.KVStore {
return k.getKVStore(storage.TokenStore)
}

func (k *knapsack) LauncherHistoryStore() types.KVStore {
return k.getKVStore(storage.LauncherHistoryStore)
}

func (k *knapsack) SetLauncherWatchdogEnabled(enabled bool) error {
return k.flags.SetLauncherWatchdogEnabled(enabled)
}
Expand Down
1 change: 1 addition & 0 deletions ee/agent/storage/bbolt/stores_bbolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func MakeStores(ctx context.Context, slogger *slog.Logger, db *bbolt.DB) (map[st
storage.ServerProvidedDataStore,
storage.TokenStore,
storage.ControlServerActionsStore,
storage.LauncherHistoryStore,
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow. Looking at the stores here, I'm not sure there was an obvious one. But creating a new one to hold a single value feels big. Done now though

}

for _, storeName := range storeNames {
Expand Down
1 change: 1 addition & 0 deletions ee/agent/storage/ci/stores_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func MakeStores(t *testing.T, slogger *slog.Logger, db *bbolt.DB) (map[storage.S
storage.StatusLogsStore,
storage.ServerProvidedDataStore,
storage.TokenStore,
storage.LauncherHistoryStore,
}

if os.Getenv("CI") == "true" {
Expand Down
1 change: 1 addition & 0 deletions ee/agent/storage/stores.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
ServerProvidedDataStore Store = "server_provided_data" // The store used for pushing values from server-backed tables.
TokenStore Store = "token_store" // The store used for holding bearer auth tokens, e.g. the ones used to authenticate with the observability ingest server.
ControlServerActionsStore Store = "action_store" // The store used for storing actions sent by control server.
LauncherHistoryStore Store = "launcher_history" // The store used for storing launcher start time history currently.
)

func (storeType Store) String() string {
Expand Down
110 changes: 20 additions & 90 deletions ee/agent/types/mocks/knapsack.go

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

1 change: 1 addition & 0 deletions ee/agent/types/stores.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ type Stores interface {
StatusLogsStore() KVStore
ServerProvidedDataStore() KVStore
TokenStore() KVStore
LauncherHistoryStore() KVStore
}
24 changes: 19 additions & 5 deletions pkg/osquery/table/launcher_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"os"
"runtime"
"time"

"github.com/kolide/kit/version"
"github.com/kolide/launcher/ee/agent"
Expand All @@ -18,7 +19,7 @@ import (
"github.com/osquery/osquery-go/plugin/table"
)

func LauncherInfoTable(store types.GetterSetter) *table.Plugin {
func LauncherInfoTable(configStore types.GetterSetter, LauncherHistoryStore types.GetterSetter) *table.Plugin {
columns := []table.ColumnDefinition{
table.TextColumn("branch"),
table.TextColumn("build_date"),
Expand All @@ -31,6 +32,7 @@ func LauncherInfoTable(store types.GetterSetter) *table.Plugin {
table.TextColumn("version_chain"),
table.TextColumn("identifier"),
table.TextColumn("osquery_instance_id"),
table.TextColumn("uptime"),

// Signing key info
table.TextColumn("signing_key"),
Expand All @@ -45,12 +47,12 @@ func LauncherInfoTable(store types.GetterSetter) *table.Plugin {
table.TextColumn("fingerprint"),
table.TextColumn("public_key"),
}
return table.NewPlugin("kolide_launcher_info", columns, generateLauncherInfoTable(store))
return table.NewPlugin("kolide_launcher_info", columns, generateLauncherInfoTable(configStore, LauncherHistoryStore))
}

func generateLauncherInfoTable(store types.GetterSetter) table.GenerateFunc {
func generateLauncherInfoTable(configStore types.GetterSetter, LauncherHistoryStore types.GetterSetter) table.GenerateFunc {
return func(ctx context.Context, queryContext table.QueryContext) ([]map[string]string, error) {
identifier, err := osquery.IdentifierFromDB(store)
identifier, err := osquery.IdentifierFromDB(configStore)
if err != nil {
return nil, err
}
Expand All @@ -60,13 +62,24 @@ func generateLauncherInfoTable(store types.GetterSetter) table.GenerateFunc {
return nil, err
}

publicKey, fingerprint, err := osquery.PublicRSAKeyFromDB(store)
publicKey, fingerprint, err := osquery.PublicRSAKeyFromDB(configStore)
if err != nil {
// No logger here, so we can't easily log. Move on with blank values
publicKey = ""
fingerprint = ""
}

uptimeBytes, err := LauncherHistoryStore.Get([]byte("process_start_time"))
if err != nil {
uptimeBytes = nil
}
uptime := "uptime not available"
if uptimeBytes != nil {
if startTime, err := time.Parse(time.RFC3339, string(uptimeBytes)); err == nil {
uptime = fmt.Sprintf("%d", int64(time.Since(startTime).Seconds()))
}
}

results := []map[string]string{
{
"branch": version.Version().Branch,
Expand All @@ -82,6 +95,7 @@ func generateLauncherInfoTable(store types.GetterSetter) table.GenerateFunc {
"osquery_instance_id": osqueryInstance.InstanceId,
"fingerprint": fingerprint,
"public_key": publicKey,
"uptime": uptime,
},
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/osquery/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func LauncherTables(k types.Knapsack) []osquery.OsqueryPlugin {
return []osquery.OsqueryPlugin{
LauncherConfigTable(k.ConfigStore()),
LauncherDbInfo(k.BboltDB()),
LauncherInfoTable(k.ConfigStore()),
LauncherInfoTable(k.ConfigStore(), k.LauncherHistoryStore()),
launcher_db.TablePlugin("kolide_server_data", k.ServerProvidedDataStore()),
launcher_db.TablePlugin("kolide_control_flags", k.AgentFlagsStore()),
LauncherAutoupdateConfigTable(k),
Expand Down
Loading