Skip to content
This repository was archived by the owner on Oct 11, 2023. It is now read-only.

Commit cb1e6cc

Browse files
committed
Generate sshd_config by go template
1 parent a297f83 commit cb1e6cc

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

cmd/control/console_init.go

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77
"os"
88
"os/exec"
99
"path"
10-
"regexp"
10+
"strconv"
1111
"strings"
1212
"syscall"
13+
"text/template"
1314

1415
"github.com/rancher/os/cmd/cloudinitexecute"
1516
"github.com/rancher/os/config"
@@ -318,37 +319,26 @@ func writeRespawn(user string, sshd, recovery bool) error {
318319
}
319320

320321
func modifySshdConfig(cfg *config.CloudConfig) error {
321-
sshdConfig, err := ioutil.ReadFile("/etc/ssh/sshd_config")
322+
os.Remove("/etc/ssh/sshd_config")
323+
sshdTpl, err := template.ParseFiles("/etc/ssh/sshd_config.tpl")
322324
if err != nil {
323325
return err
324326
}
325-
sshdConfigString := string(sshdConfig)
326-
327-
modifiedLines := []string{
328-
"UseDNS no",
329-
"PermitRootLogin no",
330-
"ServerKeyBits 2048",
331-
"AllowGroups docker",
327+
f, err := os.OpenFile("/etc/ssh/sshd_config", os.O_WRONLY|os.O_CREATE, 0644)
328+
if err != nil {
329+
return err
332330
}
331+
defer f.Close()
333332

333+
config := map[string]string{}
334334
if cfg.Rancher.SSH.Port > 0 && cfg.Rancher.SSH.Port < 65355 {
335-
modifiedLines = append(modifiedLines, fmt.Sprintf("Port %d", cfg.Rancher.SSH.Port))
335+
config["Port"] = strconv.Itoa(cfg.Rancher.SSH.Port)
336336
}
337337
if cfg.Rancher.SSH.ListenAddress != "" {
338-
modifiedLines = append(modifiedLines, fmt.Sprintf("ListenAddress %s", cfg.Rancher.SSH.ListenAddress))
339-
}
340-
341-
for _, item := range modifiedLines {
342-
match, err := regexp.Match("^"+item, sshdConfig)
343-
if err != nil {
344-
return err
345-
}
346-
if !match {
347-
sshdConfigString += fmt.Sprintf("%s\n", item)
348-
}
338+
config["ListenAddress"] = cfg.Rancher.SSH.ListenAddress
349339
}
350340

351-
return ioutil.WriteFile("/etc/ssh/sshd_config", []byte(sshdConfigString), 0644)
341+
return sshdTpl.Execute(f, config)
352342
}
353343

354344
func setupSSH(cfg *config.CloudConfig) error {

images/02-console/Dockerfile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
FROM rancher/os-base
22
COPY build/lsb-release /etc/
3+
COPY build/sshd_config.append.tpl /etc/ssh/
4+
COPY prompt.sh /etc/profile.d/
35
RUN sed -i 's/rancher:!/rancher:*/g' /etc/shadow && \
46
sed -i 's/docker:!/docker:*/g' /etc/shadow && \
5-
sed -i 's/#ClientAliveInterval 0/ClientAliveInterval 180/g' /etc/ssh/sshd_config && \
67
echo '## allow password less for rancher user' >> /etc/sudoers && \
78
echo 'rancher ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers && \
89
echo '## allow password less for docker user' >> /etc/sudoers && \
9-
echo 'docker ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
10-
COPY prompt.sh /etc/profile.d/
10+
echo 'docker ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers && \
11+
cat /etc/ssh/sshd_config > /etc/ssh/sshd_config.tpl && \
12+
cat /etc/ssh/sshd_config.append.tpl >> /etc/ssh/sshd_config.tpl && \
13+
rm -f /etc/ssh/sshd_config.append.tpl /etc/ssh/sshd_config

images/02-console/prebuild.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,19 @@ DISTRIB_ID=${DISTRIB_ID}
1313
DISTRIB_RELEASE=${VERSION}
1414
DISTRIB_DESCRIPTION="${DISTRIB_ID} ${VERSION}"
1515
EOF
16+
17+
cat > ./build/sshd_config.append.tpl << EOF
18+
{{- if .Port}}
19+
Port {{.Port}}
20+
{{- end}}
21+
22+
{{- if .ListenAddress}}
23+
ListenAddress {{.ListenAddress}}
24+
{{- end}}
25+
26+
ClientAliveInterval 180
27+
28+
UseDNS no
29+
PermitRootLogin no
30+
AllowGroups docker
31+
EOF

0 commit comments

Comments
 (0)