Skip to content
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

Set emergency login for core user with random password #3755

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/crc/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func runStart(ctx context.Context) (*types.StartResult, error) {
IngressHTTPPort: config.Get(crcConfig.IngressHTTPPort).AsUInt(),
IngressHTTPSPort: config.Get(crcConfig.IngressHTTPSPort).AsUInt(),
EnableSharedDirs: config.Get(crcConfig.EnableSharedDirs).AsBool(),

EmergencyLogin: config.Get(crcConfig.EmergencyLogin).AsBool(),
}

client := newMachine()
Expand Down
1 change: 1 addition & 0 deletions pkg/crc/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func getStartConfig(cfg crcConfig.Storage, args client.StartConfig) types.StartC
IngressHTTPSPort: cfg.Get(crcConfig.IngressHTTPSPort).AsUInt(),
Preset: crcConfig.GetPreset(cfg),
EnableSharedDirs: cfg.Get(crcConfig.EnableSharedDirs).AsBool(),
EmergencyLogin: cfg.Get(crcConfig.EmergencyLogin).AsBool(),
}
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/crc/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
SharedDirPassword = "shared-dir-password" // #nosec G101
IngressHTTPPort = "ingress-http-port"
IngressHTTPSPort = "ingress-https-port"
EmergencyLogin = "enable-emergency-login"
)

func RegisterSettings(cfg *Config) {
Expand Down Expand Up @@ -88,6 +89,8 @@ func RegisterSettings(cfg *Config) {
"Disable update check (true/false, default: false)")
cfg.AddSetting(ExperimentalFeatures, false, ValidateBool, SuccessfullyApplied,
"Enable experimental features (true/false, default: false)")
cfg.AddSetting(EmergencyLogin, false, ValidateBool, SuccessfullyApplied,
"Enable emergency login for 'core' user. Password is randomly generated. (true/false, default: false)")

// Shared directories configs
if runtime.GOOS == "windows" {
Expand Down
1 change: 1 addition & 0 deletions pkg/crc/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ var (
MachineInstanceDir = filepath.Join(MachineBaseDir, "machines")
DaemonSocketPath = filepath.Join(CrcBaseDir, "crc.sock")
KubeconfigFilePath = filepath.Join(MachineInstanceDir, DefaultName, "kubeconfig")
PasswdFilePath = filepath.Join(MachineInstanceDir, DefaultName, "passwd")
)

func GetDefaultBundlePath(preset crcpreset.Preset) string {
Expand Down
35 changes: 35 additions & 0 deletions pkg/crc/machine/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"math/rand"
"os"
"path/filepath"
"strconv"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -784,6 +796,29 @@ func addNameServerToInstance(sshRunner *crcssh.Runner, ns string) error {
return nil
}

func enableEmergencyLogin(sshRunner *crcssh.Runner) error {
gbraad marked this conversation as resolved.
Show resolved Hide resolved
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove on stop and when the setting has been disabled

Copy link
Member Author

Choose a reason for hiding this comment

The 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 core user password on stop, IMO we just need to delete it during crc delete not crc stop action.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it get a new password on restart?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reiterated and now when stop => start happen and user unset enable-emergency-login setting then core user password is locked and from shell not able to login.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 enable-emergency-login is set, we can keep the same password until the VM is deleted.

Copy link
Contributor

Choose a reason for hiding this comment

The 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())
Expand Down
3 changes: 3 additions & 0 deletions pkg/crc/machine/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type StartConfig struct {
// Ports to access openshift routes
IngressHTTPPort uint
IngressHTTPSPort uint

// Enable emergency login
EmergencyLogin bool
}

type ClusterConfig struct {
Expand Down