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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ CredHub CLI can be used to manage credentials stored in a CredHub server. You mu

To see the API calls made by each CLI command, `export CREDHUB_DEBUG=true`.

#### Config Location:

By default, the CLI stores its configuration (`config.json`) in a `.credhub` directory inside the current user's home directory (`$HOME` on Linux/macOS, `%USERPROFILE%` on Windows). Set `CREDHUB_HOME` to override this and store the configuration under a different directory instead.

[1]:https://docs.cloudfoundry.org/api/credhub/
6 changes: 5 additions & 1 deletion config/config_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import (
)

func userHomeDir() string {
return os.Getenv("HOME")
home := os.Getenv("CREDHUB_HOME")
if home == "" {
home = os.Getenv("HOME")
}
return home
}

func makeDirectory() error {
Expand Down
34 changes: 34 additions & 0 deletions config/config_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,38 @@ var _ = Describe("Config", func() {
fileStat, _ := os.Stat(config.ConfigPath())
Expect(fileStat.Mode().String()).To(Equal("-rw-------"))
})

Describe("CREDHUB_HOME", func() {
var originalCredhubHome string
var originalHome string
var hadCredhubHome bool

BeforeEach(func() {
originalCredhubHome, hadCredhubHome = os.LookupEnv("CREDHUB_HOME")
originalHome = os.Getenv("HOME")
})

AfterEach(func() {
if hadCredhubHome {
os.Setenv("CREDHUB_HOME", originalCredhubHome)
} else {
os.Unsetenv("CREDHUB_HOME")
}
os.Setenv("HOME", originalHome)
})

It("uses CREDHUB_HOME instead of HOME when set", func() {
os.Setenv("HOME", "/home/original-user")
os.Setenv("CREDHUB_HOME", "/custom/credhub/home")

Expect(config.ConfigDir()).To(Equal("/custom/credhub/home/.credhub"))
})

It("falls back to HOME when CREDHUB_HOME is not set", func() {
os.Setenv("HOME", "/home/original-user")
os.Unsetenv("CREDHUB_HOME")

Expect(config.ConfigDir()).To(Equal("/home/original-user/.credhub"))
})
})
})
5 changes: 5 additions & 0 deletions config/config_win.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
)

func userHomeDir() string {
home := os.Getenv("CREDHUB_HOME")
if home != "" {
return home
}

home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ran this through an AI tool, and it says that this will be a compile error:

config/config_win.go:17:7: no new variables on left side of :=

For re-declaration of 'home' in the same scope.

if home == "" {
home = os.Getenv("USERPROFILE")
Expand Down
35 changes: 35 additions & 0 deletions config/config_win_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package config_test

import (
"os"
"syscall"

"code.cloudfoundry.org/credhub-cli/config"
Expand Down Expand Up @@ -35,4 +36,38 @@ var _ = Describe("Config (windows specific)", func() {

Expect(attrs & syscall.FILE_ATTRIBUTE_HIDDEN).To(Equal(uint32(syscall.FILE_ATTRIBUTE_HIDDEN)))
})

Describe("CREDHUB_HOME", func() {
var originalCredhubHome string
var originalUserProfile string
var hadCredhubHome bool

BeforeEach(func() {
originalCredhubHome, hadCredhubHome = os.LookupEnv("CREDHUB_HOME")
originalUserProfile = os.Getenv("USERPROFILE")
})

AfterEach(func() {
if hadCredhubHome {
os.Setenv("CREDHUB_HOME", originalCredhubHome)
} else {
os.Unsetenv("CREDHUB_HOME")
}
os.Setenv("USERPROFILE", originalUserProfile)
})

It("uses CREDHUB_HOME instead of USERPROFILE when set", func() {
os.Setenv("USERPROFILE", `C:\Users\original-user`)
os.Setenv("CREDHUB_HOME", `C:\custom\credhub\home`)

Expect(config.ConfigDir()).To(Equal(`C:\custom\credhub\home\.credhub`))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI analysis:
Because path.Join uses /, config.ConfigDir() returns C:\custom\credhub\home/.credhub, which will cause the test assertion to fail.

})

It("falls back to USERPROFILE when CREDHUB_HOME is not set", func() {
os.Setenv("USERPROFILE", `C:\Users\original-user`)
os.Unsetenv("CREDHUB_HOME")

Expect(config.ConfigDir()).To(Equal(`C:\Users\original-user\.credhub`))
})
})
})