From 15d5273b32c930252d37644b0a456b04286dbc06 Mon Sep 17 00:00:00 2001 From: james pickett Date: Thu, 12 Oct 2023 14:27:17 -0700 Subject: [PATCH] add running user in upload request, unexport type --- pkg/debug/checkups/checkups.go | 10 +++++----- pkg/debug/shipper/shipper.go | 25 +++++++++++++++++-------- pkg/debug/shipper/shipper_test.go | 3 ++- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/pkg/debug/checkups/checkups.go b/pkg/debug/checkups/checkups.go index de00876ff..22358602b 100644 --- a/pkg/debug/checkups/checkups.go +++ b/pkg/debug/checkups/checkups.go @@ -257,14 +257,14 @@ func RunDoctor(ctx context.Context, k types.Knapsack, w io.Writer) { } } -type RuntimeEnvironmentType string +type runtimeEnvironmentType string const ( - StandaloneEnviroment RuntimeEnvironmentType = "standalone" - InSituEnvironment RuntimeEnvironmentType = "in situ" + StandaloneEnviroment runtimeEnvironmentType = "standalone" + InSituEnvironment runtimeEnvironmentType = "in situ" ) -func RunFlare(ctx context.Context, k types.Knapsack, flareStream io.WriteCloser, runtimeEnvironment RuntimeEnvironmentType) error { +func RunFlare(ctx context.Context, k types.Knapsack, flareStream io.WriteCloser, runtimeEnvironment runtimeEnvironmentType) error { flare := zip.NewWriter(flareStream) combinedSummary := bytes.Buffer{} @@ -303,7 +303,7 @@ func RunFlare(ctx context.Context, k types.Knapsack, flareStream io.WriteCloser, return close() } -func writeFlareEnv(z *zip.Writer, runtimeEnvironment RuntimeEnvironmentType) error { +func writeFlareEnv(z *zip.Writer, runtimeEnvironment runtimeEnvironmentType) error { if _, err := z.Create(fmt.Sprintf("FLARE_RUNNING_%s", strings.ReplaceAll(strings.ToUpper(string(runtimeEnvironment)), " ", "_"))); err != nil { return fmt.Errorf("making env note file: %s", err) } diff --git a/pkg/debug/shipper/shipper.go b/pkg/debug/shipper/shipper.go index c864e8153..c97239082 100644 --- a/pkg/debug/shipper/shipper.go +++ b/pkg/debug/shipper/shipper.go @@ -12,6 +12,7 @@ import ( "net/http" "net/url" "os" + "os/user" "strings" "sync" "time" @@ -204,20 +205,19 @@ func launcherData(k types.Knapsack, note string) ([]byte, error) { ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() - usernames := "" + var consoleUsers string foundConsoleUsers, err := consoleuser.CurrentUsers(ctx) - switch { case err != nil: - usernames = fmt.Sprintf("error getting current users: %s", err) + consoleUsers = fmt.Sprintf("error getting console users: %s", err) case len(foundConsoleUsers) == 0: - usernames = "no console users found" + consoleUsers = "no console users found" default: - currentUserNames := make([]string, len(foundConsoleUsers)) + consoleUserNames := make([]string, len(foundConsoleUsers)) for i, u := range foundConsoleUsers { - currentUserNames[i] = u.Username + consoleUserNames[i] = u.Username } - usernames = strings.Join(currentUserNames, ", ") + consoleUsers = strings.Join(consoleUserNames, ", ") } hostname, err := os.Hostname() @@ -225,9 +225,18 @@ func launcherData(k types.Knapsack, note string) ([]byte, error) { hostname = fmt.Sprintf("error getting hostname: %s", err) } + runningUser, err := user.Current() + var runningUsername string + if err != nil { + runningUsername = fmt.Sprintf("error getting running user: %s", err) + } else { + runningUsername = runningUser.Username + } + b, err := json.Marshal(map[string]string{ "enroll_secret": enrollSecret(k), - "usernames": usernames, + "console_users": consoleUsers, + "running_user": runningUsername, "hostname": hostname, "note": note, }) diff --git a/pkg/debug/shipper/shipper_test.go b/pkg/debug/shipper/shipper_test.go index 0d5c973ee..8010dbac5 100644 --- a/pkg/debug/shipper/shipper_test.go +++ b/pkg/debug/shipper/shipper_test.go @@ -90,8 +90,9 @@ func TestShip(t *testing.T) { //nolint:paralleltest require.Equal(t, tt.expectSecret, len(data["enroll_secret"]) > 0) require.NotEmpty(t, data["hostname"]) - require.NotEmpty(t, data["usernames"]) require.NotEmpty(t, data["note"]) + require.NotEmpty(t, data["console_users"]) + require.NotEmpty(t, data["running_user"]) urlData := struct { URL string }{