mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
fix: linux sshd_config support (#23342)
Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -99,6 +99,8 @@ type IRootFsDriver interface {
|
||||
CleanNetworkScripts(rootFs IDiskPartition) error
|
||||
|
||||
AllowAdminLogin() bool
|
||||
|
||||
ConfigSshd(loginAccount, loginPassword string, sshPort int) error
|
||||
}
|
||||
|
||||
type IDebianRootFsDriver interface {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user