Skip to content

Commit 5cf7991

Browse files
committed
gpg: Use a Pipe() rather than a file
Use a Pipe() rather than a file to pass the passphrase to the command line tool. Pass the file descriptor to read the passphrase from as fd '3'. Signed-off-by: Stefan Berger <[email protected]>
1 parent 6a25128 commit 5cf7991

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

pkg/encryption/gpg.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,24 @@ func (gc *gpgv2Client) GetGPGPrivateKey(keyid uint64, passphrase string) ([]byte
131131
args = append(args, []string{"--homedir", gc.gpgHomeDir}...)
132132
}
133133

134-
tempfile, err := ioutil.TempFile("", "gpg2*")
134+
rfile, wfile, err := os.Pipe()
135135
if err != nil {
136-
return nil, errors.Wrapf(err, "could not create temporary file")
136+
return nil, errors.Wrapf(err, "could not create pipe")
137137
}
138-
defer os.Remove(tempfile.Name())
139-
if err := ioutil.WriteFile(tempfile.Name(), []byte(passphrase), 0600); err != nil {
140-
return nil, errors.Wrapf(err, "could not write to temporary file")
141-
}
142-
143-
args = append(args, []string{"--pinentry-mode", "loopback", "--batch", "--passphrase-file", tempfile.Name(), "--export-secret-key", fmt.Sprintf("0x%x", keyid)}...)
138+
defer func() {
139+
rfile.Close()
140+
wfile.Close()
141+
}()
142+
// fill pipe in background
143+
go func(passphrase string) {
144+
wfile.Write([]byte(passphrase))
145+
wfile.Close()
146+
}(passphrase)
147+
148+
args = append(args, []string{"--pinentry-mode", "loopback", "--batch", "--passphrase-fd", fmt.Sprintf("%d", 3), "--export-secret-key", fmt.Sprintf("0x%x", keyid)}...)
144149

145150
cmd := exec.Command("gpg2", args...)
151+
cmd.ExtraFiles = []*os.File{rfile}
146152

147153
return runGPGGetOutput(cmd)
148154
}

0 commit comments

Comments
 (0)