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

fix-resolve-home-v1 #4519

Merged
merged 4 commits into from Oct 18, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
@@ -1,5 +1,7 @@
### SDK Features

### SDK Enhancements
* `aws/session`: Modified config resolution strategy when `$HOME` or `%USERPROFILE%` environment variables are not set.
* When the environment variables are not set, the SDK will attempt to determine the home directory using `user.Current()`.

### SDK Bugs
18 changes: 12 additions & 6 deletions internal/shareddefaults/shared_config.go
@@ -1,9 +1,8 @@
package shareddefaults

import (
"os"
"os/user"
"path/filepath"
"runtime"
)

// SharedCredentialsFilename returns the SDK's default file path
Expand Down Expand Up @@ -31,10 +30,17 @@ func SharedConfigFilename() string {
// UserHomeDir returns the home directory for the user the process is
// running under.
func UserHomeDir() string {
if runtime.GOOS == "windows" { // Windows
return os.Getenv("USERPROFILE")
var home string

home = userHomeDir()
if len(home) > 0 {
return home
}

currUser, _ := user.Current()
if currUser != nil {
home = currUser.HomeDir
}

// *nix
return os.Getenv("HOME")
return home
}
18 changes: 18 additions & 0 deletions internal/shareddefaults/shared_config_resolve_home.go
@@ -0,0 +1,18 @@
//go:build !go1.12
// +build !go1.12

package shareddefaults

import (
"os"
"runtime"
)

func userHomeDir() string {
if runtime.GOOS == "windows" { // Windows
return os.Getenv("USERPROFILE")
}

// *nix
return os.Getenv("HOME")
}
13 changes: 13 additions & 0 deletions internal/shareddefaults/shared_config_resolve_home_go1.12.go
@@ -0,0 +1,13 @@
//go:build go1.12
// +build go1.12

package shareddefaults

import (
"os"
)

func userHomeDir() string {
home, _ := os.UserHomeDir()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not fix the problem with #4444, since os.UserHomeDir is also based on $HOME.
Test program to demonstrate:

// go build test.go
// env -i ./test
package main                                                                    
                                                                                
import (                                                                        
        "fmt"                                                                   
        "os"                                                                    
)                                                                               
                                                                                
func main() {                                                                   
        home, err := os.UserHomeDir()                                           
        fmt.Println(home,  err)                                            // prints "$HOME not defined" if variable is empty
}  

Better solution:

package main                                                                    
                                                                                
import (                                                                        
        "fmt"                                                                   
        "os/user"                                                               
)                                                                               
                                                                                
func main() {                                                                   
        user, err := user.Current()                                             
        fmt.Println(user.HomeDir, err)                  // Prints home directory even when running under env -i or $HOME unset.
}                                                                               

The user.Current() is based on getpwent and thus is independent of the $HOME variable.

return home
}