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

write out metadata json and plist files to root install directory #1417

Merged
merged 14 commits into from
Oct 23, 2023
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
127 changes: 127 additions & 0 deletions cmd/launcher/internal/record_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package internal

import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/groob/plist"
"github.com/kolide/kit/version"
"github.com/kolide/launcher/pkg/agent/types"
)

type (
// metadataWriter is used as a subscriber for the kolide_server_data
// subsystem. whenever new data is received, it will rewrite the metadata.json
// and metadata.plist files to our root install directory
metadataWriter struct {
logger log.Logger
k types.Knapsack
}
metadata struct {
DeviceId string `json:"device_id" plist:"device_id"`
OrganizationId string `json:"organization_id" plist:"organization_id"`
OrganizationMunemo string `json:"organization_munemo" plist:"organization_munemo"`
Timestamp string `json:"timestamp" plist:"timestamp"`
Version string `json:"version" plist:"version"`
}
)

func NewMetadataWriter(logger log.Logger, k types.Knapsack) *metadataWriter {
return &metadataWriter{
logger: logger,
k: k,
}
}

func (mw *metadataWriter) Ping() {
metadata := newMetadataTemplate()
if err := mw.populateLatestServerData(metadata); err != nil {
level.Debug(mw.logger).Log("msg", "unable to collect latest server data, metadata files will be incomplete", "err", err)
}

if err := mw.recordMetadata(metadata); err != nil {
level.Debug(mw.logger).Log("msg", "unable to write out metadata files", "err", err)
}
}

func newMetadataTemplate() *metadata {
return &metadata{
Timestamp: time.Now().String(),
Version: version.Version().Version,
}
}

func (mw *metadataWriter) populateLatestServerData(metadata *metadata) error {
store := mw.k.ServerProvidedDataStore()

if store == nil {
return errors.New("ServerProvidedDataStore is uninitialized")
}

metadata.DeviceId = mw.getServerDataValue(store, "device_id")
metadata.OrganizationId = mw.getServerDataValue(store, "organization_id")
metadata.OrganizationMunemo = mw.getServerDataValue(store, "munemo")

return nil
}

// recordMetadata writes out both a json and plist (for darwin) file including all information
// in the metadata struct to the root install directory
func (mw *metadataWriter) recordMetadata(metadata *metadata) error {
metadataJSONFile := filepath.Join(mw.k.RootDirectory(), "metadata.json")
metadataJSON, err := json.MarshalIndent(metadata, "", " ")
if err != nil {
return err
}

if err = os.WriteFile(metadataJSONFile, metadataJSON, 0644); err != nil {
return err
}

if runtime.GOOS != "darwin" {
return nil
}

metadataPlistFile := filepath.Join(mw.k.RootDirectory(), "metadata.plist")
metadataPlist, err := plist.MarshalIndent(metadata, " ")

if err != nil {
return err
}

if err = os.WriteFile(metadataPlistFile, metadataPlist, 0644); err != nil {
return err
}

return nil
}

func (mw *metadataWriter) getServerDataValue(store types.GetterSetterDeleterIteratorUpdater, key string) string {
val, err := store.Get([]byte(key))
if err != nil {
level.Debug(mw.logger).Log(
"msg", fmt.Sprintf("unable to collect value for %s from server_data, will re-attempt on next update", key),
"err", err,
)

return ""
}

if string(val) == "" {
level.Debug(mw.logger).Log(
"msg", fmt.Sprintf("server_data was missing value for %s, will re-attempt on next update", key),
"err", err,
)

return ""
}

return string(val)
}
13 changes: 13 additions & 0 deletions cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,19 @@ func runLauncher(ctx context.Context, cancel func(), opts *launcher.Options) err
controlService.RegisterSubscriber(authTokensSubsystemName, logShipper)
controlService.RegisterSubscriber(agentFlagsSubsystemName, logShipper)
}

if metadataWriter := internal.NewMetadataWriter(logger, k); metadataWriter == nil {
level.Debug(logger).Log(
"msg", "unable to set up metadata writer",
"err", err,
)
} else {
controlService.RegisterSubscriber(serverDataSubsystemName, metadataWriter)
// explicitly trigger the ping at least once to ensure updated metadata is written
// on upgrades, the subscriber will continue to do this automatically when new
// information is made available from server_data (e.g. on a fresh install)
metadataWriter.Ping()
}
}

runEECode := k.ControlServerURL() != "" || k.IAmBreakingEELicense()
Expand Down
Loading