Skip to content

Commit

Permalink
Treat error when no valid credential source is set
Browse files Browse the repository at this point in the history
  • Loading branch information
biazmoreira committed Dec 8, 2023
1 parent e44741d commit cd2dcfe
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
8 changes: 7 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/hashicorp/hcp-sdk-go/config"
Expand Down Expand Up @@ -42,7 +43,7 @@ func (h InternalHCPTokenHelper) GetHCPToken() (*HCPToken, error) {
return nil, err
}
// no valid connection to hcp
if configCache == nil || configCache.ProxyAddr == "" {
if configCache == nil {
return nil, nil
}

Expand All @@ -61,6 +62,11 @@ func (h InternalHCPTokenHelper) GetHCPToken() (*HCPToken, error) {

tk, err := hcp.Token()
if err != nil {
if strings.Contains(err.Error(), "no valid credential source available") {
_ = eraseConfig()
return nil, nil
}

return nil, fmt.Errorf("failed to retrieve the HCP token: %w", err)
}

Expand Down
65 changes: 65 additions & 0 deletions connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ func Test_HCPConnectCommand(t *testing.T) {
State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(
hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING,
),
Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{
NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{
HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED),
},
},
},
},
}, nil)
Expand Down Expand Up @@ -353,6 +358,11 @@ func Test_getCluster(t *testing.T) {
ID: "cluster-1",
DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-1.addr:8200"},
State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING),
Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{
NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{
HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED),
},
},
},
},
},
Expand All @@ -369,6 +379,11 @@ func Test_getCluster(t *testing.T) {
ID: "cluster-1",
DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-1.addr:8200"},
State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING),
Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{
NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{
HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED),
},
},
},
},
},
Expand All @@ -387,16 +402,31 @@ func Test_getCluster(t *testing.T) {
ID: "cluster-1",
DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-1.addr:8200"},
State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING),
Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{
NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{
HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED),
},
},
},
{
ID: "cluster-2",
DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-2.addr:8200"},
State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING),
Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{
NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{
HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED),
},
},
},
{
ID: "cluster-3",
DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-3.addr:8200"},
State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING),
Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{
NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{
HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED),
},
},
},
},
},
Expand All @@ -414,16 +444,31 @@ func Test_getCluster(t *testing.T) {
ID: "cluster-1",
DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-1.addr:8200"},
State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING),
Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{
NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{
HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED),
},
},
},
{
ID: "cluster-2",
DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-2.addr:8200"},
State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING),
Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{
NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{
HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED),
},
},
},
{
ID: "cluster-3",
DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-3.addr:8200"},
State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING),
Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{
NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{
HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED),
},
},
},
},
},
Expand Down Expand Up @@ -495,6 +540,11 @@ func Test_getProxyAddr(t *testing.T) {
ID: "cluster-1",
DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-1.addr:8200"},
State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING),
Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{
NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{
HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED),
},
},
},
},
},
Expand Down Expand Up @@ -531,6 +581,11 @@ func Test_getProxyAddr(t *testing.T) {
ID: "cluster-1",
DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-1.addr:8200"},
State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING),
Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{
NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{
HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED),
},
},
},
},
},
Expand All @@ -557,6 +612,11 @@ func Test_getProxyAddr(t *testing.T) {
ID: "cluster-1",
DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-1.addr:8200"},
State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING),
Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{
NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{
HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED),
},
},
},
},
},
Expand All @@ -583,6 +643,11 @@ func Test_getProxyAddr(t *testing.T) {
ID: "cluster-1",
DNSNames: &hcpvsm.HashicorpCloudVault20201125ClusterDNSNames{Proxy: "hcp-proxy-cluster-1.addr:8200"},
State: hcpvsm.NewHashicorpCloudVault20201125ClusterState(hcpvsm.HashicorpCloudVault20201125ClusterStateRUNNING),
Config: &hcpvsm.HashicorpCloudVault20201125ClusterConfig{
NetworkConfig: &hcpvsm.HashicorpCloudVault20201125NetworkConfig{
HTTPProxyOption: hcpvsm.NewHashicorpCloudVault20201125HTTPProxyOption(hcpvsm.HashicorpCloudVault20201125HTTPProxyOptionENABLED),
},
},
},
},
},
Expand Down

0 comments on commit cd2dcfe

Please sign in to comment.