From 89e5b8766736ff54efbb9eb1a62f416b6cf48f65 Mon Sep 17 00:00:00 2001 From: reubenmiller Date: Tue, 18 Jun 2024 11:32:08 +0200 Subject: [PATCH 1/2] fix(set-session): preserve encryption related environment variables when setting a session --- pkg/cmd/sessions/set/set.manual.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/sessions/set/set.manual.go b/pkg/cmd/sessions/set/set.manual.go index ae7d676d3..32b06f0db 100644 --- a/pkg/cmd/sessions/set/set.manual.go +++ b/pkg/cmd/sessions/set/set.manual.go @@ -124,7 +124,14 @@ func (n *CmdSet) RunE(cmd *cobra.Command, args []string) error { // the user is most likely switching session so does not want to inherit any environment variables // set from the last instance. // But this has a side effect that you can't control the profile handing via environment variables when using the interact session selection - allowedEnvValues := []string{"C8Y_SETTINGS_SESSION_HIDE"} + allowedEnvValues := []string{ + "C8Y_SETTINGS_SESSION_HIDE", + // Also preserve encryption settings + "C8Y_PASSPHRASE", + "C8Y_PASSPHRASE_TEXT", + "C8Y_SETTINGS_ENCRYPTION_ENABLED", + "C8Y_SETTINGS_ENCRYPTION_CACHEPASSPHRASE", + } env_prefix := strings.ToUpper(config.EnvSettingsPrefix) for _, env := range os.Environ() { if strings.HasPrefix(env, env_prefix) && !strings.HasPrefix(env, config.EnvPassphrase) && !strings.HasPrefix(env, config.EnvSessionHome) { From 3efe05ff663c82d743f3ffa452a74e30e9844a40 Mon Sep 17 00:00:00 2001 From: reubenmiller Date: Tue, 18 Jun 2024 11:35:13 +0200 Subject: [PATCH 2/2] preserve all C8Y_SETTINGS env variables --- pkg/cmd/sessions/set/set.manual.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/sessions/set/set.manual.go b/pkg/cmd/sessions/set/set.manual.go index 32b06f0db..8e60b2acb 100644 --- a/pkg/cmd/sessions/set/set.manual.go +++ b/pkg/cmd/sessions/set/set.manual.go @@ -126,18 +126,16 @@ func (n *CmdSet) RunE(cmd *cobra.Command, args []string) error { // But this has a side effect that you can't control the profile handing via environment variables when using the interact session selection allowedEnvValues := []string{ "C8Y_SETTINGS_SESSION_HIDE", - // Also preserve encryption settings + // Preserve encryption settings "C8Y_PASSPHRASE", "C8Y_PASSPHRASE_TEXT", - "C8Y_SETTINGS_ENCRYPTION_ENABLED", - "C8Y_SETTINGS_ENCRYPTION_CACHEPASSPHRASE", } env_prefix := strings.ToUpper(config.EnvSettingsPrefix) for _, env := range os.Environ() { if strings.HasPrefix(env, env_prefix) && !strings.HasPrefix(env, config.EnvPassphrase) && !strings.HasPrefix(env, config.EnvSessionHome) { parts := strings.SplitN(env, "=", 2) if len(parts) == 2 { - if !slices.Contains(allowedEnvValues, parts[0]) { + if !slices.Contains(allowedEnvValues, parts[0]) && !strings.HasPrefix("C8Y_SETTINGS_", parts[0]) { os.Unsetenv(parts[0]) } }