Skip to content

[supervisor] support multi-line environment variable in ssh #13822

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

Merged
merged 1 commit into from
Oct 13, 2022
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: 0 additions & 2 deletions components/supervisor/openssh/BUILD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ packages:
- ["rm", "-rf", "components-supervisor-openssh--docker-build"]
- name: docker-build
type: docker
srcs:
- "*.patch"
config:
dockerfile: leeway.Dockerfile
2 changes: 0 additions & 2 deletions components/supervisor/openssh/leeway.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ RUN ./configure \
--with-privsep-user=nobody \
--with-ssl-engine

COPY supervisorenv.patch .
ENV aports=https://raw.githubusercontent.com/alpinelinux/aports/master/main/openssh
RUN curl -fsSL \
"${aports}/{avoid-redefined-warnings-when-building-with-utmps,disable-forwarding-by-default,fix-utmp,fix-verify-dns-segfault,gss-serv.c,sftp-interactive}.patch" \
| patch -p1
RUN cat supervisorenv.patch | patch -p1
RUN make install-nosysconf exec_prefix=/openssh

RUN TEST_SSH_UNSAFE_PERMISSIONS=1 \
Expand Down
14 changes: 0 additions & 14 deletions components/supervisor/openssh/supervisorenv.patch

This file was deleted.

25 changes: 15 additions & 10 deletions components/supervisor/pkg/supervisor/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func newSSHServer(ctx context.Context, cfg *Config, envvars []string) (*sshServe
return nil, xerrors.Errorf("unexpected error creating SSH key: %w", err)
}
}
err = writeSSHEnv(cfg, envvars)
err = ensureSSHDir(cfg)
if err != nil {
return nil, xerrors.Errorf("unexpected error creating SSH env: %w", err)
return nil, xerrors.Errorf("unexpected error creating SSH dir: %w", err)
}

return &sshServer{
Expand Down Expand Up @@ -103,6 +103,18 @@ func (s *sshServer) handleConn(ctx context.Context, conn net.Conn) {
"-oLogLevel DEBUG", // enabled DEBUG mode by default
}

envs := make([]string, 0)
for _, env := range s.envvars {
s := strings.SplitN(env, "=", 2)
if len(s) != 2 {
continue
}
envs = append(envs, fmt.Sprintf("%s=%s", s[0], fmt.Sprintf("\"%s\"", strings.ReplaceAll(s[1], "\"", "\\\""))))
}
if len(envs) > 0 {
args = append(args, fmt.Sprintf("-oSetEnv %s", strings.Join(envs, " ")))
}

socketFD, err := conn.(*net.TCPConn).File()
if err != nil {
log.WithError(err).Error("cannot start SSH server")
Expand Down Expand Up @@ -189,21 +201,14 @@ func prepareSSHKey(ctx context.Context, sshkey string) error {
return nil
}

func writeSSHEnv(cfg *Config, envvars []string) error {
func ensureSSHDir(cfg *Config) error {
home := "/home/gitpod"

d := filepath.Join(home, ".ssh")
err := os.MkdirAll(d, 0o700)
if err != nil {
return xerrors.Errorf("cannot create $HOME/.ssh: %w", err)
}

fn := filepath.Join(d, "supervisor_env")
err = os.WriteFile(fn, []byte(strings.Join(envvars, "\n")), 0o644)
if err != nil {
return xerrors.Errorf("cannot write %s: %w", fn, err)
}

_ = exec.Command("chown", "-R", fmt.Sprintf("%d:%d", gitpodUID, gitpodGID), d).Run()

return nil
Expand Down