|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +var defaultUrl = "local:user:pass@host" |
| 15 | +var defaultUrlReplaced = fmt.Sprintf("local:user:%s@host", ConfidentialReplacement) |
| 16 | + |
| 17 | +func TestConfidentialHideAll(t *testing.T) { |
| 18 | + value := NewConfidentialValue("val") |
| 19 | + |
| 20 | + assert.Equal(t, value.String(), value.Value()) |
| 21 | + assert.Equal(t, "val", value.String()) |
| 22 | + |
| 23 | + value.hideValue() |
| 24 | + assert.Equal(t, "val", value.Value()) |
| 25 | + assert.Equal(t, ConfidentialReplacement, value.String()) |
| 26 | +} |
| 27 | + |
| 28 | +func TestConfidentialHideSubmatch(t *testing.T) { |
| 29 | + value := NewConfidentialValue("some-vAl-with-sEcRet-parts") |
| 30 | + |
| 31 | + assert.Equal(t, value.String(), value.Value()) |
| 32 | + assert.Equal(t, "some-vAl-with-sEcRet-parts", value.String()) |
| 33 | + |
| 34 | + value.hideSubmatches(regexp.MustCompile("(?i).+(val).+(secret).+")) |
| 35 | + assert.Equal(t, "some-vAl-with-sEcRet-parts", value.Value()) |
| 36 | + |
| 37 | + expected := fmt.Sprintf("some-%s-with-%s-parts", ConfidentialReplacement, ConfidentialReplacement) |
| 38 | + assert.Equal(t, expected, value.String()) |
| 39 | +} |
| 40 | + |
| 41 | +func TestFmtStringDoesntLeakConfidentialValues(t *testing.T) { |
| 42 | + value := NewConfidentialValue("secret") |
| 43 | + value.hideValue() |
| 44 | + |
| 45 | + assert.Equal(t, ConfidentialReplacement, fmt.Sprintf("%s", value)) |
| 46 | + assert.Equal(t, ConfidentialReplacement, fmt.Sprintf("%v", value)) |
| 47 | + assert.Equal(t, ConfidentialReplacement, value.String()) |
| 48 | + assert.Equal(t, "secret", value.Value()) |
| 49 | +} |
| 50 | + |
| 51 | +func TestStringifyPassesConfidentialValues(t *testing.T) { |
| 52 | + value := NewConfidentialValue("secret") |
| 53 | + value.hideValue() |
| 54 | + |
| 55 | + v1, _ := stringifyValue(reflect.ValueOf(value)) |
| 56 | + v2, _ := stringifyConfidentialValue(reflect.ValueOf(value)) |
| 57 | + assert.Equal(t, []string{ConfidentialReplacement}, v1) |
| 58 | + assert.Equal(t, []string{"secret"}, v2) |
| 59 | +} |
| 60 | + |
| 61 | +func TestConfidentialURLs(t *testing.T) { |
| 62 | + // https://restic.readthedocs.io/en/latest/030_preparing_a_new_repo.html |
| 63 | + urls := map[string]string{ |
| 64 | + "local:some/path": "-", |
| 65 | + "sftp:user@host:/srv/restic-repo": "-", |
| 66 | + "sftp://user@[::1]:2222//srv/restic-repo": "-", |
| 67 | + "sftp:restic-backup-host:/srv/restic-repo": "-", |
| 68 | + "rest:http://host:8000/": "-", |
| 69 | + "rest:https://user:1234fdfASDasfwY.-+;@host:8000/": fmt.Sprintf("rest:https://user:%s@host:8000/", ConfidentialReplacement), |
| 70 | + "rest:https://user:35%3Asad%C3%B6p%C3%9F@host:8000/": fmt.Sprintf("rest:https://user:%s@host:8000/", ConfidentialReplacement), |
| 71 | + "rest:https://user:pass@host:8000/f/": fmt.Sprintf("rest:https://user:%s@host:8000/f/", ConfidentialReplacement), |
| 72 | + "s3:s3.amazonaws.com/bucket_name": "-", |
| 73 | + "s3:https://<WASABI-SERVICE-URL>/<WASABI-BUCKET-NAME>": "-", |
| 74 | + "swift:container_name:/path": "-", |
| 75 | + "azure:foo:/": "-", |
| 76 | + "gs:foo:/": "-", |
| 77 | + "rclone:b2prod:yggdrasil/foo/bar/baz": "-", |
| 78 | + } |
| 79 | + |
| 80 | + for url, expected := range urls { |
| 81 | + testConfig := fmt.Sprintf(` |
| 82 | +[profile] |
| 83 | +repository = "%s" |
| 84 | +`, url) |
| 85 | + |
| 86 | + profile, err := getProfile("toml", testConfig, "profile") |
| 87 | + assert.Nil(t, err) |
| 88 | + assert.NotNil(t, profile) |
| 89 | + |
| 90 | + if expected == "-" { |
| 91 | + expected = url |
| 92 | + } |
| 93 | + assert.Equal(t, expected, profile.Repository.String()) |
| 94 | + assert.Equal(t, url, profile.Repository.Value()) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestConfidentialEnvironment(t *testing.T) { |
| 99 | + // https://restic.readthedocs.io/en/latest/030_preparing_a_new_repo.html |
| 100 | + vars := map[string]string{ |
| 101 | + "MY_VALUE": "-", |
| 102 | + "MY_KEY": "*", |
| 103 | + "PASSWORD": "*", |
| 104 | + "MY_URL": "<url>", |
| 105 | + // AWS, MinIO, Wasabi, Alibaba Cloud |
| 106 | + "AWS_ACCESS_KEY_ID": "-", |
| 107 | + "AWS_SECRET_ACCESS_KEY": "*", |
| 108 | + "AWS_DEFAULT_REGION": "-", |
| 109 | + // OpenStack Swift |
| 110 | + "ST_AUTH": "<url>", |
| 111 | + "ST_USER": "-", |
| 112 | + "ST_KEY": "*", |
| 113 | + "OS_AUTH_URL": "<url>", |
| 114 | + "OS_REGION_NAME": "-", |
| 115 | + "OS_USERNAME": "-", |
| 116 | + "OS_PASSWORD": "*", |
| 117 | + "OS_TENANT_ID": "-", |
| 118 | + "OS_TENANT_NAME": "-", |
| 119 | + "OS_USER_ID": "-", |
| 120 | + "OS_USER_DOMAIN_NAME": "-", |
| 121 | + "OS_USER_DOMAIN_ID": "-", |
| 122 | + "OS_PROJECT_NAME": "-", |
| 123 | + "OS_PROJECT_DOMAIN_NAME": "-", |
| 124 | + "OS_PROJECT_DOMAIN_ID": "-", |
| 125 | + "OS_TRUST_ID": "-", |
| 126 | + "OS_APPLICATION_CREDENTIAL_ID": "-", |
| 127 | + "OS_APPLICATION_CREDENTIAL_NAME": "-", |
| 128 | + "OS_APPLICATION_CREDENTIAL_SECRET": "*", |
| 129 | + "OS_STORAGE_URL": "<url>", |
| 130 | + "OS_AUTH_TOKEN": "*", |
| 131 | + "SWIFT_DEFAULT_CONTAINER_POLICY": "-", |
| 132 | + // Backblaze B2 |
| 133 | + "B2_ACCOUNT_ID": "-", |
| 134 | + "B2_ACCOUNT_KEY": "*", |
| 135 | + // Microsoft Azure Blob Storage |
| 136 | + "AZURE_ACCOUNT_NAME": "-", |
| 137 | + "AZURE_ACCOUNT_KEY": "*", |
| 138 | + // Google Cloud Storage |
| 139 | + "GOOGLE_PROJECT_ID": "-", |
| 140 | + "GOOGLE_APPLICATION_CREDENTIALS": "-", |
| 141 | + "GOOGLE_ACCESS_TOKEN": "*", |
| 142 | + } |
| 143 | + |
| 144 | + for name, expected := range vars { |
| 145 | + testConfig := fmt.Sprintf(` |
| 146 | +[profile.env] |
| 147 | +%s = "%s" |
| 148 | +`, name, defaultUrl) |
| 149 | + |
| 150 | + profile, err := getProfile("toml", testConfig, "profile") |
| 151 | + assert.Nil(t, err) |
| 152 | + assert.NotNil(t, profile) |
| 153 | + |
| 154 | + switch expected { |
| 155 | + case "<url>": |
| 156 | + expected = defaultUrlReplaced |
| 157 | + case "*": |
| 158 | + expected = ConfidentialReplacement |
| 159 | + default: |
| 160 | + expected = defaultUrl |
| 161 | + } |
| 162 | + |
| 163 | + name = strings.ToLower(name) |
| 164 | + env := profile.Environment[name] |
| 165 | + assert.Equal(t, expected, env.String()) |
| 166 | + assert.Equal(t, defaultUrl, env.Value()) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func TestShowConfigHidesConfidentialValues(t *testing.T) { |
| 171 | + testConfig := ` |
| 172 | +profile: |
| 173 | + repository: "local:user:pass@host" |
| 174 | + env: |
| 175 | + MY_VALUE: "val" |
| 176 | + MY_URL: "local:user:pass@host" |
| 177 | + MY_KEY: 1234 |
| 178 | + MY_TOKEN: false |
| 179 | + MY_PASSWORD: "otherval" |
| 180 | +` |
| 181 | + profile, err := getProfile("yaml", testConfig, "profile") |
| 182 | + assert.Nil(t, err) |
| 183 | + assert.NotNil(t, profile) |
| 184 | + |
| 185 | + buffer := &bytes.Buffer{} |
| 186 | + assert.Nil(t, ShowStruct(buffer, profile, "p")) |
| 187 | + |
| 188 | + result := regexp.MustCompile("\\s+").ReplaceAllString(buffer.String(), " ") |
| 189 | + result = strings.TrimSpace(result) |
| 190 | + |
| 191 | + assert.Contains(t, result, "my_value: val") |
| 192 | + assert.Contains(t, result, "my_url: "+defaultUrlReplaced) |
| 193 | + assert.Contains(t, result, "my_key: "+ConfidentialReplacement) |
| 194 | + assert.Contains(t, result, "my_token: "+ConfidentialReplacement) |
| 195 | + assert.Contains(t, result, "my_password: "+ConfidentialReplacement) |
| 196 | + assert.Contains(t, result, "repository: "+defaultUrlReplaced) |
| 197 | +} |
| 198 | + |
| 199 | +func TestGetNonConfidentialValues(t *testing.T) { |
| 200 | + testConfig := ` |
| 201 | +profile: |
| 202 | + verbose: false |
| 203 | + repository: "local:user:pass@host" |
| 204 | + env: |
| 205 | + MY_PASSWORD: "otherval" |
| 206 | +` |
| 207 | + profile, err := getProfile("yaml", testConfig, "profile") |
| 208 | + assert.Nil(t, err) |
| 209 | + assert.NotNil(t, profile) |
| 210 | + |
| 211 | + result := GetNonConfidentialValues(profile, []string{"a", defaultUrl, "b", "otherval", "c"}) |
| 212 | + assert.Equal(t, []string{"a", defaultUrlReplaced, "b", ConfidentialReplacement, "c"}, result) |
| 213 | +} |
0 commit comments