Skip to content

Commit c71e92f

Browse files
kristenjacobsowainlewis
authored andcommitted
Add OCI proxy support (#135)
* Add OCI proxy support
1 parent c5816bf commit c71e92f

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

pkg/oci/client/client.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
package client
1616

1717
import (
18+
"crypto/tls"
19+
"crypto/x509"
1820
"fmt"
21+
"io/ioutil"
1922
"net"
23+
"net/http"
24+
"net/url"
25+
"os"
2026
"sort"
2127
"strings"
2228
"time"
@@ -145,6 +151,52 @@ func New(cfg *Config) (Interface, error) {
145151
opts = append(opts, baremetal.PrivateKeyPassword(cfg.Auth.PrivateKeyPassphrase))
146152
}
147153

154+
// Handles the case where we want to talk to OCI via a proxy.
155+
ociProxy := os.Getenv("OCI_PROXY")
156+
trustedCACertPath := os.Getenv("TRUSTED_CA_CERT_PATH")
157+
if ociProxy != "" || trustedCACertPath != "" {
158+
transport := http.Transport{
159+
DialContext: (&net.Dialer{
160+
Timeout: 30 * time.Second,
161+
KeepAlive: 30 * time.Second,
162+
DualStack: true,
163+
}).DialContext,
164+
MaxIdleConns: 100,
165+
IdleConnTimeout: 90 * time.Second,
166+
TLSHandshakeTimeout: 10 * time.Second,
167+
ExpectContinueTimeout: 1 * time.Second,
168+
}
169+
170+
if ociProxy != "" {
171+
glog.Infof("using OCI proxy server: %s", ociProxy)
172+
proxyURL, err := url.Parse(ociProxy)
173+
if err != nil {
174+
return nil, fmt.Errorf("failed to parse OCI proxy url: %s, err: %v", ociProxy, err)
175+
}
176+
transport.Proxy = func(req *http.Request) (*url.URL, error) {
177+
return proxyURL, nil
178+
}
179+
}
180+
181+
if trustedCACertPath != "" {
182+
glog.Infof("configuring OCI client with a new trusted ca: %s", trustedCACertPath)
183+
trustedCACert, err := ioutil.ReadFile(trustedCACertPath)
184+
if err != nil {
185+
return nil, fmt.Errorf("failed to read root certificate: %s, err: %v", trustedCACertPath, err)
186+
}
187+
caCertPool := x509.NewCertPool()
188+
ok := caCertPool.AppendCertsFromPEM(trustedCACert)
189+
if !ok {
190+
return nil, fmt.Errorf("failed to parse root certificate: %s", trustedCACertPath)
191+
}
192+
transport.TLSClientConfig = &tls.Config{RootCAs: caCertPool}
193+
}
194+
195+
opts = append(opts, func(o *baremetal.NewClientOptions) {
196+
o.Transport = &transport
197+
})
198+
}
199+
148200
ociClient, err := baremetal.NewClient(
149201
cfg.Auth.UserOCID,
150202
cfg.Auth.TenancyOCID,

0 commit comments

Comments
 (0)