-
Notifications
You must be signed in to change notification settings - Fork 253
Set emergency login for core user with random password #3755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
"crypto/x509" | ||
"encoding/json" | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
|
@@ -32,6 +33,7 @@ import ( | |
"github.com/crc-org/crc/pkg/crc/telemetry" | ||
crctls "github.com/crc-org/crc/pkg/crc/tls" | ||
"github.com/crc-org/crc/pkg/libmachine/host" | ||
crcos "github.com/crc-org/crc/pkg/os" | ||
"github.com/crc-org/machine/libmachine/drivers" | ||
libmachinestate "github.com/crc-org/machine/libmachine/state" | ||
"github.com/docker/go-units" | ||
|
@@ -417,6 +419,16 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig) | |
} | ||
logging.Info("CRC VM is running") | ||
|
||
if startConfig.EmergencyLogin { | ||
if err := enableEmergencyLogin(sshRunner); err != nil { | ||
return nil, errors.Wrap(err, "Error enabling emergency login") | ||
} | ||
} else { | ||
if err := disableEmergencyLogin(sshRunner); err != nil { | ||
return nil, errors.Wrap(err, "Error deleting the password for core user") | ||
} | ||
} | ||
|
||
// Post VM start immediately update SSH key and copy kubeconfig to instance | ||
// dir and VM | ||
if err := updateSSHKeyPair(sshRunner); err != nil { | ||
|
@@ -784,6 +796,29 @@ func addNameServerToInstance(sshRunner *crcssh.Runner, ns string) error { | |
return nil | ||
} | ||
|
||
func enableEmergencyLogin(sshRunner *crcssh.Runner) error { | ||
if crcos.FileExists(constants.PasswdFilePath) { | ||
return nil | ||
} | ||
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
b := make([]byte, 8) | ||
for i := range b { | ||
b[i] = charset[rand.Intn(len(charset))] //nolint | ||
} | ||
if err := os.WriteFile(constants.PasswdFilePath, b, 0600); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove on stop and when the setting has been disabled There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gbraad On the stop we shouldnt' remove it otherwise we need to also remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wouldn't it get a new password on restart? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reiterated and now when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't regenerate the ssh key on start/stop/start, I don't think the user password needs to behave differently, so as long as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also don't think this should happen, but it is the behaviour that existed in the PR: "wouldn't it get a new password on restart?" was not something I implied being happy with, but rather an observation of the current suggestion. |
||
return err | ||
} | ||
logging.Infof("Emergency login password for core user is stored to %s", constants.PasswdFilePath) | ||
_, _, err := sshRunner.Run(fmt.Sprintf("sudo passwd core --unlock && echo %s | sudo passwd core --stdin", b)) | ||
return err | ||
} | ||
|
||
func disableEmergencyLogin(sshRunner *crcssh.Runner) error { | ||
defer os.Remove(constants.PasswdFilePath) | ||
_, _, err := sshRunner.RunPrivileged("disable core user password", "passwd", "--lock", "core") | ||
return err | ||
} | ||
|
||
func updateSSHKeyPair(sshRunner *crcssh.Runner) error { | ||
// Read generated public key | ||
publicKey, err := os.ReadFile(constants.GetPublicKeyPath()) | ||
|
Uh oh!
There was an error while loading. Please reload this page.