### Start APIcast with example policy Source: https://github.com/3scale/apicast/blob/master/examples/policies/README.md Starts APIcast with the example policy configuration. Requires APIcast tools and dependencies to be installed first. ```shell bin/apicast -c examples/policies/example.json -b --policy-load-path examples/policies -v ``` -------------------------------- ### Start Vagrant with libvirt provider Source: https://github.com/3scale/apicast/blob/master/doc/profiling.md Installs the vagrant-libvirt plugin and starts the Vagrant environment using the libvirt provider, then opens an SSH session. ```shell vagrant plugin install vagrant-libvirt vagrant up --provider=libvirt vagrant ssh ``` -------------------------------- ### Install OpenResty debuginfo packages Source: https://github.com/3scale/apicast/blob/master/script/traces/Readme.md Installs OpenResty debuginfo packages to get better user stack traces when using bpftrace. ```shell docker exec -ti --user root --privileged apicast_build_0_development_1 dnf install -y openresty-debuginfo ``` -------------------------------- ### Mock System API with adsf Source: https://github.com/3scale/apicast/blob/master/doc/oidc.md Commands to install adsf, change to the OIDC configuration directory, and start a simple web server to mock the System API for OIDC discovery. ```shell gem install adsf cd examples/configuration/oidc/ adsf ``` -------------------------------- ### Start development environment with make development Source: https://github.com/3scale/apicast/blob/master/README.md Starts a Docker container with bash and syncs the project source code with your local apicast directory for development. ```shell make development ``` -------------------------------- ### Start Vagrant environment Source: https://github.com/3scale/apicast/blob/master/doc/profiling.md Starts the Vagrant environment and opens an SSH session to it. Requires Vagrant and QEMU or VirtualBox, and at least 20GB of free space. ```shell vagrant up vagrant ssh ``` -------------------------------- ### Example CORS policy configuration Source: https://github.com/3scale/apicast/blob/master/gateway/src/apicast/policy/cors/Readme.md Example configuration for the CORS policy, specifying allowed headers, credentials, methods, origin, and max age. Use this as a starting point for enabling CORS on a service. ```json { "name": "cors", "version": "builtin", "configuration": { "allow_headers": [ "App-Id", "App-Key", "Content-Type", "Accept" ], "allow_credentials": true, "allow_methods": [ "GET", "POST" ], "allow_origin": "https://example.com", "max_age" : 200 } } ``` -------------------------------- ### Start APIcast from mounted source Source: https://github.com/3scale/apicast/blob/master/doc/profiling.md Starts APIcast from the mounted source directory. APIcast is mounted into /opt/app-src. ```shell cd /opt/app-src bin/apicast ``` -------------------------------- ### HTTP response with Example header Source: https://github.com/3scale/apicast/blob/master/examples/policies/README.md Example HTTP response showing the 'Example' header set to 'Value' by the ngx_example policy. ```http GET /test HTTP/1.1 X-Real-IP: 127.0.0.1 Host: echo User-Agent: curl/7.57.0 Accept: */* Example: Value ``` -------------------------------- ### Start APIcast server with hello_world_config.json Source: https://github.com/3scale/apicast/blob/master/doc/policy_tutorial/POLICY_RUN_LOCALLY.md Command to start the APIcast server with the hello_world_config.json file. Sets log-level to debug for verbose logging; use notice to reduce log lines while still logging custom policy entries. ```bash $ APICAST_LOG_LEVEL=debug APICAST_WORKERS=1 APICAST_CONFIGURATION_LOADER=lazy APICAST_CONFIGURATION_CACHE=0 THREESCALE_CONFIG_FILE=examples/configuration/hello_world_config.json ./bin/apicast ``` -------------------------------- ### Conditional policy example config Source: https://github.com/3scale/apicast/blob/master/gateway/src/apicast/policy/conditional/README.md Example configuration that executes the upstream policy only when the `Backend` header is `staging`. ```json { "name":"conditional", "version":"builtin", "configuration":{ "condition":{ "operations":[ { "left":"{{ headers['Backend'] }}", "left_type":"liquid", "op":"==", "right":"staging" } ] }, "policy_chain":[ { "name":"upstream", "version": "builtin", "configuration":{ "rules":[ { "regex":"/", "url":"http://my_staging_environment" } ] } } ] } } ``` -------------------------------- ### Compile pcredemo with standard PCRE installation Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcresample.html Compiles the pcredemo.c demonstration program when PCRE is installed in standard include and library directories. ```bash gcc -o pcredemo pcredemo.c -lpcre ``` -------------------------------- ### Install dependencies with make dependencies Source: https://github.com/3scale/apicast/blob/master/README.md Installs project dependencies inside the development container. ```shell make dependencies ``` -------------------------------- ### a+(*COMMIT)b example Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepattern.html Example pattern using (*COMMIT). It matches "xxaab" but not "aacaab". It can be thought of as a kind of dynamic anchor, or "I've started, so I must finish." ```regex a+(*COMMIT)b ``` -------------------------------- ### Display configure options with ./configure --help Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrebuild.html Run this command to display the complete list of configure options, including standard ones such as installation directory selection. ```bash ./configure --help ``` -------------------------------- ### Install bpftrace in container Source: https://github.com/3scale/apicast/blob/master/script/traces/Readme.md Installs bpftrace inside the apicast container using dnf. Requires root and privileged access. ```shell docker exec -ti --user root --privileged apicast_build_0_development_1 dnf install -y bpftrace ``` -------------------------------- ### Start APIcast with custom module Source: https://github.com/3scale/apicast/blob/master/examples/custom-module/README.md Starts APIcast with the custom module verbose.lua instead of the default apicast.lua. Uses a local configuration file so no 3scale account is needed. ```sh APICAST_MODULE=$(pwd)/verbose.lua ../../bin/apicast -c $(pwd)/../configuration/local.json ``` -------------------------------- ### Start Keycloak with Docker Source: https://github.com/3scale/apicast/blob/master/doc/oidc.md Starts a Keycloak 4.3.0 container with H2 database and default credentials, mapping port 8000 to 8080. ```shell docker run -e DB_VENDOR=H2 -e KEYCLOAK_USER=keycloak -e KEYCLOAK_PASSWORD=keycloak -p 8000:8080 jboss/keycloak:4.3.0.Final ``` -------------------------------- ### Create a new git branch and start the development container Source: https://github.com/3scale/apicast/blob/master/doc/policy_tutorial/POLICY_SCAFFOLD.md Creates a new git branch for the policy development tutorial and starts the development container with the make development command. The output shows the branch switch confirmation. ```shell $ git checkout -b policy-development-tutorial Switched to a new branch 'policy-development-tutorial' $ make development ``` -------------------------------- ### Start PCRE build on OpenVOS Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/NON-AUTOTOOLS-BUILD.txt Starts the build of PCRE on Stratus OpenVOS from the root of the package. Requires GNU Tools 3.4a and the posix.save.evf.gz package from ftp://ftp.stratus.com/pub/vos/posix/ga/posix.save.evf.gz. ```shell ./build.sh ``` -------------------------------- ### OpenTelemetry TOML configuration example Source: https://github.com/3scale/apicast/blob/master/doc/parameters.md Example configuration for the OpenTelemetry tracer. Uses the OTLP exporter and batch processor. The host and port can be overridden by the OTEL_EXPORTER_OTLP_ENDPOINT environment variable. The sampler can be set to AlwaysOn, AlwaysOff, or TraceIdRatioBased; the ratio is used only for TraceIdRatioBased. ```toml exporter = "otlp" processor = "batch" [exporters.otlp] # Alternatively, the OTEL_EXPORTER_OTLP_ENDPOINT environment variable can also be used. host = "localhost" port = 4317 [processors.batch] max_queue_size = 2048 schedule_delay_millis = 5000 max_export_batch_size = 512 [service] name = "apicast" # Opentelemetry resource name [sampler] name = "AlwaysOn" # Also: AlwaysOff, TraceIdRatioBased ratio = 0.1 parent_based = false ``` -------------------------------- ### Start APIcast for profiling with stapxx Source: https://github.com/3scale/apicast/blob/master/doc/profiling.md Starts APIcast with a single process and worker for profiling with stapxx. The output is redirected to /dev/null. ```shell bin/apicast -c examples/configuration/echo.json > /dev/null ``` -------------------------------- ### TLS policy configuration with generated certificates Source: https://github.com/3scale/apicast/blob/master/gateway/src/apicast/policy/tls/README.md Example configuration using the generated certificates for test.example.com. The certificate and key paths point to the files created by the Makefile. ```json { "name": "apicast.policy.tls", "configuration": { "certificates": [ { "certificate_path": "/home/centos/customCerts/test.example.com.crt", "certificate_key_path": "/home/centos/customCerts/test.example.com.key" } ] } } ``` -------------------------------- ### Run the gateway with make gateway and custom image Source: https://github.com/3scale/apicast/blob/master/dev-environments/grpc/README.md Start the APIcast gateway. The first command uses the local apicast-test Docker image; the second uses a custom apicast image specified by IMAGE_NAME. ```sh make gateway ``` ```sh make gateway IMAGE_NAME=quay.io/3scale/apicast:latest ``` -------------------------------- ### pcre_dfa_exec() call example Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcreapi.html Example call to pcre_dfa_exec() showing the required arguments: compiled pattern, subject string and length, start offset, options, ovector, workspace vector, and their element counts. The comments clarify that the counts are number of elements, not byte sizes. ```c int rc; int ovector[10]; int wspace[20]; rc = pcre_dfa_exec( re, /* result of pcre_compile() */ NULL, /* we didn't study the pattern */ "some string", /* the subject string */ 11, /* the length of the subject string */ 0, /* start at offset 0 in the subject */ 0, /* default options */ ovector, /* vector of integers for substring information */ 10, /* number of elements (NOT size in bytes) */ wspace, /* working space vector */ 20); /* number of elements (NOT size in bytes) */ ``` -------------------------------- ### Subject string for matching example Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/pcre.txt Example subject string that matches the pattern ^<.*> in three possible ways, demonstrating the difference between the standard and alternative algorithms. ```text ``` -------------------------------- ### gRPC server TLS listener in Go Source: https://github.com/3scale/apicast/blob/master/doc/grpc.md Example of setting up a gRPC server with TLS credentials in Go. It loads the server certificate and key, listens on a TCP port, and creates a new gRPC server with those credentials. ```go creds, err := credentials.NewServerTLSFromFile(*cert, *key) if err != nil { log.Fatalf("Failed to setup TLS: %v", err) } lis, err := net.Listen("tcp", port) if err != nil { log.Fatalf("failed to listen: %v", err) } s := grpc.NewServer(grpc.Creds(creds)) ``` -------------------------------- ### Conditional policy example config with on_failed Source: https://github.com/3scale/apicast/blob/master/gateway/src/apicast/policy/conditional/README.md Example configuration with an `on_failed` policy to terminate the chain if a policy fails, returning error status code 419. ```json { "name":"conditional", "version":"builtin", "configuration":{ "condition":{ "operations":[ { "left":"{{ headers['Backend'] }}", "left_type":"liquid", "op":"==", "right":"staging" } ] }, "policy_chain":[ { "name": "example", "version": "1.0", "configuration": {} }, { "name": "on_failed", "version": "builtin", "configuration": { "error_status_code": 419 } } ] } } ``` -------------------------------- ### Install PCRE on OpenVOS Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/NON-AUTOTOOLS-BUILD.txt Installs PCRE and its man pages into /usr/local on Stratus OpenVOS. Run as root in the SysAdmin group. Optionally add (master_disk)>usr>local>bin to command search paths or /usr/local/bin to PATH in BASH. ```VOS command [ !create_dir (master_disk)>usr --if needed ] [ !create_dir (master_disk)>usr>local --if needed ] !gmake install ``` -------------------------------- ### Start APIcast with HTTPS in Docker Source: https://github.com/3scale/apicast/blob/master/examples/add-ssl/README.md Starts APIcast in a Docker container with HTTPS enabled. Requires a certificate and key mounted at /var/run/secrets/apicast. The --dev flag starts APIcast without configuration in development mode. ```sh docker run \ --env APICAST_HTTPS_PORT=8443 --publish 8443:8443 \ --volume $(pwd)/cert:/var/run/secrets/apicast \ --env APICAST_HTTPS_CERTIFICATE=/var/run/secrets/apicast/server.crt \ --env APICAST_HTTPS_CERTIFICATE_KEY=/var/run/secrets/apicast/server.key \ quay.io/3scale/apicast:master apicast \ --dev # this flag makes APIcast start without configuration in development mode ``` -------------------------------- ### Compile pcredemo.c with gcc Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcredemo.html Compile the PCRE demonstration program on Unix-like systems. The first command assumes PCRE is in standard system libraries. The second uses pkg-config if available. The third is a fallback when pkg-config is not installed, requiring you to replace /usr/local/include and /usr/local/lib with the actual PCRE installation paths; the -R option is only used on some operating systems like Solaris. ```bash gcc -Wall pcredemo.c -lpcre -o pcredemo ``` ```bash gcc -Wall pcredemo.c `pkg-config --cflags --libs libpcre` -o pcredemo ``` ```bash gcc -Wall pcredemo.c -I/usr/local/include -L/usr/local/lib \ -R/usr/local/lib -lpcre -o pcredemo ``` -------------------------------- ### C comment example string Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepattern.html Example string used to demonstrate the failure of the greedy C comment pattern. ```plaintext /* first comment */ not comment /* second comment */ ``` -------------------------------- ### GET /status/ready Source: https://github.com/3scale/apicast/blob/master/doc/management-api.md Returns the readiness status of the gateway. ```APIDOC ## GET /status/ready ### Description Returns the readiness status of the gateway. ### Method GET ### Endpoint /status/ready ### Response #### Success Response (200) - **status** (string) - Status: "error", "warning", or "ready". - **success** (boolean) - Whether the gateway is ready. - **error** (string) - Error message if status is "error". - **warning** (string) - Warning message if status is "warning". #### Response Examples ```json {"status":"error","error":"not configured","success":false} ``` ```json {"status":"warning","warning":"no services","success":true} ``` ```json {"status":"ready","success":true} ``` ``` -------------------------------- ### Complete rate_limit policy configuration Source: https://github.com/3scale/apicast/blob/master/gateway/src/apicast/policy/rate_limit/README.md Complete configuration example for the rate_limit policy, showing leaky_bucket_limiters, connection_limiters, fixed_window_limiters, and the redis_url setting. ```json { "name": "rate_limit", "version": "builtin", "configuration": { "leaky_bucket_limiters": [ { "key": { "name": "service_A" }, "rate": 20, "burst": 10 } ], "connection_limiters": [ { "key": { "name": "service_B" }, "conn": 20, "burst": 10, "delay": 0.5 } ], "fixed_window_limiters": [ { "key": { "name": "service_C" }, "count": 20, "window": 10 } ], "redis_url": "redis://localhost:6379" } } ``` -------------------------------- ### Run APIcast with notice log level Source: https://github.com/3scale/apicast/blob/master/doc/policy_tutorial/POLICY_RUN_LOCALLY.md Starts APIcast with the notice log level and development configuration. The output shows the loading of environment configurations and a log entry indicating that the request is not authorized because secrets do not match. ```shell $ bin/apicast --log-level=notice --dev -c examples/configuration/hello_world_config.json loading production environment configuration: /home/centos/gateway/config/production.lua loading development environment configuration: /home/centos/gateway/config/development.lua 2019/04/29 09:32:33 [notice] 257#257: [lua] environment.lua:194: add(): loading environment configuration: /home/centos/gateway/config/production.lua 2019/04/29 09:32:33 [notice] 257#257: [lua] environment.lua:194: add(): loading environment configuration: /home/centos/gateway/config/development.lua 2019/04/29 09:32:33 [notice] 257#257: using the "epoll" event method 2019/04/29 09:32:33 [notice] 257#257: openresty/1.13.6.2 2019/04/29 09:32:33 [notice] 257#257: built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 2019/04/29 09:32:33 [notice] 257#257: OS: Linux 4.4.0-146-generic 2019/04/29 09:32:33 [notice] 257#257: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2019/04/29 09:43:17 [notice] 257#257: *6 [lua] hello_world.lua:53: request is not authorized, secrets do not match, client: 127.0.0.1, server: _, request: "GET / HTTP/1.1", host: "localhost:8080" [29/Apr/2019:09:43:17 +0000] localhost:8080 127.0.0.1:40946 "GET / HTTP/1.1" 403 175 (0.000) 0 ``` -------------------------------- ### PartialMatch examples Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrecpp.html Shows how to use pcrecpp::RE::PartialMatch to search for a substring or extract a number from a string. The second example captures the first number into an int variable and asserts it equals 100. ```cpp Example: simple search for a string: pcrecpp::RE("ell").PartialMatch("hello"); Example: find first number in a string: int number; pcrecpp::RE re("(\\d+)"); re.PartialMatch("x*100 + 20", &number); assert(number == 100); ``` -------------------------------- ### APIcast log entry for setting header Source: https://github.com/3scale/apicast/blob/master/examples/policies/README.md Log entry from APIcast showing the policy setting the 'Example' header to 'Value'. ```text 2018/02/08 10:01:48 [notice] 72249#10132050: *9 [lua] init.lua:19: op(): setting header: Example to: Value, client: 127.0.0.1, server: _, request: "GET /test HTTP/1.1", host: "localhost:8080" ``` -------------------------------- ### pcre_fullinfo() usage example Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcreapi.html Example call to pcre_fullinfo() to retrieve information about a compiled pattern. The third argument specifies the information required, and the fourth argument points to a variable where the result is stored. ```c int rc; size_t length; rc = pcre_fullinfo( re, /* result of pcre_compile() */ sd, /* result of pcre_study(), or NULL */ PCRE_INFO_SIZE, /* what is required */ &length); /* where to put the data */ ``` -------------------------------- ### POST /boot Source: https://github.com/3scale/apicast/blob/master/doc/management-api.md Runs the initialization process to download new configuration. ```APIDOC ## POST /boot ### Description Runs the initialization process and tries to download new configuration. ### Method POST ### Endpoint /boot ``` -------------------------------- ### FullMatch basic examples Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrecpp.html Shows basic FullMatch usage: successful match, unsuccessful match (requires full match), and creating a temporary RE object. ```cpp Example: successful match pcrecpp::RE re("h.*o"); re.FullMatch("hello"); Example: unsuccessful match (requires full match): pcrecpp::RE re("e"); !re.FullMatch("hello"); Example: creating a temporary RE object: pcrecpp::RE("h.*o").FullMatch("hello"); ``` -------------------------------- ### Example: /caseless/i modifier Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcretest.html Example of using the /i modifier to set PCRE_CASELESS when pcre[16|32]_compile() is called. The /i, /m, /s, and /x modifiers have the same effect as in Perl. ```text /caseless/i ``` -------------------------------- ### pcre-config --version command Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/README.txt Prints the version number of the installed PCRE library. ```bash pcre-config --version ``` -------------------------------- ### JWT Claim Check Policy Example Source: https://github.com/3scale/apicast/blob/master/gateway/src/apicast/policy/jwt_claim_check/readme.md Example policy configuration that blocks non-GET requests and validates JWT operations only for GET requests. If the method does not match, the request continues to the backend API. ```json { "name": "apicast.policy.jwt_claim_check", "configuration": { "error_message": "Invalid JWT check", "rules": [ { "operations": [ {"op": "==", "jwt_claim": "role", "jwt_claim_type": "plain", "value": "admin"} ], "combine_op":"and", "methods": ["GET"], "resource": "/resource", "resource_type": "plain" } ] } } ``` -------------------------------- ### libreadline INSTALL note about termcap functions Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrebuild.html Quoted text from the libreadline INSTALL file explaining that readline uses termcap functions but does not link with termcap or curses itself, allowing applications to choose an appropriate library. ```text "Readline uses the termcap functions, but does not link with the termcap or curses library itself, allowing applications which link with readline the to choose an appropriate library." ``` -------------------------------- ### init.lua entry point Source: https://github.com/3scale/apicast/blob/master/doc/policies.md Recommended content for the 'init.lua' file of a custom policy. It simply loads the actual implementation from 'my_stuff.lua' to keep the entry point minimal and easier to find. ```lua return require('my_stuff') ``` -------------------------------- ### Start APIcast with custom policy load path Source: https://github.com/3scale/apicast/blob/master/doc/policies.md Starts APIcast with a custom policy load path. The '--policy-load-path' flag (or 'APICAST_POLICY_LOAD_PATH' environment variable) overrides the default path where policies are searched. ```shell bin/apicast start --policy-load-path examples/policies:spec/fixtures/policies ``` -------------------------------- ### PCRE extended comment example Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepattern.html Example pattern when PCRE_EXTENDED is set and the default newline convention is in force. The # starts a comment that continues until an actual newline character (0x0a) is encountered; the escape sequence \n does not terminate the comment. ```text abc #comment \n still comment ``` -------------------------------- ### pcretest output for (*COMMIT) with and without start optimization Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepattern.html Shows pcretest output for the pattern /(*COMMIT)abc/ against two data lines. The first match succeeds because PCRE's start-of-match optimization skips to 'a'. The second uses \Y to set PCRE_NO_START_OPTIMIZE, disabling the optimization, so the match fails at the starting position. ```text re> /(*COMMIT)abc/ data> xyzabc 0: abc data> xyzabc\Y No match ``` -------------------------------- ### GET /status/ready - Possible responses Source: https://github.com/3scale/apicast/blob/master/doc/management-api.md Possible responses from the readiness endpoint. The gateway returns an error if not configured, a warning if no services are defined, or a ready status when operational. ```json { "status": "error", "error": "not configured", "success": false } ``` ```json { "status": "warning", "warning": "no services", "success": true } ``` ```json { "status": "ready", "success": true } ``` -------------------------------- ### Pattern a+(*SKIP)b Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepattern.html Example pattern showing (*SKIP) behavior. After a failed match attempt, the starting point skips to the position of (*SKIP), not to the next character. ```text a+(*SKIP)b ``` -------------------------------- ### Create SSL certificates with make gateway-certs and make upstream-certs Source: https://github.com/3scale/apicast/blob/master/dev-environments/grpc/README.md Generate the SSL certificates required for the gateway and upstream services. Run these commands in the project root before starting the gateway. ```sh make gateway-certs ``` ```sh make upstream-certs ``` -------------------------------- ### Example policy chain with request_unbuffered and apicast policy Source: https://github.com/3scale/apicast/blob/master/gateway/src/apicast/policy/request_unbuffered/README.md Shows a minimal policy chain configuration that enables the request_unbuffered policy alongside the apicast policy. ```json "policy_chain": [ { "name": "request_unbuffered", "version": "builtin", }, { "name": "apicast.policy.apicast" } ] ``` -------------------------------- ### Follow example.com service logs Source: https://github.com/3scale/apicast/blob/master/dev-environments/http-proxy-plain-http-upstream/README.md Inspects traffic between the proxy and upstream by following logs from the 'example.com' service. ```sh docker compose -p http-proxy-plain-http-upstream logs -f example.com ``` -------------------------------- ### Relative back reference with \g{-1} Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepattern.html Shows a relative back reference: \g{-1} refers to the most recently started capturing subpattern before it, equivalent to \2 in this example. ```regex (abc(def)ghi)\g{-1} ``` -------------------------------- ### Bundle certificates for Nginx with cat Source: https://github.com/3scale/apicast/blob/master/t/fixtures/ocsp/README.md Bundles the leaf, intermediate, and root certificates into a single file (leaf-bundle.pem) for installation at Nginx. ```bash cat leaf.pem intermediate_ca.pem ca.pem > leaf-bundle.pem ``` -------------------------------- ### Compile pcredemo with PCRE in /usr/local Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcresample.html Compiles pcredemo.c when PCRE is installed in /usr/local on a Unix-like system, adding include and library paths. ```bash gcc -o pcredemo -I/usr/local/include pcredemo.c -L/usr/local/lib -lpcre ``` -------------------------------- ### Configure PCRE with custom CFLAGS and prefix Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/README.txt Runs the configure script with custom C compiler flags and an installation prefix. The CFLAGS value overrides the default compiler flags, and --prefix changes the installation directory from the default /usr/local to /opt/local. ```shell CFLAGS='-O2 -Wall' ./configure --prefix=/opt/local ``` -------------------------------- ### Multiple matches from pcre_dfa_exec() Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcreapi.html Example showing that pcre_dfa_exec() can return multiple matches starting at the same point. The pattern <.*> matched against the given string yields three matched strings, returned in reverse order of length. ```regex <.*> ``` ```text This is no more ``` ```text ``` -------------------------------- ### Follow example.com service logs Source: https://github.com/3scale/apicast/blob/master/dev-environments/opentelemetry-instrumented-gateway/README.md Inspects traffic between the proxy and upstream by following logs from the example.com service. ```sh docker compose -p opentelemetry-instrumented-gateway logs -f example.com ``` -------------------------------- ### pcretest example of partial matching with PCRE_PARTIAL_SOFT Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepartial.html Shows pcretest output for a date pattern with \P (PCRE_PARTIAL_SOFT). Demonstrates complete match, partial matches, and no match cases. ```pcretest re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/ data> 25jun04\P 0: 25jun04 1: jun data> 25dec3\P Partial match: 23dec3 data> 3ju\P Partial match: 3ju data> 3juj\P No match data> j\P No match ``` -------------------------------- ### Enable pcregrep compressed file support with --enable-pcregrep-libz and --enable-pcregrep-libbz2 Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrebuild.html Add these options to the configure command to build pcregrep with support for .gz and .bz2 compressed files. The relevant libraries (libz and libbz2) must be installed; configuration fails if they are not. ```text --enable-pcregrep-libz --enable-pcregrep-libbz2 ``` -------------------------------- ### Example configuration for apicast.policy.content_caching Source: https://github.com/3scale/apicast/blob/master/gateway/src/apicast/policy/content_caching/Readme.md JSON configuration for the content caching policy. It defines a rule that enables caching when the request method is GET, and sets a custom header 'X-Cache-Status-POLICY'. The condition uses a liquid template for the method. ```json { "name": "apicast.policy.content_caching", "version": "builtin", "configuration": { "rules": [ { "cache": true, "header": "X-Cache-Status-POLICY", "condition": { "combine_op": "and", "operations": [ { "left": "{{method}}", "left_type": "liquid", "op": "==", "right": "GET" } ] } } ] } } ``` -------------------------------- ### Make a test request with curl Source: https://github.com/3scale/apicast/blob/master/examples/policies/README.md Makes a request to the running APIcast instance to test the example policy. ```shell curl 'localhost:8080/test' ``` -------------------------------- ### pcretest output after failed match Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepattern.html pcretest output after a failed match showing that the last encountered name in the entire match process is returned. In this unanchored example, the mark is retained from the match attempt that started at the letter "X". ```text re> /X(*MARK:A)Y|X(*MARK:B)Z/K data> XP No match, mark = B ``` -------------------------------- ### Follow example.com service logs Source: https://github.com/3scale/apicast/blob/master/dev-environments/keycloak-env/README.md Follows logs from the example.com service to inspect traffic between the proxy and upstream. ```sh docker compose -p keycloak-env logs -f example.com ``` -------------------------------- ### Generate a policy scaffold with bin/apicast generate policy Source: https://github.com/3scale/apicast/blob/master/doc/policy_tutorial/POLICY_SCAFFOLD.md Runs the APIcast scaffolding utility to generate a policy scaffold named hello_world. The command is executed inside the development container. ```shell $ bin/apicast generate policy hello_world ``` -------------------------------- ### Send GET request Source: https://github.com/3scale/apicast/blob/master/dev-environments/http-proxy-plain-http-upstream/README.md Sends a GET request to the gateway with a user key. ```sh curl --resolve get.example.com:8080:127.0.0.1 -v "http://get.example.com:8080/?user_key=123" ``` -------------------------------- ### Example policy chain with request_unbuffered and HTTP Proxy policy Source: https://github.com/3scale/apicast/blob/master/gateway/src/apicast/policy/request_unbuffered/README.md Shows a policy chain that combines request_unbuffered with the http_proxy policy, including proxy configuration for all_proxy, https_proxy, and http_proxy. ```json "policy_chain": [ { "name": "request_unbuffered", "version": "builtin", }, { "name": "apicast.policy.http_proxy", "configuration": { "all_proxy": "http://foo:bar@192.168.15.103:8888/", "https_proxy": "http://192.168.15.103:8888/", "http_proxy": "http://192.168.15.103:8888/" } } ] ``` -------------------------------- ### Specify several sources to get the IP from Source: https://github.com/3scale/apicast/blob/master/gateway/src/apicast/policy/ip_check/README.md Configuration for the ip_check policy to try multiple client IP sources in order: X-Forwarded-For, X-Real-IP, and last_caller. ```json { "name": "ip_check", "configuration": { "ips": [ "3.4.5.6", "1.2.3.0/4" ], "check_type": "blacklist", "client_ip_sources": ["X-Forwarded-For", "X-Real-IP", "last_caller"] } } ``` -------------------------------- ### Compile a PCRE pattern with pcre_compile Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/pcre.txt Shows how to call pcre_compile() with a pattern, options, and error pointers. The example uses pattern "^A.*Z" with default options and default character tables. ```c pcre *re; const char *error; int erroffset; re = pcre_compile( "^A.*Z", /* the pattern */ 0, /* default options */ &error, /* for error message */ &erroffset, /* for error offset */ NULL); /* use default character tables */ ``` -------------------------------- ### Oniguruma subroutine syntax examples Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepattern.html Oniguruma-compatible subroutine syntax using \g or \g'number'. The first example is a recursive pattern for balanced parentheses; the second is the earlier subroutine example rewritten with Oniguruma syntax. ```regex (? \( ( (?>[^()]+) | \g )* \) ) ``` ```regex (sens|respons)e and \g'1'ibility ``` -------------------------------- ### Show available make targets with make help Source: https://github.com/3scale/apicast/blob/master/README.md Displays available make targets for the project. ```shell make help ``` -------------------------------- ### Show kcadm.sh help Source: https://github.com/3scale/apicast/blob/master/dev-environments/keycloak-env/README.md Shows help for the Keycloak admin CLI (kcadm.sh). ```sh /opt/keycloak/bin/kcadm.sh --help ``` -------------------------------- ### Enable JIT with pcre_study() and free with pcre_free_study() Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrejit.html Two-step process to use JIT: call pcre_study() with PCRE_STUDY_JIT_COMPILE and pass the pcre_extra block to pcre_exec(); free it with pcre_free_study() instead of freeing manually. ```c (1) Call pcre_study() with the PCRE_STUDY_JIT_COMPILE option for each compiled pattern, and pass the resulting pcre_extra block to pcre_exec(). (2) Use pcre_free_study() to free the pcre_extra block when it is no longer needed, instead of just freeing it yourself. This ensures that any JIT data is also freed. ``` -------------------------------- ### Subroutine call example (sens|respons)e and (?1)ibility Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepattern.html Uses a subroutine call (?1) instead of a back reference, so it matches "sense and responsibility" as well as the other two strings. ```regex (sens|respons)e and (?1)ibility ``` -------------------------------- ### Run unit tests with make and busted Source: https://github.com/3scale/apicast/blob/master/doc/unittests.md Commands to set up the Docker environment, install dependencies, run all unit tests, run a single test file, and filter tests by a describe token. The Lua snippet shows adding 'ONLY' to a describe function to target it with --filter. ```shell make development ``` ```shell make dependencies ``` ```shell make busted ``` ```shell make busted BUSTED_FILES=path/to/file_spec.lua ``` ```lua describe("ONLY should be awesome", function() ``` ```shell make busted BUSTED_FILES=path/to/file_spec.lua BUSTED_ARGS="--filter='ONLY'" ``` -------------------------------- ### Compile and run pcredemo with PCRE library Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/pcre.txt Commands to compile and run the pcredemo demonstration program, including a typical runtime error and the linker flag to fix it. The first two commands compile pcredemo.c with the PCRE library; the second adds include and library paths for a non-standard install. The next two run the program with and without the -g option. The error message appears when the shared library is not found, and -R/usr/local/lib is added to the compile command to resolve it. ```bash gcc -o pcredemo pcredemo.c -lpcre ``` ```bash gcc -o pcredemo -I/usr/local/include pcredemo.c \ -L/usr/local/lib -lpcre ``` ```bash ./pcredemo 'cat|dog' 'the cat sat on the mat' ``` ```bash ./pcredemo -g 'cat|dog' 'the dog sat on the cat' ``` ```text ld.so.1: a.out: fatal: libpcre.so.0: open failed: No such file or directory ``` ```bash -R/usr/local/lib ``` -------------------------------- ### pcretest example of multi-segment matching with PCRE_DFA_RESTART Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepartial.html Shows pcretest output for continuing a partial match using \R (PCRE_DFA_RESTART) and \D (DFA matching). Demonstrates that only the last part is shown when match completes. ```pcretest re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/ data> 23ja\P\D Partial match: 23ja data> n05\R\D 0: n05 ``` -------------------------------- ### Get JWT and export ACCESS_TOKEN Source: https://github.com/3scale/apicast/blob/master/dev-environments/keycloak-env/README.md Gets a JWT for the my-client client as user bob with password p and stores it in the ACCESS_TOKEN environment variable. ```sh export ACCESS_TOKEN=$(make token) ``` -------------------------------- ### Enable JIT support with --enable-jit Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrebuild.html Add this option to the configure command to include just-in-time compiler support. This support is available only for certain hardware architectures; setting it for an unsupported architecture causes a compile time error. When JIT support is enabled, pcregrep automatically uses it unless --disable-pcregrep-jit is added. ```bash --enable-jit ``` -------------------------------- ### Complex conditional subpattern example Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepattern.html An example of a conditional subpattern with complex alternatives. It uses a reference to subpattern 1 as the condition and includes nested conditional subpatterns. ```regex (?(1) (A|B|C) | (D | (?(2)E|F) | E) ) ``` -------------------------------- ### Prepare apicast-config.json with make template Source: https://github.com/3scale/apicast/blob/master/dev-environments/mtls/README.md Prepares the apicast-config.json file for the gateway. ```sh make template ``` -------------------------------- ### Example: /^abc/m Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcretest.html Example that sets multiline matching with CRLF as the line ending sequence. The /m modifier sets PCRE_MULTILINE and the literal sets PCRE_NEWLINE_CRLF. ```text /^abc/m ``` -------------------------------- ### Build grpcurl with make grpcurl Source: https://github.com/3scale/apicast/blob/master/dev-environments/grpc/README.md Build the grpcurl tool. Requires Go SDK version 1.18 or later. ```sh make grpcurl ``` -------------------------------- ### Controlling the JIT stack Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrejit.html Describes how to set up a JIT callback function and control the JIT stack, which is necessary for handling stack limits and for knowing whether JIT was used. ```APIDOC ## Controlling the JIT stack ### Description Describes how to set up a JIT callback function and control the JIT stack, which is necessary for handling stack limits and for knowing whether JIT was used. ### Method Configuration ### Details - A JIT callback function can be set up to be called whenever JIT code is about to be obeyed. - The callback is not called if execution options are not right for JIT execution. - The JIT stack can be controlled to avoid PCRE_ERROR_JIT_STACKLIMIT errors. ### Usage Refer to the section "Controlling the JIT stack" in the original documentation for detailed instructions on setting up the callback and stack. ``` -------------------------------- ### Branch reset numbering example Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepattern.html Example from Perl documentation showing how capturing parentheses are numbered inside a branch reset group. The numbers underneath indicate which buffer the captured content is stored in. ```regex # before ---------------branch-reset----------- after / ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x # 1 2 2 3 2 3 4 ``` -------------------------------- ### Output of generating the hello_world policy scaffold Source: https://github.com/3scale/apicast/blob/master/doc/policy_tutorial/POLICY_SCAFFOLD.md Shows the output of the bin/apicast generate policy hello_world command, listing the files created in the spec, t, and gateway/src/apicast/policy directories. ```shell bash-4.4$ bin/apicast generate policy hello_world source: /home/centos/examples/scaffold/policy destination: /home/centos exists: spec exists: spec/policy created: spec/policy/hello_world created: spec/policy/hello_world/hello_world_spec.lua exists: t created: t/apicast-policy-hello_world.t exists: gateway exists: gateway/src exists: gateway/src/apicast exists: gateway/src/apicast/policy created: gateway/src/apicast/policy/hello_world created: gateway/src/apicast/policy/hello_world/apicast-policy.json created: gateway/src/apicast/policy/hello_world/init.lua created: gateway/src/apicast/policy/hello_world/hello_world.lua ``` -------------------------------- ### Atomic grouping example: (?>\d+)foo Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepattern.html Atomic grouping example that prevents backtracking into the digit sequence. When applied to the subject '123456bar', the matcher gives up immediately on failing to match 'foo' the first time. ```regex (?>\d+)foo ``` -------------------------------- ### POSIX character class example: [01[:alpha:]%] Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepattern.html A POSIX character class example that matches '0', '1', any alphabetic character, or '%'. The class name 'alpha' is enclosed by '[:' and ':]' within the outer square brackets. ```regex [01[:alpha:]%] ``` -------------------------------- ### Enable 32-bit library with --enable-pcre32 Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrebuild.html Add this option to the configure command to build the separate libpcre32 library, which handles 32-bit data units and UTF-32 strings. ```bash --enable-pcre32 ``` -------------------------------- ### a+(?C)(*FAIL) pattern with callout Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrepattern.html Example pattern using (*FAIL) with a callout. A match with the string "aaaa" always fails, but the callout is taken before each backtrack happens (in this example, 10 times). ```regex a+(?C)(*FAIL) ``` -------------------------------- ### Get Docker container IP and curl from local machine Source: https://github.com/3scale/apicast/blob/master/doc/policy_tutorial/POLICY_RUN_LOCALLY.md Alternative way to test by getting the Docker container IP address and sending a curl request from the local machine. The response will be a 403 Forbidden error. ```shell APICAST_IP=$(docker inspect apicast_build_0-development-1 | yq e -P '.[0].NetworkSettings.Networks.apicast_build_0_default.IPAddress' -) ``` ```shell curl -i -H "Host: one" "http://${APICAST_IP}:8080/?user_key=0123456789" ``` -------------------------------- ### hello_world_config.json - APIcast configuration with hello_world policy Source: https://github.com/3scale/apicast/blob/master/doc/policy_tutorial/POLICY_RUN_LOCALLY.md JSON configuration file for APIcast that defines a service with the hello_world policy and an upstream policy acting as an echo mock. The hello_world policy is configured with overwrite and secret properties. The upstream policy routes requests to http://echo:8081. ```json { "services": [ { "proxy": { "hosts": ["one"], "api_backend": "https://echo-api.3scale.net:443", "backend": { "endpoint": "http://127.0.0.1:8081", "host": "backend" }, "policy_chain": [ { "name": "hello_world", "version": "builtin", "configuration": { "overwrite": true, "secret": "mysecret" } }, { "name": "apicast.policy.upstream", "configuration": { "rules": [ { "regex": "/", "url": "http://echo:8081" } ] } } ] } } ] } ``` -------------------------------- ### GET /dns/cache Source: https://github.com/3scale/apicast/blob/master/doc/management-api.md Returns the DNS cache. ```APIDOC ## GET /dns/cache ### Description Returns the DNS cache. ### Method GET ### Endpoint /dns/cache ### Response #### Success Response (200) - **DNS cache** (object) - A map of DNS names to their cache entries. #### Response Example ```json {"127.0.0.1.xip.io":{"expires_in":139.0790002346,"value":{"1":{"address":"127.0.0.1","section":1,"type":1,"class":1,"name":"127.0.0.1.xip.io","ttl":250},"name":"127.0.0.1.xip.io","ttl":250}}} ``` ``` -------------------------------- ### PCRE character class examples: [aeiou], [^aeiou], [d-m], [b-d-z] Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/pcre.txt Examples of character classes: [aeiou] matches lowercase vowels, [^aeiou] matches any non-vowel, [d-m] matches letters d through m, and [b-d-z] matches b-d, a hyphen, or z. ```regex [aeiou] ``` ```regex [^aeiou] ``` ```regex [d-m] ``` ```regex [b-d-z] ``` -------------------------------- ### Enable 16-bit library with --enable-pcre16 Source: https://github.com/3scale/apicast/blob/master/dependencies/rpm-specs/openresty-pcre/sources/pcre-8.44/doc/html/pcrebuild.html Add this option to the configure command to build the separate libpcre16 library, which handles 16-bit data units and UTF-16 strings. ```bash --enable-pcre16 ```