Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: kong plugin #1073

Merged
merged 12 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/MAINTAINERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ This will output a new key, associated with your email address to /tmp/new-ssh-k
5. Update `./bin/split.sh` and `./.github/workflows/split-monorepo.yml` to include the new mirror and SSH key.
6. Update the main README.md to include information about the new package.

### Kong Plugin

This does not require a release. Users will simply copy the `kong` directory into their Kong image [see this guide](https://docs.konghq.com/gateway/latest/plugin-development/get-started/deploy/).
anandkumarpatel marked this conversation as resolved.
Show resolved Hide resolved

In the future:

- We will build a Docker image for the plugin and release it to the Kong Community plugins site.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anandkumarpatel do you have any tickets in place for these tasks? If not, can we start tracking them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not yet! where should I add these tickets to? (note, this is just for hackathon, so we wont work on the tickets unless there is traction so not sure if its worth adding them?)

- We will offer a zip file with the plugin code.

## 🧑‍🔬 Integration Testing

We have an integration testing layer for both Metrics and webhooks to ensure that the different SDKs, and how they may be used within a variety of HTTP frameworks, are compliant to the Metrics API and all behave the same given certain parameters.
Expand Down
7 changes: 7 additions & 0 deletions packages/kong-plugin/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
return {
default = {
verbose = true,
coverage = false,
output = "gtest",
},
}
22 changes: 22 additions & 0 deletions packages/kong-plugin/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

[*.lua]
indent_style = space
indent_size = 2

[kong/templates/nginx*]
indent_style = space
indent_size = 4

[*.template]
indent_style = space
indent_size = 4

[Makefile]
indent_style = tab
12 changes: 12 additions & 0 deletions packages/kong-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# servroot typically is the Kong working directory for tests
servroot
# exclude generated packed rocks
*.rock
# exclude Pongo shell history
.pongo/.bash_history
# exclude LuaCov statistics file
luacov.stats.out
# exclude LuaCov report
luacov.report.out
# exclude Pongo containerid file
.containerid
35 changes: 35 additions & 0 deletions packages/kong-plugin/.luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
std = "ngx_lua"
unused_args = false
redefined = false
max_line_length = false


include_files = {
"**/*.lua",
"*.rockspec",
".busted",
".luacheckrc",
}


globals = {
"_KONG",
"kong",
"ngx.IS_CLI",
}


not_globals = {
"string.len",
"table.getn",
}


ignore = {
"6.", -- ignore whitespace warnings
}


files["spec/**/*.lua"] = {
std = "ngx_lua+busted",
}
7 changes: 7 additions & 0 deletions packages/kong-plugin/.luacov
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include = {
"%/kong%-plugin%/kong%/.+$",
}

statsfile = "/kong-plugin/luacov.stats.out"
reportfile = "/kong-plugin/luacov.report.out"
runreport = true
1 change: 1 addition & 0 deletions packages/kong-plugin/.pongo/pongorc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--postgres
17 changes: 17 additions & 0 deletions packages/kong-plugin/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM kong/kong-gateway:3.7
# Ensure any patching steps are executed as root user
USER root

# Add custom plugin to the image
COPY ./kong/plugins/readme-plugin /usr/local/share/lua/5.1/kong/plugins/readme-plugin
ENV KONG_PLUGINS=bundled,readme-plugin

# Ensure kong user is selected for image execution
USER kong

# Run kong
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 8000 8443 8001 8444
STOPSIGNAL SIGQUIT
HEALTHCHECK --interval=10s --timeout=10s --retries=10 CMD kong health
CMD ["kong", "docker-start"]
90 changes: 90 additions & 0 deletions packages/kong-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# ReadMe Metrics + Kong

<p align="center">
<img src="https://user-images.githubusercontent.com/33762/182927634-2aebeb46-c215-4ac3-9e98-61f931e33583.png" />
</p>

<p align="center">
Track usage of your API and troubleshoot issues faster.
</p>

With [ReadMe's Metrics API](https://readme.com/metrics) your team can get deep insights into your API's usage. If you're a developer, it takes a few small steps to send your API logs to [ReadMe](http://readme.com). Here's an overview of how the integration works:

- Install this plugin into your Kong Gateway by copying the `kong` directory into your Kong environment or use the provided Dockerfile. see [this](https://docs.konghq.com/gateway/latest/plugin-development/get-started/deploy/) for details.
- The plugin runs during the log phase of the Kong pipeline and sends ReadMe the details of your API's incoming requests and outgoing responses, with the option for you to redact any private headers using the configuration options.
- ReadMe uses these request and response details to create an API Metrics Dashboard which can be used to analyze specific API calls or monitor aggregate usage data.

### 📦 Deploying locally
anandkumarpatel marked this conversation as resolved.
Show resolved Hide resolved

```bash
# Build kong image with the plugin
docker build -t kong-readme-plugin:latest .
# Run kong with the plugin
curl -Ls https://get.konghq.com/quickstart | bash -s -- -r "" -i kong-readme-plugin -t latest
# Enable the plugin
curl -isX POST http://localhost:8001/plugins -d name=readme-plugin -d 'config.api_key=<Your API Key>'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why port 8001? What if this is exposed on another port?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if they ran the above script, its 8001. I am assuming if they don't follow the script they would know what they are doing.

```

### 🧑‍🔬 Testing

Requires [pongo](https://github.com/Kong/kong-pongo) to test and develop.

```bash
pongo up
pongo shell
kms

# Check if the plugin is available
curl -s localhost:8001 | jq '.plugins.available_on_server."readme-plugin"'
```

#### Enable for all services
anandkumarpatel marked this conversation as resolved.
Show resolved Hide resolved

```bash
curl -sX POST http://localhost:8001/plugins -d name=readme-plugin -d 'config.api_key=<Your API Key>' | jq
```

#### Enable for a specific service

```bash
# Add a new service
curl -isX POST http://localhost:8001/services -d name=example_service -d url='http://httpbin.org'

# Associate the custom plugin with the `example_service` service
curl -isX POST http://localhost:8001/services/example_service/plugins -d 'name=readme-plugin' -d "config.queue.max_retry_time=1"

# Add a new route for sending requests through the `example_service` service
curl -iX POST http://localhost:8001/services/example_service/routes -d 'paths[]=/mock' -d name=example_route

# Test
curl -i http://localhost:8000/mock/anything
```

### 🧙 Development tricks

```bash
# Get plugin config
curl -s http://localhost:8001/plugins | jq '.data | map(select(.name == "readme-plugin")) | first'

# Retrieve the plugin ID
export PLUGIN_ID=$(curl -s http://localhost:8001/plugins | jq '.data | map(select(.name == "readme-plugin")) | first | .id' | tr -d '"')

# Configure the plugin with your API key
curl -sX PATCH http://localhost:8001/plugins/$PLUGIN_ID -d "config.api_key=<Your API Key>" | jq '.config.api_key'

# Configure `hide_headers`
curl -sX PATCH -H'Content-Type: application/json' http://localhost:8001/plugins/$PLUGIN_ID -d '{"config": {"hide_headers": {"foo": "", "bar": "default"}}}' | jq '.config.hide_headers'

# Configure `id_header`
curl -sX PATCH -H'Content-Type: application/json' http://localhost:8001/plugins/$PLUGIN_ID -d '{"config": {"id_header": "email"}}' | jq '.config.id_header'

# Configure `group_by`
curl -sX PATCH -H'Content-Type: application/json' http://localhost:8001/plugins/$PLUGIN_ID -d '{"config": {"group_by": {"x-user-email": "email", "x-org-name": "label"}}}' | jq '.config.group_by'

# Delete the plugin
curl -sX DELETE http://localhost:8001/plugins/$PLUGIN_ID
```

> 🚧 Any Issues?
>
> Integrations can be tricky! [Contact support](https://docs.readme.com/guides/docs/contact-support) if you have any questions/issues.
Loading
Loading