Beginner tutorial: Developing Web service drivers

In this tutorial you will create a basic Web service driver that reverses strings, learning the basic structure of a driver and how to invoke it from Siren Investigate.

Prerequisites

Before you begin, download and install the following tools:

Steps

  1. Create a directory for the Web service project by running the following commands:

    mkdir reverse
    cd reverse
    npm init --yes
  2. Add Kibana to the package.json file. Ensure that the file contains the following parameters:

    {
      "name": "reverse",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC",
      "kibana": {
        "version": "5.6.10"
      }
    }
  3. Add the web-service-interface package by running the following command:

    npm install @sirensolutions/web-service-interface
  4. Create an index.js file with the following contents:

    import { registerServices, ServiceDefinition } from '@sirensolutions/web-service-interface';
    
    class StringReverser extends ServiceDefinition { (1)
    
      name = 'stringreverser';
      inputSchema = { (2)
          message: {
            type: 'text',
            required: true // This defaults to false if you do not set it.
          }
        };
      outputConfiguration = { reversed_message: 'text' }; (3)
    
      invoke(inputs) { (4)
        // Reverse my input
        const reversed_input = inputs.message.split('').reverse().join('');
        return {
          reversed_message: reversed_input
        };
      }
    }
    
    export default registerServices('reverse', [StringReverser]); (5)
    1 Extends the class, so that it complies with the Web service interface.
    2 Specifies the inputs that this Web service accepts and whether or not they are mandatory parameters.
    3 Defines the structure and types of the output fields, which are used to create the Elasticsearch field mapping.
    4 The function that is called when your Web service is invoked. It receives the parameters that are defined in inputSchema and must return an object that has the same structure as the outputConfiguration.
    5 This export and function call is how you hook the Web service to Siren Investigate.
  5. It’s ready to go! Copy the reverse folder into the Siren Investigate plugins folder by running the following command:

    cp -r . /path/to/investigate/plugins/reverse
  6. Start Elasticsearch by running the following commands:

    cd path/to/elasticsearch
    bin/elasticseach
  7. Go the directory where Siren Investigate has been installed:

    cd path/to/investigate
  8. Open the config/investigate.yml file and add the following snippet to enable Web services if it is not present:

    web_services:
      global:
        enabled: true
  9. Start Siren Investigate by running the following command:

    bin/investigate
  10. Open Siren Investigate in your browser (running on http://localhost:5606 by default), and click on the Web services dashboard.

    Web services dashboard

  11. Select reverse stringreverser as the active Web service profile in the Query Web services visualization and click Save.

    The Query Web services visualization

  12. Enter a string in the message field and click Query; you should see the result of your request in the Invocation Results visualization.

    The result of the invocation