Skip to content

Commit

Permalink
acme/autocert: remove tempfile after dircache write failed
Browse files Browse the repository at this point in the history
Per https://golang.org/pkg/io/ioutil/#TempFile description, caller should remove the file when no longer needed.

Change-Id: I4c2a83c1c9bbd89f423d1a3334751e86f35b1cf6
GitHub-Last-Rev: fac91d1
GitHub-Pull-Request: #92
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/189337
Run-TryBot: Alex Vaghin <ddos@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Vaghin <ddos@google.com>
  • Loading branch information
yuanhh authored and Alex Vaghin committed Aug 20, 2019
1 parent 4def268 commit 60c769a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion acme/autocert/cache.go
Expand Up @@ -77,6 +77,7 @@ func (d DirCache) Put(ctx context.Context, name string, data []byte) error {
if tmp, err = d.writeTempFile(name, data); err != nil {
return
}
defer os.Remove(tmp)
select {
case <-ctx.Done():
// Don't overwrite the file if the context was canceled.
Expand Down Expand Up @@ -116,12 +117,17 @@ func (d DirCache) Delete(ctx context.Context, name string) error {
}

// writeTempFile writes b to a temporary file, closes the file and returns its path.
func (d DirCache) writeTempFile(prefix string, b []byte) (string, error) {
func (d DirCache) writeTempFile(prefix string, b []byte) (name string, reterr error) {
// TempFile uses 0600 permissions
f, err := ioutil.TempFile(string(d), prefix)
if err != nil {
return "", err
}
defer func() {
if reterr != nil {
os.Remove(f.Name())
}
}()
if _, err := f.Write(b); err != nil {
f.Close()
return "", err
Expand Down
9 changes: 9 additions & 0 deletions acme/autocert/cache_test.go
Expand Up @@ -48,6 +48,15 @@ func TestDirCache(t *testing.T) {
t.Error(err)
}

// test put deletes temp file
tmp, err := filepath.Glob(name + "?*")
if err != nil {
t.Error(err)
}
if tmp != nil {
t.Errorf("temp file exists: %s", tmp)
}

// test delete
if err := cache.Delete(ctx, "dummy"); err != nil {
t.Fatalf("delete: %v", err)
Expand Down

0 comments on commit 60c769a

Please sign in to comment.