diff --git a/pkg/hostman/guestfs/core.go b/pkg/hostman/guestfs/core.go index 9ad049c6c5..759a220ce6 100644 --- a/pkg/hostman/guestfs/core.go +++ b/pkg/hostman/guestfs/core.go @@ -190,6 +190,10 @@ func DoDeployGuestFs(rootfs fsdriver.IRootFsDriver, guestDesc *deployapi.GuestDe } } + if err := rootfs.ConfigSshd(ret.Account, deployInfo.Password, 0); err != nil { + return nil, errors.Wrap(err, "ConfigSshd") + } + if err = rootfs.DeployYunionroot(partition, deployInfo.PublicKey, deployInfo.IsInit, deployInfo.EnableCloudInit); err != nil { return nil, errors.Wrap(err, "DeployYunionroot") } diff --git a/pkg/hostman/guestfs/fsdriver/android.go b/pkg/hostman/guestfs/fsdriver/android.go index ce9c36f63a..4484d03221 100644 --- a/pkg/hostman/guestfs/fsdriver/android.go +++ b/pkg/hostman/guestfs/fsdriver/android.go @@ -116,6 +116,10 @@ func (m *sBaseAndroidRootFs) DeployNetworkingScripts(rootfs IDiskPartition, nics return nil } +func (m *sBaseAndroidRootFs) ConfigSshd(loginAccount, loginPassword string, sshPort int) error { + return nil +} + func (m *sBaseAndroidRootFs) CommitChanges(part IDiskPartition) error { spath := fmt.Sprintf("/%s/system/build.prop", m.rootDir) lines, _ := m.rootFs.FileGetContents(spath, false) diff --git a/pkg/hostman/guestfs/fsdriver/esxi.go b/pkg/hostman/guestfs/fsdriver/esxi.go index 5b859ce5f9..e618d85c00 100644 --- a/pkg/hostman/guestfs/fsdriver/esxi.go +++ b/pkg/hostman/guestfs/fsdriver/esxi.go @@ -91,3 +91,7 @@ func (m *SEsxiRootFs) PrepareFsForTemplate(IDiskPartition) error { func (m *SEsxiRootFs) DeployNetworkingScripts(rootfs IDiskPartition, nics []*types.SServerNic) error { return nil } + +func (d *SEsxiRootFs) ConfigSshd(loginAccount, loginPassword string, sshPort int) error { + return nil +} diff --git a/pkg/hostman/guestfs/fsdriver/interface.go b/pkg/hostman/guestfs/fsdriver/interface.go index 0c44a0b801..9385a93538 100644 --- a/pkg/hostman/guestfs/fsdriver/interface.go +++ b/pkg/hostman/guestfs/fsdriver/interface.go @@ -99,6 +99,8 @@ type IRootFsDriver interface { CleanNetworkScripts(rootFs IDiskPartition) error AllowAdminLogin() bool + + ConfigSshd(loginAccount, loginPassword string, sshPort int) error } type IDebianRootFsDriver interface { diff --git a/pkg/hostman/guestfs/fsdriver/linux.go b/pkg/hostman/guestfs/fsdriver/linux.go index 640a669313..aced806148 100644 --- a/pkg/hostman/guestfs/fsdriver/linux.go +++ b/pkg/hostman/guestfs/fsdriver/linux.go @@ -846,6 +846,29 @@ func (d *sLinuxRootFs) DeployTelegraf(config string) (bool, error) { return true, nil } +func (d *sLinuxRootFs) ConfigSshd(loginAccount, loginPassword string, sshPort int) error { + if d.rootFs.Exists("/etc/ssh/sshd_config.d", false) { + content := "### sshd config for cloud config\n" + if loginAccount == "root" { + content += "PermitRootLogin yes\n" + } + if len(loginPassword) > 0 { + content += "PasswordAuthentication yes\n" + } + if sshPort > 0 && sshPort != 22 { + content += fmt.Sprintf("Port %d\n", sshPort) + } + return d.rootFs.FilePutContents("/etc/ssh/sshd_config.d/00-cloud-config.conf", content, false, false) + } else { + content, err := d.rootFs.FileGetContents("/etc/ssh/sshd_config", false) + if err != nil { + return errors.Wrap(err, "read sshd config") + } + lines := genSshdConfig(strings.Split(string(content), "\n"), loginAccount, loginPassword, sshPort) + return d.rootFs.FilePutContents("/etc/ssh/sshd_config", strings.Join(lines, "\n"), false, false) + } +} + type sDebianLikeRootFs struct { *sLinuxRootFs } @@ -2325,3 +2348,7 @@ func (d *SCoreOsRootFs) CommitChanges(IDiskPartition) error { } return d.rootFs.FilePutContents("/cloud-config.yml", conf.String(), false, false) } + +func (d *SCoreOsRootFs) ConfigSshd(loginAccount, loginPassword string, sshPort int) error { + return nil +} diff --git a/pkg/hostman/guestfs/fsdriver/macos.go b/pkg/hostman/guestfs/fsdriver/macos.go index 396809fc8a..78ea7ccfe8 100644 --- a/pkg/hostman/guestfs/fsdriver/macos.go +++ b/pkg/hostman/guestfs/fsdriver/macos.go @@ -206,3 +206,7 @@ func (m *SMacOSRootFs) CommitChanges(part IDiskPartition) error { cont = strings.Join(m.scripts, "\n") + "\n" return m.rootFs.FilePutContents(spath, cont, false, false) } + +func (d *SMacOSRootFs) ConfigSshd(loginAccount, loginPassword string, sshPort int) error { + return nil +} diff --git a/pkg/hostman/guestfs/fsdriver/sshd_config.go b/pkg/hostman/guestfs/fsdriver/sshd_config.go new file mode 100644 index 0000000000..45c0c19e3c --- /dev/null +++ b/pkg/hostman/guestfs/fsdriver/sshd_config.go @@ -0,0 +1,49 @@ +package fsdriver + +import ( + "fmt" + "strings" +) + +func genSshdConfig(lines []string, loginAccount, loginPassword string, sshPort int) []string { + var permitRootLogin, passwordAuthentication, port string + if loginAccount == "root" { + permitRootLogin = "PermitRootLogin yes" + } + if len(loginPassword) > 0 { + passwordAuthentication = "PasswordAuthentication yes" + } + if sshPort > 0 && sshPort != 22 { + port = fmt.Sprintf("Port %d", sshPort) + } + for i := range lines { + line := strings.TrimSpace(lines[i]) + if strings.HasPrefix(line, "PermitRootLogin") { + if len(permitRootLogin) > 0 { + line = permitRootLogin + permitRootLogin = "" + } + } else if strings.HasPrefix(line, "PasswordAuthentication") { + if len(passwordAuthentication) > 0 { + line = passwordAuthentication + passwordAuthentication = "" + } + } else if strings.HasPrefix(line, "Port") { + if len(port) > 0 { + line = port + port = "" + } + } + lines[i] = line + } + if len(permitRootLogin) > 0 { + lines = append(lines, permitRootLogin) + } + if len(passwordAuthentication) > 0 { + lines = append(lines, passwordAuthentication) + } + if len(port) > 0 { + lines = append(lines, port) + } + return lines +} diff --git a/pkg/hostman/guestfs/fsdriver/sshd_config_test.go b/pkg/hostman/guestfs/fsdriver/sshd_config_test.go new file mode 100644 index 0000000000..cef1315dc6 --- /dev/null +++ b/pkg/hostman/guestfs/fsdriver/sshd_config_test.go @@ -0,0 +1,117 @@ +package fsdriver + +import ( + "reflect" + "testing" +) + +func TestGenSshdConfig(t *testing.T) { + cases := []struct { + config []string + loginAccount string + loginPassword string + sshPort int + expected []string + }{ + { + config: []string{ + "PermitRootLogin no", + "PasswordAuthentication no", + "# Port 22", + }, + loginAccount: "root", + loginPassword: "123456", + sshPort: 22, + expected: []string{ + "PermitRootLogin yes", + "PasswordAuthentication yes", + "# Port 22", + }, + }, + { + config: []string{ + "PermitRootLogin no", + "PasswordAuthentication no", + "#Port 22", + }, + loginAccount: "yunion", + loginPassword: "123456", + sshPort: 22, + expected: []string{ + "PermitRootLogin no", + "PasswordAuthentication yes", + "#Port 22", + }, + }, + { + config: []string{ + "PermitRootLogin no", + "PasswordAuthentication no", + "Port 22", + }, + loginAccount: "yunion", + loginPassword: "123456", + sshPort: 9000, + expected: []string{ + "PermitRootLogin no", + "PasswordAuthentication yes", + "Port 9000", + }, + }, + { + config: []string{ + "# PermitRootLogin no", + "PasswordAuthentication no", + "Port 22", + }, + loginAccount: "root", + loginPassword: "123456", + sshPort: 9000, + expected: []string{ + "# PermitRootLogin no", + "PasswordAuthentication yes", + "Port 9000", + "PermitRootLogin yes", + }, + }, + { + config: []string{ + "# PermitRootLogin no", + "PasswordAuthentication no", + "Port 22", + }, + loginAccount: "root", + loginPassword: "123456", + sshPort: 22, + expected: []string{ + "# PermitRootLogin no", + "PasswordAuthentication yes", + "Port 22", + "PermitRootLogin yes", + }, + }, + { + config: []string{ + "# PermitRootLogin no", + "PasswordAuthentication no", + "Port 22", + }, + loginAccount: "root", + loginPassword: "", + sshPort: 22, + expected: []string{ + "# PermitRootLogin no", + "PasswordAuthentication no", + "Port 22", + "PermitRootLogin yes", + }, + }, + } + + for _, c := range cases { + actual := genSshdConfig(c.config, c.loginAccount, c.loginPassword, c.sshPort) + if !reflect.DeepEqual(actual, c.expected) { + t.Errorf("expected %v, got %v", c.expected, actual) + } + } +} diff --git a/pkg/hostman/guestfs/fsdriver/windows.go b/pkg/hostman/guestfs/fsdriver/windows.go index 06f52d3246..1d117122f8 100644 --- a/pkg/hostman/guestfs/fsdriver/windows.go +++ b/pkg/hostman/guestfs/fsdriver/windows.go @@ -666,3 +666,7 @@ func (w *SWindowsRootFs) DeployTelegraf(config string) (bool, error) { } return true, nil } + +func (w *SWindowsRootFs) ConfigSshd(loginAccount, loginPassword string, sshPort int) error { + return nil +}