command: Fix bug when using terraform login on Windows

This commit is contained in:
Alisdair McDiarmid 2020-03-17 13:21:35 -04:00
parent fb7035ac3e
commit 8c7a44355b
1 changed files with 14 additions and 11 deletions

View File

@ -317,23 +317,18 @@ func (s *CredentialsSource) updateLocalHostCredentials(host svchost.Hostname, ne
tmpName := f.Name()
moved := false
defer func(f *os.File, name string) {
// Always close our file, and remove it if it's still at its
// temporary name. We're ignoring errors here because there's
// nothing we can do about them anyway.
f.Close()
// Remove the temporary file if it hasn't been moved yet. We're
// ignoring errors here because there's nothing we can do about
// them anyway.
if !moved {
os.Remove(name)
}
}(f, tmpName)
// Credentials file should be readable only by its owner. (This may
// not be effective on all platforms, but should at least work on
// Unix-like targets and should be harmless elsewhere.)
if err := f.Chmod(0600); err != nil {
return fmt.Errorf("cannot set mode for temporary file %s: %s", tmpName, err)
}
// Write the credentials to the temporary file, then immediately close
// it, whether or not the write succeeds.
_, err = f.Write(newSrc)
f.Close()
if err != nil {
return fmt.Errorf("cannot write to temporary file %s: %s", tmpName, err)
}
@ -345,6 +340,14 @@ func (s *CredentialsSource) updateLocalHostCredentials(host svchost.Hostname, ne
if err != nil {
return fmt.Errorf("failed to replace %s with temporary file %s: %s", filename, tmpName, err)
}
// Credentials file should be readable only by its owner. (This may
// not be effective on all platforms, but should at least work on
// Unix-like targets and should be harmless elsewhere.)
if err := os.Chmod(filename, 0600); err != nil {
return fmt.Errorf("cannot set mode for credentials file %s: %s", filename, err)
}
moved = true
}