NewApiClient creates a new synchronize ApiClient
( ctx gocontext.Context, endpoint string, headers map[string]string, timeout time.Duration, proxy string, br context.BasicRes, )
| 103 | |
| 104 | // NewApiClient creates a new synchronize ApiClient |
| 105 | func NewApiClient( |
| 106 | ctx gocontext.Context, |
| 107 | endpoint string, |
| 108 | headers map[string]string, |
| 109 | timeout time.Duration, |
| 110 | proxy string, |
| 111 | br context.BasicRes, |
| 112 | ) (*ApiClient, errors.Error) { |
| 113 | cfg := br.GetConfigReader() |
| 114 | log := br.GetLogger() |
| 115 | |
| 116 | // endpoint blacklist |
| 117 | endpointCidrBlacklist := cfg.GetString("ENDPOINT_CIDR_BLACKLIST") |
| 118 | if endpointCidrBlacklist != "" { |
| 119 | err := checkCidrBlacklist(endpointCidrBlacklist, endpoint, log) |
| 120 | if err != nil { |
| 121 | return nil, err |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | apiClient := &ApiClient{} |
| 126 | apiClient.Setup( |
| 127 | endpoint, |
| 128 | headers, |
| 129 | timeout, |
| 130 | ) |
| 131 | // create the Transport |
| 132 | apiClient.client.Transport = &http.Transport{} |
| 133 | |
| 134 | // set insecureSkipVerify |
| 135 | insecureSkipVerify := cfg.GetBool("IN_SECURE_SKIP_VERIFY") |
| 136 | if insecureSkipVerify { |
| 137 | apiClient.client.Transport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} |
| 138 | } |
| 139 | |
| 140 | if proxy != "" { |
| 141 | err := apiClient.SetProxy(proxy) |
| 142 | if err != nil { |
| 143 | return nil, errors.Convert(err) |
| 144 | } |
| 145 | // check connectivity |
| 146 | res, err := apiClient.Get("/", nil, nil) |
| 147 | if err != nil { |
| 148 | return nil, err |
| 149 | } |
| 150 | if res.StatusCode == http.StatusBadGateway { |
| 151 | return nil, errors.BadInput.New(fmt.Sprintf("fail to connect to %v via %v", endpoint, proxy)) |
| 152 | } |
| 153 | } else { |
| 154 | // check connectivity |
| 155 | parsedUrl, err := url.Parse(endpoint) |
| 156 | if err != nil { |
| 157 | return nil, errors.BadInput.Wrap(err, fmt.Sprintf("Invalid URL: %s", endpoint)) |
| 158 | } |
| 159 | if parsedUrl.Scheme == "" { |
| 160 | return nil, errors.BadInput.New("Invalid URL scheme") |
| 161 | } |
| 162 | err = utils.CheckDNS(parsedUrl.Hostname()) |
no test coverage detected