Skip to content

Commit 100d30a

Browse files
committed
properly restore environment variables to avoid failures in config.ValidateConfig in REST API mode, fix #940
1 parent 56e09db commit 100d30a

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# v2.5.16
2+
BUG FIXES
3+
- allow backup/restore tables and databases which contains additional special characters set, fix [938](https://github.com/Altinity/clickhouse-backup/issues/938)
4+
- properly restore environment variables to avoid failures in config.ValidateConfig in REST API mode, fix [940](https://github.com/Altinity/clickhouse-backup/issues/940)
5+
16
# v2.5.15
27
IMPROVEMENTS
38
- increase `s3_request_timeout_ms` (23.7+) and turn off `s3_use_adaptive_timeouts` (23.11+) when `use_embedded_backup_restore: true`

pkg/common/utils_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package common
2+
3+
import (
4+
"github.com/stretchr/testify/require"
5+
"net/url"
6+
"testing"
7+
)
8+
9+
func TestTablePathEncode(t *testing.T) {
10+
r := require.New(t)
11+
str := `!@#$^&*()+-=[]{}|;':\",./<>?~`
12+
expected := "%21%40%23%24%5E%26%2A%28%29%2B%2D%3D%5B%5D%7B%7D%7C%3B%27%3A%5C%22%2C%2E%2F%3C%3E%3F%7E"
13+
14+
actual := TablePathEncode(str)
15+
r.Equal(expected, actual)
16+
decoded, err := url.PathUnescape(actual)
17+
r.NoError(err)
18+
r.Equal(str, decoded)
19+
}

pkg/config/config.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -645,17 +645,26 @@ func GetConfigPath(ctx *cli.Context) string {
645645
return DefaultConfigPath
646646
}
647647

648-
func OverrideEnvVars(ctx *cli.Context) map[string]string {
648+
type oldEnvValues struct {
649+
OldValue string
650+
WasPresent bool
651+
}
652+
653+
func OverrideEnvVars(ctx *cli.Context) map[string]oldEnvValues {
649654
env := ctx.StringSlice("env")
650-
oldValues := map[string]string{}
655+
oldValues := map[string]oldEnvValues{}
651656
if len(env) > 0 {
652657
for _, v := range env {
653658
envVariable := strings.SplitN(v, "=", 2)
654659
if len(envVariable) < 2 {
655660
envVariable = append(envVariable, "true")
656661
}
657662
log.Infof("override %s=%s", envVariable[0], envVariable[1])
658-
oldValues[envVariable[0]] = os.Getenv(envVariable[0])
663+
oldValue, wasPresent := os.LookupEnv(envVariable[0])
664+
oldValues[envVariable[0]] = oldEnvValues{
665+
OldValue: oldValue,
666+
WasPresent: wasPresent,
667+
}
659668
if err := os.Setenv(envVariable[0], envVariable[1]); err != nil {
660669
log.Warnf("can't override %s=%s, error: %v", envVariable[0], envVariable[1], err)
661670
}
@@ -664,10 +673,16 @@ func OverrideEnvVars(ctx *cli.Context) map[string]string {
664673
return oldValues
665674
}
666675

667-
func RestoreEnvVars(envVars map[string]string) {
668-
for name, value := range envVars {
669-
if err := os.Setenv(name, value); err != nil {
670-
log.Warnf("can't restore %s=%s, error: %v", name, value, err)
676+
func RestoreEnvVars(envVars map[string]oldEnvValues) {
677+
for name, oldEnv := range envVars {
678+
if oldEnv.WasPresent {
679+
if err := os.Setenv(name, oldEnv.OldValue); err != nil {
680+
log.Warnf("RestoreEnvVars can't restore %s=%s, error: %v", name, oldEnv.OldValue, err)
681+
}
682+
} else {
683+
if err := os.Unsetenv(name); err != nil {
684+
log.Warnf("RestoreEnvVars can't delete %s, error: %v", name, err)
685+
}
671686
}
672687
}
673688
}

0 commit comments

Comments
 (0)