argo-cd/util/proxy/proxy.go
Chetan Banavikalmutt 9476ab5e18
feat: add first class support to access repositories using proxy (#5581) (#6286)
* feat: add first class support to access repositories using proxy

Currently, users need to set the proxy URLs as env variables in the repo server. This is not user-friendly and also error-prone. This PR adds support to maintain proxy URLs along with repository configs in the argocd-cm. Argo CD uses this proxy to access your repository. In case the custom proxy is absent, it defaults to reading the proxy from the env variables.

Signed-off-by: Chetan Banavikalmutt <chetanrns1997@gmail.com>

* set both http & https proxy variables

Signed-off-by: Chetan Banavikalmutt <chetanrns1997@gmail.com>
2021-06-16 14:45:10 +02:00

45 lines
1.1 KiB
Go

package proxy
import (
"fmt"
"net/http"
"net/url"
"os/exec"
"strings"
)
// UpsertEnv removes the existing proxy env variables and adds the custom proxy variables
func UpsertEnv(cmd *exec.Cmd, proxy string) []string {
envs := []string{}
if proxy == "" {
return cmd.Env
}
// remove the existing proxy env variable if present
for i, env := range cmd.Env {
proxyEnv := strings.ToLower(env)
if strings.HasPrefix(proxyEnv, "http_proxy") || strings.HasPrefix(proxyEnv, "https_proxy") {
continue
}
envs = append(envs, cmd.Env[i])
}
return append(envs, httpProxy(proxy), httpsProxy(proxy))
}
// GetCallback returns the proxy callback function
func GetCallback(proxy string) func(*http.Request) (*url.URL, error) {
if proxy != "" {
return func(r *http.Request) (*url.URL, error) {
return url.Parse(proxy)
}
}
// read proxy from env variable if custom proxy is missing
return http.ProxyFromEnvironment
}
func httpProxy(url string) string {
return fmt.Sprintf("http_proxy=%s", url)
}
func httpsProxy(url string) string {
return fmt.Sprintf("https_proxy=%s", url)
}