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

Commit

Permalink
Generate sshd_config by go template
Browse files Browse the repository at this point in the history
  • Loading branch information
niusmallnan committed Feb 5, 2019
1 parent a297f83 commit cb1e6cc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
34 changes: 12 additions & 22 deletions cmd/control/console_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"os"
"os/exec"
"path"
"regexp"
"strconv"
"strings"
"syscall"
"text/template"

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

func modifySshdConfig(cfg *config.CloudConfig) error {
sshdConfig, err := ioutil.ReadFile("/etc/ssh/sshd_config")
os.Remove("/etc/ssh/sshd_config")
sshdTpl, err := template.ParseFiles("/etc/ssh/sshd_config.tpl")
if err != nil {
return err
}
sshdConfigString := string(sshdConfig)

modifiedLines := []string{
"UseDNS no",
"PermitRootLogin no",
"ServerKeyBits 2048",
"AllowGroups docker",
f, err := os.OpenFile("/etc/ssh/sshd_config", os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
defer f.Close()

config := map[string]string{}
if cfg.Rancher.SSH.Port > 0 && cfg.Rancher.SSH.Port < 65355 {
modifiedLines = append(modifiedLines, fmt.Sprintf("Port %d", cfg.Rancher.SSH.Port))
config["Port"] = strconv.Itoa(cfg.Rancher.SSH.Port)
}
if cfg.Rancher.SSH.ListenAddress != "" {
modifiedLines = append(modifiedLines, fmt.Sprintf("ListenAddress %s", cfg.Rancher.SSH.ListenAddress))
}

for _, item := range modifiedLines {
match, err := regexp.Match("^"+item, sshdConfig)
if err != nil {
return err
}
if !match {
sshdConfigString += fmt.Sprintf("%s\n", item)
}
config["ListenAddress"] = cfg.Rancher.SSH.ListenAddress
}

return ioutil.WriteFile("/etc/ssh/sshd_config", []byte(sshdConfigString), 0644)
return sshdTpl.Execute(f, config)
}

func setupSSH(cfg *config.CloudConfig) error {
Expand Down
9 changes: 6 additions & 3 deletions images/02-console/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
FROM rancher/os-base
COPY build/lsb-release /etc/
COPY build/sshd_config.append.tpl /etc/ssh/
COPY prompt.sh /etc/profile.d/
RUN sed -i 's/rancher:!/rancher:*/g' /etc/shadow && \
sed -i 's/docker:!/docker:*/g' /etc/shadow && \
sed -i 's/#ClientAliveInterval 0/ClientAliveInterval 180/g' /etc/ssh/sshd_config && \
echo '## allow password less for rancher user' >> /etc/sudoers && \
echo 'rancher ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers && \
echo '## allow password less for docker user' >> /etc/sudoers && \
echo 'docker ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
COPY prompt.sh /etc/profile.d/
echo 'docker ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers && \
cat /etc/ssh/sshd_config > /etc/ssh/sshd_config.tpl && \
cat /etc/ssh/sshd_config.append.tpl >> /etc/ssh/sshd_config.tpl && \
rm -f /etc/ssh/sshd_config.append.tpl /etc/ssh/sshd_config
16 changes: 16 additions & 0 deletions images/02-console/prebuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,19 @@ DISTRIB_ID=${DISTRIB_ID}
DISTRIB_RELEASE=${VERSION}
DISTRIB_DESCRIPTION="${DISTRIB_ID} ${VERSION}"
EOF

cat > ./build/sshd_config.append.tpl << EOF
{{- if .Port}}
Port {{.Port}}
{{- end}}
{{- if .ListenAddress}}
ListenAddress {{.ListenAddress}}
{{- end}}
ClientAliveInterval 180
UseDNS no
PermitRootLogin no
AllowGroups docker
EOF

0 comments on commit cb1e6cc

Please sign in to comment.