Skip to content

Commit

Permalink
Rename main to hub
Browse files Browse the repository at this point in the history
Signed-off-by: Byron Ruth <b@devel.io>
  • Loading branch information
bruth committed Aug 17, 2022
1 parent 051d324 commit 02cba05
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions examples/topologies/leafnode-jwt/cli/main.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
#!/bin/sh

set -xeuo pipefail
set -euo pipefail

# For this example, we are going to have a service connected
# to the main server and then another client send a request
# to the hub server and then another client send a request
# via a connection to the leaf node.
NATS_MAIN_URL="nats://0.0.0.0:4222"
NATS_HUB_URL="nats://0.0.0.0:4222"
NATS_LEAF_URL="nats://0.0.0.0:4223"

# Create the operator, generate a signing key (which is a best practice),
# and initialize the default SYS account and sys user.
nsc add operator --generate-signing-key --sys --name local
nsc add operator \
--generate-signing-key \
--sys local

# A follow-up edit of the operator enforces signing keys are used for
# accounts as well. Setting the server URL is a convenience so that
# it does not need to be specified with call `nsc push`.
nsc edit operator --require-signing-keys \
--account-jwt-server-url "$NATS_MAIN_URL"
nsc edit operator \
--require-signing-keys \
--account-jwt-server-url \
"$NATS_HUB_URL"

# Next we need to create an account intended for application usage. The
# `SYS` account should be used for operational purposes. These commands create
Expand All @@ -30,22 +34,23 @@ nsc edit account APP \
--js-disk-storage -1 \
--js-mem-storage -1

nsc add user --account APP user
nsc add user \
--account APP user

# Check out the current settings of nsc.
nsc env

# The `nats` CLI provides a way to manage different _contexts_ by name.
# Here we define the server and the credentials (via `nsc` integration)
# (notice the operator/account/user hierarchy).
# We save two one for the main server and one for the leaf node. Note
# We save two one for the hub server and one for the leaf node. Note
# how didn't provide credentials for the leaf node..
nats context save main-user \
--server "$NATS_MAIN_URL" \
nats context save hub-user \
--server "$NATS_HUB_URL" \
--nsc nsc://local/APP/user

nats context save main-sys \
--server "$NATS_MAIN_URL" \
nats context save hub-sys \
--server "$NATS_HUB_URL" \
--nsc nsc://local/SYS/sys

nats context save leaf-user \
Expand All @@ -54,28 +59,30 @@ nats context save leaf-user \

# This command generates the bit of configuration to be used by the server
# to setup the embedded JWT resolver.
nsc generate config --nats-resolver --sys-account SYS > resolver.conf
nsc generate config \
--nats-resolver \
--sys-account SYS > resolver.conf

APP_PUB_KEY=$(nsc describe account --json APP | jq -r .sub)

# Create the most basic server config which enables leaf node connections
# and include the JWT resolver config.
echo 'Creating the main server conf...'
cat <<- EOF > main.conf
echo 'Creating the hub server conf...'
cat <<- EOF > hub.conf
port: 4222
leafnodes: {
port: 7422
}
jetstream: {
store_dir: /main/jetstream
store_dir: /hub/jetstream
}
include resolver.conf
EOF

# The second config is for the leaf node itself. This needs to define
# the leaf node _remotes_ which is the main server it will be connecting
# the leaf node _remotes_ which is the hub server it will be connecting
# to.
echo 'Creating the leaf node conf...'
cat <<- EOF > leaf.conf
Expand All @@ -97,14 +104,14 @@ jetstream: {
include resolver.conf
EOF

# Start the main server first.
nats-server -c main.conf 2> /dev/null &
MAIN_PID=$!
# Start the hub server first.
nats-server -c hub.conf 2> /dev/null &
HUB_PID=$!

sleep 1

# We need to put up the `APP` account JWT we created previously to the
# main server so that the user credentials file used both for the
# hub server so that the user credentials file used both for the
# client providing the service and the client making the request is
# trusted.
echo 'Pushing the account JWT...'
Expand All @@ -117,11 +124,11 @@ LEAF_PID=$!

sleep 1

# Connecting directly to the main server with the user creds, we can
# Connecting directly to the hub server with the user creds, we can
# create a simple service that will reply to any request published
# to `greet` with the text `hello`. This is put in the background
# since this will block while serving.
nats --context main-user reply 'greet' 'hello' &
nats --context hub-user reply 'greet' 'hello' &
SERVICE_PID=$!

# Tiny sleep to ensure the service is connected.
Expand All @@ -131,12 +138,12 @@ sleep 1
# a request to `greet`. Notice two things, one is that no credentials
# file is specified since the leaf server does not have authentication
# setup. Instead the `leafnodes.remotes` section of the config defines
# the main server and provides the credentials so that the leaf node
# the hub server and provides the credentials so that the leaf node
# is authenticated for forwarding messaging. This request will be
# transparently fullfilled by the service connected to the main server.
# transparently fullfilled by the service connected to the hub server.
nats --context leaf-user request 'greet' ''

# Finally stop the service and servers.
kill $SERVICE_PID
kill $LEAF_PID
kill $MAIN_PID
kill $HUB_PID

1 comment on commit 02cba05

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for nats-by-example ready!

✅ Preview
https://nats-by-example-kzajvdreo-connecteverything.vercel.app

Built with commit 02cba05.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.