Ejemplo n.º 1
0
// GetProxySettings is defined on the PackageManager interface.
func (apt *apt) GetProxySettings() (proxy.Settings, error) {
	var res proxy.Settings

	args := strings.Fields(apt.cmder.GetProxyCmd())
	if len(args) <= 1 {
		return proxy.Settings{}, fmt.Errorf("expected at least 2 arguments, got %d %v", len(args), args)
	}

	cmd := exec.Command(args[0], args[1:]...)
	out, err := CommandOutput(cmd)

	if err != nil {
		logger.Errorf("command failed: %v\nargs: %#v\n%s",
			err, args, string(out))
		return res, fmt.Errorf("command failed: %v", err)
	}

	output := string(bytes.Join(proxyRE.FindAll(out, -1), []byte("\n")))

	for _, match := range proxyRE.FindAllStringSubmatch(output, -1) {
		switch match[1] {
		case "http":
			res.Http = match[2]
		case "https":
			res.Https = match[2]
		case "ftp":
			res.Ftp = match[2]
		}
	}

	return res, nil
}
Ejemplo n.º 2
0
func (s *proxySuite) TestSetEnvironmentValues(c *gc.C) {
	s.PatchEnvironment("http_proxy", "initial")
	s.PatchEnvironment("HTTP_PROXY", "initial")
	s.PatchEnvironment("https_proxy", "initial")
	s.PatchEnvironment("HTTPS_PROXY", "initial")
	s.PatchEnvironment("ftp_proxy", "initial")
	s.PatchEnvironment("FTP_PROXY", "initial")
	s.PatchEnvironment("no_proxy", "initial")
	s.PatchEnvironment("NO_PROXY", "initial")

	proxySettings := proxy.Settings{
		Http:  "http proxy",
		Https: "https proxy",
		// Ftp left blank to show clearing env.
		NoProxy: "10.0.3.1,localhost",
	}
	proxySettings.SetEnvironmentValues()

	obtained := proxy.DetectProxies()

	c.Assert(obtained, gc.DeepEquals, proxySettings)

	c.Assert(os.Getenv("http_proxy"), gc.Equals, "http proxy")
	c.Assert(os.Getenv("HTTP_PROXY"), gc.Equals, "http proxy")
	c.Assert(os.Getenv("https_proxy"), gc.Equals, "https proxy")
	c.Assert(os.Getenv("HTTPS_PROXY"), gc.Equals, "https proxy")
	c.Assert(os.Getenv("ftp_proxy"), gc.Equals, "")
	c.Assert(os.Getenv("FTP_PROXY"), gc.Equals, "")
	c.Assert(os.Getenv("no_proxy"), gc.Equals, "10.0.3.1,localhost")
	c.Assert(os.Getenv("NO_PROXY"), gc.Equals, "10.0.3.1,localhost")
}
Ejemplo n.º 3
0
// GetProxySettings is defined on the PackageManager interface.
func (yum *yum) GetProxySettings() (proxy.Settings, error) {
	var res proxy.Settings

	args := strings.Fields(yum.cmder.GetProxyCmd())
	if len(args) <= 1 {
		return proxy.Settings{}, fmt.Errorf("expected at least 2 arguments, got %d %v", len(args), args)
	}

	cmd := exec.Command(args[0], args[1:]...)
	out, err := CommandOutput(cmd)

	if err != nil {
		logger.Errorf("command failed: %v\nargs: %#v\n%s",
			err, args, string(out))
		return res, fmt.Errorf("command failed: %v", err)
	}

	for _, match := range strings.Split(string(out), "\n") {
		fields := strings.Split(match, "=")
		if len(fields) != 2 {
			continue
		}

		if strings.HasPrefix(fields[0], "https") {
			res.Https = strings.TrimSpace(fields[1])
		} else if strings.HasPrefix(fields[0], "http") {
			res.Http = strings.TrimSpace(fields[1])
		} else if strings.HasPrefix(fields[0], "ftp") {
			res.Ftp = strings.TrimSpace(fields[1])
		}
	}

	return res, nil
}
Ejemplo n.º 4
0
func (s *proxySuite) TestAsScriptEnvironmentOneValue(c *gc.C) {
	proxies := proxy.Settings{
		Http: "some-value",
	}
	expected := `
export http_proxy=some-value
export HTTP_PROXY=some-value`[1:]
	c.Assert(proxies.AsScriptEnvironment(), gc.Equals, expected)
}
Ejemplo n.º 5
0
func (s *proxySuite) TestAsEnvironmentValuesOneValue(c *gc.C) {
	proxies := proxy.Settings{
		Http: "some-value",
	}
	expected := []string{
		"http_proxy=some-value",
		"HTTP_PROXY=some-value",
	}
	c.Assert(proxies.AsEnvironmentValues(), gc.DeepEquals, expected)
}
Ejemplo n.º 6
0
func (w *proxyWorker) handleProxyValues(proxySettings proxyutils.Settings) {
	proxySettings.SetEnvironmentValues()
	if proxySettings != w.proxy || w.first {
		logger.Debugf("new proxy settings %#v", proxySettings)
		w.proxy = proxySettings
		if err := w.writeEnvironment(); err != nil {
			// It isn't really fatal, but we should record it.
			logger.Errorf("error writing proxy environment file: %v", err)
		}
	}
}
Ejemplo n.º 7
0
func (s *ConfigSuite) TestProxyConfigMap(c *gc.C) {
	s.addJujuFiles(c)
	cfg := newTestConfig(c, testing.Attrs{})
	proxySettings := proxy.Settings{
		Http:    "http proxy",
		Https:   "https proxy",
		Ftp:     "ftp proxy",
		NoProxy: "no proxy",
	}
	cfg, err := cfg.Apply(config.ProxyConfigMap(proxySettings))
	c.Assert(err, gc.IsNil)
	c.Assert(cfg.ProxySettings(), gc.DeepEquals, proxySettings)
	// Apt proxy and proxy differ by the content of the no-proxy values.
	proxySettings.NoProxy = ""
	c.Assert(cfg.AptProxySettings(), gc.DeepEquals, proxySettings)
}
Ejemplo n.º 8
0
func (s *proxySuite) TestAsScriptEnvironmentAllValue(c *gc.C) {
	proxies := proxy.Settings{
		Http:    "some-value",
		Https:   "special",
		Ftp:     "who uses this?",
		NoProxy: "10.0.3.1,localhost",
	}
	expected := `
export http_proxy=some-value
export HTTP_PROXY=some-value
export https_proxy=special
export HTTPS_PROXY=special
export ftp_proxy=who uses this?
export FTP_PROXY=who uses this?
export no_proxy=10.0.3.1,localhost
export NO_PROXY=10.0.3.1,localhost`[1:]
	c.Assert(proxies.AsScriptEnvironment(), gc.Equals, expected)
}
Ejemplo n.º 9
0
func (s *proxySuite) TestAsEnvironmentValuesAllValue(c *gc.C) {
	proxies := proxy.Settings{
		Http:    "some-value",
		Https:   "special",
		Ftp:     "who uses this?",
		NoProxy: "10.0.3.1,localhost",
	}
	expected := []string{
		"http_proxy=some-value",
		"HTTP_PROXY=some-value",
		"https_proxy=special",
		"HTTPS_PROXY=special",
		"ftp_proxy=who uses this?",
		"FTP_PROXY=who uses this?",
		"no_proxy=10.0.3.1,localhost",
		"NO_PROXY=10.0.3.1,localhost",
	}
	c.Assert(proxies.AsEnvironmentValues(), gc.DeepEquals, expected)
}
Ejemplo n.º 10
0
func (s *proxySuite) TestAsEnvironmentValuesEmpty(c *gc.C) {
	proxies := proxy.Settings{}
	c.Assert(proxies.AsEnvironmentValues(), gc.HasLen, 0)
}
Ejemplo n.º 11
0
func (s *proxySuite) TestAsScriptEnvironmentEmpty(c *gc.C) {
	proxies := proxy.Settings{}
	c.Assert(proxies.AsScriptEnvironment(), gc.Equals, "")
}