Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

command: Fix bug when using terraform login on Windows #24397

Merged
merged 1 commit into from
Mar 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions command/cliconfig/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}

Expand Down