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 11 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
129 changes: 129 additions & 0 deletions cmd/launcher/internal/record_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package internal

import (
"context"
"encoding/json"
"errors"
"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 {
ctx context.Context
Copy link
Contributor

Choose a reason for hiding this comment

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

context should not be embedded in structs. (and probably isn't needed)

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(ctx context.Context, logger log.Logger, k types.Knapsack) *metadataWriter {
return &metadataWriter{
ctx: ctx,
logger: logger,
k: k,
}
}

func (mw *metadataWriter) Ping() {
metadata := newMetadataTemplate()
if err := mw.populateLatestServerData(metadata); err != nil {
level.Error(mw.logger).Log("msg", "unable to collect latest server data, metadata files will be incomplete", "err", err)
zackattack01 marked this conversation as resolved.
Show resolved Hide resolved
}

if err := mw.recordMetadata(metadata); err != nil {
level.Error(mw.logger).Log("msg", "unable to write out metadata files", "err", err)
zackattack01 marked this conversation as resolved.
Show resolved Hide resolved
}
}

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")
}

deviceId, err := store.Get([]byte("device_id"))
if err != nil {
return err
} else if string(deviceId) == "" {
return errors.New("device_id is not yet present in ServerProvidedDataStore")
}
Copy link
Contributor

@James-Pickett James-Pickett Oct 20, 2023

Choose a reason for hiding this comment

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

dont need the else if here since you're returning

Suggested change
if err != nil {
return err
} else if string(deviceId) == "" {
return errors.New("device_id is not yet present in ServerProvidedDataStore")
}
if err != nil {
return err
}
if string(deviceId) == "" {
return errors.New("device_id is not yet present in ServerProvidedDataStore")
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't return the error. If we don't have that data, skip and and try the next piece of data. (Probably log the error)


metadata.DeviceId = string(deviceId)

organizationId, err := store.Get([]byte("organization_id"))
if err != nil {
return err
} else if string(organizationId) == "" {
return errors.New("organization_id is not yet present in ServerProvidedDataStore")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

same

Suggested change
if err != nil {
return err
} else if string(organizationId) == "" {
return errors.New("organization_id is not yet present in ServerProvidedDataStore")
}
if err != nil {
return err
}
if string(organizationId) == "" {
return errors.New("organization_id is not yet present in ServerProvidedDataStore")
}


metadata.OrganizationId = string(organizationId)

munemo, err := store.Get([]byte("munemo"))
if err != nil {
return err
} else if string(munemo) == "" {
return errors.New("munemo is not yet present in ServerProvidedDataStore")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

same ... starting to feel like this should be a little helper func

Suggested change
if err != nil {
return err
} else if string(munemo) == "" {
return errors.New("munemo is not yet present in ServerProvidedDataStore")
}
if err != nil {
return err
}
if string(munemo) == "" {
return errors.New("munemo is not yet present in ServerProvidedDataStore")
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agreed, I will pull these into a helper. thank you!


metadata.OrganizationMunemo = string(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
}
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(ctx, 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