Example tool demonstrating how to programmatically create NATS user credentials with IP-based access controls using the Synadia Cloud API.
NATS user JWTs can include a src claim that restricts which IP addresses or
CIDR ranges are allowed to connect. This provides network-level access control
independent of the credentials themselves.
This tool:
- Creates a new user if it doesn't exist, or updates an existing user
- Sets CIDR restrictions on the user's JWT settings
- Downloads the credentials file
- Go 1.21+
- Synadia Cloud account with API access
- Account ID and signing key group ID from Synadia Cloud
go build -o cidr-user .Set environment variables:
export CLOUD_URL="https://cloud.synadia.com"
export ACCESS_TOKEN="your-api-token"To get an API token, go to Synadia Cloud > Personal Access Tokens.
Create a new user with CIDR restrictions:
./cidr-user \
-account 2bGxXXXXXXXXXXXXXXXXXXXX \
-sk-group 2cHyYYYYYYYYYYYYYYYYYYYY \
-name "restricted-user" \
-cidrs "10.0.0.0/8,192.168.0.0/16" \
-output restricted-user.credsUpdate an existing user's CIDR restrictions:
./cidr-user \
-account 2bGxXXXXXXXXXXXXXXXXXXXX \
-name "restricted-user" \
-cidrs "172.16.0.0/12"Remove CIDR restrictions (allow all IPs):
./cidr-user \
-account 2bGxXXXXXXXXXXXXXXXXXXXX \
-name "restricted-user" \
-cidrs ""| Flag | Description |
|---|---|
-account |
Account ID (required) |
-sk-group |
Signing key group ID (required for new users) |
-name |
User name (required) |
-cidrs |
Comma-separated CIDR list |
-output |
Output file for credentials (default: stdout) |
-expires |
JWT expiration in seconds (0 = account default) |
The tool uses the Synadia Cloud REST API:
GET /core/beta/accounts/{id}/nats-users- List users to check if name existsPOST /core/beta/accounts/{id}/nats-users- Create new user withjwt_settings.srcPATCH /core/beta/nats-users/{id}- Update existing user'sjwt_settings.srcPOST /core/beta/nats-users/{id}/creds- Download credentials file
The jwt_settings.src field accepts an array of CIDR strings that are embedded
in the user JWT's claims. NATS servers enforce these restrictions at connection
time.