Skip to content
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

add filename to exec-file #761

Merged
merged 2 commits into from Feb 9, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.rst
Expand Up @@ -1013,6 +1013,9 @@ encrypted file is only readable by root, but the target program does not
need root privileges to function. This flag should be used where possible
for added security.

To overwrite the default file name (``tmp-file``) in ``exec-file`` use the
``--filename <filename>`` parameter.

.. code:: bash

# the encrypted file can't be read by the current user
Expand Down
10 changes: 10 additions & 0 deletions cmd/sops/main.go
Expand Up @@ -195,6 +195,10 @@ func main() {
Name: "output-type",
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",
},
cli.StringFlag{
Name: "filename",
Usage: "filename for the temporarily file (default: tmp-file)",
},
}, keyserviceFlags...),
Action: func(c *cli.Context) error {
if len(c.Args()) != 2 {
Expand Down Expand Up @@ -222,12 +226,18 @@ func main() {
return toExitError(err)
}

filename := c.String("filename")
if filename == "" {
filename = "tmp-file"
}
cgroschupp marked this conversation as resolved.
Show resolved Hide resolved

if err := exec.ExecWithFile(exec.ExecOpts{
Command: command,
Plaintext: output,
Background: c.Bool("background"),
Fifo: !c.Bool("no-fifo"),
User: c.String("user"),
Filename: filename,
}); err != nil {
return toExitError(err)
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/sops/subcommand/exec/exec.go
Expand Up @@ -24,10 +24,11 @@ type ExecOpts struct {
Background bool
Fifo bool
User string
Filename string
}

func GetFile(dir string) *os.File {
handle, err := ioutil.TempFile(dir, "tmp-file")
func GetFile(dir, filename string) *os.File {
handle, err := ioutil.TempFile(dir, filename)
if err != nil {
log.Fatal(err)
}
Expand All @@ -54,10 +55,10 @@ func ExecWithFile(opts ExecOpts) error {
if opts.Fifo {
// fifo handling needs to be async, even opening to write
// will block if there is no reader present
filename = GetPipe(dir)
filename = GetPipe(dir, opts.Filename)
go WritePipe(filename, opts.Plaintext)
} else {
handle := GetFile(dir)
handle := GetFile(dir, opts.Filename)
handle.Write(opts.Plaintext)
handle.Close()
filename = handle.Name()
Expand Down
4 changes: 2 additions & 2 deletions cmd/sops/subcommand/exec/exec_unix.go
Expand Up @@ -27,8 +27,8 @@ func WritePipe(pipe string, contents []byte) {
handle.Close()
}

func GetPipe(dir string) string {
tmpfn := filepath.Join(dir, "tmp-file")
func GetPipe(dir, filename string) string {
tmpfn := filepath.Join(dir, filename)
err := syscall.Mkfifo(tmpfn, 0600)
if err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/sops/subcommand/exec/exec_windows.go
Expand Up @@ -12,7 +12,7 @@ func WritePipe(pipe string, contents []byte) {
log.Fatal("fifos are not available on windows")
}

func GetPipe(dir string) string {
func GetPipe(dir, filename string) string {
log.Fatal("fifos are not available on windows")
return ""
}
Expand Down