mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-26 20:26:13 +01:00
Merge pull request #3800 from vmware/add_proxy_settings
[SKIP CI] add proxy settings to the API client for the api-testing part
This commit is contained in:
commit
5b522fcd40
@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -22,6 +23,7 @@ type APIClientConfig struct {
|
||||
CaFile string
|
||||
CertFile string
|
||||
KeyFile string
|
||||
Proxy string
|
||||
}
|
||||
|
||||
//APIClient provided the http client for trigger http requests
|
||||
@ -60,6 +62,13 @@ func NewAPIClient(config APIClientConfig) (*APIClient, error) {
|
||||
TLSClientConfig: tlsConfig,
|
||||
}
|
||||
|
||||
//If proxy should be set
|
||||
if len(strings.TrimSpace(config.Proxy)) > 0 {
|
||||
if proxyURL, err := url.Parse(config.Proxy); err == nil {
|
||||
transport.Proxy = http.ProxyURL(proxyURL)
|
||||
}
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ type Environment struct {
|
||||
CAFile string //env var: CA_FILE_PATH
|
||||
CertFile string //env var: CERT_FILE_PATH
|
||||
KeyFile string //env var: KEY_FILE_PATH
|
||||
ProxyURL string //env var: http_proxy, https_proxy, HTTP_PROXY, HTTPS_PROXY
|
||||
|
||||
//API client
|
||||
HTTPClient *client.APIClient
|
||||
@ -95,6 +96,18 @@ func (env *Environment) Load() error {
|
||||
env.CertFile = certFile
|
||||
}
|
||||
|
||||
proxyEnvVar := "https_proxy"
|
||||
if env.Protocol == "http" {
|
||||
proxyEnvVar = "http_proxy"
|
||||
}
|
||||
proxyURL := os.Getenv(proxyEnvVar)
|
||||
if !isNotEmpty(proxyURL) {
|
||||
proxyURL = os.Getenv(strings.ToUpper(proxyEnvVar))
|
||||
}
|
||||
if isNotEmpty(proxyURL) {
|
||||
env.ProxyURL = proxyURL
|
||||
}
|
||||
|
||||
if !env.loaded {
|
||||
cfg := client.APIClientConfig{
|
||||
Username: env.Admin,
|
||||
@ -102,6 +115,7 @@ func (env *Environment) Load() error {
|
||||
CaFile: env.CAFile,
|
||||
CertFile: env.CertFile,
|
||||
KeyFile: env.KeyFile,
|
||||
Proxy: env.ProxyURL,
|
||||
}
|
||||
|
||||
httpClient, err := client.NewAPIClient(cfg)
|
||||
|
Loading…
Reference in New Issue
Block a user