Skip to content

Commit

Permalink
cli: fixed HOME not set error (#26243)
Browse files Browse the repository at this point in the history
* cli: fixed HOME not set error

* changelog

* changelog but better

* Slightly better error handling
  • Loading branch information
jasonodonnell committed Apr 2, 2024
1 parent a942597 commit 39499e6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
4 changes: 4 additions & 0 deletions changelog/26243.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```release-note:bug
cli: fixed a bug where the Vault CLI would error out if
HOME was not set.
```
23 changes: 17 additions & 6 deletions command/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/hashicorp/vault/command/config"
"github.com/hashicorp/vault/helper/namespace"
"github.com/mattn/go-isatty"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/posener/complete"
)
Expand Down Expand Up @@ -84,8 +85,13 @@ type BaseCommand struct {
func (c *BaseCommand) Client() (*api.Client, error) {
// Read the test client if present
if c.client != nil {
if err := c.applyHCPConfig(); err != nil {
return nil, err
// Ignoring homedir errors here and moving on to avoid
// spamming user with warnings/errors that homedir isn't set.
path, err := homedir.Dir()
if err == nil {
if err := c.applyHCPConfig(path); err != nil {
return nil, err
}
}

return c.client, nil
Expand Down Expand Up @@ -196,8 +202,13 @@ func (c *BaseCommand) Client() (*api.Client, error) {

c.client = client

if err := c.applyHCPConfig(); err != nil {
return nil, err
// Ignoring homedir errors here and moving on to avoid
// spamming user with warnings/errors that homedir isn't set.
path, err := homedir.Dir()
if err == nil {
if err := c.applyHCPConfig(path); err != nil {
return nil, err
}
}

if c.addrWarning != "" && c.UI != nil {
Expand All @@ -211,12 +222,12 @@ func (c *BaseCommand) Client() (*api.Client, error) {
return client, nil
}

func (c *BaseCommand) applyHCPConfig() error {
func (c *BaseCommand) applyHCPConfig(path string) error {
if c.hcpTokenHelper == nil {
c.hcpTokenHelper = c.HCPTokenHelper()
}

hcpToken, err := c.hcpTokenHelper.GetHCPToken()
hcpToken, err := c.hcpTokenHelper.GetHCPToken(path)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ require (
github.com/hashicorp/raft-boltdb/v2 v2.3.0
github.com/hashicorp/raft-snapshot v1.0.4
github.com/hashicorp/raft-wal v0.4.0
github.com/hashicorp/vault-hcp-lib v0.0.0-20240126195955-473e9a48e7b7
github.com/hashicorp/vault-hcp-lib v0.0.0-20240402205111-2312b38227ab
github.com/hashicorp/vault-plugin-auth-alicloud v0.17.0
github.com/hashicorp/vault-plugin-auth-azure v0.17.0
github.com/hashicorp/vault-plugin-auth-centrify v0.15.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2520,8 +2520,8 @@ github.com/hashicorp/raft-wal v0.4.0/go.mod h1:A6vP5o8hGOs1LHfC1Okh9xPwWDcmb6Vvu
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/hashicorp/serf v0.10.1 h1:Z1H2J60yRKvfDYAOZLd2MU0ND4AH/WDz7xYHDWQsIPY=
github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfEvMqbG+4=
github.com/hashicorp/vault-hcp-lib v0.0.0-20240126195955-473e9a48e7b7 h1:D9XTgYgpQgdZHToRpJQ6fZwWyOOSpLX3Y+D2aMlxQh4=
github.com/hashicorp/vault-hcp-lib v0.0.0-20240126195955-473e9a48e7b7/go.mod h1:KpSNItDH9ojFPf4UkGCB0vv3cAAwvVxBU2On4EZ0f7c=
github.com/hashicorp/vault-hcp-lib v0.0.0-20240402205111-2312b38227ab h1:n1GzFf7LwpVebVIjh5XKW2IQa/BqI/zPlFg2mmB26dQ=
github.com/hashicorp/vault-hcp-lib v0.0.0-20240402205111-2312b38227ab/go.mod h1:Nb41BTPvmFbKB73D/+XpxIw6Nf2Rt+AOUvLzlDxwAGQ=
github.com/hashicorp/vault-plugin-auth-alicloud v0.17.0 h1:0SOkYxjMjph3Tbtv37+pANJQnYDvlAdjKpdEbK6zzZs=
github.com/hashicorp/vault-plugin-auth-alicloud v0.17.0/go.mod h1:79KUWOxY6Ftoad7b+vEmyCmY6eYKdHiADTP0w0TunsE=
github.com/hashicorp/vault-plugin-auth-azure v0.17.0 h1:nFsWQV+sMEdJCvKpVODNeTPP36n5bi6yiQpBOdBsQWw=
Expand Down

0 comments on commit 39499e6

Please sign in to comment.