Skip to content
This repository has been archived by the owner on Aug 14, 2020. It is now read-only.

Commit

Permalink
discovery: add port parameters
Browse files Browse the repository at this point in the history
Currently the ACI spec only supports discovery on ports 443 and 80, this
does not allow using a discovery server on any other ports.

This commit adds an argument allowing for discovery on arbitrary ports.
If the argument is not specified (as in, is 0), the default ports are
used.
  • Loading branch information
Derek Gonyeo committed Jun 21, 2016
1 parent baa3e9c commit ecfdae2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
7 changes: 5 additions & 2 deletions actool/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ var (
Name: "discover",
Description: "Discover the download URLs for an app",
Summary: "Discover the download URLs for one or more app container images",
Usage: "[--json] APP...",
Usage: "[--json] [--port] [--insecure] APP...",
Run: runDiscover,
}
flagPort uint
)

func init() {
cmdDiscover.Flags.BoolVar(&transportFlags.Insecure, "insecure", false,
"Don't check TLS certificates and allow insecure non-TLS downloads over http")
cmdDiscover.Flags.BoolVar(&outputJson, "json", false,
"Output result as JSON")
cmdDiscover.Flags.UintVar(&flagPort, "port", 0,
"Port to connect when performing discovery")
}

func runDiscover(args []string) (exit int) {
Expand All @@ -62,7 +65,7 @@ func runDiscover(args []string) (exit int) {
if transportFlags.Insecure {
insecure = discovery.InsecureTLS | discovery.InsecureHTTP
}
eps, attempts, err := discovery.DiscoverACIEndpoints(*app, nil, insecure)
eps, attempts, err := discovery.DiscoverACIEndpoints(*app, nil, insecure, flagPort)
if err != nil {
stderr("error fetching endpoints for %s: %s", name, err)
return 1
Expand Down
22 changes: 12 additions & 10 deletions discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ func createTemplateVars(app App) []string {
return tplVars
}

func doDiscover(pre string, hostHeaders map[string]http.Header, app App, insecure InsecureOption) (*discoveryData, error) {
func doDiscover(pre string, hostHeaders map[string]http.Header, app App, insecure InsecureOption, port uint) (*discoveryData, error) {
app = *app.Copy()
if app.Labels["version"] == "" {
app.Labels["version"] = defaultVersion
}

_, body, err := httpsOrHTTP(pre, hostHeaders, insecure)
_, body, err := httpsOrHTTP(pre, hostHeaders, insecure, port)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -174,14 +174,15 @@ func doDiscover(pre string, hostHeaders map[string]http.Header, app App, insecur
// DiscoverWalk will make HTTPS requests to find discovery meta tags and
// optionally will use HTTP if insecure is set. hostHeaders specifies the
// header to apply depending on the host (e.g. authentication). Based on the
// response of the discoverFn it will continue to recurse up the tree.
func DiscoverWalk(app App, hostHeaders map[string]http.Header, insecure InsecureOption, discoverFn DiscoverWalkFunc) (dd *discoveryData, err error) {
// response of the discoverFn it will continue to recurse up the tree. If port
// is 0, the default port will be used.
func DiscoverWalk(app App, hostHeaders map[string]http.Header, insecure InsecureOption, port uint, discoverFn DiscoverWalkFunc) (dd *discoveryData, err error) {
parts := strings.Split(string(app.Name), "/")
for i := range parts {
end := len(parts) - i
pre := strings.Join(parts[:end], "/")

dd, err = doDiscover(pre, hostHeaders, app, insecure)
dd, err = doDiscover(pre, hostHeaders, app, insecure, port)
if derr := discoverFn(pre, dd, err); derr != nil {
return dd, derr
}
Expand Down Expand Up @@ -217,8 +218,8 @@ func walker(attempts *[]FailedAttempt, testFn DiscoverWalkFunc) DiscoverWalkFunc
// tags and optionally will use HTTP if insecure is set. hostHeaders
// specifies the header to apply depending on the host (e.g. authentication).
// It will not give up until it has exhausted the path or found an image
// discovery.
func DiscoverACIEndpoints(app App, hostHeaders map[string]http.Header, insecure InsecureOption) (ACIEndpoints, []FailedAttempt, error) {
// discovery. If port is 0, the default port will be used.
func DiscoverACIEndpoints(app App, hostHeaders map[string]http.Header, insecure InsecureOption, port uint) (ACIEndpoints, []FailedAttempt, error) {
testFn := func(pre string, dd *discoveryData, err error) error {
if len(dd.ACIEndpoints) != 0 {
return errEnough
Expand All @@ -227,7 +228,7 @@ func DiscoverACIEndpoints(app App, hostHeaders map[string]http.Header, insecure
}

attempts := []FailedAttempt{}
dd, err := DiscoverWalk(app, hostHeaders, insecure, walker(&attempts, testFn))
dd, err := DiscoverWalk(app, hostHeaders, insecure, port, walker(&attempts, testFn))
if err != nil && err != errEnough {
return nil, attempts, err
}
Expand All @@ -239,7 +240,8 @@ func DiscoverACIEndpoints(app App, hostHeaders map[string]http.Header, insecure
// tags and optionally will use HTTP if insecure is set. hostHeaders
// specifies the header to apply depending on the host (e.g. authentication).
// It will not give up until it has exhausted the path or found an public key.
func DiscoverPublicKeys(app App, hostHeaders map[string]http.Header, insecure InsecureOption) (PublicKeys, []FailedAttempt, error) {
// If port is 0, the default port will be used.
func DiscoverPublicKeys(app App, hostHeaders map[string]http.Header, insecure InsecureOption, port uint) (PublicKeys, []FailedAttempt, error) {
testFn := func(pre string, dd *discoveryData, err error) error {
if len(dd.PublicKeys) != 0 {
return errEnough
Expand All @@ -248,7 +250,7 @@ func DiscoverPublicKeys(app App, hostHeaders map[string]http.Header, insecure In
}

attempts := []FailedAttempt{}
dd, err := DiscoverWalk(app, hostHeaders, insecure, walker(&attempts, testFn))
dd, err := DiscoverWalk(app, hostHeaders, insecure, port, walker(&attempts, testFn))
if err != nil && err != errEnough {
return nil, attempts, err
}
Expand Down
4 changes: 2 additions & 2 deletions discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ func TestDiscoverEndpoints(t *testing.T) {
InsecureTLS | InsecureHTTP,
}
for _, insecure := range insecureList {
eps, _, err := DiscoverACIEndpoints(tt.app, hostHeaders, insecure)
eps, _, err := DiscoverACIEndpoints(tt.app, hostHeaders, insecure, 0)
if err != nil && !tt.expectDiscoveryACIEndpointsSuccess {
continue
}
Expand All @@ -559,7 +559,7 @@ func TestDiscoverEndpoints(t *testing.T) {
if err != nil {
t.Fatalf("#%d DiscoverACIEndpoints failed: %v", i, err)
}
publicKeys, _, err := DiscoverPublicKeys(tt.app, hostHeaders, insecure)
publicKeys, _, err := DiscoverPublicKeys(tt.app, hostHeaders, insecure, 0)
if err != nil && !tt.expectDiscoveryPublicKeysSuccess {
continue
}
Expand Down
12 changes: 8 additions & 4 deletions discovery/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net"
"net/http"
"net/url"
"strconv"
"time"
)

Expand Down Expand Up @@ -75,13 +76,16 @@ func init() {
httpDoInsecureTLS = ClientInsecureTLS
}

func httpsOrHTTP(name string, hostHeaders map[string]http.Header, insecure InsecureOption) (urlStr string, body io.ReadCloser, err error) {
fetch := func(scheme string) (urlStr string, res *http.Response, err error) {
func httpsOrHTTP(name string, hostHeaders map[string]http.Header, insecure InsecureOption, port uint) (urlStr string, body io.ReadCloser, err error) {
fetch := func(scheme string, port uint) (urlStr string, res *http.Response, err error) {
u, err := url.Parse(scheme + "://" + name)
if err != nil {
return "", nil, err
}
u.RawQuery = "ac-discovery=1"
if port != 0 {
u.Host += ":" + strconv.FormatUint(uint64(port), 10)
}
urlStr = u.String()
req, err := http.NewRequest("GET", urlStr, nil)
if err != nil {
Expand All @@ -102,11 +106,11 @@ func httpsOrHTTP(name string, hostHeaders map[string]http.Header, insecure Insec
res.Body.Close()
}
}
urlStr, res, err := fetch("https")
urlStr, res, err := fetch("https", port)
if err != nil || res.StatusCode != http.StatusOK {
if insecure&InsecureHTTP != 0 {
closeBody(res)
urlStr, res, err = fetch("http")
urlStr, res, err = fetch("http", port)
}
}

Expand Down
2 changes: 1 addition & 1 deletion discovery/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func TestHTTPSOrHTTP(t *testing.T) {
hostHeaders := map[string]http.Header{
tt.name: tt.authHeader,
}
urlStr, body, err := httpsOrHTTP(tt.name, hostHeaders, tt.insecure)
urlStr, body, err := httpsOrHTTP(tt.name, hostHeaders, tt.insecure, 0)
if tt.expectSuccess {
if err != nil {
t.Fatalf("#%d httpsOrHTTP failed: %v", i, err)
Expand Down

0 comments on commit ecfdae2

Please sign in to comment.