Skip to content

Commit f780531

Browse files
cfergeaupraveenkumar
authored andcommitted
config: Add config Path type and use it for proxy-ca-file
This allows to convert relative paths to absolute paths at the time the config value is set. fixes #3641
1 parent 9354dd4 commit f780531

File tree

6 files changed

+46
-1
lines changed

6 files changed

+46
-1
lines changed

pkg/crc/api/api_client_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ func TestConfigGetAll(t *testing.T) {
196196
switch v := v.Value.(type) {
197197
case int:
198198
configs[k] = float64(v)
199+
// when config of type Path is converted to JSON it is converted to string
200+
case crcConfig.Path:
201+
configs[k] = string(v)
199202
default:
200203
configs[k] = v
201204
}

pkg/crc/config/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"fmt"
5+
"path/filepath"
56
"reflect"
67

78
"github.com/crc-org/crc/v2/pkg/crc/preset"
@@ -101,6 +102,14 @@ func (c *Config) Set(key string, value interface{}) (string, error) {
101102
if err != nil {
102103
return "", fmt.Errorf(invalidProp, value, key, err)
103104
}
105+
case Path:
106+
path := cast.ToString(value)
107+
path, err := filepath.Abs(path)
108+
if err != nil {
109+
return "", fmt.Errorf(invalidProp, value, key, err)
110+
}
111+
castValue = path
112+
104113
case preset.Preset:
105114
castValue = cast.ToString(value)
106115
default:
@@ -200,6 +209,8 @@ func (c *Config) Get(key string) SettingValue {
200209
}
201210
case string:
202211
value = cast.ToString(value)
212+
case Path:
213+
value = Path(cast.ToString(value))
203214
case bool:
204215
value, err = cast.ToBoolE(value)
205216
if err != nil {

pkg/crc/config/settings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func RegisterSettings(cfg *Config) {
122122
"HTTPS proxy URL (string, like 'https://my-proxy.com:8443')")
123123
cfg.AddSetting(NoProxy, "", validateNoProxy, SuccessfullyApplied,
124124
"Hosts, ipv4 addresses or CIDR which do not use a proxy (string, comma-separated list such as '127.0.0.1,192.168.100.1/24')")
125-
cfg.AddSetting(ProxyCAFile, "", validatePath, SuccessfullyApplied,
125+
cfg.AddSetting(ProxyCAFile, Path(""), validatePath, SuccessfullyApplied,
126126
"Path to an HTTPS proxy certificate authority (CA)")
127127

128128
cfg.AddSetting(EnableClusterMonitoring, false, ValidateBool, SuccessfullyApplied,

pkg/crc/config/settings_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"fmt"
5+
"path/filepath"
56
"testing"
67

78
"github.com/crc-org/crc/v2/pkg/crc/constants"
@@ -158,3 +159,26 @@ func TestUnsetPreset(t *testing.T) {
158159
IsSecret: false,
159160
}, cfg.Get(CPUs))
160161
}
162+
163+
func TestPath(t *testing.T) {
164+
cfg, err := newInMemoryConfig()
165+
require.NoError(t, err)
166+
167+
assert.Equal(t, SettingValue{
168+
Value: Path(""),
169+
Invalid: false,
170+
IsDefault: true,
171+
IsSecret: false,
172+
}, cfg.Get(ProxyCAFile))
173+
174+
_, err = cfg.Set(ProxyCAFile, "testdata/foo.crt")
175+
require.NoError(t, err)
176+
expectedPath, err := filepath.Abs("testdata/foo.crt")
177+
require.NoError(t, err)
178+
assert.Equal(t, SettingValue{
179+
Value: Path(expectedPath),
180+
Invalid: false,
181+
IsDefault: false,
182+
IsSecret: false,
183+
}, cfg.Get(ProxyCAFile))
184+
}

pkg/crc/config/testdata/foo.crt

Whitespace-only changes.

pkg/crc/config/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,10 @@ type RawStorage interface {
5555
Set(key string, value interface{}) error
5656
Unset(key string) error
5757
}
58+
59+
// type Path is used for a setting which is a file path
60+
type Path string
61+
62+
func (p Path) String() string {
63+
return string(p)
64+
}

0 commit comments

Comments
 (0)