Skip to content
Open
Show file tree
Hide file tree
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
14 changes: 5 additions & 9 deletions pkg/granted/registry/ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ import (
"fmt"
"os"
"path"
"path/filepath"

"github.com/common-fate/clio"
"github.com/fwdcloudsec/granted/pkg/cfaws"
"gopkg.in/ini.v1"
)

// Find the ~/.aws/config absolute path based on OS.
// getDefaultAWSConfigLocation returns the AWS config file path,
// respecting the AWS_CONFIG_FILE environment variable per
// https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html
func getDefaultAWSConfigLocation() (string, error) {
h, err := os.UserHomeDir()
if err != nil {
return "", err
}

configPath := filepath.Join(h, ".aws", "config")
return configPath, nil
return cfaws.GetAWSConfigPath(), nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of returning cfaws.GetAWSConfigPath() here can we update the uses of getDefaultAWSConfigLocation in the registry to instead just call cfaws.GetAWSConfigPath() for consistency across the codebase?

}

// loadAWSConfigFile loads the `~/.aws/config` file, and creates it if it doesn't exist.
Expand Down
70 changes: 70 additions & 0 deletions pkg/granted/registry/ini_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package registry

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetDefaultAWSConfigLocation(t *testing.T) {
tests := []struct {
name string
envValue string
wantCustom bool
}{
{
name: "uses AWS_CONFIG_FILE when set",
envValue: "/custom/path/config",
wantCustom: true,
},
{
name: "falls back to default when not set",
envValue: "",
wantCustom: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("AWS_CONFIG_FILE", tt.envValue)

got, err := getDefaultAWSConfigLocation()
assert.NoError(t, err)
if tt.wantCustom {
assert.Equal(t, tt.envValue, got)
} else {
assert.Contains(t, got, ".aws/config")
}
})
}
}

func TestLoadAWSConfigFile_RespectsEnvVar(t *testing.T) {
// Create a temp dir with an AWS config file
tmpDir := t.TempDir()
customConfigPath := filepath.Join(tmpDir, "custom-aws-config")
err := os.WriteFile(customConfigPath, []byte("[profile test]\nregion = us-east-1\n"), 0600)
assert.NoError(t, err)

t.Setenv("AWS_CONFIG_FILE", customConfigPath)

cfg, path, err := loadAWSConfigFile()
assert.NoError(t, err)
assert.Equal(t, customConfigPath, path)
assert.NotNil(t, cfg)

// Verify it loaded the correct file
sec, err := cfg.GetSection("profile test")
assert.NoError(t, err)
assert.Equal(t, "us-east-1", sec.Key("region").String())
}

func TestLoadAWSConfigFile_DefaultPath(t *testing.T) {
t.Setenv("AWS_CONFIG_FILE", "")

_, path, err := loadAWSConfigFile()
assert.NoError(t, err)
assert.Contains(t, path, ".aws/config")
}