NATS API Server is a REST based configuration server for NATS-Server. It features REST end-points to configure user authorization and reload the NATS-Server. It is written using Gin Web Framework and jsoniter to make server high performant.
NATS API Server has rest end points to add|delete user|topic where it writes the authorization configuration to a file. The API Server also has an option to send reload signal to NATS-Server where it reads this configuration file and allows only authenticated users to connect to NATS-Server.
To install NATS API Server, you need to install Go(version 1.12+ is required) and set your Golang workspace.
- This project uses go modules and provides a make file. You should be able to simply install and start:
$ git clone https://github.com/rishikeshbedre/nats-api-server.git
$ cd nats-api-server
$ make
$ ./nats-api-server
- Then you need to install NATS-Server and start the server using the configuration file present in the NATS API Server.
Adds new user to the authorization configuration.
-
URL:
/user
-
Method:
POST
-
Request:
- Header:
- Content-Type:
application/json
- Content-Type:
- Body:
{"user":"xyz","password":"123"}
- Header:
-
Success Response:
- Code:
200
- Content:
{"message":"User:xyz added"}
- Code:
-
Error Response:
- Code:
400 STATUS BAD REQUEST
- Content:
{"error":"User:xyz already present"}
OR
- Code:
400 STATUS BAD REQUEST
- Content:
{"error":"Key: 'AddUserJSON.Password' Error:Field validation for 'Password' failed on the 'required' tag"}
- Code:
-
Sample Call:
$curl --header "Content-Type: application/json" --request POST --data '{"user":"xyz","password":"123"}' http://localhost:6060/user
Deletes the user from authorization configuration.
-
URL:
/user
-
Method:
DELETE
-
Request:
- Header:
- Content-Type:
application/json
- Content-Type:
- Body:
{"user":"xyz"}
- Header:
-
Success Response:
- Code:
200
- Content:
{"message":"User:xyz deleted"}
- Code:
-
Error Response:
- Code:
400 STATUS BAD REQUEST
- Content:
{"error":"User:xyz cannot be deleted"}
OR
- Code:
400 STATUS BAD REQUEST
- Content:
{"error":"Key: 'DeleteUserJSON.User' Error:Field validation for 'User' failed on the 'required' tag"}
- Code:
-
Sample Call:
$curl --header "Content-Type: application/json" --request DELETE --data '{"user":"xyz"}' http://localhost:6060/user
Returns the current authorization configuration.
-
URL:
/user
-
Method:
GET
-
Request:
NONE
-
Success Response:
- Code:
200
- Content:
{"message":[{"user":"natsdemouser","permissions":{"publish":null,"subscribe":null}}]}
- Code:
-
Error Response:
- Code:
400 STATUS BAD REQUEST
- Content:
{"error":"???jsonbinderror"}
- Code:
-
Sample Call:
$curl --request GET http://localhost:6060/user
Adds the topics to the particular user in authorization configuration. If any of the topics are present in the request JSON are available in the authorization configuration for that particular user, this end point returns a error message.
-
URL:
/topic
-
Method:
POST
-
Request:
- Header:
- Content-Type:
application/json
- Content-Type:
- Body:
{"user":"xyz","permissions":{"publish":["test","quest"],"subscribe":["test","quest"]}}
- Header:
-
Success Response:
- Code:
200
- Content:
{"message":"Topics Added for the user:xyz"}
- Code:
-
Error Response:
- Code:
400 STATUS BAD REQUEST
- Content:
{"error":"test topic is already present for the user:xyz"}
OR
- Code:
400 STATUS BAD REQUEST
- Content:
{"error":"Key: 'AddDeleteTopicJSON.User' Error:Field validation for 'User' failed on the 'required' tag"}
- Code:
-
Sample Call:
curl --header "Content-Type: application/json" --request POST --data '{"user":"xyz","permissions":{"publish":["test","quest"],"subscribe":["test","quest"]}}' http://localhost:6060/topic
Deletes the topics from the particular user in authorization configuration. If any of the topics are present in the request JSON are not available in the authorization configuration for that particular user, this end point returns a error message.
-
URL:
/topic
-
Method:
DELETE
-
Request:
- Header:
- Content-Type:
application/json
- Content-Type:
- Body:
{"user":"xyz","permissions":{"publish":["quest"],"subscribe":["quest"]}}
- Header:
-
Success Response:
- Code:
200
- Content:
{"message":"Topics deleted for the user:xyz"}
- Code:
-
Error Response:
- Code:
400 STATUS BAD REQUEST
- Content:
{"error":"Cannot delete topics for the user:xyz"}
OR
- Code:
400 STATUS BAD REQUEST
- Content:
{"error":"Key: 'AddDeleteTopicJSON.User' Error:Field validation for 'User' failed on the 'required' tag"}
- Code:
-
Sample Call:
curl --header "Content-Type: application/json" --request DELETE --data '{"user":"xyz","permissions":{"publish":["quest"],"subscribe":["quest"]}}' http://localhost:6060/topic
Stores the authorization configuration to the file and reload the nats server.
Note: Until you send this request to NATS API Server, add|delete user|topic requests doesn't reflect in NATS Server.
-
URL:
reload
-
Method:
POST
-
Request:
NONE
-
Success Response:
- Code:
200
- Content:
{"message":"Download and reload of Configuration Successful"}
- Code:
-
Error Response:
- Code:
400 STATUS BAD REQUEST
- Content:
{"error":"??filewriteerror or ??jsonbinderror or ??cmderror"}
- Code:
-
Sample Call:
curl --request POST http://localhost:6060/reload
Building the image for nats api server acutually builds both nats api server and nats server in one container, so when you run the container two services will run in the same container.
- To build the image run following command:
$./extras/build.sh
- While running the image you can persist the configuration file by mounting the volume to the host and container. To run the container just run the following command:
$docker run -it -p 4222:4222 -p 6060:6060 -v /home/rishikesh/Desktop/nats-data:/home/nats/configuration nats-api-server:0.0.1
You can run this setup in kubernetes also by using this yaml file:
$kubectl apply -f ./nats-api-server.yaml
To run test just run following command:
$go mod download
$make test