Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue caused by config names that have uppercase characters #37

Merged
merged 3 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading