Skip to content

Commit 6aad77b

Browse files
committed
Add same-process option for exec-env
1 parent 92651ed commit 6aad77b

File tree

5 files changed

+41
-14
lines changed

5 files changed

+41
-14
lines changed

README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,11 @@ written to disk.
10521052
$ echo your password: $database_password
10531053
your password:
10541054
1055+
If you want process signals to be sent to the command, for example if you are
1056+
running ``exec-env`` to launch a server and your server handles sigterm, then the
1057+
``--same-process`` flag can be used to instruct ``sops`` to start your command in
1058+
the same process instead of a child process. This uses the ``execve`` system call
1059+
and is supported on unix-like systems.
10551060
10561061
If the command you want to run only operates on files, you can use ``exec-file``
10571062
instead. By default, SOPS will use a FIFO to pass the contents of the

cmd/sops/main.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ func main() {
162162
Name: "user",
163163
Usage: "the user to run the command as",
164164
},
165+
cli.BoolFlag{
166+
Name: "same-process",
167+
Usage: "run command in the current process instead of in a child process",
168+
},
165169
}, keyserviceFlags...),
166170
Action: func(c *cli.Context) error {
167171
if c.NArg() != 2 {
@@ -224,12 +228,13 @@ func main() {
224228
}
225229

226230
if err := exec.ExecWithEnv(exec.ExecOpts{
227-
Command: command,
228-
Plaintext: []byte{},
229-
Background: c.Bool("background"),
230-
Pristine: c.Bool("pristine"),
231-
User: c.String("user"),
232-
Env: env,
231+
Command: command,
232+
Plaintext: []byte{},
233+
Background: c.Bool("background"),
234+
Pristine: c.Bool("pristine"),
235+
User: c.String("user"),
236+
SameProcess: c.Bool("same-process"),
237+
Env: env,
233238
}); err != nil {
234239
return toExitError(err)
235240
}

cmd/sops/subcommand/exec/exec.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ func init() {
2323
}
2424

2525
type ExecOpts struct {
26-
Command string
27-
Plaintext []byte
28-
Background bool
29-
Pristine bool
30-
Fifo bool
31-
User string
32-
Filename string
33-
Env []string
26+
Command string
27+
Plaintext []byte
28+
Background bool
29+
SameProcess bool
30+
Pristine bool
31+
Fifo bool
32+
User string
33+
Filename string
34+
Env []string
3435
}
3536

3637
func GetFile(dir, filename string) *os.File {
@@ -134,6 +135,14 @@ func ExecWithEnv(opts ExecOpts) error {
134135

135136
env = append(env, opts.Env...)
136137

138+
if opts.SameProcess {
139+
if opts.Background {
140+
log.Fatal("background is not supported for same-process")
141+
}
142+
143+
return ExecSyscall(opts.Command, env)
144+
}
145+
137146
cmd := BuildCommand(opts.Command)
138147
cmd.Env = env
139148

cmd/sops/subcommand/exec/exec_unix.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import (
1111
"syscall"
1212
)
1313

14+
func ExecSyscall(command string, env []string) error {
15+
return syscall.Exec("/bin/sh", []string{"/bin/sh", "-c", command}, env)
16+
}
17+
1418
func BuildCommand(command string) *exec.Cmd {
1519
return exec.Command("/bin/sh", "-c", command)
1620
}

cmd/sops/subcommand/exec/exec_windows.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import (
44
"os/exec"
55
)
66

7+
func ExecSyscall(command string, env []string) error {
8+
log.Fatal("same-process not available on windows")
9+
}
10+
711
func BuildCommand(command string) *exec.Cmd {
812
return exec.Command("cmd.exe", "/C", command)
913
}

0 commit comments

Comments
 (0)