Added a function to allow ignoring top-level unknown config option

This will be required for NATS Streaming server since streaming
allows user to have NATS and Streaming specific options in same
file.

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
This commit is contained in:
Ivan Kozlovic
2019-05-30 15:24:39 -06:00
parent 376eee46e9
commit 437e16ca71
2 changed files with 56 additions and 1 deletions

View File

@@ -27,6 +27,7 @@ import (
"regexp"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/nats-io/jwt"
@@ -34,6 +35,19 @@ import (
"github.com/nats-io/nkeys"
)
var allowUnknownTopLevelField = int32(0)
// AllowUnknownTopLevelConfigurationField sets if the processing of a
// configuration file returns an error if it finds an unknown top-level
// configuration option.
func AllowUnknownTopLevelConfigurationField(allow bool) {
var val int32
if allow {
val = int32(1)
}
atomic.StoreInt32(&allowUnknownTopLevelField, val)
}
// ClusterOpts are options for clusters.
// NOTE: This structure is no longer used for monitoring endpoints
// and json tags are deprecated and may be removed in the future.
@@ -693,7 +707,7 @@ func (o *Options) ProcessConfigFile(configFile string) error {
case "reconnect_error_reports":
o.ReconnectErrorReports = int(v.(int64))
default:
if !tk.IsUsedVariable() {
if au := atomic.LoadInt32(&allowUnknownTopLevelField); au == 0 && !tk.IsUsedVariable() {
err := &unknownConfigFieldErr{
field: k,
configErr: configErr{

View File

@@ -1819,3 +1819,44 @@ func TestLargeMaxPayload(t *testing.T) {
t.Fatalf("Expected an error from too large of a max_payload entry")
}
}
func TestHandleUnknownTopLevelConfigurationField(t *testing.T) {
conf := createConfFile(t, []byte(`
port: 1234
streaming {
id: "me"
}
`))
defer os.Remove(conf)
// Verify that we get an error because of unknown "streaming" field.
opts := &Options{}
if err := opts.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), "streaming") {
t.Fatal("Expected error, got none")
}
// Verify that if that is set, we get no error
AllowUnknownTopLevelConfigurationField(true)
defer AllowUnknownTopLevelConfigurationField(false)
if err := opts.ProcessConfigFile(conf); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if opts.Port != 1234 {
t.Fatalf("Port was not parsed correctly: %v", opts.Port)
}
// Verify that ignore works only on top level fields.
changeCurrentConfigContentWithNewContent(t, conf, []byte(`
port: 1234
cluster {
non_top_level_unknown_field: 123
}
streaming {
id: "me"
}
`))
if err := opts.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), "non_top_level") {
t.Fatal("Expected error, got none")
}
}