Skip to content

Instantly share code, notes, and snippets.

@wallyqs
Created July 22, 2021 15:03
Show Gist options
  • Select an option

  • Save wallyqs/ecb7b61cac55d2857cc6e7209128505d to your computer and use it in GitHub Desktop.

Select an option

Save wallyqs/ecb7b61cac55d2857cc6e7209128505d to your computer and use it in GitHub Desktop.

Revisions

  1. wallyqs created this gist Jul 22, 2021.
    66 changes: 66 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    ### Mixed Auth via Leafnodes

    Server A starts with basic auth:

    ```hcl
    port = 4222
    leaf {
    port = 7422
    }
    authorization {
    users = [{ user: 'A', pass: 'A'}]
    }
    ```

    Second server has a leafnode connection that bounds it to a local account:

    ```hcl
    leaf {
    remotes = [
    {
    url: "nats://127.0.0.1:7422"
    account: "A"
    }
    ]
    }
    accounts {
    A { users = [{ user: 'A', pass: 'A'}]}
    }
    ```

    Server A starts exposing the leafnode connection, then Server B connects to it:


    ```sh
    nats-server -c a.conf -DV
    [9393] 2021/07/22 07:57:05.548886 [INF] Listening for leafnode connections on 0.0.0.0:7422
    ...
    [9393] 2021/07/22 07:57:05.569104 [INF] 127.0.0.1:58903 - lid:3 - Leafnode connection created
    ```

    ```
    nats-server -c b.conf -DV
    ...
    [9381] 2021/07/22 07:55:49.854707 [INF] 127.0.0.1:7422 - lid:3 - Leafnode connection created for account: A
    [9381] 2021/07/22 07:55:49.854748 [DBG] ip=192.168.4.21
    [9381] 2021/07/22 07:55:49.855039 [INF] Server is ready
    ```

    Messages will be routed across the servers:

    ```
    nats -s nats://A:A@localhost:4223 sub foo
    07:53:44 Subscribing on foo
    07:55:49 Disconnected due to: EOF, will attempt reconnect
    07:55:51 Reconnected [nats://A:A@localhost:4223]
    [#1] Received on "foo"
    hello
    ```

    ```
    nats -s nats://A:A@localhost:4222 pub foo hello
    07:55:54 Published 5 bytes to "foo"
    ```