liftbridge-io / liftbridge

Lightweight, fault-tolerant message streams.
https://liftbridge.io
Apache License 2.0
2.57k stars 107 forks source link

Connect to NATS cluster with username/password #108

Closed PieterScheffers closed 4 years ago

PieterScheffers commented 5 years ago

In the documentation of NATS server I found that you can connect to a NATS server with the Go NATS client with a username and password in the url. When I use a username and password in the NATS server url for Liftbridge I get the following message:

panic: failed to connect to NATS: parse nats://myuser:mypassword: invalid port ":mypassword" after host

(I have changed the username and password with placeholders, the actual password has a special character in it (^))

Should I connect in some other way to use a username/password for the NATS cluster, or is this not possible yet?

tylertreat commented 5 years ago

This sounds like a bug. I will take a look...

tylertreat commented 5 years ago

I have not been able to reproduce either through config file or CLI flag, though I think special characters are invalid in the username/password possibly?

parse nats://a:b^@localhost:4222: net/url: invalid userinfo

That parse error comes from the NATS client library. The following test worked however:

nats://a:b@localhost:4222

Can you show the full connection string you're using (with username/password masked)? Also, are you using a Liftbridge config file or passing the NATS URL through the CLI flag?

PieterScheffers commented 5 years ago

My Liftbridge config looks like this:

          listen: 0.0.0.0:9292
          data.dir: /liftbridge-data
          log.level: debug

          # Define NATS cluster to connect to.
          nats {
              servers: ["nats://admin:Fc5^mW7d1kw1LbHh1^#dZk@nats-cluster:4222"]
          }

          # Specify message log settings.
          # Keep all messages
          log {
              retention.max.age: "0"
          }

          # Specify cluster settings.
          clustering {
              server.id: "liftbridge-0"
              namespace: "liftbridge"
              raft.bootstrap.peers: [ "liftbridge-0", "liftbridge-1", "liftbridge-2" ]
          }

nats-cluster is a kubernetes service pointing to a nats-cluster made with the nats-operator.

tylertreat commented 5 years ago

Okay, I see the issue now. With special characters in username/password it looks like the NATS client requires using UserInfo. I will work on a fix.

tylertreat commented 4 years ago

This has been resolved in https://github.com/liftbridge-io/liftbridge/pull/112. To set the NATS user/password, add the following to your Liftbridge config file:

 # Define NATS cluster to connect to.
nats {
    servers: ["nats://nats-cluster:4222"]
    user: "admin"
    password: "Fc5^mW7d1kw1LbHh1^#dZk"
}
PieterScheffers commented 4 years ago

Thank you!