Skip to content

Commit 487db0b

Browse files
authored
Fix #187: homedir/env-vars in repo and other flags (#188)
1 parent b78d1a0 commit 487db0b

File tree

4 files changed

+72
-15
lines changed

4 files changed

+72
-15
lines changed

config/confidential.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ func (c *ConfidentialValue) IsConfidential() bool {
2626
return c.public != c.confidential
2727
}
2828

29+
// setValue updates the Value. The public part is updated if the value is not confidential.
30+
func (c *ConfidentialValue) setValue(value string) {
31+
if !c.IsConfidential() {
32+
c.public = value
33+
}
34+
c.confidential = value
35+
}
36+
2937
// hideValue hides the entire value in the public representation
3038
func (c *ConfidentialValue) hideValue() {
3139
if c.IsConfidential() {

config/confidential_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ func TestConfidentialHideSubmatch(t *testing.T) {
4444
assert.Equal(t, expected, value.String())
4545
}
4646

47+
func TestUpdateConfidentialValue(t *testing.T) {
48+
v := NewConfidentialValue("abc")
49+
50+
v.setValue("xyz")
51+
assert.Equal(t, "xyz", v.Value())
52+
assert.Equal(t, "xyz", v.String())
53+
54+
v.hideSubmatches(regexp.MustCompile("(z)"))
55+
assert.Equal(t, "xy"+ConfidentialReplacement, v.String())
56+
57+
v.setValue("abc")
58+
assert.Equal(t, "abc", v.Value())
59+
assert.Equal(t, "xy"+ConfidentialReplacement, v.String())
60+
}
61+
4762
func TestFmtStringDoesntLeakConfidentialValues(t *testing.T) {
4863
value := NewConfidentialValue("secret")
4964
value.hideValue()

config/profile.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ type InitSection struct {
123123
func (i *InitSection) IsEmpty() bool { return i == nil }
124124

125125
func (i *InitSection) setRootPath(_ *Profile, rootPath string) {
126-
i.FromRepositoryFile = fixPath(i.FromRepositoryFile, expandEnv, absolutePrefix(rootPath))
127-
i.FromPasswordFile = fixPath(i.FromPasswordFile, expandEnv, absolutePrefix(rootPath))
126+
i.FromRepositoryFile = fixPath(i.FromRepositoryFile, expandEnv, expandUserHome, absolutePrefix(rootPath))
127+
i.FromPasswordFile = fixPath(i.FromPasswordFile, expandEnv, expandUserHome, absolutePrefix(rootPath))
128+
i.FromRepository.setValue(fixPath(i.FromRepository.Value(), expandEnv, expandUserHome))
128129
}
129130

130131
func (i *InitSection) getCommandFlags(profile *Profile) (flags *shell.Args) {
@@ -184,10 +185,10 @@ func (b *BackupSection) resolve(p *Profile) {
184185
func (s *BackupSection) setRootPath(p *Profile, rootPath string) {
185186
s.SendMonitoringSections.setRootPath(p, rootPath)
186187

187-
s.ExcludeFile = fixPaths(s.ExcludeFile, expandEnv, absolutePrefix(rootPath))
188-
s.FilesFrom = fixPaths(s.FilesFrom, expandEnv, absolutePrefix(rootPath))
189-
s.Exclude = fixPaths(s.Exclude, expandEnv)
190-
s.Iexclude = fixPaths(s.Iexclude, expandEnv)
188+
s.ExcludeFile = fixPaths(s.ExcludeFile, expandEnv, expandUserHome, absolutePrefix(rootPath))
189+
s.FilesFrom = fixPaths(s.FilesFrom, expandEnv, expandUserHome, absolutePrefix(rootPath))
190+
s.Exclude = fixPaths(s.Exclude, expandEnv, expandUserHome)
191+
s.Iexclude = fixPaths(s.Iexclude, expandEnv, expandUserHome)
191192
}
192193

193194
// RetentionSection contains the specific configuration to
@@ -259,8 +260,9 @@ func (s *CopySection) IsEmpty() bool { return s == nil }
259260
func (c *CopySection) setRootPath(p *Profile, rootPath string) {
260261
c.SendMonitoringSections.setRootPath(p, rootPath)
261262

262-
c.PasswordFile = fixPath(c.PasswordFile, expandEnv, absolutePrefix(rootPath))
263-
c.RepositoryFile = fixPath(c.RepositoryFile, expandEnv, absolutePrefix(rootPath))
263+
c.PasswordFile = fixPath(c.PasswordFile, expandEnv, expandUserHome, absolutePrefix(rootPath))
264+
c.RepositoryFile = fixPath(c.RepositoryFile, expandEnv, expandUserHome, absolutePrefix(rootPath))
265+
c.Repository.setValue(fixPath(c.Repository.Value(), expandEnv, expandUserHome))
264266
}
265267

266268
func (s *CopySection) getInitFlags(profile *Profile) *shell.Args {
@@ -357,7 +359,7 @@ type SendMonitoringSections struct {
357359
func (s *SendMonitoringSections) setRootPath(_ *Profile, rootPath string) {
358360
for _, monitoringSections := range s.getAllSendMonitoringSections() {
359361
for index, value := range monitoringSections {
360-
monitoringSections[index].BodyTemplate = fixPath(value.BodyTemplate, expandEnv, absolutePrefix(rootPath))
362+
monitoringSections[index].BodyTemplate = fixPath(value.BodyTemplate, expandEnv, expandUserHome, absolutePrefix(rootPath))
361363
}
362364
}
363365
}
@@ -489,11 +491,12 @@ func (p *Profile) SetResticVersion(resticVersion string) (err error) {
489491
// SetRootPath changes the path of all the relative paths and files in the configuration
490492
func (p *Profile) SetRootPath(rootPath string) {
491493
p.Lock = fixPath(p.Lock, expandEnv, absolutePrefix(rootPath))
492-
p.PasswordFile = fixPath(p.PasswordFile, expandEnv, absolutePrefix(rootPath))
493-
p.RepositoryFile = fixPath(p.RepositoryFile, expandEnv, absolutePrefix(rootPath))
494-
p.CacheDir = fixPath(p.CacheDir, expandEnv, absolutePrefix(rootPath))
495-
p.CACert = fixPath(p.CACert, expandEnv, absolutePrefix(rootPath))
496-
p.TLSClientCert = fixPath(p.TLSClientCert, expandEnv, absolutePrefix(rootPath))
494+
p.PasswordFile = fixPath(p.PasswordFile, expandEnv, expandUserHome, absolutePrefix(rootPath))
495+
p.RepositoryFile = fixPath(p.RepositoryFile, expandEnv, expandUserHome, absolutePrefix(rootPath))
496+
p.Repository.setValue(fixPath(p.Repository.Value(), expandEnv, expandUserHome))
497+
p.CacheDir = fixPath(p.CacheDir, expandEnv, expandUserHome, absolutePrefix(rootPath))
498+
p.CACert = fixPath(p.CACert, expandEnv, expandUserHome, absolutePrefix(rootPath))
499+
p.TLSClientCert = fixPath(p.TLSClientCert, expandEnv, expandUserHome, absolutePrefix(rootPath))
497500

498501
// Forward to sections accepting paths
499502
for _, s := range GetSectionsWith[relativePath](p) {
@@ -513,7 +516,7 @@ func (p *Profile) SetRootPath(rootPath string) {
513516
if paths, ok := stringifyValueOf(section[flag]); ok && len(paths) > 0 {
514517
for i, path := range paths {
515518
if len(path) > 0 {
516-
paths[i] = fixPath(path, expandEnv, absolutePrefix(rootPath))
519+
paths[i] = fixPath(path, expandEnv, expandUserHome, absolutePrefix(rootPath))
517520
}
518521
}
519522
section[flag] = paths

config/profile_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package config
33
import (
44
"bytes"
55
"fmt"
6+
"math/rand"
7+
"os"
68
"path"
79
"path/filepath"
810
"runtime"
@@ -271,6 +273,35 @@ array2 = ["one", "two"]
271273
})
272274
}
273275

276+
func TestEnvironmentInProfileRepo(t *testing.T) {
277+
runForVersions(t, func(t *testing.T, version, prefix string) {
278+
testConfig := version + `
279+
[` + prefix + `profile]
280+
repository = "~/$TEST_VAR"
281+
password-file = "~/${TEST_VAR}.key"
282+
[` + prefix + `profile.copy]
283+
repository = "~/$TEST_VAR"
284+
[` + prefix + `profile.init]
285+
from-repository = "~/$TEST_VAR"
286+
`
287+
profile, err := getProfile("toml", testConfig, "profile", "")
288+
require.NoError(t, err)
289+
require.NotNil(t, profile)
290+
291+
testVar := fmt.Sprintf("v%d", rand.Int())
292+
require.NoError(t, os.Setenv("TEST_VAR", testVar))
293+
homeDir, err := os.UserHomeDir()
294+
require.NoError(t, err)
295+
repoPath := filepath.ToSlash(filepath.Join(homeDir, testVar))
296+
297+
profile.SetRootPath("/any")
298+
assert.Equal(t, repoPath, filepath.ToSlash(profile.Repository.Value()))
299+
assert.Equal(t, repoPath+".key", filepath.ToSlash(profile.PasswordFile))
300+
assert.Equal(t, repoPath, filepath.ToSlash(profile.Init.FromRepository.Value()))
301+
assert.Equal(t, repoPath, filepath.ToSlash(profile.Copy.Repository.Value()))
302+
})
303+
}
304+
274305
func TestSetRootInProfileUnix(t *testing.T) {
275306
if runtime.GOOS == "windows" {
276307
t.SkipNow()

0 commit comments

Comments
 (0)