Skip to content

Commit

Permalink
Fixed issue caused by config names that have uppercase characters (#37)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
balazsgaspar and enis authored Jul 28, 2023
1 parent 02b9a92 commit 11ac3b9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
18 changes: 18 additions & 0 deletions cdp-sdk-go/cdp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions cdp-sdk-go/cdp/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ package cdp

import (
"fmt"
"gopkg.in/ini.v1"
"os"
"strings"

"gopkg.in/ini.v1"
)

const (
Expand Down Expand Up @@ -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
}
Expand Down
25 changes: 23 additions & 2 deletions cdp-sdk-go/cdp/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
Expand All @@ -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"
Expand Down Expand Up @@ -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)
}
}
2 changes: 2 additions & 0 deletions cdp-sdk-go/cdp/testdata/test-config
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ cdp_region=value5
[profile bar]
cdp_region=value6

[profile UPPER_CASE_PROFILE]
CDP_ENDPOINT_URL=value6
6 changes: 5 additions & 1 deletion cdp-sdk-go/cdp/testdata/test-credentials
Original file line number Diff line number Diff line change
Expand Up @@ -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
cdp_private_key=value-from-file

[UPPER_CASE_PROFILE]
CDP_ACCESS_KEY_ID=value8
CDP_PRIVATE_KEY=value9

0 comments on commit 11ac3b9

Please sign in to comment.