Skip to content

Commit 2a3c373

Browse files
committed
feat(security): add config option process which allow config chroot and user
1 parent e7c5837 commit 2a3c373

File tree

6 files changed

+44
-18
lines changed

6 files changed

+44
-18
lines changed

db_lib/AnsiblePlaybook.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ func (p AnsiblePlaybook) makeCmd(command string, args []string, environmentVars
3131
cmd.Env = append(cmd.Env, fmt.Sprintf("PWD=%s", cmd.Dir))
3232
cmd.Env = append(cmd.Env, environmentVars...)
3333

34+
cmd.SysProcAttr = util.Config.GetSysProcAttr()
35+
3436
return cmd
3537
}
3638

db_lib/ShellApp.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ func (t *ShellApp) makeCmd(command string, args []string, environmentVars []stri
4949
cmd.Env = append(cmd.Env, fmt.Sprintf("PWD=%s", cmd.Dir))
5050
cmd.Env = append(cmd.Env, environmentVars...)
5151

52+
cmd.SysProcAttr = util.Config.GetSysProcAttr()
53+
5254
return cmd
5355
}
5456

db_lib/TerraformApp.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ func (t *TerraformApp) makeCmd(command string, args []string, environmentVars []
7878
cmd.Env = append(cmd.Env, environmentVars...)
7979
}
8080

81+
cmd.SysProcAttr = util.Config.GetSysProcAttr()
82+
8183
return cmd
8284
}
8385

deployment/docker/runner/Dockerfile

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,6 @@ RUN curl -O https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraf
3636
unzip terraform_${TERRAFORM_VERSION}_linux_${TARGETARCH}.zip -d /tmp && \
3737
rm terraform_${TERRAFORM_VERSION}_linux_${TARGETARCH}.zip
3838

39-
#RUN if [ "$TARGETARCH" = "amd64" ]; then \
40-
# export PULUMI_ARCH="x64"; \
41-
# else \
42-
# export PULUMI_ARCH="${TARGETARCH}"; \
43-
# fi && \
44-
# wget -O pulumi.tar.gz https://github.com/pulumi/pulumi/releases/download/v${PULUMI_VERSION}/pulumi-v${PULUMI_VERSION}-linux-${PULUMI_ARCH}.tar.gz
45-
#RUN tar xf pulumi.tar.gz --strip-components=1 -C /usr/local/bin
46-
#RUN rm pulumi.tar.gz
47-
4839
FROM alpine:3.21
4940

5041
ARG TARGETARCH="amd64"

deployment/docker/server/Dockerfile

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ RUN curl -O https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraf
3737
unzip terraform_${TERRAFORM_VERSION}_linux_${TARGETARCH}.zip -d /tmp && \
3838
rm terraform_${TERRAFORM_VERSION}_linux_${TARGETARCH}.zip
3939

40-
#RUN if [ "$TARGETARCH" = "amd64" ]; then \
41-
# export PULUMI_ARCH="x64"; \
42-
# else \
43-
# export PULUMI_ARCH="${TARGETARCH}"; \
44-
# fi && \
45-
# wget -O pulumi.tar.gz https://github.com/pulumi/pulumi/releases/download/v${PULUMI_VERSION}/pulumi-v${PULUMI_VERSION}-linux-${PULUMI_ARCH}.tar.gz
46-
#RUN tar xf pulumi.tar.gz --strip-components=1 -C /usr/local/bin
47-
#RUN rm pulumi.tar.gz
48-
4940
FROM alpine:3.21
5041

5142
ARG TARGETARCH="amd64"

util/config.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import (
1313
"net/url"
1414
"os"
1515
"os/exec"
16+
"os/user"
1617
"path"
1718
"path/filepath"
1819
"reflect"
1920
"regexp"
2021
"strconv"
2122
"strings"
23+
"syscall"
2224

2325
"github.com/google/go-github/github"
2426
"github.com/gorilla/securecookie"
@@ -151,6 +153,11 @@ type ConfigLog struct {
151153
Events *EventLogType `json:"events,omitempty"`
152154
}
153155

156+
type ConfigProcess struct {
157+
User string `json:"user,omitempty" env:"SEMAPHORE_PROCESS_USER"`
158+
Chroot string `json:"chroot,omitempty" env:"SEMAPHORE_PROCESS_CHROOT"`
159+
}
160+
154161
// ConfigType mapping between Config and the json file that sets it
155162
type ConfigType struct {
156163
MySQL *DbConfig `json:"mysql,omitempty"`
@@ -252,6 +259,8 @@ type ConfigType struct {
252259
ForwardedEnvVars []string `json:"forwarded_env_vars,omitempty" env:"SEMAPHORE_FORWARDED_ENV_VARS"`
253260

254261
Log *ConfigLog `json:"log,omitempty"`
262+
263+
Process *ConfigProcess `json:"process,omitempty"`
255264
}
256265

257266
func NewConfigType() *ConfigType {
@@ -299,6 +308,35 @@ func ClearDir(dir string, preserveFiles bool, prefix string) error {
299308
return nil
300309
}
301310

311+
func (conf *ConfigType) GetSysProcAttr() (res *syscall.SysProcAttr) {
312+
313+
if conf.Process.Chroot != "" {
314+
res = &syscall.SysProcAttr{}
315+
res.Chroot = conf.Process.Chroot
316+
}
317+
318+
if conf.Process.User != "" {
319+
if res == nil {
320+
res = &syscall.SysProcAttr{}
321+
}
322+
323+
u, err := user.Lookup(conf.Process.User)
324+
if err != nil {
325+
return
326+
}
327+
328+
uid, _ := strconv.Atoi(u.Uid)
329+
gid, _ := strconv.Atoi(u.Gid)
330+
331+
res.Credential = &syscall.Credential{
332+
Uid: uint32(uid),
333+
Gid: uint32(gid),
334+
}
335+
}
336+
337+
return
338+
}
339+
302340
func (conf *ConfigType) ClearTmpDir() error {
303341
return ClearDir(conf.TmpPath, false, "")
304342
}

0 commit comments

Comments
 (0)