-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugins): add new confluent plugin (#9947)
* feat(plugins): add new confluent plugin --------- Co-authored-by: Brent Yarger <[email protected]> Co-authored-by: samugi <[email protected]>
- Loading branch information
1 parent
c55d4fa
commit 45ee373
Showing
19 changed files
with
520 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
message: "**confluent:** Added the `confluent` plugin which allows to interface with Confluent." | ||
type: feature | ||
scope: Plugin |
3 changes: 0 additions & 3 deletions
3
changelog/unreleased/kong-ee/bump-kong-lua-resty-kafka-0.19.yml
This file was deleted.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
changelog/unreleased/kong-ee/bump-kong-lua-resty-kafka-0.20.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
message: | | ||
Bumped `kong-lua-resty-kafka` to `0.20` to support TCP socket keepalive and allow client_id to be set for the kafka client. | ||
type: dependency |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
-- This software is copyright Kong Inc. and its licensors. | ||
-- Use of the software is subject to the agreement between your organization | ||
-- and Kong Inc. If there is no such agreement, use is governed by and | ||
-- subject to the terms of the Kong Master Software License Agreement found | ||
-- at https://konghq.com/enterprisesoftwarelicense/. | ||
-- [ END OF LICENSE 0867164ffc95e54f04670b5169c09574bdbd9bba ] | ||
|
||
local kong = kong | ||
local producers = require "kong.enterprise_edition.kafka.plugins.producers" | ||
local meta = require "kong.meta" | ||
local cjson_encode = require("cjson").encode | ||
|
||
local ngx_encode_base64 = ngx.encode_base64 | ||
|
||
local CONFLUENT_CLIENT_ID = "cwc|001f100001XcA82AAF" | ||
|
||
local ConfluentHandler = {} | ||
|
||
ConfluentHandler.PRIORITY = 752 | ||
ConfluentHandler.VERSION = meta.core_version | ||
|
||
|
||
local raw_content_types = { | ||
["text/plain"] = true, | ||
["text/html"] = true, | ||
["application/xml"] = true, | ||
["text/xml"] = true, | ||
["application/soap+xml"] = true, | ||
} | ||
|
||
|
||
local function build_kafka_message_from_request(conf) | ||
local method | ||
if conf.forward_method then | ||
method = kong.request.get_method() | ||
end | ||
|
||
local headers | ||
if conf.forward_headers then | ||
headers = kong.request.get_headers() | ||
end | ||
|
||
local uri, uri_args | ||
if conf.forward_uri then | ||
uri = kong.request.get_path_with_query() | ||
uri_args = kong.request.get_query() | ||
end | ||
|
||
local body, body_args, body_base64 | ||
if conf.forward_body then | ||
body = kong.request.get_raw_body() | ||
local err | ||
body_args, err = kong.request.get_body() | ||
if err and err:match("content type") then | ||
body_args = {} | ||
local content_type = kong.request.get_header("content-type") | ||
if not raw_content_types[content_type] then | ||
-- don't know what this body MIME type is, base64 it just in case | ||
body = ngx_encode_base64(body) | ||
body_base64 = true | ||
end | ||
end | ||
end | ||
|
||
return cjson_encode({ | ||
method = method, | ||
headers = headers, | ||
uri = uri, | ||
uri_args = uri_args, | ||
body = body, | ||
body_args = body_args, | ||
body_base64 = body_base64, | ||
}) | ||
end | ||
|
||
function ConfluentHandler:access(conf) | ||
local message, err = build_kafka_message_from_request(conf) | ||
if not message then | ||
return producers.handle_error({ | ||
status_code = 500, | ||
internal_error = "could not build a Kafka message from request " .. err, | ||
external_error = "could not build Kafka message" | ||
}) | ||
end | ||
|
||
-- Translate config to producer config as this plugin needs a simplified schema. | ||
local config = { | ||
topic = conf.topic, | ||
bootstrap_servers = conf.bootstrap_servers, | ||
timeout = conf.timeout, | ||
keepalive = conf.keepalive, | ||
keepalive_enabled = conf.keepalive_enabled, | ||
authentication = { | ||
mechanism = "PLAIN", | ||
strategy = "sasl", | ||
user = conf.cluster_api_key, | ||
password = conf.cluster_api_secret, | ||
-- confluent_cloud_api_key = conf.confluent_cloud_api_key, | ||
-- confluent_cloud_api_secret = conf.confluent_cloud_api_secret, | ||
}, | ||
security = { | ||
ssl = true, | ||
}, | ||
client_id = CONFLUENT_CLIENT_ID, | ||
} | ||
local ok, s_err = producers.send_message(config, message) | ||
if not ok then | ||
return producers.handle_error(s_err) | ||
end | ||
|
||
return kong.response.exit(200, { message = "message sent" }) | ||
end | ||
|
||
return ConfluentHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
-- This software is copyright Kong Inc. and its licensors. | ||
-- Use of the software is subject to the agreement between your organization | ||
-- and Kong Inc. If there is no such agreement, use is governed by and | ||
-- subject to the terms of the Kong Master Software License Agreement found | ||
-- at https://konghq.com/enterprisesoftwarelicense/. | ||
-- [ END OF LICENSE 0867164ffc95e54f04670b5169c09574bdbd9bba ] | ||
|
||
local typedefs = require "kong.db.schema.typedefs" | ||
|
||
return { | ||
name = "confluent", | ||
fields = { | ||
{ protocols = typedefs.protocols_http }, | ||
{ consumer_group = typedefs.no_consumer_group }, | ||
{ config = { | ||
type = "record", | ||
fields = { | ||
{ bootstrap_servers = { description = "Set of bootstrap brokers in a `{host: host, port: port}` list format.", type = "set", | ||
elements = { | ||
type = "record", | ||
fields = { | ||
{ host = typedefs.host({ required = true }), }, | ||
{ port = typedefs.port({ required = true }), }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ topic = { description = "The Kafka topic to publish to.", type = "string", required = true }, }, | ||
{ timeout = { description = "Socket timeout in milliseconds.", type = "integer", default = 10000 }, }, | ||
{ keepalive = { description = "Keepalive timeout in milliseconds.", type = "integer", default = 60000 }, }, | ||
{ keepalive_enabled = { type = "boolean", default = false }, }, | ||
{ cluster_api_key = { description = "Username/Apikey for SASL authentication.", | ||
type = "string", | ||
required = true, | ||
encrypted = true, | ||
referenceable = true } }, | ||
{ cluster_api_secret = { description = "Password/ApiSecret for SASL authentication.", | ||
type = "string", | ||
required = true, | ||
encrypted = true, | ||
referenceable = true } }, | ||
{ confluent_cloud_api_key = { description = "Apikey for authentication with Confluent Cloud. This allows for management tasks such as creating topics, ACLs, etc.", | ||
type = "string", | ||
required = false, | ||
encrypted = true, | ||
referenceable = true, } }, | ||
|
||
{ confluent_cloud_api_secret = { description = "The corresponding secret for the Confluent Cloud API key.", | ||
type = "string", | ||
required = false, | ||
encrypted = true, | ||
referenceable = true, } }, | ||
|
||
{ forward_method = { description = "Include the request method in the message. At least one of these must be true: `forward_method`, `forward_uri`, `forward_headers`, `forward_body`.", type = "boolean", default = false } }, | ||
{ forward_uri = { description = "Include the request URI and URI arguments (as in, query arguments) in the message. At least one of these must be true: `forward_method`, `forward_uri`, `forward_headers`, `forward_body`.", type = "boolean", default = false } }, | ||
{ forward_headers = { description = "Include the request headers in the message. At least one of these must be true: `forward_method`, `forward_uri`, `forward_headers`, `forward_body`.", type = "boolean",default = false } }, | ||
{ forward_body = { description = "Include the request body in the message. At least one of these must be true: `forward_method`, `forward_uri`, `forward_headers`, `forward_body`.", type = "boolean", default = true } }, | ||
|
||
-- TODO change cluster_name to required in 3.0 | ||
{ cluster_name = { description = "An identifier for the Kafka cluster. By default, this field generates a random string. You can also set your own custom cluster identifier. If more than one Kafka plugin is configured without a `cluster_name` (that is, if the default autogenerated value is removed), these plugins will use the same producer, and by extension, the same cluster. Logs will be sent to the leader of the cluster.", type = "string", required = false, auto = true } }, | ||
|
||
{ producer_request_acks = { description = "The number of acknowledgments the producer requires the leader to have received before considering a request complete. Allowed values: 0 for no acknowledgments; 1 for only the leader; and -1 for the full ISR (In-Sync Replica set).", type = "integer", default = 1, one_of = { -1, 0, 1 }, }, }, | ||
{ producer_request_timeout = { description = "Time to wait for a Produce response in milliseconds.", type = "integer", default = 2000 }, }, | ||
{ producer_request_limits_messages_per_request = { description = "Maximum number of messages to include into a single producer request.", type = "integer", default = 200 }, }, | ||
{ producer_request_limits_bytes_per_request = { description = "Maximum size of a Produce request in bytes.", type = "integer", default = 1048576 }, }, | ||
{ producer_request_retries_max_attempts = { description = "Maximum number of retry attempts per single Produce request.", type = "integer", default = 10 }, }, | ||
{ producer_request_retries_backoff_timeout = { description = "Backoff interval between retry attempts in milliseconds.", type = "integer", default = 100 }, }, | ||
{ producer_async = { description = "Flag to enable asynchronous mode.", type = "boolean", default = true }, }, | ||
{ producer_async_flush_timeout = { description = "Maximum time interval in milliseconds between buffer flushes in asynchronous mode.", type = "integer", default = 1000 }, }, | ||
{ producer_async_buffering_limits_messages_in_memory = { description = "Maximum number of messages that can be buffered in memory in asynchronous mode.", type = "integer", default = 50000 }, }, | ||
}, | ||
|
||
entity_checks = { | ||
{ custom_entity_check = { | ||
field_sources = { "forward_method", "forward_uri", "forward_headers", "forward_body" }, | ||
fn = function(entity) | ||
if entity.forward_method or entity.forward_uri | ||
or entity.forward_headers or entity.forward_body then | ||
return true | ||
end | ||
return nil, "at least one of these attributes must be true: forward_method, forward_uri, forward_headers, forward_body" | ||
end | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.