From 615f81d23d016a9f8c603c5e5016ad3b40aab6f8 Mon Sep 17 00:00:00 2001 From: Thomas Manville Date: Wed, 18 Sep 2019 15:38:39 -0700 Subject: [PATCH] Ensure VPC and Bluemix are non-nil in IBM config (#299) * Ensure VPC and Bluemix are non-nil in IBM config refactoring tests to use SL_* creds env vars adding missing optional, but required api field * Update client.go --- pkg/blockstorage/ibm/client.go | 7 +- pkg/blockstorage/ibm/client_kube_test.go | 43 ++++++++++++- pkg/blockstorage/ibm/client_test.go | 64 +++++++++++++------ .../ibm/testdata/correct/libconfig_old.toml | 21 ++++++ 4 files changed, 115 insertions(+), 20 deletions(-) create mode 100644 pkg/blockstorage/ibm/testdata/correct/libconfig_old.toml diff --git a/pkg/blockstorage/ibm/client.go b/pkg/blockstorage/ibm/client.go index b041e31915..cc7dbed230 100644 --- a/pkg/blockstorage/ibm/client.go +++ b/pkg/blockstorage/ibm/client.go @@ -152,7 +152,11 @@ func getDefIBMStoreSecret(ctx context.Context, args map[string]string) (*ibmcfg. if err != nil { return nil, errors.Wrap(err, "Failed to read Default IBM storage secret.") } - retConfig := ibmcfg.Config{Softlayer: &softLayerCfg} + retConfig := ibmcfg.Config{ + Softlayer: &softLayerCfg, + Bluemix: &ibmcfg.BluemixConfig{}, + VPC: &ibmcfg.VPCProviderConfig{}, + } _, err = toml.Decode(string(storeSecret.Data[IBMK8sSecretData]), &retConfig) if slapi, ok := args[SLAPIKeyArgName]; ok { retConfig.Softlayer.SoftlayerAPIKey = slapi @@ -160,5 +164,6 @@ func getDefIBMStoreSecret(ctx context.Context, args map[string]string) (*ibmcfg. if slusername, ok := args[SLAPIUsernameArgName]; ok { retConfig.Softlayer.SoftlayerUsername = slusername } + return &retConfig, err } diff --git a/pkg/blockstorage/ibm/client_kube_test.go b/pkg/blockstorage/ibm/client_kube_test.go index 3db68d088d..6318ec5f45 100644 --- a/pkg/blockstorage/ibm/client_kube_test.go +++ b/pkg/blockstorage/ibm/client_kube_test.go @@ -30,7 +30,9 @@ import ( ) const ( - testSecretName = "unitetestsecret" + testSecretName = "unitetestsecret" + testOldSecretName = "oldibmsecret" + oldTestTomlPath = "./testdata/correct/libconfig_old.toml" ) type KubeTestIBMClient struct { @@ -89,6 +91,45 @@ func (s KubeTestIBMClient) TestIBMSecret(c *C) { c.Assert(err, IsNil) } +func (s KubeTestIBMClient) TestIBMOldSecret(c *C) { + apiKey := os.Getenv(IBMApiKeyEnv) + err := os.Unsetenv(IBMApiKeyEnv) + c.Assert(err, IsNil) + defer os.Setenv(IBMApiKeyEnv, apiKey) + secData, err := ioutil.ReadFile(oldTestTomlPath) + c.Assert(err, IsNil) + secretData := make(map[string][]byte) + secretData[IBMK8sSecretData] = secData + + s.k8scli, err = kube.NewClient() + c.Assert(err, IsNil) + k8sSec := v1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: testOldSecretName, + }, + Type: v1.SecretTypeOpaque, + Data: secretData, + } + s.k8sSec, err = s.k8scli.CoreV1().Secrets(IBMK8sSecretNS).Create(&k8sSec) + defer s.k8scli.CoreV1().Secrets(IBMK8sSecretNS).Delete(testOldSecretName, &metav1.DeleteOptions{}) + c.Assert(err, IsNil) + slAPIKey, ok := os.LookupEnv(IBMSLApiKeyEnv) + c.Check(slAPIKey, NotNil) + c.Check(ok, Equals, true) + slAPIUsername, ok := os.LookupEnv(IBMSLApiUsernameEnv) + c.Check(slAPIUsername, NotNil) + c.Check(ok, Equals, true) + ibmCli, err := newClient(context.Background(), map[string]string{CfgSecretNameArgName: testOldSecretName, SLAPIKeyArgName: slAPIKey, SLAPIUsernameArgName: slAPIUsername}) + c.Assert(err, IsNil) + c.Assert(ibmCli, NotNil) + c.Assert(ibmCli.Service, NotNil) + defer ibmCli.Service.Close() + defer s.k8scli.CoreV1().Secrets(IBMK8sSecretNS).Delete(testOldSecretName, &metav1.DeleteOptions{}) + c.Assert(*ibmCli, FitsTypeOf, client{}) + _, err = ibmCli.Service.ListSnapshots() + c.Assert(err, IsNil) +} + func (s *KubeTestIBMClient) TestSecretWSLApiKey(c *C) { testSlKey := "TestSlKey" testSlUserName := "TestUserName" diff --git a/pkg/blockstorage/ibm/client_test.go b/pkg/blockstorage/ibm/client_test.go index 195b2c8a25..11daf2b337 100644 --- a/pkg/blockstorage/ibm/client_test.go +++ b/pkg/blockstorage/ibm/client_test.go @@ -27,10 +27,12 @@ import ( ) const ( - testTomlPath = "testdata/correct" - testBogusPath = "testdata/incorrect" - workAroundEnv = "IBM_STORE_TOML" - IBMApiKeyEnv = "IBM_API_KEY" + testTomlPath = "testdata/correct" + testBogusPath = "testdata/incorrect" + workAroundEnv = "IBM_STORE_TOML" + IBMApiKeyEnv = "IBM_API_KEY" + IBMSLApiKeyEnv = "IBM_SL_API_KEY" + IBMSLApiUsernameEnv = "IBM_SL_API_USERNAME" ) //These are not executed as part of Pipeline, but usefull for development @@ -38,16 +40,24 @@ const ( func Test(t *testing.T) { TestingT(t) } type ClientSuite struct { - apiKey string + apiKey string + slAPIKey string + slAPIUsername string } var _ = Suite(&ClientSuite{}) func (s *ClientSuite) SetUpSuite(c *C) { - if os.Getenv(IBMApiKeyEnv) == "" { - c.Skip(IBMApiKeyEnv + " envionment variable not set") + var ok bool + if s.slAPIKey, ok = os.LookupEnv(IBMSLApiKeyEnv); ok { + if s.slAPIUsername, ok = os.LookupEnv(IBMSLApiUsernameEnv); ok { + return + } } - s.apiKey = os.Getenv(IBMApiKeyEnv) + if s.apiKey, ok = os.LookupEnv(IBMApiKeyEnv); ok { + return + } + c.Skip(fmt.Sprintf("One of %s, %s and %s environment variable is not set", IBMApiKeyEnv, IBMSLApiKeyEnv, IBMSLApiUsernameEnv)) } func (s *ClientSuite) TearDownSuite(c *C) { @@ -56,13 +66,15 @@ func (s *ClientSuite) TearDownSuite(c *C) { } func (s *ClientSuite) TestAPIClient(c *C) { - var apiKey string - if apiK, ok := os.LookupEnv(IBMApiKeyEnv); ok { - apiKey = apiK + if tomlPath, ok := os.LookupEnv(workAroundEnv); ok { + err := os.Setenv(LibDefCfgEnv, filepath.Dir(tomlPath)) + c.Assert(err, IsNil) + defer os.Unsetenv(LibDefCfgEnv) } else { - c.Skip(fmt.Sprintf("Could not find env var %s with API key", IBMApiKeyEnv)) + c.Skip(workAroundEnv + " TOML path is not present") } - ibmCli, err := newClient(context.Background(), map[string]string{APIKeyArgName: apiKey}) + args := s.getCredsMap(c) + ibmCli, err := newClient(context.Background(), args) c.Assert(err, IsNil) c.Assert(ibmCli, NotNil) c.Assert(ibmCli.Service, NotNil) @@ -73,13 +85,16 @@ func (s *ClientSuite) TestAPIClient(c *C) { } func (s *ClientSuite) TestIBMClientSoftlayerFile(c *C) { - var apiKey string - if apiK, ok := os.LookupEnv(IBMApiKeyEnv); ok { - apiKey = apiK + if tomlPath, ok := os.LookupEnv(workAroundEnv); ok { + err := os.Setenv(LibDefCfgEnv, filepath.Dir(tomlPath)) + c.Assert(err, IsNil) + defer os.Unsetenv(LibDefCfgEnv) } else { - c.Skip(fmt.Sprintf("Could not find env var %s with API key", IBMApiKeyEnv)) + c.Skip(workAroundEnv + " TOML path is not present") } - ibmCli, err := newClient(context.Background(), map[string]string{APIKeyArgName: apiKey, SoftlayerFileAttName: "true"}) + args := s.getCredsMap(c) + args[SoftlayerFileAttName] = "true" + ibmCli, err := newClient(context.Background(), args) defer ibmCli.Service.Close() c.Assert(err, IsNil) c.Assert(ibmCli.Service, NotNil) @@ -121,8 +136,21 @@ func (s *ClientSuite) TestErrorsCases(c *C) { c.Assert(err, NotNil) c.Assert(ibmCli, IsNil) err = os.Setenv(LibDefCfgEnv, testBogusPath) + defer os.Unsetenv(LibDefCfgEnv) c.Assert(err, IsNil) ibmCli, err = newClient(context.Background(), make(map[string]string)) c.Assert(err, NotNil) c.Assert(ibmCli, IsNil) + +} + +func (s *ClientSuite) getCredsMap(c *C) map[string]string { + if s.slAPIKey != "" { + return map[string]string{SLAPIKeyArgName: s.slAPIKey, SLAPIUsernameArgName: s.slAPIUsername} + } + if s.apiKey != "" { + return map[string]string{APIKeyArgName: s.apiKey} + } + c.Skip(fmt.Sprintf("Neither of %s, %s environment variables set", IBMApiKeyEnv, IBMSLApiKeyEnv)) + return map[string]string{} } diff --git a/pkg/blockstorage/ibm/testdata/correct/libconfig_old.toml b/pkg/blockstorage/ibm/testdata/correct/libconfig_old.toml new file mode 100644 index 0000000000..3726ba6d16 --- /dev/null +++ b/pkg/blockstorage/ibm/testdata/correct/libconfig_old.toml @@ -0,0 +1,21 @@ +[bluemix] +iam_url = "https://iam.bluemix.net" +iam_client_id = "bx" +iam_client_secret = "bx" +iam_api_key = "free" +refresh_token = "" +containers_api_route = "https://origin.containers.test.cloud.ibm.com" + +[softlayer] +softlayer_block_enabled = true +softlayer_block_provider_name = "SOFTLAYER-BLOCK" +softlayer_file_enabled = false +softlayer_file_provider_name = "SOFTLAYER-FILE" +softlayer_username = "" +softlayer_api_key = "" +softlayer_endpoint_url = "https://api.softlayer.com/rest/v3" +softlayer_iam_endpoint_url = "https://api.softlayer.com/mobile/v3" +softlayer_datacenter = "sjc03" +softlayer_api_timeout = "30s" +softlayer_vol_provision_timeout = "30m" +softlayer_api_retry_interval = "5s"