Skip to content

Commit ea75b43

Browse files
committed
add InstancePrincipalWithCerts auth mode which allow terraform tests to load local certs for signing purpose.
fix comments. fix the fmt of the code.
1 parent 52a3c28 commit ea75b43

File tree

2 files changed

+80
-19
lines changed

2 files changed

+80
-19
lines changed

oci/provider.go

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net"
1111
"net/http"
1212
"os"
13+
"path/filepath"
1314
"runtime"
1415
"strings"
1516
"time"
@@ -26,20 +27,21 @@ var descriptions map[string]string
2627
var disableAutoRetries bool
2728

2829
const (
29-
authAPIKeySetting = "ApiKey"
30-
authInstancePrincipalSetting = "InstancePrincipal"
31-
requestHeaderOpcOboToken = "opc-obo-token"
32-
requestHeaderOpcHostSerial = "opc-host-serial"
33-
defaultRequestTimeout = 0
34-
defaultConnectionTimeout = 10 * time.Second
35-
defaultTLSHandshakeTimeout = 5 * time.Second
36-
defaultUserAgentProviderName = "Oracle-TerraformProvider"
37-
userAgentFormatter = "Oracle-GoSDK/%s (go/%s; %s/%s; terraform/%s) %s/%s"
38-
userAgentProviderNameEnv = "USER_AGENT_PROVIDER_NAME"
39-
domainNameOverrideEnv = "domain_name_override"
40-
customCertLocationEnv = "custom_cert_location"
41-
oracleR1DomainNameEnv = "oracle_r1_domain_name" // deprecate
42-
r1CertLocationEnv = "R1_CERT_LOCATION" // deprecate
30+
authAPIKeySetting = "ApiKey"
31+
authInstancePrincipalSetting = "InstancePrincipal"
32+
authInstancePrincipalWithCertsSetting = "InstancePrincipalWithCerts"
33+
requestHeaderOpcOboToken = "opc-obo-token"
34+
requestHeaderOpcHostSerial = "opc-host-serial"
35+
defaultRequestTimeout = 0
36+
defaultConnectionTimeout = 10 * time.Second
37+
defaultTLSHandshakeTimeout = 5 * time.Second
38+
defaultUserAgentProviderName = "Oracle-TerraformProvider"
39+
userAgentFormatter = "Oracle-GoSDK/%s (go/%s; %s/%s; terraform/%s) %s/%s"
40+
userAgentProviderNameEnv = "USER_AGENT_PROVIDER_NAME"
41+
domainNameOverrideEnv = "domain_name_override"
42+
customCertLocationEnv = "custom_cert_location"
43+
oracleR1DomainNameEnv = "oracle_r1_domain_name" // deprecate
44+
r1CertLocationEnv = "R1_CERT_LOCATION" // deprecate
4345
)
4446

4547
// OboTokenProvider interface that wraps information about auth tokens so the sdk client can make calls
@@ -96,7 +98,7 @@ func schemaMap() map[string]*schema.Schema {
9698
Optional: true,
9799
Description: descriptions["auth"],
98100
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"TF_VAR_auth", "OCI_AUTH"}, authAPIKeySetting),
99-
ValidateFunc: validation.StringInSlice([]string{authAPIKeySetting, authInstancePrincipalSetting}, true),
101+
ValidateFunc: validation.StringInSlice([]string{authAPIKeySetting, authInstancePrincipalSetting, authInstancePrincipalWithCertsSetting}, true),
100102
},
101103
"tenancy_ocid": {
102104
Type: schema.TypeString,
@@ -473,6 +475,18 @@ func validateConfigForAPIKeyAuth(d *schema.ResourceData) error {
473475
return nil
474476
}
475477

478+
func getCertificateFileBytes(certificateFileFullPath string) (pemRaw []byte, err error) {
479+
absFile, err := filepath.Abs(certificateFileFullPath)
480+
if err != nil {
481+
return nil, fmt.Errorf("can't form absolute path of %s: %v", certificateFileFullPath, err)
482+
}
483+
484+
if pemRaw, err = ioutil.ReadFile(absFile); err != nil {
485+
return nil, fmt.Errorf("can't read %s: %v", certificateFileFullPath, err)
486+
}
487+
return
488+
}
489+
476490
func ProviderConfig(d *schema.ResourceData) (clients interface{}, err error) {
477491
clients = &OracleClients{configuration: map[string]string{}}
478492
disableAutoRetries = d.Get("disable_auto_retries").(bool)
@@ -511,8 +525,52 @@ func ProviderConfig(d *schema.ResourceData) (clients interface{}, err error) {
511525
return nil, err
512526
}
513527
configProviders = append(configProviders, cfg)
528+
case strings.ToLower(authInstancePrincipalWithCertsSetting):
529+
region, ok := d.GetOkExists("region")
530+
if !ok {
531+
return nil, fmt.Errorf("can not get region from Terraform configuration (InstancePrincipalWithCerts)")
532+
}
533+
534+
defaultCertsDir, err := os.Getwd()
535+
if err != nil {
536+
return nil, fmt.Errorf("can not get working directory for current os platform")
537+
}
538+
539+
certsDir := filepath.Clean(getEnvSettingWithDefault("test_certificates_location", defaultCertsDir))
540+
leafCertificateBytes, err := getCertificateFileBytes(filepath.Join(certsDir, "ip_cert.pem"))
541+
if err != nil {
542+
return nil, fmt.Errorf("can not read leaf certificate from %s", filepath.Join(certsDir, "ip_cert.pem"))
543+
}
544+
545+
leafPrivateKeyBytes, err := getCertificateFileBytes(filepath.Join(certsDir, "ip_key.pem"))
546+
if err != nil {
547+
return nil, fmt.Errorf("can not read leaf private key from %s", filepath.Join(certsDir, "ip_key.pem"))
548+
}
549+
550+
leafPassphraseBytes := []byte{}
551+
if _, err := os.Stat(certsDir + "/leaf_passphrase"); !os.IsNotExist(err) {
552+
leafPassphraseBytes, err = getCertificateFileBytes(filepath.Join(certsDir + "leaf_passphrase"))
553+
if err != nil {
554+
return nil, fmt.Errorf("can not read leafPassphraseBytes from %s", filepath.Join(certsDir+"leaf_passphrase"))
555+
}
556+
}
557+
558+
intermediateCertificateBytes, err := getCertificateFileBytes(filepath.Join(certsDir, "intermediate.pem"))
559+
if err != nil {
560+
return nil, fmt.Errorf("can not read intermediate certificate from %s", filepath.Join(certsDir, "intermediate.pem"))
561+
}
562+
563+
intermediateCertificatesBytes := [][]byte{
564+
intermediateCertificateBytes,
565+
}
566+
567+
cfg, err := oci_common_auth.InstancePrincipalConfigurationWithCerts(oci_common.StringToRegion(region.(string)), leafCertificateBytes, leafPassphraseBytes, leafPrivateKeyBytes, intermediateCertificatesBytes)
568+
if err != nil {
569+
return nil, err
570+
}
571+
configProviders = append(configProviders, cfg)
514572
default:
515-
return nil, fmt.Errorf("auth must be one of '%s' or '%s'", authAPIKeySetting, authInstancePrincipalSetting)
573+
return nil, fmt.Errorf("auth must be one of '%s' or '%s' or '%s'", authAPIKeySetting, authInstancePrincipalSetting, authInstancePrincipalWithCertsSetting)
516574
}
517575

518576
configProviders = append(configProviders, ResourceDataConfigProvider{d})

oci/provider_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,15 @@ func GetTestClients(data *schema.ResourceData) *OracleClients {
249249
d.Set("tenancy_ocid", getEnvSettingWithBlankDefault("tenancy_ocid"))
250250
d.Set("region", getEnvSettingWithDefault("region", "us-phoenix-1"))
251251

252-
if getEnvSettingWithDefault("use_obo_token", "false") == "false" {
252+
if auth := getEnvSettingWithDefault("auth", authAPIKeySetting); auth == authAPIKeySetting {
253253
d.Set("auth", getEnvSettingWithDefault("auth", authAPIKeySetting))
254254
d.Set("user_ocid", getEnvSettingWithBlankDefault("user_ocid"))
255255
d.Set("fingerprint", getEnvSettingWithBlankDefault("fingerprint"))
256256
d.Set("private_key_path", getEnvSettingWithBlankDefault("private_key_path"))
257257
d.Set("private_key_password", getEnvSettingWithBlankDefault("private_key_password"))
258258
d.Set("private_key", getEnvSettingWithBlankDefault("private_key"))
259259
} else {
260-
d.Set("auth", getEnvSettingWithDefault("auth", authInstancePrincipalSetting))
260+
d.Set("auth", getEnvSettingWithDefault("auth", auth))
261261
}
262262

263263
client, err := ProviderConfig(d)
@@ -372,8 +372,11 @@ func providerConfigTest(t *testing.T, disableRetries bool, skipRequiredField boo
372372
case authInstancePrincipalSetting:
373373
assert.Regexp(t, "failed to create a new key provider for instance principal.*", err.Error())
374374
return
375+
case authInstancePrincipalWithCertsSetting:
376+
assert.Regexp(t, "failed to create a new key provider for instance principal.*", err.Error())
377+
return
375378
default:
376-
assert.Error(t, err, fmt.Sprintf("auth must be one of '%s' or '%s'", authAPIKeySetting, authInstancePrincipalSetting))
379+
assert.Error(t, err, fmt.Sprintf("auth must be one of '%s' or '%s' or '%s'", authAPIKeySetting, authInstancePrincipalSetting, authInstancePrincipalWithCertsSetting))
377380
return
378381
}
379382
assert.Nil(t, err)

0 commit comments

Comments
 (0)