Skip to content

NATS API Server is a REST based configuration server for the nats-server to configure authorization and reload the nats-server

License

Notifications You must be signed in to change notification settings

rishikeshbedre/nats-api-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NATS API SERVER

Licence Build Status Codacy Badge codecov Go Report Card

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.

Contents

How it Works

nats-api-server

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.

Usage

To install NATS API Server, you need to install Go(version 1.12+ is required) and set your Golang workspace.

  1. 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
  1. Then you need to install NATS-Server and start the server using the configuration file present in the NATS API Server.

API Documentation

Add User

Adds new user to the authorization configuration.

  • URL: /user

  • Method: POST

  • Request:

    • Header:
      • Content-Type: application/json
    • Body: {"user":"xyz","password":"123"}
  • Success Response:

    • Code: 200
    • Content: {"message":"User:xyz added"}
  • 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"}
  • Sample Call:

      $curl --header "Content-Type: application/json" --request POST --data '{"user":"xyz","password":"123"}' http://localhost:6060/user
    

Delete User

Deletes the user from authorization configuration.

  • URL: /user

  • Method: DELETE

  • Request:

    • Header:
      • Content-Type: application/json
    • Body: {"user":"xyz"}
  • Success Response:

    • Code: 200
    • Content: {"message":"User:xyz deleted"}
  • 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"}
  • Sample Call:

      $curl --header "Content-Type: application/json" --request DELETE --data '{"user":"xyz"}' http://localhost:6060/user
    

Show 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}}]}
  • Error Response:

    • Code: 400 STATUS BAD REQUEST
    • Content: {"error":"???jsonbinderror"}
  • Sample Call:

      $curl --request GET http://localhost:6060/user
    

Add Topic

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
    • Body: {"user":"xyz","permissions":{"publish":["test","quest"],"subscribe":["test","quest"]}}
  • Success Response:

    • Code: 200
    • Content: {"message":"Topics Added for the user:xyz"}
  • 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"}
  • Sample Call:

      curl --header "Content-Type: application/json" --request POST --data '{"user":"xyz","permissions":{"publish":["test","quest"],"subscribe":["test","quest"]}}' http://localhost:6060/topic
    

Delete 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
    • Body: {"user":"xyz","permissions":{"publish":["quest"],"subscribe":["quest"]}}
  • Success Response:

    • Code: 200
    • Content: {"message":"Topics deleted for the user:xyz"}
  • 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"}
  • Sample Call:

      curl --header "Content-Type: application/json" --request DELETE --data '{"user":"xyz","permissions":{"publish":["quest"],"subscribe":["quest"]}}' http://localhost:6060/topic
    

Download Configuration

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"}
  • Error Response:

    • Code: 400 STATUS BAD REQUEST
    • Content: {"error":"??filewriteerror or ??jsonbinderror or ??cmderror"}
  • Sample Call:

      curl --request POST http://localhost:6060/reload
    

Docker

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.

  1. To build the image run following command:
  $./extras/build.sh
  1. 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

Kubernetes

You can run this setup in kubernetes also by using this yaml file:

  $kubectl apply -f ./nats-api-server.yaml

Testing

To run test just run following command:

  $go mod download
  $make test

About

NATS API Server is a REST based configuration server for the nats-server to configure authorization and reload the nats-server

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published