This plugin invokes OpenWhisk Action. It can be used in combination with other request plugins to secure, manage or extend the function.


Terminology

  • plugin: a plugin executing actions inside Kong before or after a request has been proxied to the upstream API.
  • Service: the Kong entity representing an external upstream API or microservice.
  • Route: the Kong entity representing a way to map downstream requests to upstream services.
  • Consumer: the Kong entity representing a developer or machine using the API. When using Kong, a Consumer only communicates with Kong which proxies every call to the said upstream API.
  • Credential: a unique string associated with a Consumer, also referred to as an API key.
  • upstream service: this refers to your own API/service sitting behind Kong, to which client requests are forwarded.
  • API: a legacy entity used to represent your upstream services. Deprecated in favor of Services since CE 0.13.0 and EE 0.32.

Installation

You can either use the LuaRocks package manager to install the plugin

$ luarocks install kong-plugin-openwhisk

or install it from source. For more information on Plugin installation, please see the documentation Plugin Development - (un)Install your plugin


Configuration

Enabling the plugin on a Service

Configure this plugin on a Service by making the following request:

$ curl -X POST http://kong:8001/services/{service}/plugins \
    --data "name=openwhisk"  \
    --data "config.host=OPENWHISK_HOST" \
    --data "config.path=PATH_TO_ACTION" \
    --data "config.action=ACTION_NAME" \
    --data "config.service_token=AUTHENTICATION_TOKEN"

  • service: the id or name of the Service that this plugin configuration will target.

Enabling the plugin on a Route

Configure this plugin on a Route with:

$ curl -X POST http://kong:8001/routes/{route_id}/plugins \
    --data "name=openwhisk"  \
    --data "config.host=OPENWHISK_HOST" \
    --data "config.path=PATH_TO_ACTION" \
    --data "config.action=ACTION_NAME" \
    --data "config.service_token=AUTHENTICATION_TOKEN"

  • route_id: the id of the Route that this plugin configuration will target.

Enabling the plugin on a Consumer

You can use the http://localhost:8001/plugins endpoint to enable this plugin on specific Consumers:

$ curl -X POST http://kong:8001/plugins \
    --data "name=openwhisk" \
    --data "consumer_id={consumer_id}"  \
    --data "config.host=OPENWHISK_HOST" \
    --data "config.path=PATH_TO_ACTION" \
    --data "config.action=ACTION_NAME" \
    --data "config.service_token=AUTHENTICATION_TOKEN"

Where consumer_id is the id of the Consumer we want to associate with this plugin.

You can combine consumer_id and service_id

in the same request, to furthermore narrow the scope of the plugin.

Enabling the plugin on an API

If you are using an older version of Kong with the legacy API entity (deprecated in favor of Services since CE 0.13.0 and EE 0.32.), you can configure this plugin on top of such an API by making the following request:

$ curl -X POST http://kong:8001/apis/{api}/plugins \
    --data "name=openwhisk"  \
    --data "config.host=OPENWHISK_HOST" \
    --data "config.path=PATH_TO_ACTION" \
    --data "config.action=ACTION_NAME" \
    --data "config.service_token=AUTHENTICATION_TOKEN"

  • api: either id or name of the API that this plugin configuration will target.

Global plugins

All plugins can be configured using the http://kong:8001/plugins/ endpoint. A plugin which is not associated to any Service, Route or Consumer (or API, if you are using an older version of Kong) is considered "global", and will be run on every request. Read the Plugin Reference and the Plugin Precedence sections for more information.

Parameters

Here's a list of all the parameters which can be used in this plugin's configuration:

form parameterdefaultdescription
nameThe name of the plugin to use, in this case openwhisk
service_idThe id of the Service which this plugin will target.
route_idThe id of the Route which this plugin will target.
enabledtrueWhether this plugin will be applied.
consumer_idThe id of the Consumer which this plugin will target.
api_idThe id of the API which this plugin will target. Note: The API Entity is deprecated in favor of Services since CE 0.13.0 and EE 0.32.
config.host

Host of the OpenWhisk server.

config.port
optional

443

Port of the OpenWhisk server.

config.path

The path to Action resource.

config.action

Name of the Action to be invoked by the plugin.

config.service_token

The service token to access Openwhisk resources.

config.https_verify
optional

false

Set it to true to authenticate Openwhisk server.

config.https
optional

true

Use of HTTPS to connect with the OpenWhisk server.

config.result
optional

true

Return only the result of the Action invoked.

config.timeout
optional

60000

Timeout in milliseconds before aborting a connection to OpenWhisk server.

config.keepalive
optional

60000

Time in milliseconds for which an idle connection to OpenWhisk server will live before being closed.

Note: If config.https_verify is set as true then the server certificate will be verified according to the CA certificates specified by the lua_ssl_trusted_certificate directive in your Kong configuration.

Demonstration

For this demonstration we are running Kong and Openwhisk platform locally on a Vagrant machine on a MacOS.

  1. Create a javascript Action hello with the following code snippet on the Openwhisk platform using wsk cli.

     function main(params) {
         var name = params.name || 'World';
         return {payload:  'Hello, ' + name + '!'};
     }
    
     $ wsk action create hello hello.js
    
     ok: created action hello
    
  2. Create a Service or Route (or use the depreciated API entity)

    Create a Service.

     $ curl -i -X  POST http://localhost:8001/services/ \
       --data "name=openwhisk-test" \
       --data "url=http://example.com"
    
     HTTP/1.1 201 Created
     ...
    
    

    Create a Route that uses the Service.

     $ curl -i -f -X  POST http://localhost:8001/services/openwhisk-test/routes/ \
       --data "paths[]=/"
    
     HTTP/1.1 201 Created
     ...
    
    

    Or you could use the API entity.

     $ curl -i -X  POST http://localhost:8001/apis/ \
       --data "name=openwhisk-test" -d "hosts=example.com" \
       --data "upstream_url=http://example.com"
    
     HTTP/1.1 201 Created
     ...
    
    
  3. Enable the openwhisk plugin on the Route

Plugins can be enabled on a Service or a Route. This example uses a Service.

```bash
$ curl -i -X POST http://localhost:8001/services/openwhisk-test/plugins \
    --data "name=openwhisk" \
    --data "config.host=192.168.33.13" \
    --data "config.service_token=username:key" \
    --data "config.action=hello" \
    --data "config.path=/api/v1/namespaces/guest"

HTTP/1.1 201 Created
...

```
  1. Make a request to invoke the action

    Without parameters

       $ curl -i -X POST http://localhost:8000/ -H "Host:example.com"
       HTTP/1.1 200 OK
       ...
    
       {
         "payload": "Hello, World!"
       }
    

    Parameters as form-urlencoded

       $ curl -i -X POST http://localhost:8000/ -H "Host:example.com" --data "name=bar"
       HTTP/1.1 200 OK
       ...
    
       {
         "payload": "Hello, bar!"
       }
    

    Parameters as JSON body

       $ curl -i -X POST http://localhost:8000/ -H "Host:example.com" \
         -H "Content-Type:application/json" --data '{"name":"bar"}'
       HTTP/1.1 200 OK
       ...
    
       {
         "payload": "Hello, bar!"
       }
    

    Parameters as multipart form

       $ curl -i -X POST http://localhost:8000/ -H "Host:example.com"  -F name=bar
       HTTP/1.1 100 Continue
    
       HTTP/1.1 200 OK
       ...
    
       {
         "payload": "Hello, bar!"
       }
    

    Parameters as querystring

       $ curl -i -X POST http://localhost:8000/?name=foo -H "Host:example.com"
       HTTP/1.1 200 OK
       ...
    
       {
         "payload": "Hello, foo!"
       }
    

    OpenWhisk metadata in response

    When Kong’s config.result is set to false, OpenWhisk’s metadata will be returned in response:

       $ curl -i -X POST http://localhost:8000/?name=foo -H "Host:example.com"
       HTTP/1.1 200 OK
       ...
    
       {
         "duration": 4,
         "name": "hello",
         "subject": "guest",
         "activationId": "50218ff03f494f62abbde5dfd2fcc68a",
         "publish": false,
         "annotations": [{
           "key": "limits",
           "value": {
             "timeout": 60000,
             "memory": 256,
             "logs": 10
           }
         }, {
           "key": "path",
           "value": "guest/hello"
         }],
         "version": "0.0.4",
         "response": {
           "result": {
             "payload": "Hello, foo!"
           },
           "success": true,
           "status": "success"
         },
         "end": 1491855076125,
         "logs": [],
         "start": 1491855076121,
         "namespace": "guest"
       }
    

Limitations

Use a fake upstream service

When using the OpenWhisk plugin, the response will be returned by the plugin itself without proxying the request to any upstream service. This means that a Service’s host, port, path properties will be ignored, but must still be specified for the entity to be validated by Kong. The host property in particular must either be an IP address, or a hostname that gets resolved by your nameserver.

When the plugin is added to an API entity (which is deprecated as of 0.13.0), it is the upsream_url property which must be specified and resolvable as well (but ignored).

Response plugins

There is a known limitation in the system that prevents some response plugins from being executed. We are planning to remove this limitation in the future.