cleaniquecoders/kong-admin-api

A PHP package for seamless interaction with Kong Gateway's Admin API

v1.0.1 2024-11-05 14:41 UTC

This package is auto-updated.

Last update: 2024-11-18 02:19:31 UTC


README

Latest Version on Packagist Tests Total Downloads

Kong Admin API

A PHP package for seamless interaction with Kong Gateway's Admin API

Installation

You can install the package via Composer:

composer require cleaniquecoders/kong-admin-api

Setup Kong Gateway API Loopback

For initial setup, you need Kong Gateway configured with loopback it's Admin API and enable header based authentication. Following are an example of the setup. These commands will simply create the new consumer, add admin API service, create API key for consumer.

#!/bin/bash

echo " ๐Ÿš€ Create Admin API Service"
curl --request POST \
  --url http://localhost:8001/services \
  --data name=admin-api-service \
  --data url='http://localhost:8001' | jq '.' > admin-api-service.json

echo " ๐Ÿš€ Create Admin API Route"
curl --request POST \
  --url http://localhost:8001/services/admin-api-service/routes \
  --data 'paths[]=/admin-api' \
  --data name=admin-api-route | jq '.' > admin-api-route.json

echo " ๐Ÿš€ Enable Key Auth on Admin API Service"
curl --request POST \
  --url http://localhost:8001/services/admin-api-service/plugins \
  --header 'Content-Type: application/json' \
  --header 'accept: application/json' \
  --data '{"name":"key-auth","config":{"key_names":["api-key"],"key_in_query":false}}' | jq '.' > admin-api-key.json

echo " ๐Ÿš€ Create Admin API Consumer"
curl --request POST \
  --url http://localhost:8001/consumers \
  --header 'Content-Type: application/json' \
  --header 'accept: application/json' \
  --data '{"username":"apim","custom_id":"apim"}' | jq '.' > consumer-apim.json

echo " ๐Ÿš€ Create APIM API Key"
curl -X POST http://localhost:8001/consumers/apim/key-auth | jq '.' > admin-api-consumer-key.json

Usage

Here are examples of using this package:

Initialize Configuration

use CleaniqueCoders\KongAdminApi\Configuration;

$configuration = new Configuration(
    base: 'http://127.0.0.1:8000',
    uri: 'admin-api',
    apiKey: 'your-api-key',
    keyName: 'api-key'
);

Create Connector and Client

use CleaniqueCoders\KongAdminApi\Client;
use CleaniqueCoders\KongAdminApi\Connector;

$connector = new Connector($configuration);
$client = new Client($connector);

Example: Store a New Service

Stores a new service with the specified name and URL.

use CleaniqueCoders\KongAdminApi\Enums\Endpoint;
use CleaniqueCoders\KongAdminApi\Request;

$response = $client->send(
    (new Request)
        ->setEndPoint(Endpoint::SERVICES)
        ->store([
            'name' => 'example-service',
            'url' => 'http://example.com'
        ])
);

print_r($response);

Example: Update an Existing Service

Updates an existing service (specified by identifier) with new data.

$response = $client->send(
    (new Request)
        ->setEndPoint(Endpoint::SERVICES)
        ->update('example-service', [
            'url' => 'http://new-example.com'
        ])
);

print_r($response);

Example: Delete a Service

Deletes a service specified by the identifier.

$response = $client->send(
    (new Request)
        ->setEndPoint(Endpoint::SERVICES)
        ->delete('example-service')
);

print_r($response);

Example: Get a Specific Service

Retrieves details for a single service by passing the identifier.

$response = $client->send(
    (new Request)
        ->setEndPoint(Endpoint::SERVICES)
        ->get('example-service')
);

print_r($response);

Example: Get a List of All Services

Retrieves a list of all services without specifying an identifier.

$response = $client->send(
    (new Request)
        ->setEndPoint(Endpoint::SERVICES)
        ->get()
);

print_r($response);

Example: Add a Rate-Limiting Plugin to a Consumer

Associates a rate-limiting plugin with a specific consumer, configuring the rate limit to 5 requests per minute.

use CleaniqueCoders\KongAdminApi\Enums\Plugin;

$response = $client->send(
    (new Request)
        ->plugin(Plugin::RATE_LIMITING, Endpoint::CONSUMERS, 'consumer-id')
        ->store(['config' => ['minute' => 5]])
);

print_r($response);

Example: Update a CORS Plugin for a Route

Updates an existing CORS plugin associated with a specific route, allowing all origins.

$response = $client->send(
    (new Request)
        ->plugin(Plugin::CORS, Endpoint::ROUTES, 'route-id', 'plugin-id')
        ->update('plugin-id', ['config' => ['origins' => ['*']]])
);

print_r($response);

Example: Get a JWT Plugin for a Service

Retrieves details of a JWT plugin associated with a specific service.

$response = $client->send(
    (new Request)
        ->plugin(Plugin::JWT, Endpoint::SERVICES, 'service-id', 'plugin-id')
        ->get()
);

print_r($response);

Example: Delete an HMAC Authentication Plugin from a Service

Deletes an HMAC authentication plugin associated with a specific service.

$response = $client->send(
    (new Request)
        ->plugin(Plugin::HMAC_AUTH, Endpoint::SERVICES, 'service-id', 'plugin-id')
        ->delete('plugin-id')
);

print_r($response);

References

For more details on available endpoints, parameters, and usage of the Kong Admin API, please refer to the Kong Admin API documentation.

To explore the plugins available for free and open-source use, visit the Kong Hub Plugins page.

The Endpoint enum in this package reflects the endpoints defined in the OpenAPI specification provided by Kong.

Certainly! Hereโ€™s the list of free and open-source plugins available in Kong's free tier, as per the Kong Hub documentation:

List of Free Tier Plugins for Kong

This list includes each pluginโ€™s identifier, endpoint, label, and a brief description of its functionality. These plugins can be used to add various features such as rate limiting, authentication, logging, and monitoring to Kong APIs in the free and open-source tier.

Testing

To run tests:

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details on how to contribute. See development documentation details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.