Skip to content

Instantly share code, notes, and snippets.

@mayorova
Last active June 20, 2018 14:52
Show Gist options
  • Save mayorova/b56ee7e93b83b72399b8f5fb1dcccda4 to your computer and use it in GitHub Desktop.
Save mayorova/b56ee7e93b83b72399b8f5fb1dcccda4 to your computer and use it in GitHub Desktop.
Running apisonator locally

Here are the steps to get apisonator (opensource project for 3scale backend) run locally, which can be useful for testing.

The image can be either built manually from the GitHub source, or there is an image available on quay.io.

The attached file starts redis, backend worker and backend listener.

In theory it could be possible to start APIcast with the same docker-compose file, but there is some issue with the resolver - it doesn't pick up the services hostnames that docker-compose resolves normally (i.e. I couldn't use http://listener:3000 from APIcast). So, I am starting APIcast locally.

Steps:

  1. Start the apisonator components using docker-compose. Make sure the docker-compose.yml is in the current directory.
docker-compose up
  1. Initialize backend using the internal API using the curls in the attached file init_backend.sh.
chmod a+x init_backend.sh
./init_backend.sh

The script has some hardcoded values, you can modify them as you wish.

  1. Check that you have successfully initialized backend by runnning a curl command.
curl -XGET -v "http://127.0.0.1:3000/transactions/authorize.xml?service_token=servicetoken&service_id=1&user_key=12345"

This should give OK.

  1. Start APIcast locally.
git clone git@github.com:3scale/apicast.git
cd apicast
git checkout tags/v3.1.0
  1. start APIcast with the following command. Make sure you place the attached config.json inside your local copy of APIcast (or change the THREESCALE_CONFIG_FILE value):
THREESCALE_CONFIG_FILE=$(pwd)/config.json \
    APICAST_CONFIGURATION_LOADER=boot \
    THREESCALE_DEPLOYMENT_ENV=production \
    BACKEND_ENDPOINT_OVERRIDE=http://127.0.0.1:3000 \
    bin/apicast -vvv

You can adjust the file, but make sure you also adjust the backend init curls (namely, service ID and service token). Also note that it is adjusted to work with "API key" scheme.

  1. Make a test call to APIcast
curl -v "http://localhost:8080/hello" -H "user-key: 12345"

This should return a successfull response from Echo API.

You can also call Service Management API directly as follows:

curl -v "http://localhost:3000/transactions/authrep.xml?service_token=servicetoken&service_id=1&user_key=12345&usage%5Bhits%5D=1"
{
"services": [
{
"id": 1,
"backend_version": "1",
"backend_authentication_type": "service_token",
"backend_authentication_value": "servicetoken",
"proxy": {
"api_backend": "https://echo-api.3scale.net:443",
"auth_app_key": "app-key",
"auth_app_id": "app-id",
"auth_user_key": "user-key",
"credentials_location": "headers",
"error_auth_failed": "Authentication failed",
"error_auth_missing": "Authentication parameters missing",
"error_status_auth_failed": 403,
"error_headers_auth_failed": "text/plain; charset=us-ascii",
"error_status_auth_missing": 403,
"error_headers_auth_missing": "text/plain; charset=us-ascii",
"error_no_match": "No Mapping Rule matched",
"error_status_no_match": 404,
"error_headers_no_match": "text/plain; charset=us-ascii",
"secret_token": "secret",
"hostname_rewrite": null,
"oauth_login_url": null,
"oidc_issuer_endpoint": null,
"authentication_method": "1",
"hosts": [
"localhost",
"apicast",
"apicast-production",
"apicast-staging"
],
"backend": {
"endpoint": "https://su1.3scale.net",
"host": "su1.3scale.net"
},
"proxy_rules": [
{
"http_method": "GET",
"pattern": "/hello",
"metric_system_name": "hits",
"delta": 1,
"parameters": [],
"querystring_parameters": {}
},
{
"http_method": "GET",
"pattern": "/goodbye",
"metric_system_name": "hits",
"delta": 1,
"parameters": [],
"querystring_parameters": {}
}
]
}
}
]
}
version: '2'
services:
redis:
image: registry.access.redhat.com/rhscl/redis-32-rhel7:3.2
ports:
- "6379:6379"
listener:
image: quay.io/3scale/apisonator:v2.85.0
ports:
- "3000:3000"
command: 3scale_backend start -e production -p 3000 -x /dev/stdout
links:
- redis:redis
environment:
CONFIG_REDIS_PROXY: redis:6379
CONFIG_QUEUES_MASTER_NAME: redis:6379/1
RACK_ENV: production
CONFIG_INTERNAL_API_USER: internal_api_user
CONFIG_INTERNAL_API_PASSWORD: internal_api_password
PUMA_WORKERS: 0
worker:
# image: registry.access.redhat.com/3scale-amp21/backend:1.4-2
image: quay.io/3scale/apisonator:v2.85.0
command: bin/3scale_backend_worker run
links:
- redis:redis
environment:
CONFIG_REDIS_PROXY: redis:6379
CONFIG_QUEUES_MASTER_NAME: redis:6379/1
RACK_ENV: production
CONFIG_WORKERS_LOGGER_FORMATTER: json
#!/bin/bash
# Create service
curl -XPUT -v -u internal_api_user:internal_api_password "http://127.0.0.1:3000/internal/services/1" -d '{"service":{"id":"1","backend_version":"1","provider_key":"providerkey"}}'
# Create service token
curl -XPOST -v -u internal_api_user:internal_api_password "http://127.0.0.1:3000/internal/service_tokens/" -d '{"service_tokens":{"servicetoken": {"service_id": 1}}}'
# Create metric
curl -XPUT -v -u internal_api_user:internal_api_password "http://127.0.0.1:3000/internal/services/1/metrics/1001" -d '{"metric":{"service_id": 1, "id": 1001, "name": "hits"}}'
# Create application
curl -XPUT -v -u internal_api_user:internal_api_password "http://127.0.0.1:3000/internal/services/1/applications/111" -d '{"application":{"service_id":"1","id":"111","state":"active","plan_id":"1","plan_name":"dummy plan", "user_key": "userkey"}}'
# Create user key
curl -XPUT -v -u internal_api_user:internal_api_password "http://127.0.0.1:3000/internal/services/1/applications/111/key/12345"
# Optionally create some usage limits. For example, 3/minute for the metric 'hits' (id=1001) on application plan id=1
curl -XPUT -v -u internal_api_user:internal_api_password "http://127.0.0.1:3000/internal/services/1/plans/1/usagelimits/1001/minute" -d '{"usagelimit": {"service_id": "1", "plan_id": "1", "metric_id": "1001", "minute": 3}}'
# To delete the previous usage limits run
# curl -XDELETE -v -u internal_api_user:internal_api_password "http://127.0.0.1:3000/internal/services/1/plans/1/usagelimits/1001/minute"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment