Skip to content

Commit dcfdc4a

Browse files
authored
Merge pull request #504 from reubenmiller/fix-encryption-caching-option
fix: encryption passphrase caching option
2 parents f5617c2 + 312b020 commit dcfdc4a

File tree

2 files changed

+16
-81
lines changed

2 files changed

+16
-81
lines changed

pkg/c8ysession/c8ysession.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func WriteOutput(w io.Writer, client *c8y.Client, cfg *config.Config, session *C
158158

159159
shell, isShell := utilities.ShellType.Parse(utilities.ShellBash, format)
160160
if isShell {
161-
output := GetVariablesFromSession(session, client, cfg.AlwaysIncludePassword())
161+
output := GetVariablesFromSession(session, cfg, client, cfg.AlwaysIncludePassword())
162162
utilities.WriteShellVariables(w, output, shell)
163163
return nil
164164
}
@@ -175,7 +175,7 @@ func WriteOutput(w io.Writer, client *c8y.Client, cfg *config.Config, session *C
175175
}
176176
fmt.Fprintf(w, "%s\n", out)
177177
case "env", "dotenv":
178-
output := GetVariablesFromSession(session, client, cfg.AlwaysIncludePassword())
178+
output := GetVariablesFromSession(session, cfg, client, cfg.AlwaysIncludePassword())
179179
for k, v := range output {
180180
if v != "" {
181181
fmt.Fprintf(w, "%s=%s\n", k, v)
@@ -188,7 +188,7 @@ func WriteOutput(w io.Writer, client *c8y.Client, cfg *config.Config, session *C
188188
}
189189

190190
// GetVariablesFromSession gets all the environment variables associated with the current session
191-
func GetVariablesFromSession(session *CumulocitySession, client *c8y.Client, setPassword bool) map[string]interface{} {
191+
func GetVariablesFromSession(session *CumulocitySession, cfg *config.Config, client *c8y.Client, setPassword bool) map[string]interface{} {
192192
host := session.Host
193193
domain := session.GetDomain()
194194
tenant := session.Tenant
@@ -227,6 +227,17 @@ func GetVariablesFromSession(session *CumulocitySession, client *c8y.Client, set
227227
"C8Y_SETTINGS_LOGIN_TYPE": loginType,
228228
}
229229

230+
cache := cfg.CachePassphraseVariables()
231+
cfg.Logger.Debugf("Cache passphrase: %v", cache)
232+
if cache {
233+
if cfg.Passphrase != "" {
234+
output[config.EnvPassphrase] = cfg.Passphrase
235+
}
236+
if cfg.SecretText != "" {
237+
output[config.EnvPassphraseText] = cfg.SecretText
238+
}
239+
}
240+
230241
if loginType != c8y.AuthMethodOAuth2Internal {
231242
output["C8Y_TOKEN"] = ""
232243
}
@@ -240,13 +251,8 @@ func GetVariablesFromSession(session *CumulocitySession, client *c8y.Client, set
240251
return output
241252
}
242253

243-
func ShowClientEnvironmentVariables(cfg *config.Config, c8yclient *c8y.Client, shell utilities.ShellType) {
244-
output := cfg.GetEnvironmentVariables(c8yclient, cfg.AlwaysIncludePassword())
245-
utilities.WriteShellVariables(os.Stdout, output, shell)
246-
}
247-
248254
func ShowSessionEnvironmentVariables(session *CumulocitySession, cfg *config.Config, c8yclient *c8y.Client, shell utilities.ShellType) {
249-
output := GetVariablesFromSession(session, c8yclient, cfg.AlwaysIncludePassword())
255+
output := GetVariablesFromSession(session, cfg, c8yclient, cfg.AlwaysIncludePassword())
250256
utilities.WriteShellVariables(os.Stdout, output, shell)
251257
}
252258

pkg/config/cliConfiguration.go

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ func (c *Config) bindSettings() {
474474
c.viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
475475
c.viper.SetEnvPrefix(EnvSettingsPrefix)
476476
err := c.WithOptions(
477-
WithBindEnv(SettingEncryptionCachePassphrase, true),
477+
WithBindEnv(SettingEncryptionCachePassphrase, false),
478478
WithBindEnv(SettingsMaxWorkers, 50),
479479
WithBindEnv(SettingsWorkers, 1),
480480
WithBindEnv(SettingsIncludeAllPageSize, 2000),
@@ -754,77 +754,6 @@ func (c Config) DecryptAllProperties() (err error) {
754754
return err
755755
}
756756

757-
// GetEnvironmentVariables gets all the environment variables associated with the current session
758-
func (c Config) GetEnvironmentVariables(client *c8y.Client, setPassword bool) map[string]interface{} {
759-
host := c.GetHost()
760-
domain := c.GetDomain()
761-
tenant := c.GetTenant()
762-
c8yVersion := c.GetCumulocityVersion()
763-
username := c.GetUsername()
764-
password := c.MustGetPassword()
765-
token := c.MustGetToken(false)
766-
authHeaderValue := ""
767-
authHeader := ""
768-
769-
if client != nil {
770-
if client.TenantName != "" {
771-
tenant = client.TenantName
772-
}
773-
if client.Username != "" {
774-
username = client.Username
775-
}
776-
if client.BaseURL.Host != "" {
777-
host = client.BaseURL.Scheme + "://" + client.BaseURL.Host
778-
}
779-
if client.Domain != "" {
780-
domain = client.Domain
781-
}
782-
if client.Password != "" {
783-
password = client.Password
784-
}
785-
if client.Token != "" {
786-
token = client.Token
787-
}
788-
if dummyReq, err := client.NewRequest("GET", "/", "", nil); err == nil {
789-
authHeaderValue = dummyReq.Header.Get("Authorization")
790-
authHeader = "Authorization: " + authHeaderValue
791-
}
792-
}
793-
794-
// hide password if it is not needed
795-
if !setPassword && token != "" {
796-
password = ""
797-
}
798-
799-
output := map[string]interface{}{
800-
"C8Y_SESSION": c.GetSessionFile(),
801-
"C8Y_URL": host,
802-
"C8Y_BASEURL": host,
803-
"C8Y_HOST": host,
804-
"C8Y_DOMAIN": domain,
805-
"C8Y_TENANT": tenant,
806-
"C8Y_VERSION": c8yVersion,
807-
"C8Y_USER": username,
808-
"C8Y_TOKEN": token,
809-
"C8Y_USERNAME": username,
810-
"C8Y_PASSWORD": password,
811-
"C8Y_HEADER_AUTHORIZATION": authHeaderValue,
812-
"C8Y_HEADER": authHeader,
813-
}
814-
815-
cache := c.CachePassphraseVariables()
816-
c.Logger.Debugf("Cache passphrase: %v", cache)
817-
if cache {
818-
if c.Passphrase != "" {
819-
output[EnvPassphrase] = c.Passphrase
820-
}
821-
if c.SecretText != "" {
822-
output[EnvPassphraseText] = c.SecretText
823-
}
824-
}
825-
return output
826-
}
827-
828757
// GetEnvKey returns the environment key value associated
829758
func (c Config) GetEnvKey(key string) string {
830759
return "C8Y_" + strings.ToUpper(strings.ReplaceAll(key, ".", "_"))

0 commit comments

Comments
 (0)