Skip to content

Commit 72fb5b9

Browse files
committed
Fix panic caused by setting unsupported private key path values
Previously, setting any sort of unsupported key path value would have resulted in panic due to quirk in SDK, where config provider errors are silently ignored and config provider is set to nil. This is a partial fix to allow home paths to be resolved to avoid such common errors. This also fixes identity user resource to avoid panicking if config provider is empty. SDK still needs to address the ignored error bug, which will be followed-up separately.
1 parent 3f0dc9e commit 72fb5b9

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
- Support for reading OBO token from local file
55
- Support for Oracle Key Vault with ExaCC
66

7+
### Fixed
8+
- Fix an issue where identity user resource panics if an invalid API key configuration is given
9+
- Allow `~` home directories to be specified in private_key_path of provider oci blocks
10+
711
## 4.0.0 (October 21, 2020)
812

913
### Added

oci/identity_user_resource.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,12 @@ func (s *IdentityUserResourceCrud) Create() error {
215215
return fmt.Errorf("compartment_id must be specified for this resource")
216216
}
217217
// Maintain legacy contract of compartment_id defaulting to tenancy ocid if not specified
218-
c := *s.Client.ConfigurationProvider()
219-
if c == nil {
220-
return fmt.Errorf("cannot access tenancyOCID")
218+
configProvider := s.Client.ConfigurationProvider()
219+
if configProvider == nil {
220+
return fmt.Errorf("cannot access tenancy OCID. No configuration provider could be found for identity client")
221221
}
222+
223+
c := *configProvider
222224
tenancy, err := c.TenancyOCID()
223225
if err != nil {
224226
return err

oci/provider.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,15 @@ func getHomeFolder() string {
661661
return current.HomeDir
662662
}
663663

664+
// cleans and expands the path if it contains a tilde , returns the expanded path or the input path as is if not expansion
665+
// was performed
666+
func expandPath(filepath string) string {
667+
if strings.HasPrefix(filepath, fmt.Sprintf("~%c", os.PathSeparator)) {
668+
filepath = path.Join(getHomeFolder(), filepath[2:])
669+
}
670+
return path.Clean(filepath)
671+
}
672+
664673
func checkProfile(profile string, path string) (err error) {
665674
var profileRegex = regexp.MustCompile(`^\[(.*)\]`)
666675
data, err := ioutil.ReadFile(path)
@@ -753,7 +762,8 @@ func (p ResourceDataConfigProvider) PrivateRSAKey() (key *rsa.PrivateKey, err er
753762
}
754763

755764
if privateKeyPath, hasPrivateKeyPath := p.D.GetOkExists(privateKeyPathAttrName); hasPrivateKeyPath {
756-
pemFileContent, readFileErr := ioutil.ReadFile(privateKeyPath.(string))
765+
resolvedPath := expandPath(privateKeyPath.(string))
766+
pemFileContent, readFileErr := ioutil.ReadFile(resolvedPath)
757767
if readFileErr != nil {
758768
return nil, fmt.Errorf("can not read private key from: '%s', Error: %q", privateKeyPath, readFileErr)
759769
}

oci/provider_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,41 @@ func TestUnitVerifyConfigForAPIKeyAuthIsNotSet_basic(t *testing.T) {
916916
assert.True(t, len(apiKeyConfigVariablesToUnset) == 5, "apiKey config variables to unset: %v", apiKeyConfigVariablesToUnset)
917917
}
918918

919+
// This test verifies that user can specify private key paths with "~/" and they should resolve to the home directory
920+
func TestUnitHomeDirectoryPrivateKeyPath_basic(t *testing.T) {
921+
privateKeyName := "TestUnitHomeDirectoryPrivateKeyPath_basic.pem"
922+
privateKeyPath := path.Join(getHomeFolder(), privateKeyName)
923+
err := writeTempFile(testPrivateKey, privateKeyPath)
924+
if err != nil {
925+
t.Fatalf("unable to write test private key into directory %s. Error: %v", privateKeyPath, err)
926+
}
927+
928+
defer removeFile(privateKeyPath)
929+
930+
r := &schema.Resource{
931+
Schema: schemaMap(),
932+
}
933+
d := r.Data(nil)
934+
d.Set(privateKeyPathAttrName, path.Join("~", privateKeyName))
935+
936+
d.Set(tenancyOcidAttrName, testTenancyOCID)
937+
d.Set(authAttrName, authAPIKeySetting)
938+
d.Set(userOcidAttrName, testUserOCID)
939+
d.Set(fingerprintAttrName, testKeyFingerPrint)
940+
d.Set(regionAttrName, "us-phoenix-1")
941+
942+
clients := &OracleClients{
943+
sdkClientMap: make(map[string]interface{}, len(oracleClientRegistrations.registeredClients)),
944+
configuration: make(map[string]string),
945+
}
946+
sdkConfigProvider, err := getSdkConfigProvider(d, clients)
947+
assert.NoError(t, err)
948+
949+
privateRsaKey, err := sdkConfigProvider.PrivateRSAKey()
950+
assert.NoError(t, err)
951+
assert.True(t, privateRsaKey != nil)
952+
}
953+
919954
func TestUnitSecurityToken_basic(t *testing.T) {
920955
t.Skip("Run manual with a valid security token")
921956
for _, apiKeyConfigAttribute := range apiKeyConfigAttributes {

0 commit comments

Comments
 (0)