nats-io / nats-server

High-Performance server for NATS.io, the cloud and edge native messaging system.
https://nats.io
Apache License 2.0
15.91k stars 1.41k forks source link

Unable to add multiple trusted operators using a config file #5810

Closed protochron closed 1 month ago

protochron commented 2 months ago

Observed behavior

It looks like parsing the operators field in a config file is broken since it's trying to interpret the line as an []interface instead of []string.

This patch mostly ripped off from the implementation of the server_tags case shows a fix illustrating that the parser is misidentifying the type of the line:

index ac398823..414d2312 100644
--- a/server/opts.go
+++ b/server/opts.go
@@ -1195,6 +1195,22 @@ func (o *Options) processConfigFileLine(k string, v any, errors *[]error, warnin
            opFiles = append(opFiles, v)
        case []string:
            opFiles = append(opFiles, v...)
+       case []any:
+           for _, t := range v {
+               if token, ok := t.(token); ok {
+                   if ts, ok := token.Value().(string); ok {
+                       opFiles = append(opFiles, ts)
+                       continue
+                   } else {
+                       err := &configErr{tk, fmt.Sprintf("error parsing tags: unsupported type %T where string is expected", token)}
+                       *errors = append(*errors, err)
+                   }
+               } else {
+                   err := &configErr{tk, fmt.Sprintf("error parsing tags: unsupported type %T", t)}
+                   *errors = append(*errors, err)
+               }
+               break
+           }
        default:
            err := &configErr{tk, fmt.Sprintf("error parsing operators: unsupported type %T", v)}
            *errors = append(*errors, err)```

Running nats-server v2.10.18 with multiple operator JWTs defined as an array then works.

Expected behavior

The parser should correctly identify `operators: ["$jwt", "$jwt2"] as a valid configuration option so that you can add multiple trusted operators if needed.

Server and client version

nats-server: v2.10.18

Host environment

No response

Steps to reproduce

No response

derekcollison commented 2 months ago

Can you share an actual config file that is not working for you?

protochron commented 2 months ago

@derekcollison here's a minimal reproducible example using two operator JWTs

server_tags = ["test:value"]

operators: [
eyJ0eXAiOiJKV1QiLCJhbGciOiJlZDI1NTE5LW5rZXkifQ.eyJqdGkiOiJFTUtZM1RDV01QSVRNTEpTUUtJT0dSNlk2TkRIU1BORkdDQk9NVTRFUU9PRkFMWTY1SE9RIiwiaWF0IjoxNzI0MzM1MDYxLCJpc3MiOiJPQVlOWUxBNzVVR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: resolver.conf:2:1: error parsing operators: unsupported type []interface {}

Reading the code in the parser, the intent is that the above should be interpreted as a []string, but that doesn't seem to work. I could be missing something, since it doesn't seem like there is another way of adding additional trusted operators to a NATS server, which isn't blocking me but is confusing if that's intended to be supported.

derekcollison commented 2 months ago

ok thanks. If you put "" around the jwts does it work?

protochron commented 2 months ago

Unfortunately no. That was the first thing I tried :D