Skip to content

Commit 3cd9542

Browse files
ccushingdshelbyo
authored andcommitted
Prevent User, Group and Compartment from resolving the wrong TenancyOCID with InstancePrincipal auth
1 parent 2c9181f commit 3cd9542

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

oci/identity_compartment_resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func createCompartment(d *schema.ResourceData, m interface{}) error {
9696
sync := &CompartmentResourceCrud{}
9797
sync.D = d
9898
sync.Client = m.(*OracleClients).identityClient
99+
sync.Configuration = m.(*OracleClients).configuration
99100

100101
return CreateResource(d, sync)
101102
}
@@ -132,6 +133,7 @@ func deleteCompartment(d *schema.ResourceData, m interface{}) error {
132133
type CompartmentResourceCrud struct {
133134
BaseCrud
134135
Client *oci_identity.IdentityClient
136+
Configuration map[string]string
135137
Res *oci_identity.Compartment
136138
DisableNotFoundRetries bool
137139
}
@@ -171,6 +173,10 @@ func (s *CompartmentResourceCrud) Create() error {
171173
tmp := compartmentId.(string)
172174
request.CompartmentId = &tmp
173175
} else { // @next-break: remove
176+
// Prevent potentially inferring wrong TenancyOCID from InstancePrincipal
177+
if auth := s.Configuration["auth"]; strings.ToLower(auth) == strings.ToLower(authInstancePrincipalSetting) {
178+
return fmt.Errorf("compartment_id must be specified for this resource")
179+
}
174180
// Maintain legacy contract of compartment_id defaulting to tenancy ocid if not specified
175181
c := *s.Client.ConfigurationProvider()
176182
if c == nil {

oci/identity_group_resource.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"fmt"
88
"strconv"
9+
"strings"
910
"time"
1011

1112
"github.com/hashicorp/terraform/helper/schema"
@@ -86,6 +87,7 @@ func createGroup(d *schema.ResourceData, m interface{}) error {
8687
sync := &GroupResourceCrud{}
8788
sync.D = d
8889
sync.Client = m.(*OracleClients).identityClient
90+
sync.Configuration = m.(*OracleClients).configuration
8991

9092
return CreateResource(d, sync)
9193
}
@@ -118,6 +120,7 @@ func deleteGroup(d *schema.ResourceData, m interface{}) error {
118120
type GroupResourceCrud struct {
119121
BaseCrud
120122
Client *oci_identity.IdentityClient
123+
Configuration map[string]string
121124
Res *oci_identity.Group
122125
DisableNotFoundRetries bool
123126
}
@@ -156,7 +159,12 @@ func (s *GroupResourceCrud) Create() error {
156159
if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok {
157160
tmp := compartmentId.(string)
158161
request.CompartmentId = &tmp
159-
} else {
162+
} else { // @next-break: remove
163+
// Prevent potentially inferring wrong TenancyOCID from InstancePrincipal
164+
if auth := s.Configuration["auth"]; strings.ToLower(auth) == strings.ToLower(authInstancePrincipalSetting) {
165+
return fmt.Errorf("compartment_id must be specified for this resource")
166+
}
167+
// Maintain legacy contract of compartment_id defaulting to tenancy ocid if not specified
160168
c := *s.Client.ConfigurationProvider()
161169
if c == nil {
162170
return fmt.Errorf("cannot access tenancyOCID")

oci/identity_user_resource.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ package provider
55
import (
66
"context"
77
"fmt"
8+
"strconv"
9+
"strings"
810

911
"github.com/hashicorp/terraform/helper/schema"
1012

11-
"strconv"
12-
1313
oci_identity "github.com/oracle/oci-go-sdk/identity"
1414
)
1515

@@ -86,6 +86,7 @@ func createUser(d *schema.ResourceData, m interface{}) error {
8686
sync := &UserResourceCrud{}
8787
sync.D = d
8888
sync.Client = m.(*OracleClients).identityClient
89+
sync.Configuration = m.(*OracleClients).configuration
8990

9091
return CreateResource(d, sync)
9192
}
@@ -118,6 +119,7 @@ func deleteUser(d *schema.ResourceData, m interface{}) error {
118119
type UserResourceCrud struct {
119120
BaseCrud
120121
Client *oci_identity.IdentityClient
122+
Configuration map[string]string
121123
Res *oci_identity.User
122124
DisableNotFoundRetries bool
123125
}
@@ -156,7 +158,12 @@ func (s *UserResourceCrud) Create() error {
156158
if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok {
157159
tmp := compartmentId.(string)
158160
request.CompartmentId = &tmp
159-
} else {
161+
} else { // @next-break: remove
162+
// Prevent potentially inferring wrong TenancyOCID from InstancePrincipal
163+
if auth := s.Configuration["auth"]; strings.ToLower(auth) == strings.ToLower(authInstancePrincipalSetting) {
164+
return fmt.Errorf("compartment_id must be specified for this resource")
165+
}
166+
// Maintain legacy contract of compartment_id defaulting to tenancy ocid if not specified
160167
c := *s.Client.ConfigurationProvider()
161168
if c == nil {
162169
return fmt.Errorf("cannot access tenancyOCID")

oci/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,10 @@ func validateConfigForAPIKeyAuth(d *schema.ResourceData) error {
473473
}
474474

475475
func ProviderConfig(d *schema.ResourceData) (clients interface{}, err error) {
476-
clients = &OracleClients{}
476+
clients = &OracleClients{configuration: map[string]string{}}
477477
disableAutoRetries = d.Get("disable_auto_retries").(bool)
478478
auth := strings.ToLower(d.Get("auth").(string))
479+
clients.(*OracleClients).configuration["auth"] = auth
479480

480481
userAgentProviderName := getEnvSettingWithDefault(userAgentProviderNameEnv, defaultUserAgentProviderName)
481482
userAgent := fmt.Sprintf(userAgentFormatter, oci_common.Version(), runtime.Version(), runtime.GOOS, runtime.GOARCH, terraform.VersionString(), userAgentProviderName, Version)

oci/provider_clients.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ type OracleClients struct {
279279
loadBalancerClient *oci_load_balancer.LoadBalancerClient
280280
objectStorageClient *oci_object_storage.ObjectStorageClient
281281
virtualNetworkClient *oci_core.VirtualNetworkClient
282+
configuration map[string]string
282283
}
283284

284285
func (m *OracleClients) KmsCryptoClient(endpoint string) (*oci_kms.KmsCryptoClient, error) {

0 commit comments

Comments
 (0)