Skip to content

Commit 49c2514

Browse files
cgroschuppChristian Groschupp
andauthored
add filename to exec-file (#761)
* add filename to exec-file * update README.rst Co-authored-by: Christian Groschupp <christian.groschupp.ext@hermesworld.com>
1 parent 1049773 commit 49c2514

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,9 @@ encrypted file is only readable by root, but the target program does not
10131013
need root privileges to function. This flag should be used where possible
10141014
for added security.
10151015
1016+
To overwrite the default file name (``tmp-file``) in ``exec-file`` use the
1017+
``--filename <filename>`` parameter.
1018+
10161019
.. code:: bash
10171020
10181021
# the encrypted file can't be read by the current user

cmd/sops/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ func main() {
195195
Name: "output-type",
196196
Usage: "currently json, yaml, dotenv and binary are supported. If not set, sops will use the input file's extension to determine the output format",
197197
},
198+
cli.StringFlag{
199+
Name: "filename",
200+
Usage: "filename for the temporarily file (default: tmp-file)",
201+
},
198202
}, keyserviceFlags...),
199203
Action: func(c *cli.Context) error {
200204
if len(c.Args()) != 2 {
@@ -222,12 +226,18 @@ func main() {
222226
return toExitError(err)
223227
}
224228

229+
filename := c.String("filename")
230+
if filename == "" {
231+
filename = "tmp-file"
232+
}
233+
225234
if err := exec.ExecWithFile(exec.ExecOpts{
226235
Command: command,
227236
Plaintext: output,
228237
Background: c.Bool("background"),
229238
Fifo: !c.Bool("no-fifo"),
230239
User: c.String("user"),
240+
Filename: filename,
231241
}); err != nil {
232242
return toExitError(err)
233243
}

cmd/sops/subcommand/exec/exec.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ type ExecOpts struct {
2424
Background bool
2525
Fifo bool
2626
User string
27+
Filename string
2728
}
2829

29-
func GetFile(dir string) *os.File {
30-
handle, err := ioutil.TempFile(dir, "tmp-file")
30+
func GetFile(dir, filename string) *os.File {
31+
handle, err := ioutil.TempFile(dir, filename)
3132
if err != nil {
3233
log.Fatal(err)
3334
}
@@ -54,10 +55,10 @@ func ExecWithFile(opts ExecOpts) error {
5455
if opts.Fifo {
5556
// fifo handling needs to be async, even opening to write
5657
// will block if there is no reader present
57-
filename = GetPipe(dir)
58+
filename = GetPipe(dir, opts.Filename)
5859
go WritePipe(filename, opts.Plaintext)
5960
} else {
60-
handle := GetFile(dir)
61+
handle := GetFile(dir, opts.Filename)
6162
handle.Write(opts.Plaintext)
6263
handle.Close()
6364
filename = handle.Name()

cmd/sops/subcommand/exec/exec_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ func WritePipe(pipe string, contents []byte) {
2727
handle.Close()
2828
}
2929

30-
func GetPipe(dir string) string {
31-
tmpfn := filepath.Join(dir, "tmp-file")
30+
func GetPipe(dir, filename string) string {
31+
tmpfn := filepath.Join(dir, filename)
3232
err := syscall.Mkfifo(tmpfn, 0600)
3333
if err != nil {
3434
log.Fatal(err)

cmd/sops/subcommand/exec/exec_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func WritePipe(pipe string, contents []byte) {
1212
log.Fatal("fifos are not available on windows")
1313
}
1414

15-
func GetPipe(dir string) string {
15+
func GetPipe(dir, filename string) string {
1616
log.Fatal("fifos are not available on windows")
1717
return ""
1818
}

0 commit comments

Comments
 (0)