You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "slow-groovin (via GitHub)" <gi...@apache.org> on 2025/05/22 07:04:51 UTC

[PR] feat: add `authorization` attribute for `loki-logger` (#11420) [apisix]

slow-groovin opened a new pull request, #12243:
URL: https://github.com/apache/apisix/pull/12243

   ### Description
   add an attribute/property  `authorization`  for `loki-logger`  to enable providing http authorization header for non-local loki service.
   <!-- Please include a summary of the change and which issue is fixed. -->
   <!-- Please also include relevant motivation and context. -->
   
   #### Which issue(s) this PR fixes:
   <!--
   *Automatically closes linked issue when PR is merged.
   Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
   -->
   Fixes #11420
   
   ### Checklist
   
   - [x] I have explained the need for this PR and the problem it solves
   - [x] I have explained the changes or the new features added to this PR
   - [x] I have added tests corresponding to this change
   - [x] I have updated the documentation to reflect this change
   - [x] I have verified that this change is backward compatible (If not, please discuss on the [APISIX mailing list](https://github.com/apache/apisix/tree/master#community) first)
   
   <!--
   
   Note
   
   1. Mark the PR as draft until it's ready to be reviewed.
   2. Always add/update tests for any changes unless you have a good reason.
   3. Always update the documentation to reflect the changes made in the PR.
   4. Make a new commit to resolve conversations instead of `push -f`.
   5. To resolve merge conflicts, merge master instead of rebasing.
   6. Use "request review" to notify the reviewer after making changes.
   7. Only a reviewer can mark a conversation as resolved.
   
   -->
   


-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add `authorization` attribute for `loki-logger` (#11420) [apisix]

Posted by "bzp2010 (via GitHub)" <gi...@apache.org>.
bzp2010 commented on code in PR #12243:
URL: https://github.com/apache/apisix/pull/12243#discussion_r2117628008


##########
t/plugin/loki-logger.t:
##########
@@ -80,6 +82,7 @@ done
                         "loki-logger": {
                             "endpoint_addrs": ["http://127.0.0.1:3100"],
                             "tenant_id": "tenant_1",
+                            "authorization": "Bearer 1234",

Review Comment:
   Then, please do this:
   
   1. Create route with the following upstream and URI
   ```text
   === TEST XX: setup route (test auth headers)
   --- config
       location /t {
           content_by_lua_block {
               local t = require("lib.test_admin").test
               local code, body = t('/apisix/admin/routes/1',
                   ngx.HTTP_PUT,
                   [[{
                       "plugins": {
                           "loki-logger": {
                               "endpoint_addrs": ["http://127.0.0.1:1980"],
                               "endpoint_uri": "/log_request",
                               "request_headers": {"Authorization": "test1234"},
                               "batch_max_size": 1
                           }
                       },
                       "upstream": {
                           "nodes": {
                               "127.0.0.1:1980": 1
                           },
                           "type": "roundrobin"
                       },
                       "uri": "/hello"
                   }]]
               )
   
               if code >= 300 then
                   ngx.status = code
               end
               ngx.say(body)
           }
       }
   --- response_body
   passed
   ```
   
   2. Send a request to `/hello` endpoint to trigger the log, then check the error.log. 
   ```text
   === TEST XX+1: hit route
   --- request
   GET /hello
   --- response_body
   hello world
   --- error_log
   go(): authorization: test1234
   ```
   
   The /log_request endpoint used by loki will log the following error.log.
   
   ![image](https://github.com/user-attachments/assets/87482b23-f57b-4ea1-9113-124411492cc7)
   
   It contains every request header, which obviously includes the one we configured for loki for authentication.



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add `authorization` attribute for `loki-logger` (#11420) [apisix]

Posted by "bzp2010 (via GitHub)" <gi...@apache.org>.
bzp2010 commented on code in PR #12243:
URL: https://github.com/apache/apisix/pull/12243#discussion_r2108722539


##########
apisix/plugins/loki-logger.lua:
##########
@@ -47,6 +47,9 @@ local schema = {
             default = "/loki/api/v1/push"
         },
         tenant_id = {type = "string", default = "fake"},
+
+        -- authorization header
+        authorization = { type = "string", minLength = 1, description = "Authorization header" },

Review Comment:
   As mentioned in https://grafana.com/docs/loki/latest/operations/authentication/#manage-authentication, Loki does not have any built-in conventions for authentication mechanisms, so the request header used for authentication can be quite flexible; it does not necessarily be `Authorization: XXX`.
   This is different from `clickhouse-logger` and `elasticsearch-logger`, which have built-in authentication.
   
   This is exactly why I didn't add any authentication to the current plugin.
   
   Therefore, it is better to design a generic mechanism for it, i.e., provide a user-configurable object to store one or more request headers.
   
   ```suggestion
           headers = {
               type = "object",
               patternProperties = {
                   [".*"] = {
                       type = "string",
                       minLength = 1,
                   },
               },
           },
   ```
   
   I don't have any idea about the name of the field, it could use `headers`, `request_headers` or `auth_headers`, ask other reviewers to take a look.



##########
t/plugin/loki-logger.t:
##########
@@ -80,6 +82,7 @@ done
                         "loki-logger": {
                             "endpoint_addrs": ["http://127.0.0.1:3100"],
                             "tenant_id": "tenant_1",
+                            "authorization": "Bearer 1234",

Review Comment:
   IMHO, this tests nothing. There isn't even a validation to prove that the header actually sends to the configured endpoint, it's too perfunctory.
   
   Use the `/echo` endpoint to validate the request header, which is sent back as a response header.
   
   https://github.com/apache/apisix/blob/485f04dd6bfc73ec5937ae9ad30a2786b24fc213/t/plugin/basic-auth.t#L416-L458



##########
apisix/plugins/loki-logger.lua:
##########
@@ -236,5 +247,4 @@ function _M.log(conf, ctx)
     batch_processor_manager:add_entry_to_new_processor(conf, entry, ctx, func)
 end
 
-

Review Comment:
   ditto



##########
apisix/plugins/loki-logger.lua:
##########
@@ -97,6 +100,7 @@ local schema = {
             }
         },
     },
+    encrypt_fields = {"authorization"},

Review Comment:
   This cannot be encrypted. If it needs to implement some kind of security, it is best to provide secret reference support.



##########
apisix/plugins/loki-logger.lua:
##########
@@ -180,7 +192,6 @@ function _M.body_filter(conf, ctx)
     log_util.collect_body(conf, ctx)
 end
 
-

Review Comment:
   Please keep the code style.



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add `authorization` attribute for `loki-logger` (#11420) [apisix]

Posted by "bzp2010 (via GitHub)" <gi...@apache.org>.
bzp2010 commented on code in PR #12243:
URL: https://github.com/apache/apisix/pull/12243#discussion_r2117630310


##########
apisix/plugins/loki-logger.lua:
##########
@@ -138,11 +148,17 @@ end
 
 
 local function send_http_data(conf, log)
+    local headers = {}
+    if conf.request_headers ~= nil then
+        headers = conf.request_headers
+    end
+    if conf.tenant_id then
+        headers["X-Scope-OrgID"] = conf.tenant_id
+    end
+    headers ["Content-Type"] = "application/json"

Review Comment:
   indent



##########
apisix/plugins/loki-logger.lua:
##########
@@ -47,6 +47,16 @@ local schema = {
             default = "/loki/api/v1/push"
         },
         tenant_id = {type = "string", default = "fake"},
+        request_headers = {
+            type = "object",
+            patternProperties = {
+                [".*"] = {
+                    type = "string",
+                    minLength = 1,
+                },
+            },
+        },
+        authorization = { type = "string", minLength = 1, description = "Authorization header" },

Review Comment:
   This authorization is no longer needed.



##########
docs/en/latest/plugins/loki-logger.md:
##########
@@ -45,6 +45,7 @@ When enabled, the Plugin will serialize the request context information to [JSON
 | endpoint_addrs | array[string] | True |  | | Loki API base URLs, such as `http://127.0.0.1:3100`. If multiple endpoints are configured, the log will be pushed to a randomly determined endpoint from the list. |
 | endpoint_uri | string | False | /loki/api/v1/push | | URI path to the Loki ingest endpoint. |
 | tenant_id | string | False | fake | | Loki tenant ID. According to Loki's [multi-tenancy documentation](https://grafana.com/docs/loki/latest/operations/multi-tenancy/#multi-tenancy), the default value is set to `fake` under single-tenancy. |
+| authorization | string | False |  |  | HTTP Authorization Header, used to provide credentials. |

Review Comment:
   Finally, please update the document.



##########
t/plugin/loki-logger.t:
##########
@@ -63,6 +64,7 @@ property "endpoint_addrs" is required
 done
 property "endpoint_uri" validation failed: wrong type: expected string, got number
 property "tenant_id" validation failed: wrong type: expected string, got number
+property "request_headers" validation failed: wrong type: expected object, got number
 property "log_labels" validation failed: wrong type: expected object, got string
 done
 

Review Comment:
   Add new test cases according to the guidelines.
   
   https://github.com/apache/apisix/pull/12243#discussion_r2117628008



##########
apisix/plugins/loki-logger.lua:
##########
@@ -167,6 +183,7 @@ local function send_http_data(conf, log)
         return false, err
     end
 
+

Review Comment:
   Remove this new blank line, keep old code style



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add `authorization` attribute for `loki-logger` (#11420) [apisix]

Posted by "slow-groovin (via GitHub)" <gi...@apache.org>.
slow-groovin commented on code in PR #12243:
URL: https://github.com/apache/apisix/pull/12243#discussion_r2123083772


##########
docs/en/latest/plugins/loki-logger.md:
##########
@@ -45,6 +45,7 @@ When enabled, the Plugin will serialize the request context information to [JSON
 | endpoint_addrs | array[string] | True |  | | Loki API base URLs, such as `http://127.0.0.1:3100`. If multiple endpoints are configured, the log will be pushed to a randomly determined endpoint from the list. |
 | endpoint_uri | string | False | /loki/api/v1/push | | URI path to the Loki ingest endpoint. |
 | tenant_id | string | False | fake | | Loki tenant ID. According to Loki's [multi-tenancy documentation](https://grafana.com/docs/loki/latest/operations/multi-tenancy/#multi-tenancy), the default value is set to `fake` under single-tenancy. |
+| authorization | string | False |  |  | HTTP Authorization Header, used to provide credentials. |

Review Comment:
   Thanks for the guidance. I have made the changes.



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add `authorization` attribute for `loki-logger` (#11420) [apisix]

Posted by "bzp2010 (via GitHub)" <gi...@apache.org>.
bzp2010 commented on code in PR #12243:
URL: https://github.com/apache/apisix/pull/12243#discussion_r2123446191


##########
apisix/plugins/loki-logger.lua:
##########
@@ -138,11 +147,15 @@ end
 
 
 local function send_http_data(conf, log)
+    local headers = {}
+    if conf.headers ~= nil then
+        headers = conf.headers
+    end

Review Comment:
   ```suggestion
       local headers = conf.headers or {}
   ```
   



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add `headers` attribute for `loki-logger` (#11420) [apisix]

Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop merged PR #12243:
URL: https://github.com/apache/apisix/pull/12243


-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add `authorization` attribute for `loki-logger` (#11420) [apisix]

Posted by "membphis (via GitHub)" <gi...@apache.org>.
membphis commented on code in PR #12243:
URL: https://github.com/apache/apisix/pull/12243#discussion_r2125390915


##########
apisix/plugins/loki-logger.lua:
##########
@@ -138,11 +147,12 @@ end
 
 
 local function send_http_data(conf, log)
+    local headers = conf.headers or {}

Review Comment:
   ```suggestion
       local headers = conf.headers or {}
       headers = core.table.clone(headers)  -- avoid changing the original table `conf`
   ```



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add `authorization` attribute for `loki-logger` (#11420) [apisix]

Posted by "bzp2010 (via GitHub)" <gi...@apache.org>.
bzp2010 commented on PR #12243:
URL: https://github.com/apache/apisix/pull/12243#issuecomment-2934683055

   It looks much more reliable and once the tests pass I can approve the change.
   Then I'll call other reviewers.


-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add `authorization` attribute for `loki-logger` (#11420) [apisix]

Posted by "slow-groovin (via GitHub)" <gi...@apache.org>.
slow-groovin commented on code in PR #12243:
URL: https://github.com/apache/apisix/pull/12243#discussion_r2111079133


##########
t/plugin/loki-logger.t:
##########
@@ -80,6 +82,7 @@ done
                         "loki-logger": {
                             "endpoint_addrs": ["http://127.0.0.1:3100"],
                             "tenant_id": "tenant_1",
+                            "authorization": "Bearer 1234",

Review Comment:
   Sorry, there is no local setup guide, and I cannot run `make init` and the local tests successfully, I only conducted actual manual testing.
   And I don’t know how to use `/echo` to verify requests sent to the Loki URL.
   



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org