This repository has been archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from DataDog/talwai/proxy
config: respect proxy_* settings in agent
- Loading branch information
Showing
5 changed files
with
215 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"github.com/go-ini/ini" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// mirror default behavior of the infra agent | ||
const defaultProxyPort = 3128 | ||
|
||
// ProxySettings contains configuration for http/https proxying | ||
type ProxySettings struct { | ||
User string | ||
Password string | ||
Host string | ||
Port int | ||
Scheme string | ||
} | ||
|
||
func getProxySettings(m *ini.Section) *ProxySettings { | ||
p := ProxySettings{Port: defaultProxyPort, Scheme: "http"} | ||
|
||
if v := m.Key("proxy_host").MustString(""); v != "" { | ||
// accept either http://myproxy.com or myproxy.com | ||
if i := strings.Index(v, "://"); i != -1 { | ||
// when available, parse the scheme from the url | ||
p.Scheme = v[0:i] | ||
p.Host = v[i+3:] | ||
} else { | ||
p.Host = v | ||
} | ||
} | ||
if v := m.Key("proxy_port").MustInt(-1); v != -1 { | ||
p.Port = v | ||
} | ||
if v := m.Key("proxy_user").MustString(""); v != "" { | ||
p.User = v | ||
} | ||
if v := m.Key("proxy_password").MustString(""); v != "" { | ||
p.Password = v | ||
} | ||
|
||
return &p | ||
} | ||
|
||
// URL turns ProxySettings into an idiomatic URL struct | ||
func (p *ProxySettings) URL() (*url.URL, error) { | ||
// construct scheme://user:pass@host:port | ||
var userpass *url.Userinfo | ||
if p.User != "" { | ||
if p.Password != "" { | ||
userpass = url.UserPassword(p.User, p.Password) | ||
} else { | ||
userpass = url.User(p.User) | ||
} | ||
} | ||
|
||
var path string | ||
if userpass != nil { | ||
path = fmt.Sprintf("%s://%s@%s:%v", p.Scheme, userpass.String(), p.Host, p.Port) | ||
} else { | ||
path = fmt.Sprintf("%s://%s:%v", p.Scheme, p.Host, p.Port) | ||
} | ||
|
||
return url.Parse(path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package config | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"testing" | ||
|
||
"github.com/go-ini/ini" | ||
) | ||
|
||
func getURL(f *ini.File) (*url.URL, error) { | ||
conf := File{ | ||
f, | ||
"some/path", | ||
} | ||
m, _ := conf.GetSection("Main") | ||
p := getProxySettings(m) | ||
return p.URL() | ||
} | ||
|
||
func TestGetProxySettings(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
f, _ := ini.Load([]byte("[Main]\n\nproxy_host = myhost")) | ||
|
||
s, err := getURL(f) | ||
assert.Nil(err) | ||
assert.Equal("http://myhost:3128", s.String()) | ||
|
||
f, _ = ini.Load([]byte("[Main]\n\nproxy_host = http://myhost")) | ||
|
||
s, err = getURL(f) | ||
assert.Nil(err) | ||
assert.Equal("http://myhost:3128", s.String()) | ||
|
||
f, _ = ini.Load([]byte("[Main]\n\nproxy_host = https://myhost")) | ||
|
||
s, err = getURL(f) | ||
assert.Nil(err) | ||
assert.Equal("https://myhost:3128", s.String()) | ||
|
||
// generic user name | ||
f, _ = ini.Load([]byte(strings.Join([]string{ | ||
"[Main]", | ||
"proxy_host = https://myhost", | ||
"proxy_port = 3129", | ||
"proxy_user = aaditya", | ||
}, "\n"))) | ||
|
||
s, err = getURL(f) | ||
assert.Nil(err) | ||
|
||
assert.Equal("https://aaditya@myhost:3129", s.String()) | ||
|
||
// special char in user name <3 | ||
f, _ = ini.Load([]byte(strings.Join([]string{ | ||
"[Main]", | ||
"proxy_host = myhost", | ||
"proxy_port = 3129", | ||
"proxy_user = léo", | ||
}, "\n"))) | ||
|
||
s, err = getURL(f) | ||
assert.Nil(err) | ||
|
||
// user is url-encoded and decodes to original string | ||
assert.Equal("http://l%C3%A9o@myhost:3129", s.String()) | ||
assert.Equal("léo", s.User.Username()) | ||
|
||
// generic user-pass | ||
f, _ = ini.Load([]byte(strings.Join([]string{ | ||
"[Main]", | ||
"proxy_host = myhost", | ||
"proxy_port = 3129", | ||
"proxy_user = aaditya", | ||
"proxy_password = password_12", | ||
}, "\n"))) | ||
|
||
s, err = getURL(f) | ||
assert.Nil(err) | ||
assert.Equal("http://aaditya:password_12@myhost:3129", s.String()) | ||
|
||
// user-pass with schemed host | ||
f, _ = ini.Load([]byte(strings.Join([]string{ | ||
"[Main]", | ||
"proxy_host = https://myhost", | ||
"proxy_port = 3129", | ||
"proxy_user = aaditya", | ||
"proxy_password = password_12", | ||
}, "\n"))) | ||
|
||
s, err = getURL(f) | ||
assert.Nil(err) | ||
assert.Equal("https://aaditya:password_12@myhost:3129", s.String()) | ||
|
||
// special characters in password | ||
f, _ = ini.Load([]byte(strings.Join([]string{ | ||
"[Main]", | ||
"proxy_host = https://myhost", | ||
"proxy_port = 3129", | ||
"proxy_user = aaditya", | ||
"proxy_password = /:!?&=@éÔγλῶσσα", | ||
}, "\n"))) | ||
|
||
s, err = getURL(f) | ||
assert.Nil(err) | ||
|
||
// password is url-encoded and decodes to the original string | ||
assert.Equal("https://aaditya:%2F%3A%21%3F&=%40%C3%A9%C3%94%CE%B3%CE%BB%E1%BF%B6%CF%83%CF%83%CE%B1@myhost:3129", s.String()) | ||
|
||
pass, _ := s.User.Password() | ||
assert.Equal("/:!?&=@éÔγλῶσσα", pass) | ||
} |