shreemaan-abhishek commented on code in PR #11541:
URL: https://github.com/apache/apisix/pull/11541#discussion_r1740442835


##########
apisix/plugins/content-moderation.lua:
##########
@@ -0,0 +1,155 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local aws = require("resty.aws")
+local aws_instance = aws()
+local http = require("resty.http")
+local next = next
+local pairs = pairs
+local unpack = unpack
+
+local aws_comprehend_schema = {
+    type = "object",
+    properties = {
+        access_key_id = { type = "string" },
+        secret_access_key = { type = "string" },

Review Comment:
   it cannot be done due to the limitation of `encrypt_fields` 
https://github.com/apache/apisix/issues/11099, however, I have added support 
for apisix secrets.



##########
apisix/plugins/content-moderation.lua:
##########
@@ -0,0 +1,155 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local aws = require("resty.aws")
+local aws_instance = aws()
+local http = require("resty.http")
+local next = next
+local pairs = pairs
+local unpack = unpack
+
+local aws_comprehend_schema = {
+    type = "object",
+    properties = {
+        access_key_id = { type = "string" },
+        secret_access_key = { type = "string" },
+        region = { type = "string" },
+        endpoint = {
+            type = "string",
+            pattern = [[^https?://]]
+        },
+    },
+    required = { "access_key_id", "secret_access_key", "region", }
+}
+
+local schema = {
+    type = "object",
+    properties = {
+        provider = {
+            type = "object",
+            properties = {
+                aws_comprehend = aws_comprehend_schema
+            },
+            -- change to oneOf/enum while implementing support for other 
services
+            required = { "aws_comprehend" }
+        },
+        moderation_categories = {
+            type = "object",
+            patternProperties = {
+                -- luacheck: push max code line length 300
+                
["^(PROFANITY|HATE_SPEECH|INSULT|HARASSMENT_OR_ABUSE|SEXUAL|VIOLENCE_OR_THREAT)$"]
 = {
+                -- luacheck: pop
+                type = "number",
+                    minimum = 0,
+                    maximum = 1
+                }
+            },
+            additionalProperties = false
+        },
+        toxicity_level = {
+            type = "number",
+            minimum = 0,
+            maximum = 1,
+            default = 0.5
+        },
+        reject_requests = {
+            type = "boolean",
+            default = true,
+        }
+    },
+    required = { "provider" },
+}
+
+
+local _M = {
+    version  = 0.1,
+    priority = 1040, -- TODO: might change
+    name     = "content-moderation",
+    schema   = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+function _M.rewrite(conf, ctx)
+    local body = core.request.get_body()
+    if not body then
+        return
+    end
+
+    local provider = conf.provider[next(conf.provider)]
+
+    -- TODO support secret
+    local credentials = aws_instance:Credentials({
+        accessKeyId = provider.access_key_id,
+        secretAccessKey = provider.secret_access_key,
+        sessionToken = provider.session_token,
+    })
+
+    local default_endpoint = "https://comprehend."; .. provider.region .. 
".amazonaws.com"
+    local scheme, host, port = unpack(http:parse_uri(provider.endpoint or 
default_endpoint))
+    local endpoint = scheme .. "://" .. host
+    aws_instance.config.endpoint = endpoint
+    aws_instance.config.ssl_verify = false
+
+    local comprehend = aws_instance:Comprehend({
+        credentials = credentials,
+        endpoint = endpoint,
+        region = provider.region,
+        port = port,
+    })
+
+    local res, err = comprehend:detectToxicContent({
+        LanguageCode = "en",
+        TextSegments = {
+            {
+                Text = body

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to