Skip to content

Commit 2304098

Browse files
committed
feat : Add config option to set developer password for cluster (#2539)
Add option to set developer password for the user. ``` $ crc config set developer-password mypassword $ crc start [...] INFO Adding crc-admin and crc-developer contexts to kubeconfig... Started the OpenShift cluster. The server is accessible via web console at: https://console-openshift-console.apps-crc.testing Log in as user: Username: developer Password: mypassword ``` Signed-off-by: Rohan Kumar <[email protected]>
1 parent e7fee3e commit 2304098

File tree

7 files changed

+213
-3
lines changed

7 files changed

+213
-3
lines changed

cmd/crc/cmd/start.go

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func runStart(ctx context.Context) (*types.StartResult, error) {
7575
NameServer: config.Get(crcConfig.NameServer).AsString(),
7676
PullSecret: cluster.NewInteractivePullSecretLoader(config),
7777
KubeAdminPassword: config.Get(crcConfig.KubeAdminPassword).AsString(),
78+
DeveloperPassword: config.Get(crcConfig.DeveloperPassword).AsString(),
7879
Preset: crcConfig.GetPreset(config),
7980
IngressHTTPPort: config.Get(crcConfig.IngressHTTPPort).AsUInt(),
8081
IngressHTTPSPort: config.Get(crcConfig.IngressHTTPSPort).AsUInt(),

pkg/crc/api/handlers.go

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func getStartConfig(cfg crcConfig.Storage, args client.StartConfig) types.StartC
127127
NameServer: cfg.Get(crcConfig.NameServer).AsString(),
128128
PullSecret: cluster.NewNonInteractivePullSecretLoader(cfg, args.PullSecretFile),
129129
KubeAdminPassword: cfg.Get(crcConfig.KubeAdminPassword).AsString(),
130+
DeveloperPassword: cfg.Get(crcConfig.DeveloperPassword).AsString(),
130131
IngressHTTPPort: cfg.Get(crcConfig.IngressHTTPPort).AsUInt(),
131132
IngressHTTPSPort: cfg.Get(crcConfig.IngressHTTPSPort).AsUInt(),
132133
Preset: crcConfig.GetPreset(cfg),

pkg/crc/cluster/kubeadmin_password.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func UpdateUserPassword(ctx context.Context, ocConfig oc.Config, newKubeAdminPas
5050
return nil
5151
}
5252

53-
logging.Infof("Changing the password for the kubeadmin user")
53+
logging.Infof("Changing the password for the users")
5454
expected, err := getHtpasswd(credentials, externals)
5555
if err != nil {
5656
return err
@@ -60,7 +60,7 @@ func UpdateUserPassword(ctx context.Context, ocConfig oc.Config, newKubeAdminPas
6060
"-n", "openshift-config", "--type", "merge"}
6161
_, stderr, err = ocConfig.RunOcCommandPrivate(cmdArgs...)
6262
if err != nil {
63-
return fmt.Errorf("Failed to update kubeadmin password %v: %s", err, stderr)
63+
return fmt.Errorf("failed to update user passwords %v: %s", err, stderr)
6464
}
6565
return nil
6666
}

pkg/crc/config/settings.go

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const (
2929
ConsentTelemetry = "consent-telemetry"
3030
EnableClusterMonitoring = "enable-cluster-monitoring"
3131
KubeAdminPassword = "kubeadmin-password"
32+
DeveloperPassword = "developer-password"
3233
Preset = "preset"
3334
EnableSharedDirs = "enable-shared-dirs"
3435
SharedDirPassword = "shared-dir-password" // #nosec G101
@@ -134,6 +135,8 @@ func RegisterSettings(cfg *Config) {
134135

135136
cfg.AddSetting(KubeAdminPassword, "", validateString, SuccessfullyApplied,
136137
"User defined kubeadmin password")
138+
cfg.AddSetting(DeveloperPassword, "", validateString, SuccessfullyApplied,
139+
"User defined developer password")
137140
cfg.AddSetting(IngressHTTPPort, constants.OpenShiftIngressHTTPPort, validatePort, RequiresHTTPPortChangeWarning,
138141
fmt.Sprintf("HTTP port to use for OpenShift ingress/routes on the host (1024-65535, default: %d)", constants.OpenShiftIngressHTTPPort))
139142
cfg.AddSetting(IngressHTTPSPort, constants.OpenShiftIngressHTTPSPort, validatePort, RequiresHTTPSPortChangeWarning,

pkg/crc/config/settings_test.go

+202
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,205 @@ func TestPath(t *testing.T) {
182182
IsSecret: false,
183183
}, cfg.Get(ProxyCAFile))
184184
}
185+
186+
func TestWhenInvalidKeySetThenErrorIsThrown(t *testing.T) {
187+
// Given
188+
cfg, err := newInMemoryConfig()
189+
require.NoError(t, err)
190+
191+
// When + Then
192+
_, err = cfg.Set("i-dont-exist", "i-should-not-be-set")
193+
assert.Error(t, err, "Configuration property 'i-dont-exist' does not exist")
194+
}
195+
196+
func TestSetDeveloperPasswordIsSetInConfig(t *testing.T) {
197+
// Given
198+
cfg, err := newInMemoryConfig()
199+
require.NoError(t, err)
200+
201+
// When
202+
_, err = cfg.Set(DeveloperPassword, "secret-developer-password")
203+
require.NoError(t, err)
204+
205+
// Then
206+
assert.Equal(t, SettingValue{
207+
Value: "secret-developer-password",
208+
Invalid: false,
209+
}, cfg.Get(DeveloperPassword))
210+
}
211+
212+
var configDefaultValuesTestArguments = []struct {
213+
key string
214+
defaultValue interface{}
215+
}{
216+
{
217+
KubeAdminPassword, "",
218+
},
219+
{
220+
DeveloperPassword, "",
221+
},
222+
{
223+
CPUs, uint(4),
224+
},
225+
{
226+
Memory, uint(10752),
227+
},
228+
{
229+
DiskSize, 31,
230+
},
231+
{
232+
NameServer, "",
233+
},
234+
{
235+
PullSecretFile, "",
236+
},
237+
{
238+
DisableUpdateCheck, false,
239+
},
240+
{
241+
ExperimentalFeatures, false,
242+
},
243+
{
244+
EmergencyLogin, false,
245+
},
246+
{
247+
PersistentVolumeSize, 15,
248+
},
249+
{
250+
HostNetworkAccess, false,
251+
},
252+
{
253+
HTTPProxy, "",
254+
},
255+
{
256+
HTTPSProxy, "",
257+
},
258+
{
259+
NoProxy, "",
260+
},
261+
{
262+
ProxyCAFile, Path(""),
263+
},
264+
{
265+
EnableClusterMonitoring, false,
266+
},
267+
{
268+
ConsentTelemetry, "",
269+
},
270+
{
271+
IngressHTTPPort, 80,
272+
},
273+
{
274+
IngressHTTPSPort, 443,
275+
},
276+
{
277+
EnableBundleQuayFallback, false,
278+
},
279+
{
280+
Preset, "openshift",
281+
},
282+
}
283+
284+
func TestDefaultKeyValuesSetInConfig(t *testing.T) {
285+
for _, tt := range configDefaultValuesTestArguments {
286+
t.Run(tt.key, func(t *testing.T) {
287+
// Given
288+
cfg, err := newInMemoryConfig()
289+
require.NoError(t, err)
290+
291+
// When + Then
292+
assert.Equal(t, SettingValue{
293+
Value: tt.defaultValue,
294+
Invalid: false,
295+
IsDefault: true,
296+
}, cfg.Get(tt.key))
297+
})
298+
}
299+
}
300+
301+
var configProvidedValuesTestArguments = []struct {
302+
key string
303+
providedValue interface{}
304+
}{
305+
{
306+
KubeAdminPassword, "kubeadmin-secret-password",
307+
},
308+
{
309+
DeveloperPassword, "developer-secret-password",
310+
},
311+
{
312+
CPUs, uint(8),
313+
},
314+
{
315+
Memory, uint(21504),
316+
},
317+
{
318+
DiskSize, 62,
319+
},
320+
{
321+
NameServer, "127.0.0.1",
322+
},
323+
{
324+
DisableUpdateCheck, true,
325+
},
326+
{
327+
ExperimentalFeatures, true,
328+
},
329+
{
330+
EmergencyLogin, true,
331+
},
332+
{
333+
PersistentVolumeSize, 20,
334+
},
335+
{
336+
HTTPProxy, "http://proxy-via-http-proxy-property:3128",
337+
},
338+
{
339+
HTTPSProxy, "https://proxy-via-http-proxy-property:3128",
340+
},
341+
{
342+
NoProxy, "http://no-proxy-property:3128",
343+
},
344+
{
345+
EnableClusterMonitoring, true,
346+
},
347+
{
348+
ConsentTelemetry, "yes",
349+
},
350+
{
351+
IngressHTTPPort, 8080,
352+
},
353+
{
354+
IngressHTTPSPort, 6443,
355+
},
356+
{
357+
EnableBundleQuayFallback, true,
358+
},
359+
{
360+
Preset, "microshift",
361+
},
362+
}
363+
364+
func TestSetProvidedValuesOverrideDefaultValuesInConfig(t *testing.T) {
365+
for _, tt := range configProvidedValuesTestArguments {
366+
t.Run(tt.key, func(t *testing.T) {
367+
368+
// When + Then
369+
370+
// Given
371+
cfg, err := newInMemoryConfig()
372+
require.NoError(t, err)
373+
374+
// When
375+
_, err = cfg.Set(tt.key, tt.providedValue)
376+
require.NoError(t, err)
377+
378+
// Then
379+
assert.Equal(t, SettingValue{
380+
Value: tt.providedValue,
381+
Invalid: false,
382+
IsDefault: false,
383+
}, cfg.Get(tt.key))
384+
})
385+
}
386+
}

pkg/crc/machine/start.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
575575
return nil, errors.Wrap(err, "Failed to update pull secret on the disk")
576576
}
577577

578-
if err := cluster.UpdateKubeAdminUserPassword(ctx, ocConfig, startConfig.KubeAdminPassword); err != nil {
578+
if err := cluster.UpdateUserPassword(ctx, ocConfig, startConfig.KubeAdminPassword, startConfig.DeveloperPassword); err != nil {
579579
return nil, errors.Wrap(err, "Failed to update kubeadmin user password")
580580
}
581581

pkg/crc/machine/types/types.go

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ type StartConfig struct {
2626
// User defined kubeadmin password
2727
KubeAdminPassword string
2828

29+
// User defined developer password
30+
DeveloperPassword string
31+
2932
// Preset
3033
Preset crcpreset.Preset
3134

0 commit comments

Comments
 (0)