From 11ac3b9a73e72b266360dd0e34f3d4e4af71c179 Mon Sep 17 00:00:00 2001 From: Balazs Gaspar Date: Sat, 29 Jul 2023 00:23:16 +0200 Subject: [PATCH] Fixed issue caused by config names that have uppercase characters (#37) * Fixed issue caused by config names that have uppercase characters * Fixed unit test and add more test cases We decided to follow the Python CDP CLI semantics for case sensitivity to be compatible between the two tools and least surprises to the end user. Python INI file parsing defaults to case sensitive section names, but case insensitive option names. We do the same in the go SDK. This commit adds some more unit tests. --------- Co-authored-by: Enis Soztutar --- cdp-sdk-go/cdp/config_test.go | 18 +++++++++++++++++ cdp-sdk-go/cdp/credentials.go | 9 +++++++-- cdp-sdk-go/cdp/credentials_test.go | 25 ++++++++++++++++++++++-- cdp-sdk-go/cdp/testdata/test-config | 2 ++ cdp-sdk-go/cdp/testdata/test-credentials | 6 +++++- 5 files changed, 55 insertions(+), 5 deletions(-) diff --git a/cdp-sdk-go/cdp/config_test.go b/cdp-sdk-go/cdp/config_test.go index 14a3b209..aeab6cf1 100644 --- a/cdp-sdk-go/cdp/config_test.go +++ b/cdp-sdk-go/cdp/config_test.go @@ -204,6 +204,24 @@ func TestLoadConfigFileFromEnv(t *testing.T) { common.AssertEquals(t, "value1", endpoint) } +func TestGetCdpProfileCaseSensitivity(t *testing.T) { + // We support case sensitive section names, but case insensitive option names + config := Config{ + ConfigFile: "testdata/test-config", + Profile: "UPPER_CASE_PROFILE", + } + err := config.loadConfig() + if err != nil { + t.Fatal(err) + } + + endpoint, err := config.GetCdpApiEndpoint() + if err != nil { + t.Fatalf("Error getting the config value: %v", err) + } + common.AssertEquals(t, "value6", endpoint) +} + func TestGetCdpRegionFromConfig(t *testing.T) { config := Config{ CdpRegion: "foo", diff --git a/cdp-sdk-go/cdp/credentials.go b/cdp-sdk-go/cdp/credentials.go index ea48fcf9..c5ab188c 100644 --- a/cdp-sdk-go/cdp/credentials.go +++ b/cdp-sdk-go/cdp/credentials.go @@ -12,9 +12,10 @@ package cdp import ( "fmt" - "gopkg.in/ini.v1" "os" "strings" + + "gopkg.in/ini.v1" ) const ( @@ -140,7 +141,11 @@ func (p *ChainCredentialsProvider) GetCredentials() (*Credentials, error) { // maps to a set of key value pairs. func rawParseConfigFile(path string) (map[string]map[string]string, error) { properties := make(map[string]map[string]string) - cfg, err := ini.InsensitiveLoad(path) + cfg, err := ini.LoadSources( + ini.LoadOptions{ + InsensitiveKeys: true, + }, + path) if err != nil { return nil, err } diff --git a/cdp-sdk-go/cdp/credentials_test.go b/cdp-sdk-go/cdp/credentials_test.go index eeae172c..b4282091 100644 --- a/cdp-sdk-go/cdp/credentials_test.go +++ b/cdp-sdk-go/cdp/credentials_test.go @@ -11,9 +11,10 @@ package cdp import ( - "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/common" "os" "testing" + + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/common" ) func TestEnvCdpCredentialsProviderWithUnsetEnv(t *testing.T) { @@ -73,8 +74,9 @@ func TestEnvCdpCredentialsProviderWithAccessKeyAndPrivateKey(t *testing.T) { } } -func TestLoadCdpCredentialsFile(t *testing.T) { +func TestRawParseConfigFile(t *testing.T) { expected := map[string]map[string]string{ + "DEFAULT": {}, "default": { "cdp_access_key_id": "value1", "cdp_private_key": "value2", @@ -92,6 +94,10 @@ func TestLoadCdpCredentialsFile(t *testing.T) { "cdp_access_key_id": "value-from-file", "cdp_private_key": "value-from-file", }, + "UPPER_CASE_PROFILE": { + "cdp_access_key_id": "value8", + "cdp_private_key": "value9", + }, } path := "testdata/test-credentials" @@ -177,3 +183,18 @@ func TestConfigCdpCredentialsProviderNonEmptyConfig(t *testing.T) { t.Errorf("Wrong values returned as CDP credentials: accesKey: %s privateKey:%s", res.AccessKeyId, res.PrivateKey) } } + +func TestFileCdpCredentialsProviderCaseSensitivity(t *testing.T) { + path := "testdata/test-credentials" + profile := "UPPER_CASE_PROFILE" + cdpCredentials, err := GetCredentialsFromFileProvider(t, path, profile) + if err != nil || cdpCredentials == nil { + t.Fatal(err) + } + if cdpCredentials.AccessKeyId != "value8" { + t.Errorf("%s should have been %s for the profile: %s", cdpAccessKeyIdPropertyKey, "value6", profile) + } + if cdpCredentials.PrivateKey != "value9" { + t.Errorf("%s should have been %s for the profile: %s", cdpPrivateKeyPropertyKey, "value7", profile) + } +} diff --git a/cdp-sdk-go/cdp/testdata/test-config b/cdp-sdk-go/cdp/testdata/test-config index 2ac01bc3..fa0c1955 100644 --- a/cdp-sdk-go/cdp/testdata/test-config +++ b/cdp-sdk-go/cdp/testdata/test-config @@ -10,3 +10,5 @@ cdp_region=value5 [profile bar] cdp_region=value6 +[profile UPPER_CASE_PROFILE] +CDP_ENDPOINT_URL=value6 diff --git a/cdp-sdk-go/cdp/testdata/test-credentials b/cdp-sdk-go/cdp/testdata/test-credentials index 2f74e882..9a909cf2 100644 --- a/cdp-sdk-go/cdp/testdata/test-credentials +++ b/cdp-sdk-go/cdp/testdata/test-credentials @@ -13,4 +13,8 @@ cdp_private_key=value7 [file_cdp_credentials_provider_profile] cdp_access_key_id=value-from-file -cdp_private_key=value-from-file \ No newline at end of file +cdp_private_key=value-from-file + +[UPPER_CASE_PROFILE] +CDP_ACCESS_KEY_ID=value8 +CDP_PRIVATE_KEY=value9 \ No newline at end of file