Skip to content

Commit 4351daf

Browse files
committed
Adding support for R1
1 parent 4879bd3 commit 4351daf

File tree

1 file changed

+69
-9
lines changed

1 file changed

+69
-9
lines changed

provider/provider.go

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package provider
55
import (
66
"crypto/rsa"
77
"crypto/tls"
8+
"crypto/x509"
89
"fmt"
910
"io/ioutil"
1011
"net"
@@ -40,6 +41,7 @@ const (
4041
defaultConnectionTimeout = 10 * time.Second
4142
defaultTLSHandshakeTimeout = 5 * time.Second
4243
userAgentFormatter = "Oracle-GoSDK/%s (go/%s; %s/%s; terraform/%s) Oracle-TerraformProvider/%s"
44+
r1CertLocationEnv = "R1_CERT_LOCATION"
4345
)
4446

4547
type oboTokenProviderFromEnv struct{}
@@ -420,20 +422,68 @@ func setGoSDKClients(clients *OracleClients, officialSdkConfigProvider oci_commo
420422
oboTokenProvider = oci_common.NewEmptyOboTokenProvider()
421423
}
422424

423-
configureClient := func(client *oci_common.BaseClient) {
425+
configureClient := func(client *oci_common.BaseClient) error {
424426
client.HTTPClient = httpClient
425427
client.UserAgent = userAgent
426428
client.Obo = oboTokenProvider
429+
430+
// R1 Support
431+
if region, err := officialSdkConfigProvider.Region(); err == nil && strings.ToLower(region) == "r1" {
432+
service := strings.Split(client.Host, ".")[0]
433+
client.Host = fmt.Sprintf("%s.r1.oracleiaas.com", service)
434+
435+
pool := x509.NewCertPool()
436+
//readCertPem reads the pem files to a []byte
437+
cert, err := readCertPem()
438+
if err != nil {
439+
return err
440+
}
441+
if ok := pool.AppendCertsFromPEM(cert); !ok {
442+
return fmt.Errorf("failed to append R1 cert to the cert pool")
443+
}
444+
//install the certificates to the client
445+
if h, ok := client.HTTPClient.(*http.Client); ok {
446+
tr := &http.Transport{TLSClientConfig: &tls.Config{RootCAs: pool}}
447+
h.Transport = tr
448+
} else {
449+
return fmt.Errorf("the client dispatcher is not of http.Client type. can not patch the tls config")
450+
}
451+
}
452+
return nil
427453
}
428454

429-
configureClient(&blockStorageClient.BaseClient)
430-
configureClient(&computeClient.BaseClient)
431-
configureClient(&databaseClient.BaseClient)
432-
configureClient(&fileStorageClient.BaseClient)
433-
configureClient(&identityClient.BaseClient)
434-
configureClient(&loadBalancerClient.BaseClient)
435-
configureClient(&objectStorageClient.BaseClient)
436-
configureClient(&virtualNetworkClient.BaseClient)
455+
err = configureClient(&blockStorageClient.BaseClient)
456+
if err != nil {
457+
return
458+
}
459+
err = configureClient(&computeClient.BaseClient)
460+
if err != nil {
461+
return
462+
}
463+
err = configureClient(&databaseClient.BaseClient)
464+
if err != nil {
465+
return
466+
}
467+
err = configureClient(&fileStorageClient.BaseClient)
468+
if err != nil {
469+
return
470+
}
471+
err = configureClient(&identityClient.BaseClient)
472+
if err != nil {
473+
return
474+
}
475+
err = configureClient(&loadBalancerClient.BaseClient)
476+
if err != nil {
477+
return
478+
}
479+
err = configureClient(&objectStorageClient.BaseClient)
480+
if err != nil {
481+
return
482+
}
483+
err = configureClient(&virtualNetworkClient.BaseClient)
484+
if err != nil {
485+
return
486+
}
437487

438488
clients.blockStorageClient = &blockStorageClient
439489
clients.computeClient = &computeClient
@@ -535,3 +585,13 @@ func (p ResourceDataConfigProvider) PrivateRSAKey() (key *rsa.PrivateKey, err er
535585

536586
return nil, fmt.Errorf("can not get private_key or private_key_path from Terraform configuration")
537587
}
588+
589+
func readCertPem() (file []byte, err error) {
590+
r1CertLoc := getEnvSetting(r1CertLocationEnv, "")
591+
if r1CertLoc == "" {
592+
err = fmt.Errorf("the R1 Certificate Location must be specified in the environment variable %s", r1CertLocationEnv)
593+
return
594+
}
595+
file, err = ioutil.ReadFile(r1CertLoc)
596+
return
597+
}

0 commit comments

Comments
 (0)