From cd2dcfe82e429a669dd142feec18ccca7c7cc794 Mon Sep 17 00:00:00 2001 From: Bianca Moreira Date: Fri, 8 Dec 2023 11:06:18 +0100 Subject: [PATCH] Treat error when no valid credential source is set --- config.go | 8 +++++- connect_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 894826f..eafcfc5 100644 --- a/config.go +++ b/config.go @@ -8,6 +8,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "time" "github.com/hashicorp/hcp-sdk-go/config" @@ -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 } @@ -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) } diff --git a/connect_test.go b/connect_test.go index b792f1e..41501b8 100644 --- a/connect_test.go +++ b/connect_test.go @@ -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) @@ -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), + }, + }, }, }, }, @@ -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), + }, + }, }, }, }, @@ -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), + }, + }, }, }, }, @@ -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), + }, + }, }, }, }, @@ -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), + }, + }, }, }, }, @@ -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), + }, + }, }, }, }, @@ -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), + }, + }, }, }, }, @@ -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), + }, + }, }, }, },