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

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

"github.com/groob/plist"
"github.com/kolide/kit/version"
"github.com/kolide/launcher/pkg/agent/types"
"github.com/kolide/launcher/pkg/debug/checkups"
)

type metadata struct {
DeviceId string `json:"device_id" plist:"device_id"`
OrganizationId string `json:"organization_id" plist:"organization_id"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add OrganizationMunemo too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep!

Timestamp string `json:"timestamp" plist:"timestamp"`
Version string `json:"version" plist:"version"`
}

// RecordMetadata writes out both a json and plist (for darwin) file including all information
// in the metadata struct to the root install directory
func RecordMetadata(rootDir string, ctx context.Context, knapsack types.Knapsack) error {
metadataJSONFile := filepath.Join(rootDir, "metadata.json")
sdc := checkups.NewServerDataCheckup(knapsack)
if err := sdc.Run(ctx, io.Discard); err != nil {
return fmt.Errorf("unable to gather metadata, error: %w", err)
}

metadata := metadata{
DeviceId: sdc.DeviceId,
OrganizationId: sdc.OrganizationId,
Timestamp: time.Now().String(),
Version: version.Version().Version,
}

metadataJSON, err := json.MarshalIndent(metadata, "", " ")
if err != nil {
return fmt.Errorf("unable to JSON marshal metadata, error: %w", err)
}

err = os.WriteFile(metadataJSONFile, metadataJSON, 0644)
if err != nil {
zackattack01 marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("unable to write JSON metadata, error: %w", err)
}

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

metadataPlistFile := filepath.Join(rootDir, "metadata.plist")
metadataPlist, err := plist.MarshalIndent(metadata, " ")

if err != nil {
return fmt.Errorf("unable to Plist marshal metadata, error: %w", err)
}

err = os.WriteFile(metadataPlistFile, metadataPlist, 0644)
if err != nil {
zackattack01 marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("unable to write plist metadata, error: %w", err)
}

return nil
}
3 changes: 3 additions & 0 deletions cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ func runLauncher(ctx context.Context, cancel func(), opts *launcher.Options) err
// we expect we're live. Record the version for osquery to
// pickup
internal.RecordLauncherVersion(rootDirectory)
if err = internal.RecordMetadata(rootDirectory, ctx, k); 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.

Are we confident we'll have all this data on a fresh install since it has to come down via control server? Maybe that's okay and it will get written on the next launcher start up? or perhaps we would to do some retry logic in a goroutine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

discussed offline^ my testing may have been flawed here- adding some async retry, thank you!

Copy link
Contributor

Choose a reason for hiding this comment

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

Hrm... if we're really concerned about that, I wonder if we should move the write into the server data consumer.

Copy link
Contributor

@James-Pickett James-Pickett Oct 19, 2023

Choose a reason for hiding this comment

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

yea, this ☝️ is better

Copy link
Contributor

Choose a reason for hiding this comment

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

actually, a subscriber makes more sense

level.Error(logger).Log("msg", "unable to write metadata", "error", err.Error())
zackattack01 marked this conversation as resolved.
Show resolved Hide resolved
}

// create the certificate pool
var rootPool *x509.CertPool
Expand Down
18 changes: 14 additions & 4 deletions pkg/debug/checkups/server_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ var serverProvidedDataKeys = []string{
}

type serverDataCheckup struct {
k types.Knapsack
status Status
summary string
data map[string]any
k types.Knapsack
status Status
summary string
data map[string]any
DeviceId string
OrganizationId string
}

func NewServerDataCheckup(k types.Knapsack) *serverDataCheckup {
return &serverDataCheckup{k: k}
}

func (sdc *serverDataCheckup) Data() any { return sdc.data }
Expand Down Expand Up @@ -48,9 +54,13 @@ func (sdc *serverDataCheckup) Run(ctx context.Context, extraFH io.Writer) error
continue
}

// we set the device and organization ids for individual access by downstream consumers
if key == "device_id" && string(val) != "" {
sdc.DeviceId = string(val)
sdc.status = Passing
sdc.summary = "successfully collected server data"
} else if key == "organization_id" && string(val) != "" {
sdc.OrganizationId = string(val)
}

sdc.data[key] = string(val)
Expand Down
Loading