ApiGateway plugin for CakePHP
Description
This CakePHP 3 plugin allows the user to specify which controller/methods are used as API endpoints through a basic UI, and enforce AWS APIGateway request parameters. For the endpoints which are specified, the plugin checks for the 'x-api-key' header in the request and verifies the value is an enabled API Key in your AWS APIGateway account. Invalid requests result in a Cake\Controller\Exception\AuthSecurityException being thrown., (*1)
Installation
You can install this plugin into your CakePHP application using composer., (*2)
The recommended way to install composer packages is:, (*3)
composer require torchlighttechnology/api-security-plugin
Configuration
Load the plugin in the host app's config/bootstrap.php file:, (*4)
Plugin::load('ApiGateway', ['bootstrap' => true, 'routes' => true]);
Add the namespace into the host app's composer.json file, in the autoload-dev section:, (*5)
"ApiGateway\\Test\\": "vendor/torchlighttechnology/api-security-plugin/tests/"
Add the AwsAuthenticator Component in the host app's src/Controller/AppController.php:, (*6)
public function initialize()
{
parent::initialize();
$this->loadComponent('ApiGateway.AwsAuthenticator');
}
Run the ApiGateway migrations:, (*7)
bin/cake migrations migrate -p ApiGateway
Environment variables
ApiGateway plugin uses the AWS API to retrieve the valid AWS API Gateway keys, and it uses Redis for caching method names and API calls. The plugin relies on environment variables to make the connection to Redis and AWS.
The AWS environment variables you need to set are:, (*8)
ASSUME_ROLE_ARN
The Redis environment variables you need to set are:, (*9)
REDIS_SERVER
The default value for REDIS_SERVER is localhost. If you don't want to use Redis you can use File caching by setting REDIS_ENGINE env variable to 'File' (REDIS_SERVER var will then be ignored)., (*10)
Setup
Once you have properly configured the plugin, navigate to http://yourapp.local/api-gateway/end-points. Click on 'Configure End Points'. You should see a list of controller names with their method names as checkboxes. Select the methods that you want the plugin to protect.
There is also a Clear AWS API Cache button. This will invalidate the Redis cache, and on the next call to a protected endpoint, it will refresh with the latest API Keys from AWS APIGateway., (*11)