Skip to content

Commit

Permalink
feat : Generate developer password instead of hardcoded value (crc-or…
Browse files Browse the repository at this point in the history
…g#2539)

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Nov 11, 2024
1 parent e22465a commit 8a1cef2
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cmd/crc/cmd/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func toConsoleClusterConfig(result *client.ConsoleResult) *clusterConfig {
},
DeveloperCredentials: credentials{
Username: "developer",
Password: "developer",
Password: result.ClusterConfig.DeveloperPass,
},
}
}
41 changes: 21 additions & 20 deletions cmd/crc/cmd/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var DummyClusterConfig = types.ClusterConfig{
ClusterCACert: "MIIDODCCAiCgAwIBAgIIRVfCKNUa1wIwDQYJ",
KubeConfig: "/tmp/kubeconfig",
KubeAdminPass: "foobar",
DeveloperPass: "foobar",
ClusterAPI: "https://foo.testing:6443",
WebConsoleURL: "https://console.foo.testing:6443",
ProxyConfig: nil,
Expand Down Expand Up @@ -60,42 +61,42 @@ func TestConsolePlainError(t *testing.T) {
}

func TestConsoleWithPrintCredentialsPlainSuccess(t *testing.T) {
expectedOut := fmt.Sprintf(`To login as a regular user, run 'oc login -u developer -p developer %s'.
expectedOut := fmt.Sprintf(`To login as a regular user, run 'oc login -u developer -p %s %s'.
To login as an admin, run 'oc login -u kubeadmin -p %s %s'
`, fakemachine.DummyClusterConfig.ClusterAPI, fakemachine.DummyClusterConfig.KubeAdminPass, fakemachine.DummyClusterConfig.ClusterAPI)
`, fakemachine.DummyClusterConfig.DeveloperPass, fakemachine.DummyClusterConfig.ClusterAPI, fakemachine.DummyClusterConfig.KubeAdminPass, fakemachine.DummyClusterConfig.ClusterAPI)
out := new(bytes.Buffer)
assert.NoError(t, runConsole(out, setUpClientForConsole(t), false, true, ""))
assert.Equal(t, expectedOut, out.String())
}

func TestConsoleWithPrintCredentialsAndURLPlainSuccess(t *testing.T) {
expectedOut := fmt.Sprintf(`%s
To login as a regular user, run 'oc login -u developer -p developer %s'.
To login as a regular user, run 'oc login -u developer -p %s %s'.
To login as an admin, run 'oc login -u kubeadmin -p %s %s'
`, fakemachine.DummyClusterConfig.WebConsoleURL, fakemachine.DummyClusterConfig.ClusterAPI, fakemachine.DummyClusterConfig.KubeAdminPass, fakemachine.DummyClusterConfig.ClusterAPI)
`, fakemachine.DummyClusterConfig.WebConsoleURL, fakemachine.DummyClusterConfig.DeveloperPass, fakemachine.DummyClusterConfig.ClusterAPI, fakemachine.DummyClusterConfig.KubeAdminPass, fakemachine.DummyClusterConfig.ClusterAPI)
out := new(bytes.Buffer)
assert.NoError(t, runConsole(out, setUpClientForConsole(t), true, true, ""))
assert.Equal(t, expectedOut, out.String())
}

func TestConsoleJSONSuccess(t *testing.T) {
expectedJSONOut := fmt.Sprintf(`{
"success": true,
"clusterConfig": {
"clusterType": "openshift",
"cacert": "%s",
"webConsoleUrl": "%s",
"url": "%s",
"adminCredentials": {
"username": "kubeadmin",
"password": "%s"
},
"developerCredentials": {
"username": "developer",
"password": "developer"
}
}
}`, fakemachine.DummyClusterConfig.ClusterCACert, fakemachine.DummyClusterConfig.WebConsoleURL, fakemachine.DummyClusterConfig.ClusterAPI, fakemachine.DummyClusterConfig.KubeAdminPass)
"success": true,
"clusterConfig": {
"clusterType": "openshift",
"cacert": "%s",
"webConsoleUrl": "%s",
"url": "%s",
"adminCredentials": {
"username": "kubeadmin",
"password": "%s"
},
"developerCredentials": {
"username": "developer",
"password": "%s"
}
}
}`, fakemachine.DummyClusterConfig.ClusterCACert, fakemachine.DummyClusterConfig.WebConsoleURL, fakemachine.DummyClusterConfig.ClusterAPI, fakemachine.DummyClusterConfig.KubeAdminPass, fakemachine.DummyClusterConfig.DeveloperPass)
out := new(bytes.Buffer)
assert.NoError(t, runConsole(out, setUpClientForConsole(t), false, false, jsonFormat))
assert.JSONEq(t, expectedJSONOut, out.String())
Expand Down
1 change: 1 addition & 0 deletions pkg/crc/api/api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func TestStart(t *testing.T) {
ClusterCACert: "MIIDODCCAiCgAwIBAgIIRVfCKNUa1wIwDQYJ",
KubeConfig: "/tmp/kubeconfig",
KubeAdminPass: "foobar",
DeveloperPass: "foobar",
ClusterAPI: "https://foo.testing:6443",
WebConsoleURL: "https://console.foo.testing:6443",
ProxyConfig: nil,
Expand Down
6 changes: 3 additions & 3 deletions pkg/crc/api/api_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ var testCases = []testCase{
// start
{
request: post("start"),
response: jSon(`{"Status":"","ClusterConfig":{"ClusterType":"openshift","ClusterCACert":"MIIDODCCAiCgAwIBAgIIRVfCKNUa1wIwDQYJ","KubeConfig":"/tmp/kubeconfig","KubeAdminPass":"foobar","ClusterAPI":"https://foo.testing:6443","WebConsoleURL":"https://console.foo.testing:6443","ProxyConfig":null},"KubeletStarted":true}`),
response: jSon(`{"Status":"","ClusterConfig":{"ClusterType":"openshift","ClusterCACert":"MIIDODCCAiCgAwIBAgIIRVfCKNUa1wIwDQYJ","KubeConfig":"/tmp/kubeconfig","KubeAdminPass":"foobar","DeveloperPass":"foobar","ClusterAPI":"https://foo.testing:6443","WebConsoleURL":"https://console.foo.testing:6443","ProxyConfig":null},"KubeletStarted":true}`),
},
{
request: get("start"),
response: jSon(`{"Status":"","ClusterConfig":{"ClusterType":"openshift","ClusterCACert":"MIIDODCCAiCgAwIBAgIIRVfCKNUa1wIwDQYJ","KubeConfig":"/tmp/kubeconfig","KubeAdminPass":"foobar","ClusterAPI":"https://foo.testing:6443","WebConsoleURL":"https://console.foo.testing:6443","ProxyConfig":null},"KubeletStarted":true}`),
response: jSon(`{"Status":"","ClusterConfig":{"ClusterType":"openshift","ClusterCACert":"MIIDODCCAiCgAwIBAgIIRVfCKNUa1wIwDQYJ","KubeConfig":"/tmp/kubeconfig","KubeAdminPass":"foobar","DeveloperPass":"foobar","ClusterAPI":"https://foo.testing:6443","WebConsoleURL":"https://console.foo.testing:6443","ProxyConfig":null},"KubeletStarted":true}`),
},

// start with failure
Expand Down Expand Up @@ -273,7 +273,7 @@ var testCases = []testCase{
// webconsoleurl
{
request: get("webconsoleurl"),
response: jSon(`{"ClusterConfig":{"ClusterType":"openshift","ClusterCACert":"MIIDODCCAiCgAwIBAgIIRVfCKNUa1wIwDQYJ","KubeConfig":"/tmp/kubeconfig","KubeAdminPass":"foobar","ClusterAPI":"https://foo.testing:6443","WebConsoleURL":"https://console.foo.testing:6443","ProxyConfig":null},"State":"Running"}`),
response: jSon(`{"ClusterConfig":{"ClusterType":"openshift","ClusterCACert":"MIIDODCCAiCgAwIBAgIIRVfCKNUa1wIwDQYJ","KubeConfig":"/tmp/kubeconfig","KubeAdminPass":"foobar","DeveloperPass":"foobar","ClusterAPI":"https://foo.testing:6443","WebConsoleURL":"https://console.foo.testing:6443","ProxyConfig":null},"State":"Running"}`),
},

// webconsoleurl with failure
Expand Down
26 changes: 14 additions & 12 deletions pkg/crc/cluster/kubeadmin_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ import (
"golang.org/x/crypto/bcrypt"
)

// GenerateKubeAdminUserPassword creates and put updated kubeadmin password to ~/.crc/machine/crc/kubeadmin-password
func GenerateKubeAdminUserPassword() error {
logging.Infof("Generating new password for the kubeadmin user")
kubeAdminPasswordFile := constants.GetKubeAdminPasswordPath()
// GenerateUserPassword creates and put updated kubeadmin password to ~/.crc/machine/crc/kubeadmin-password
func GenerateUserPassword(passwordFile string, user string) error {
logging.Infof("Generating new password for the %s user", user)
kubeAdminPassword, err := GenerateRandomPasswordHash(23)
if err != nil {
return fmt.Errorf("Cannot generate the kubeadmin user password: %w", err)
return fmt.Errorf("cannot generate the %s user password: %w", user, err)
}
return os.WriteFile(kubeAdminPasswordFile, []byte(kubeAdminPassword), 0600)
return os.WriteFile(passwordFile, []byte(kubeAdminPassword), 0600)
}

// UpdateKubeAdminUserPassword updates the htpasswd secret
Expand All @@ -37,12 +36,16 @@ func UpdateKubeAdminUserPassword(ctx context.Context, ocConfig oc.Config, newPas
}
}

kubeAdminPassword, err := GetKubeadminPassword()
kubeAdminPassword, err := GetUserPassword(constants.GetKubeAdminPasswordPath())
if err != nil {
return fmt.Errorf("Cannot read the kubeadmin user password from file: %w", err)
return fmt.Errorf("cannot read the kubeadmin user password from file: %w", err)
}
developerPassword, err := GetUserPassword(constants.GetDeveloperPasswordPath())
if err != nil {
return fmt.Errorf("cannot read the developer user password from file: %w", err)
}
credentials := map[string]string{
"developer": "developer",
"developer": developerPassword,
"kubeadmin": kubeAdminPassword,
}

Expand Down Expand Up @@ -77,9 +80,8 @@ func UpdateKubeAdminUserPassword(ctx context.Context, ocConfig oc.Config, newPas
return nil
}

func GetKubeadminPassword() (string, error) {
kubeAdminPasswordFile := constants.GetKubeAdminPasswordPath()
rawData, err := os.ReadFile(kubeAdminPasswordFile)
func GetUserPassword(passwordFile string) (string, error) {
rawData, err := os.ReadFile(passwordFile)
if err != nil {
return "", err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/crc/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ func GetKubeAdminPasswordPath() string {
return filepath.Join(MachineInstanceDir, DefaultName, "kubeadmin-password")
}

func GetDeveloperPasswordPath() string {
return filepath.Join(MachineInstanceDir, DefaultName, "developer-password")
}

func GetWin32BackgroundLauncherDownloadURL() string {
return fmt.Sprintf(BackgroundLauncherURL,
version.GetWin32BackgroundLauncherVersion())
Expand Down
1 change: 1 addition & 0 deletions pkg/crc/machine/fakemachine/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var DummyClusterConfig = types.ClusterConfig{
ClusterCACert: "MIIDODCCAiCgAwIBAgIIRVfCKNUa1wIwDQYJ",
KubeConfig: "/tmp/kubeconfig",
KubeAdminPass: "foobar",
DeveloperPass: "foobar",
ClusterAPI: "https://foo.testing:6443",
WebConsoleURL: "https://console.foo.testing:6443",
ProxyConfig: nil,
Expand Down
7 changes: 6 additions & 1 deletion pkg/crc/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ func getClusterConfig(bundleInfo *bundle.CrcBundleInfo) (*types.ClusterConfig, e
}, nil
}

kubeadminPassword, err := cluster.GetKubeadminPassword()
kubeadminPassword, err := cluster.GetUserPassword(constants.GetKubeAdminPasswordPath())
if err != nil {
return nil, fmt.Errorf("Error reading kubeadmin password from bundle %v", err)
}
developerPassword, err := cluster.GetUserPassword(constants.GetDeveloperPasswordPath())
if err != nil {
return nil, fmt.Errorf("error reading developer password from bundle %v", err)
}
proxyConfig, err := getProxyConfig(bundleInfo)
if err != nil {
return nil, err
Expand All @@ -38,6 +42,7 @@ func getClusterConfig(bundleInfo *bundle.CrcBundleInfo) (*types.ClusterConfig, e
ClusterCACert: base64.StdEncoding.EncodeToString(clusterCACert),
KubeConfig: bundleInfo.GetKubeConfigPath(),
KubeAdminPass: kubeadminPassword,
DeveloperPass: developerPassword,
WebConsoleURL: fmt.Sprintf("https://%s", bundleInfo.GetAppHostname("console-openshift-console")),
ClusterAPI: fmt.Sprintf("https://%s:6443", bundleInfo.GetAPIHostname()),
ProxyConfig: proxyConfig,
Expand Down
5 changes: 4 additions & 1 deletion pkg/crc/machine/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,9 +682,12 @@ func createHost(machineConfig config.MachineConfig, preset crcPreset.Preset) err
return fmt.Errorf("Error generating ssh key pair: %v", err)
}
if preset == crcPreset.OpenShift || preset == crcPreset.OKD {
if err := cluster.GenerateKubeAdminUserPassword(); err != nil {
if err := cluster.GenerateUserPassword(constants.GetKubeAdminPasswordPath(), "kubeadmin"); err != nil {
return errors.Wrap(err, "Error generating new kubeadmin password")
}
if err := cluster.GenerateUserPassword(constants.GetDeveloperPasswordPath(), "developer"); err != nil {
return errors.Wrap(err, "Error generating new developer password")
}
}
if err := api.SetExists(vm.Name); err != nil {
return fmt.Errorf("Failed to record VM existence: %s", err)
Expand Down
1 change: 1 addition & 0 deletions pkg/crc/machine/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type ClusterConfig struct {
ClusterCACert string
KubeConfig string
KubeAdminPass string
DeveloperPass string
ClusterAPI string
WebConsoleURL string
ProxyConfig *httpproxy.ProxyConfig
Expand Down

0 comments on commit 8a1cef2

Please sign in to comment.