Skip to content

Commit 5185f10

Browse files
committed
[FIXED] Route/Cluster override
If the server was started with a cluster section in a configuration file and one would want to override the routes (using `-routes`) the server would complain that you need to use `-cluster`. Adding an override of cluster would not work, server would still complain. Trying to override simply the cluster listen info (without override of routes) would also not work.
1 parent a2c747a commit 5185f10

File tree

3 files changed

+73
-19
lines changed

3 files changed

+73
-19
lines changed

main.go

+31-19
Original file line numberDiff line numberDiff line change
@@ -260,33 +260,45 @@ func configureTLS(opts *server.Options) {
260260
}
261261

262262
func configureClusterOpts(opts *server.Options) error {
263-
if opts.ClusterListenStr == "" {
263+
// If we don't have cluster defined in the configuration
264+
// file and no cluster listen string override, but we do
265+
// have a routes override, we need to report misconfiguration.
266+
if opts.ClusterListenStr == "" && opts.ClusterHost == "" &&
267+
opts.ClusterPort == 0 {
264268
if opts.RoutesStr != "" {
265269
server.PrintAndDie("Solicited routes require cluster capabilities, e.g. --cluster.")
266270
}
267271
return nil
268272
}
269273

270-
clusterURL, err := url.Parse(opts.ClusterListenStr)
271-
h, p, err := net.SplitHostPort(clusterURL.Host)
272-
if err != nil {
273-
return err
274-
}
275-
opts.ClusterHost = h
276-
_, err = fmt.Sscan(p, &opts.ClusterPort)
277-
if err != nil {
278-
return err
279-
}
280-
281-
if clusterURL.User != nil {
282-
pass, hasPassword := clusterURL.User.Password()
283-
if !hasPassword {
284-
return fmt.Errorf("Expected cluster password to be set.")
274+
// If cluster flag override, process it
275+
if opts.ClusterListenStr != "" {
276+
clusterURL, err := url.Parse(opts.ClusterListenStr)
277+
h, p, err := net.SplitHostPort(clusterURL.Host)
278+
if err != nil {
279+
return err
280+
}
281+
opts.ClusterHost = h
282+
_, err = fmt.Sscan(p, &opts.ClusterPort)
283+
if err != nil {
284+
return err
285285
}
286-
opts.ClusterPassword = pass
287286

288-
user := clusterURL.User.Username()
289-
opts.ClusterUsername = user
287+
if clusterURL.User != nil {
288+
pass, hasPassword := clusterURL.User.Password()
289+
if !hasPassword {
290+
return fmt.Errorf("Expected cluster password to be set.")
291+
}
292+
opts.ClusterPassword = pass
293+
294+
user := clusterURL.User.Username()
295+
opts.ClusterUsername = user
296+
} else {
297+
// Since we override from flag and there is no user/pwd, make
298+
// sure we clear what we may have gotten from config file.
299+
opts.ClusterUsername = ""
300+
opts.ClusterPassword = ""
301+
}
290302
}
291303

292304
// If we have routes but no config file, fill in here.

server/opts.go

+3
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,9 @@ func MergeOptions(fileOpts, flagOpts *Options) *Options {
637637
if flagOpts.ProfPort != 0 {
638638
opts.ProfPort = flagOpts.ProfPort
639639
}
640+
if flagOpts.ClusterListenStr != "" {
641+
opts.ClusterListenStr = flagOpts.ClusterListenStr
642+
}
640643
if flagOpts.RoutesStr != "" {
641644
mergeRoutes(&opts, flagOpts)
642645
}

server/opts_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,45 @@ func TestRouteFlagOverride(t *testing.T) {
276276
}
277277
}
278278

279+
func TestClusterFlagsOverride(t *testing.T) {
280+
routeFlag := "nats-route://ruser:top_secret@127.0.0.1:7246"
281+
rurl, _ := url.Parse(routeFlag)
282+
283+
// In this test, we override the cluster listen string. Note that in
284+
// the golden options, the cluster other infos correspond to what
285+
// is recovered from the configuration file, this explains the
286+
// discrepency between ClusterListenStr and the rest.
287+
// The server would then process the ClusterListenStr override and
288+
// correctly override ClusterHost/ClustherPort/etc..
289+
golden := &Options{
290+
Host: "127.0.0.1",
291+
Port: 7222,
292+
ClusterHost: "127.0.0.1",
293+
ClusterPort: 7244,
294+
ClusterListenStr: "nats://127.0.0.1:8224",
295+
ClusterUsername: "ruser",
296+
ClusterPassword: "top_secret",
297+
ClusterAuthTimeout: 0.5,
298+
Routes: []*url.URL{rurl},
299+
}
300+
301+
fopts, err := ProcessConfigFile("./configs/srv_a.conf")
302+
if err != nil {
303+
t.Fatalf("Received an error reading config file: %v\n", err)
304+
}
305+
306+
// Overrides via flags
307+
opts := &Options{
308+
ClusterListenStr: "nats://127.0.0.1:8224",
309+
}
310+
merged := MergeOptions(fopts, opts)
311+
312+
if !reflect.DeepEqual(golden, merged) {
313+
t.Fatalf("Options are incorrect.\nexpected: %+v\ngot: %+v",
314+
golden, merged)
315+
}
316+
}
317+
279318
func TestRouteFlagOverrideWithMultiple(t *testing.T) {
280319
routeFlag := "nats-route://ruser:top_secret@127.0.0.1:8246, nats-route://ruser:top_secret@127.0.0.1:8266"
281320
rurls := RoutesFromStr(routeFlag)

0 commit comments

Comments
 (0)