Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Fix for issue #418
Browse files Browse the repository at this point in the history
  • Loading branch information
obourdon committed May 11, 2016
1 parent a579d11 commit dd0af25
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 11 deletions.
3 changes: 2 additions & 1 deletion examples/configs/snap-config-sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"rest_auth_password": "changeme",
"rest_certificate": "/path/to/cert/file",
"rest_key": "/path/to/private/key",
"port": 8282
"port": 8282,
"bind_addresses": "127.0.0.1:12345"
},
"tribe": {
"enable": true,
Expand Down
3 changes: 3 additions & 0 deletions examples/configs/snap-config-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ restapi:
# port sets the port to start the REST API server on. Default is 8181
port: 8282

#
bind_addresses: 127.0.0.1:12345

# tribe section contains all configuration items for the tribe module
tribe:
# enable controls enabling tribe for the snapd instance. Default value is false.
Expand Down
2 changes: 1 addition & 1 deletion mgmt/rest/client/client_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func startAPI() string {
}
log.Fatal(err)
}(r.Err())
r.SetAddress("127.0.0.1:0")
r.SetAddresses("127.0.0.1", 0)
r.Start()
time.Sleep(100 * time.Millisecond)
return fmt.Sprintf("http://localhost:%d", r.Port())
Expand Down
3 changes: 1 addition & 2 deletions mgmt/rest/client/client_tribe_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"io/ioutil"
"net"
"net/http"
"strconv"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -201,7 +200,7 @@ func startTribes(count int) []int {
r.BindMetricManager(c)
r.BindTaskManager(s)
r.BindTribeManager(t)
r.SetAddress(":" + strconv.Itoa(mgtPort))
r.SetAddresses("", mgtPort)
r.Start()
wg.Add(1)
timer := time.After(10 * time.Second)
Expand Down
2 changes: 1 addition & 1 deletion mgmt/rest/rest_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ func startAPI(cfg *mockConfig) *restAPIInstance {
}
log.Fatal(err)
}(r.Err())
r.SetAddress("127.0.0.1:0")
r.SetAddresses("127.0.0.1", 0)
r.Start()
time.Sleep(time.Millisecond * 100)
return &restAPIInstance{
Expand Down
25 changes: 23 additions & 2 deletions mgmt/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
const (
defaultEnable bool = true
defaultPort int = 8181
defaultBindAddresses string = ""
defaultHTTPS bool = false
defaultRestCertificate string = ""
defaultRestKey string = ""
Expand All @@ -73,6 +74,7 @@ var (
type Config struct {
Enable bool `json:"enable,omitempty"yaml:"enable,omitempty"`
Port int `json:"port,omitempty"yaml:"port,omitempty"`
BindAddresses string `json:"bind_addresses,omitempty"yaml:"bind_addresses,omitempty"`
HTTPS bool `json:"https,omitempty"yaml:"https,omitempty"`
RestCertificate string `json:"rest_certificate,omitempty"yaml:"rest_certificate,omitempty"`
RestKey string `json:"rest_key,omitempty"yaml:"rest_key,omitempty"`
Expand Down Expand Up @@ -107,6 +109,9 @@ const (
"type": "integer",
"minimum": 0,
"maximum": 65535
},
"bind_addresses" : {
"type": "string"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -210,6 +215,7 @@ func GetDefaultConfig() *Config {
return &Config{
Enable: defaultEnable,
Port: defaultPort,
BindAddresses: defaultBindAddresses,
HTTPS: defaultHTTPS,
RestCertificate: defaultRestCertificate,
RestKey: defaultRestKey,
Expand Down Expand Up @@ -240,6 +246,10 @@ func (c *Config) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(v, &(c.Port)); err != nil {
return fmt.Errorf("%v (while parsing 'restapi::port')", err)
}
case "bind_addresses":
if err := json.Unmarshal(v, &(c.BindAddresses)); err != nil {
return fmt.Errorf("%v (while parsing 'restapi::bind_addresses')", err)
}
case "https":
if err := json.Unmarshal(v, &(c.HTTPS)); err != nil {
return fmt.Errorf("%v (while parsing 'restapi::https')", err)
Expand Down Expand Up @@ -299,8 +309,19 @@ func (s *Server) Name() string {
return "REST"
}

func (s *Server) SetAddress(addrString string) {
s.addrString = addrString
func (s *Server) SetAddresses(addrString string, dfltPort int) {
restLogger.Info(fmt.Sprintf("Setting address to: [%v] Default port: %v", addrString, dfltPort))
// In the future, we could extend this to support multiple comma separated IP[:port] values
if strings.Index(addrString, ",") != -1 {
panic("Not supported address value for binding")
}
// If no port is specified, use default port
if strings.Index(addrString, ":") != -1 {
s.addrString = addrString
} else {
s.addrString = fmt.Sprintf("%s:%d", addrString, dfltPort)
}
restLogger.Info(fmt.Sprintf("Address used for binding: [%v]", addrString))
}

func (s *Server) Start() error {
Expand Down
9 changes: 9 additions & 0 deletions mgmt/rest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func TestRestAPIConfigJSON(t *testing.T) {
Convey("Port should be 8282", func() {
So(cfg.Port, ShouldEqual, 8282)
})
Convey("BindAddresses should equal 127.0.0.1:12345", func() {
So(cfg.BindAddresses, ShouldEqual, "127.0.0.1:12345")
})
Convey("RestAuth should be true", func() {
So(cfg.RestAuth, ShouldEqual, true)
})
Expand Down Expand Up @@ -92,6 +95,9 @@ func TestRestAPIConfigYaml(t *testing.T) {
Convey("Port should be 8282", func() {
So(cfg.Port, ShouldEqual, 8282)
})
Convey("BindAddresses should equal 127.0.0.1:12345", func() {
So(cfg.BindAddresses, ShouldEqual, "127.0.0.1:12345")
})
Convey("RestAuth should be true", func() {
So(cfg.RestAuth, ShouldEqual, true)
})
Expand Down Expand Up @@ -120,6 +126,9 @@ func TestRestAPIDefaultConfig(t *testing.T) {
Convey("Port should be 8181", func() {
So(cfg.Port, ShouldEqual, 8181)
})
Convey("BindAddresses should equal empty string", func() {
So(cfg.BindAddresses, ShouldEqual, "")
})
Convey("RestAuth should be false", func() {
So(cfg.RestAuth, ShouldEqual, false)
})
Expand Down
3 changes: 1 addition & 2 deletions mgmt/rest/tribe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"fmt"
"net"
"net/http"
"strconv"
"sync"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -701,7 +700,7 @@ func startTribes(count int, seed string) ([]int, int) {
r.BindMetricManager(c)
r.BindTaskManager(s)
r.BindTribeManager(t)
r.SetAddress(":" + strconv.Itoa(mgtPort))
r.SetAddresses("", mgtPort)
r.Start()
wg.Add(1)
timer := time.After(10 * time.Second)
Expand Down
12 changes: 10 additions & 2 deletions snapd.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ var (
Usage: "Disable the agent REST API",
}
flAPIPort = cli.IntFlag{
Name: "api-port, p",
Name: "api-port, p",
Usage: "API port (Default: 8181)",
EnvVar: "SNAP_PORT",
}
flAPIBindAddresses = cli.StringFlag{
Name: "bind-addresses, b",
Usage: "Address[:port] to bind to.",
EnvVar: "SNAP_BIND_ADDRESSES",
}
flMaxProcs = cli.IntFlag{
Name: "max-procs, c",
Expand Down Expand Up @@ -227,6 +233,7 @@ func main() {
app.Flags = []cli.Flag{
flAPIDisabled,
flAPIPort,
flAPIBindAddresses,
flLogLevel,
flLogPath,
flMaxProcs,
Expand Down Expand Up @@ -354,7 +361,7 @@ func action(ctx *cli.Context) {
r.BindTribeManager(tr)
}
go monitorErrors(r.Err())
r.SetAddress(fmt.Sprintf(":%d", cfg.RestAPI.Port))
r.SetAddresses(cfg.RestAPI.BindAddresses, cfg.RestAPI.Port)
coreModules = append(coreModules, r)
log.Info("REST API is enabled")
} else {
Expand Down Expand Up @@ -663,6 +670,7 @@ func applyCmdLineFlags(cfg *Config, ctx *cli.Context) {
// next for the RESTful server related flags
cfg.RestAPI.Enable = setBoolVal(cfg.RestAPI.Enable, ctx, "disable-api", invertBoolean)
cfg.RestAPI.Port = setIntVal(cfg.RestAPI.Port, ctx, "api-port")
cfg.RestAPI.BindAddresses = setStringVal(cfg.RestAPI.BindAddresses, ctx, "bind-addresses")
cfg.RestAPI.HTTPS = setBoolVal(cfg.RestAPI.HTTPS, ctx, "rest-https")
cfg.RestAPI.RestCertificate = setStringVal(cfg.RestAPI.RestCertificate, ctx, "rest-cert")
cfg.RestAPI.RestKey = setStringVal(cfg.RestAPI.RestKey, ctx, "rest-key")
Expand Down

0 comments on commit dd0af25

Please sign in to comment.