Files
rclone/fs/configmap_test.go
T
Nick Craig-Wood 6a0f081380 fs: stop writing config for on the fly and environment variable remotes into the config file
When a backend saved config, for example a refreshed OAuth token, for
a remote which wasn't in the config file, a section with just that
value was created in the config file for environment variable remotes
and the config file was rewritten for on the fly remotes.

Config is now only saved if the remote is in the config file.
2026-10-08 10:53:05 +01:00

113 lines
3.3 KiB
Go

package fs
import (
"bytes"
"context"
"log/slog"
"testing"
"github.com/stretchr/testify/assert"
)
// captureLog redirects the fs logger into a buffer and enables debug
// logging for the duration of the test.
func captureLog(t *testing.T) *bytes.Buffer {
var buf bytes.Buffer
oldLogger := logger
t.Cleanup(func() { logger = oldLogger })
SetLogger(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
ci := GetConfig(context.Background())
oldLevel := ci.LogLevel
t.Cleanup(func() { ci.LogLevel = oldLevel })
ci.LogLevel = LogLevelDebug
return &buf
}
// TestConfigEnvVarsRedactsPassword checks that the value of a
// password config option set via the environment
// (RCLONE_CONFIG_backend_option) is not logged (see #5794).
func TestConfigEnvVarsRedactsPassword(t *testing.T) {
t.Setenv("RCLONE_CONFIG_SRC_PASS", "obscured_pass")
t.Setenv("RCLONE_CONFIG_SRC_HOST", "example.com")
buf := captureLog(t)
config := configEnvVars{
configName: "src",
options: Options{
{Name: "pass", IsPassword: true},
{Name: "host"},
},
}
// The value is still returned to the caller...
value, ok := config.Get("pass")
assert.True(t, ok)
assert.Equal(t, "obscured_pass", value)
// ...but not logged
assert.NotContains(t, buf.String(), "obscured_pass")
// Non sensitive values are still logged
buf.Reset()
value, ok = config.Get("host")
assert.True(t, ok)
assert.Equal(t, "example.com", value)
assert.Contains(t, buf.String(), "example.com")
}
// TestConfigEnvVarsDumpAuthShowsPassword checks that with --dump auth
// set the password value is still shown in the log for debugging.
func TestConfigEnvVarsDumpAuthShowsPassword(t *testing.T) {
t.Setenv("RCLONE_CONFIG_SRC_PASS", "obscured_pass")
buf := captureLog(t)
ci := GetConfig(context.Background())
oldDump := ci.Dump
t.Cleanup(func() { ci.Dump = oldDump })
ci.Dump = DumpAuth
config := configEnvVars{
configName: "src",
options: Options{
{Name: "pass", IsPassword: true},
},
}
_, ok := config.Get("pass")
assert.True(t, ok)
assert.Contains(t, buf.String(), "obscured_pass")
}
// TestOptionEnvVarsRedactsPassword checks that the value of a
// password option set via the environment (RCLONE_backend_option) is
// not logged (see #5794).
func TestOptionEnvVarsRedactsPassword(t *testing.T) {
t.Setenv("RCLONE_SFTP_PASS", "obscured_pass")
buf := captureLog(t)
oev := optionEnvVars{
prefix: "sftp",
options: Options{{Name: "pass", IsPassword: true}},
}
value, ok := oev.Get("pass")
assert.True(t, ok)
assert.Equal(t, "obscured_pass", value)
assert.NotContains(t, buf.String(), "obscured_pass")
}
// TestSetConfigFileOnlyExistingSection checks that config is only
// saved to sections which exist in the config file.
func TestSetConfigFileOnlyExistingSection(t *testing.T) {
oldHasSection, oldSet := ConfigFileHasSection, ConfigFileSet
t.Cleanup(func() { ConfigFileHasSection, ConfigFileSet = oldHasSection, oldSet })
ConfigFileHasSection = func(section string) bool { return section == "remote" }
saved := map[string]string{}
ConfigFileSet = func(section, key, value string) error {
saved[section+"."+key] = value
return nil
}
setConfigFile("remote").Set("key", "value")
setConfigFile(":backend").Set("key", "value")
setConfigFile("envremote").Set("key", "value")
assert.Equal(t, map[string]string{"remote.key": "value"}, saved)
}