Skip to content

Commit 618c0ff

Browse files
authored
Merge pull request #880 from ricmatsui/rm/exec-env-same-process
Add same process option for exec-env
2 parents 2110731 + aa7ea02 commit 618c0ff

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,11 @@ written to disk.
10691069
$ echo your password: $database_password
10701070
your password:
10711071
1072+
If you want process signals to be sent to the command, for example if you are
1073+
running ``exec-env`` to launch a server and your server handles SIGTERM, then the
1074+
``--same-process`` flag can be used to instruct ``sops`` to start your command in
1075+
the same process instead of a child process. This uses the ``execve`` system call
1076+
and is supported on Unix-like systems.
10721077
10731078
If the command you want to run only operates on files, you can use ``exec-file``
10741079
instead. By default, SOPS will use a FIFO to pass the contents of the

cmd/sops/main.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ func main() {
163163
Name: "user",
164164
Usage: "the user to run the command as",
165165
},
166+
cli.BoolFlag{
167+
Name: "same-process",
168+
Usage: "run command in the current process instead of in a child process",
169+
},
166170
}, keyserviceFlags...),
167171
Action: func(c *cli.Context) error {
168172
if c.NArg() != 2 {
@@ -195,6 +199,10 @@ func main() {
195199

196200
if c.Bool("background") {
197201
log.Warn("exec-env's --background option is deprecated and will be removed in a future version of sops")
202+
203+
if c.Bool("same-process") {
204+
return common.NewExitError("Error: The --same-process flag cannot be used with --background", codes.ErrorConflictingParameters)
205+
}
198206
}
199207

200208
tree, err := decryptTree(opts)
@@ -225,12 +233,13 @@ func main() {
225233
}
226234

227235
if err := exec.ExecWithEnv(exec.ExecOpts{
228-
Command: command,
229-
Plaintext: []byte{},
230-
Background: c.Bool("background"),
231-
Pristine: c.Bool("pristine"),
232-
User: c.String("user"),
233-
Env: env,
236+
Command: command,
237+
Plaintext: []byte{},
238+
Background: c.Bool("background"),
239+
Pristine: c.Bool("pristine"),
240+
User: c.String("user"),
241+
SameProcess: c.Bool("same-process"),
242+
Env: env,
234243
}); err != nil {
235244
return toExitError(err)
236245
}

cmd/sops/subcommand/exec/exec.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package exec
22

33
import (
44
"bytes"
5+
"fmt"
56
"os"
67
"path/filepath"
78
"runtime"
@@ -23,14 +24,15 @@ func init() {
2324
}
2425

2526
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
27+
Command string
28+
Plaintext []byte
29+
Background bool
30+
SameProcess bool
31+
Pristine bool
32+
Fifo bool
33+
User string
34+
Filename string
35+
Env []string
3436
}
3537

3638
func GetFile(dir, filename string) *os.File {
@@ -115,6 +117,10 @@ func ExecWithEnv(opts ExecOpts) error {
115117
SwitchUser(opts.User)
116118
}
117119

120+
if runtime.GOOS == "windows" && opts.SameProcess {
121+
return fmt.Errorf("The --same-process flag is not supported on Windows")
122+
}
123+
118124
var env []string
119125

120126
if !opts.Pristine {
@@ -134,6 +140,15 @@ func ExecWithEnv(opts ExecOpts) error {
134140

135141
env = append(env, opts.Env...)
136142

143+
if opts.SameProcess {
144+
if opts.Background {
145+
log.Fatal("background is not supported for same-process")
146+
}
147+
148+
// Note that the call does NOT return, unless an error happens.
149+
return ExecSyscall(opts.Command, env)
150+
}
151+
137152
cmd := BuildCommand(opts.Command)
138153
cmd.Env = env
139154

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import (
44
"os/exec"
55
)
66

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

0 commit comments

Comments
 (0)