Skip to content

Commit

Permalink
OSD-15841: Check proxy and API connection if login failed
Browse files Browse the repository at this point in the history
  • Loading branch information
samanthajayasinghe committed Jun 16, 2023
1 parent d933c9e commit a22eb46
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cmd/ocm-backplane/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ func runLogin(cmd *cobra.Command, argv []string) (err error) {
// Hibernating, print an error
return fmt.Errorf("cluster %s is hibernating, login failed", clusterKey)
}
// Check API connection with configured proxy
err = bpConfig.CheckAPIConnection()
if err != nil {
return fmt.Errorf("cannot connect to backplane API URL,Check if you need to use a proxy/VPN to access backplane. error: %v", err)
}

// Otherwise, return the failure
return fmt.Errorf("can't login to cluster: %v", err)
}
Expand Down
19 changes: 18 additions & 1 deletion cmd/ocm-backplane/login/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,24 @@ var _ = Describe("Login command", func() {
err = runLogin(nil, []string{testClusterId})

Expect(err).NotTo(BeNil())
Expect(err.Error()).Should(ContainSubstring("unable to connect to backplane api"))
Expect(err.Error()).Should(ContainSubstring("cannot connect to backplane API URL"))

})

It("should fail when proxy not avaliable", func() {

err := utils.CreateTempKubeConfig(nil)
Expect(err).To(BeNil())
mockOcmInterface.EXPECT().GetTargetCluster(testClusterId).Return(trueClusterId, testClusterId, nil)
mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterId)).Return(false, nil).AnyTimes()
mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil)
mockClientUtil.EXPECT().MakeRawBackplaneAPIClientWithAccessToken(backplaneAPIUri, testToken).Return(mockClient, nil)
mockClient.EXPECT().LoginCluster(gomock.Any(), gomock.Eq(trueClusterId)).Return(fakeResp, errors.New("proxyconnect tcp: dial tcp: lookup yourproxy.com: no such host "))

err = runLogin(nil, []string{testClusterId})

Expect(err).NotTo(BeNil())
Expect(err.Error()).Should(ContainSubstring("Check if you need to use a proxy/VPN to access backplane"))

})

Expand Down
47 changes: 47 additions & 0 deletions pkg/cli/config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package config

import (
"errors"
"net/http"
"net/url"
"os"
"path/filepath"
"time"

"github.com/openshift/backplane-cli/pkg/info"
"github.com/spf13/viper"
Expand Down Expand Up @@ -70,3 +74,46 @@ func GetBackplaneConfiguration() (bpConfig BackplaneConfiguration, err error) {

return bpConfig, nil
}

// CheckAPIConnection validate API connection via configured proxy and VPN
func (config BackplaneConfiguration) CheckAPIConnection() error {

// Check backplane Proxy URL
if config.ProxyURL == "" {
//Cannot connect to backplane API URL: Check if you need to use a proxy/VPN to access backplane
return errors.New("empty proxy url - check your backplane-cli configuration in $HOME/.config/backplane/config.json")
}

// Check proxy connection
connectionOk, err := config.pingBackplaneAPIviaProxy()

if !connectionOk {
return err
}

return nil
}

// pingBackplaneAPIviaProxy returns status of the the API connection
func (config BackplaneConfiguration) pingBackplaneAPIviaProxy() (bool, error) {
client := http.Client{
Timeout: 5 * time.Second,
}

proxyUrl, err := url.Parse(config.ProxyURL)
if err != nil {
return false, err
}
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}

req, err := http.NewRequest("HEAD", config.URL, nil)
if err != nil {
return false, err
}
_, err = client.Do(req)
if err != nil {
return false, err
}

return true, nil
}

0 comments on commit a22eb46

Please sign in to comment.