-
Notifications
You must be signed in to change notification settings - Fork 294
Fix for issue #418 #911
Fix for issue #418 #911
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ const ( | |
const ( | ||
defaultEnable bool = true | ||
defaultPort int = 8181 | ||
defaultAddress string = "" | ||
defaultHTTPS bool = false | ||
defaultRestCertificate string = "" | ||
defaultRestKey string = "" | ||
|
@@ -73,6 +74,7 @@ var ( | |
type Config struct { | ||
Enable bool `json:"enable,omitempty"yaml:"enable,omitempty"` | ||
Port int `json:"port,omitempty"yaml:"port,omitempty"` | ||
Address string `json:"addr,omitempty"yaml:"addr,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"` | ||
|
@@ -107,6 +109,9 @@ const ( | |
"type": "integer", | ||
"minimum": 0, | ||
"maximum": 65535 | ||
}, | ||
"addr" : { | ||
"type": "string" | ||
} | ||
}, | ||
"additionalProperties": false | ||
|
@@ -210,6 +215,7 @@ func GetDefaultConfig() *Config { | |
return &Config{ | ||
Enable: defaultEnable, | ||
Port: defaultPort, | ||
Address: defaultAddress, | ||
HTTPS: defaultHTTPS, | ||
RestCertificate: defaultRestCertificate, | ||
RestKey: defaultRestKey, | ||
|
@@ -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 "addr": | ||
if err := json.Unmarshal(v, &(c.Address)); err != nil { | ||
return fmt.Errorf("%v (while parsing 'restapi::addr')", err) | ||
} | ||
case "https": | ||
if err := json.Unmarshal(v, &(c.HTTPS)); err != nil { | ||
return fmt.Errorf("%v (while parsing 'restapi::https')", err) | ||
|
@@ -299,8 +309,20 @@ func (s *Server) Name() string { | |
return "REST" | ||
} | ||
|
||
func (s *Server) SetAddress(addrString string) { | ||
s.addrString = addrString | ||
func (s *Server) SetAddress(addrString string, dfltPort int) error { | ||
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 { | ||
return serror.ErrBadAddress | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's consider returning errors.New("Invalid address") here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error here isn't used anywhere. Remove it entirely since the func you added in snapd There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood and agreed. However I will still run the test and call log.Fatal because we might enter this code when running tests like PluginRestCalls for instance which will not go through the main snapd.go code. Considering this, I would therefore like to "factorize/refactor/modify" the checkCmdLineFlags and extract from it the address validation mechanisms so that this can be common to both snapd.go and server.go. However, where should I put such a code in the current code hierarchy ? |
||
} | ||
// 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]", s.addrString)) | ||
return nil | ||
} | ||
|
||
func (s *Server) Start() error { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ package main | |
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
@@ -54,8 +55,14 @@ var ( | |
Usage: "Disable the agent REST API", | ||
} | ||
flAPIPort = cli.IntFlag{ | ||
Name: "api-port, p", | ||
Usage: "API port (Default: 8181)", | ||
Name: "api-port, p", | ||
Usage: "API port (Default: 8181)", | ||
EnvVar: "SNAP_PORT", | ||
} | ||
flAPIAddr = cli.StringFlag{ | ||
Name: "api-addr, b", | ||
Usage: "API Address[:port] to bind to/listen on. Default: empty string => listen on all interfaces", | ||
EnvVar: "SNAP_ADDR", | ||
} | ||
flMaxProcs = cli.IntFlag{ | ||
Name: "max-procs, c", | ||
|
@@ -227,6 +234,7 @@ func main() { | |
app.Flags = []cli.Flag{ | ||
flAPIDisabled, | ||
flAPIPort, | ||
flAPIAddr, | ||
flLogLevel, | ||
flLogPath, | ||
flMaxProcs, | ||
|
@@ -354,7 +362,7 @@ func action(ctx *cli.Context) { | |
r.BindTribeManager(tr) | ||
} | ||
go monitorErrors(r.Err()) | ||
r.SetAddress(fmt.Sprintf(":%d", cfg.RestAPI.Port)) | ||
r.SetAddress(cfg.RestAPI.Address, cfg.RestAPI.Port) | ||
coreModules = append(coreModules, r) | ||
log.Info("REST API is enabled") | ||
} else { | ||
|
@@ -645,9 +653,37 @@ func setDurationVal(field time.Duration, ctx *cli.Context, flagName string) time | |
return field | ||
} | ||
|
||
// | ||
func checkCmdLineFlags(ctx *cli.Context) error { | ||
// Bind address is specified | ||
if ctx.IsSet("api-addr") { | ||
addr := ctx.String("api-addr") | ||
// Contains a comma | ||
if strings.Index(addr, ",") != -1 { | ||
return serror.ErrBadAddress | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's consider returning errors.New("Invalid address") here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
} | ||
idx := strings.Index(addr, ":") | ||
// Port is specified in address string | ||
if idx != -1 { | ||
// Port is also specified on command line | ||
if ctx.IsSet("api-port") { | ||
return errors.New("Port can not be specified in both --api-addr and --port") | ||
} | ||
if idx == (len(addr) - 1) { | ||
return errors.New("Empty port specified") | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Apply the command line flags set (if any) to override the values | ||
// in the input configuration | ||
func applyCmdLineFlags(cfg *Config, ctx *cli.Context) { | ||
err := checkCmdLineFlags(ctx) | ||
if err != nil { | ||
panic(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove the panic and call Calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure will. This answers one of my questions thanks. However coming from a Java world I am not used to have log.Fatal being "entitled" to also provide the exit mechanism ;-) |
||
} | ||
invertBoolean := true | ||
// apply any command line flags that might have been set, first for the | ||
// snapd-related flags | ||
|
@@ -663,6 +699,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.Address = setStringVal(cfg.RestAPI.Address, ctx, "ip-addr") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be "api-addr" not "ip-addr" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch thanks |
||
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") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case I feel snapd and the rest package should use their own errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK just removing this part then.