membphis commented on code in PR #11436:
URL: https://github.com/apache/apisix/pull/11436#discussion_r1743281291


##########
apisix/utils/google-cloud-oauth.lua:
##########
@@ -0,0 +1,137 @@
+--
+-- 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 type = type
+local setmetatable = setmetatable
+
+local ngx_update_time = ngx.update_time
+local ngx_time = ngx.time
+local ngx_encode_args = ngx.encode_args
+
+local http = require("resty.http")
+local jwt = require("resty.jwt")
+
+
+local function get_timestamp()
+    ngx_update_time()
+    return ngx_time()
+end
+
+
+local _M = {}
+
+
+function _M:generate_access_token()

Review Comment:
   ```suggestion
   function _M.generate_access_token(self)
   ```
   
   code style, APISIX mainly use this style, pls update it



##########
apisix/utils/google-cloud-oauth.lua:
##########
@@ -0,0 +1,137 @@
+--
+-- 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 type = type
+local setmetatable = setmetatable
+
+local ngx_update_time = ngx.update_time
+local ngx_time = ngx.time
+local ngx_encode_args = ngx.encode_args
+
+local http = require("resty.http")
+local jwt = require("resty.jwt")
+
+
+local function get_timestamp()
+    ngx_update_time()
+    return ngx_time()
+end
+
+
+local _M = {}
+
+
+function _M:generate_access_token()
+    if not self.access_token or get_timestamp() > 
self.access_token_expire_time - 60 then
+        self:refresh_access_token()
+    end
+    return self.access_token
+end
+
+
+function _M:refresh_access_token()
+    local http_new = http.new()
+    local res, err = http_new:request_uri(self.token_uri, {
+        ssl_verify = self.ssl_verify,
+        method = "POST",
+        body = ngx_encode_args({
+            grant_type = "urn:ietf:params:oauth:grant-type:jwt-bearer",
+            assertion = self:generate_jwt_token()
+        }),
+        headers = {
+            ["Content-Type"] = "application/x-www-form-urlencoded",
+        },
+    })
+
+    if not res then
+        core.log.error("failed to refresh google oauth access token, ", err)
+        return
+    end
+
+    if res.status ~= 200 then
+        core.log.error("failed to refresh google oauth access token: ", 
res.body)
+        return
+    end
+
+    res, err = core.json.decode(res.body)
+    if not res then
+        core.log.error("failed to parse google oauth response data: ", err)
+        return
+    end
+
+    self.access_token = res.access_token
+    self.access_token_type = res.token_type
+    self.access_token_expire_time = get_timestamp() + res.expires_in
+end
+
+
+function _M:generate_jwt_token()
+    local payload = core.json.encode({
+        iss = self.client_email,
+        aud = self.token_uri,
+        scope = self.scope,
+        iat = get_timestamp(),
+        exp = get_timestamp() + (60 * 60)
+    })
+
+    local jwt_token = jwt:sign(self.private_key, {
+        header = { alg = "RS256", typ = "JWT" },
+        payload = payload,
+    })
+
+    return jwt_token
+end
+
+
+function _M:new(config, ssl_verify)

Review Comment:
   we should use `_M.new` here.
   
   we are trying to create a new object



##########
apisix/utils/google-cloud-oauth.lua:
##########
@@ -0,0 +1,137 @@
+--
+-- 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 type = type
+local setmetatable = setmetatable
+
+local ngx_update_time = ngx.update_time
+local ngx_time = ngx.time
+local ngx_encode_args = ngx.encode_args
+
+local http = require("resty.http")
+local jwt = require("resty.jwt")
+
+
+local function get_timestamp()
+    ngx_update_time()
+    return ngx_time()
+end
+
+
+local _M = {}
+
+
+function _M:generate_access_token()
+    if not self.access_token or get_timestamp() > 
self.access_token_expire_time - 60 then
+        self:refresh_access_token()
+    end
+    return self.access_token
+end
+
+
+function _M:refresh_access_token()

Review Comment:
   ditto



##########
t/secret/gcp.t:
##########
@@ -0,0 +1,737 @@
+#
+# 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.
+#
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+log_level("info");
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+    location /t {
+        content_by_lua_block {
+            local test_case = {
+                {},
+                {auth_file = "123"},
+                {auth_file = 123},
+                {auth_config = {client_email = "client", private_key = 
"private_key"}},
+                {auth_config = {private_key = "private_key", project_id = 
"project_id"}},
+                {auth_config = {client_email = "client", project_id = 
"project_id"}},
+                {auth_config = {client_email = "client", private_key = 
"private_key", project_id = "project_id"}},
+                {auth_config = {client_email = 1234, private_key = 
"private_key", project_id = "project_id"}},
+                {auth_config = {client_email = "client", private_key = 1234, 
project_id = "project_id"}},
+                {auth_config = {client_email = "client", private_key = 
"private_key", project_id = 1234}},
+                {auth_config = {client_email = "client", private_key = 
"private_key", project_id = "project_id"}, ssl_verify = 1234},
+                {auth_config = {client_email = "client", private_key = 
"private_key", project_id = "project_id", token_uri = 1234}},
+                {auth_config = {client_email = "client", private_key = 
"private_key", project_id = "project_id", scopes = 1234}},
+                {auth_config = {client_email = "client", private_key = 
"private_key", project_id = "project_id", entries_uri = 1234}},
+                {auth_config = {client_email = "client", private_key = 
"private_key", project_id = "project_id", token_uri = "token_uri",
+                    scopes = {"scopes"}, entries_uri = "entries_uri"}, 
ssl_verify = true},
+            }
+            local gcp = require("apisix.secret.gcp")
+            local core = require("apisix.core")
+            local metadata_schema = gcp.schema
+
+            for _, conf in ipairs(test_case) do
+                local ok, err = core.schema.check(metadata_schema, conf)
+                ngx.say(ok and "done" or err)
+            end
+        }
+    }
+--- request
+GET /t
+--- response_body
+value should match only one schema, but matches none
+done
+property "auth_file" validation failed: wrong type: expected string, got number
+property "auth_config" validation failed: property "project_id" is required
+property "auth_config" validation failed: property "client_email" is required
+property "auth_config" validation failed: property "private_key" is required
+done
+property "auth_config" validation failed: property "client_email" validation 
failed: wrong type: expected string, got number
+property "auth_config" validation failed: property "private_key" validation 
failed: wrong type: expected string, got number
+property "auth_config" validation failed: property "project_id" validation 
failed: wrong type: expected string, got number
+property "ssl_verify" validation failed: wrong type: expected boolean, got 
number
+property "auth_config" validation failed: property "token_uri" validation 
failed: wrong type: expected string, got number
+property "auth_config" validation failed: property "scopes" validation failed: 
wrong type: expected array, got number
+property "auth_config" validation failed: property "entries_uri" validation 
failed: wrong type: expected string, got number
+done
+
+
+
+=== TEST 2: check key: no main key
+--- config
+    location /t {
+        content_by_lua_block {
+            local gcp = require("apisix.secret.gcp")
+            local conf = {
+                auth_config = {
+                    client_email = "[email protected]",
+                    private_key = [[
+-----BEGIN PRIVATE KEY-----
+MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDDzrFwnA3EvYyR
+aeMgaLD3hBjvxKrz10uox1X8q7YYhf2ViRtLRUMa2bEMYksE5hbhwpNf6mKAnLOC
+UuAT6cPPdUl/agKpJXviBPIR2LuzD17WsLJHp1HxUDssSkgfCaGcOGGNfLUhhIpF
+2JUctLmxiZoAZySlSjcwupSuDJ0aPm0XO8r9H8Qu5kF2Vkz5e5bFivLTmvzrQTe4
+v5V1UI6hThElCSeUmdNF3uG3wopxlvq4zXgLTnuLbrNf/Gc4mlpV+UDgTISj32Ep
+AB2vxKEbvQw4ti8YJnGXWjxLerhfrszFw+V8lpeduiDYA44ZFoVqvzxeIsVZNtcw
+Iu7PvEPNAgMBAAECggEAVpyN9m7A1F631/aLheFpLgMbeKt4puV7zQtnaJ2XrZ9P
+PR7pmNDpTu4uF3k/D8qrIm+L+uhVa+hkquf3wDct6w1JVnfQ93riImbnoKdK13ic
+DcEZCwLjByfjFMNCxZ/gAZca55fbExlqhFy6EHmMjhB8s2LsXcTHRuGxNI/Vyi49
+sxECibe0U53aqdJbVWrphIS67cpwl4TUkN6mrHsNuDYNJ9dgkpapoqp4FTFQsBqC
+afOK5qgJ68dWZ47FBUng+AZjdCncqAIuJxxItGVQP6YPsFs+OXcivIVHJr363TpC
+l85FfdvqWV5OGBbwSKhNwiTNUVvfSQVmtURGWG/HbQKBgQD4gZ1z9+Lx19kT9WTz
+lw93lxso++uhAPDTKviyWSRoEe5aN3LCd4My+/Aj+sk4ON/s2BV3ska5Im93j+vC
+rCv3uPn1n2jUhWuJ3bDqipeTW4n/CQA2m/8vd26TMk22yOkkqw2MIA8sjJ//SD7g
+tdG7up6DgGMP4hgbO89uGU7DAwKBgQDJtkKd0grh3u52Foeh9YaiAgYRwc65IE16
+UyD1OJxIuX/dYQDLlo5KyyngFa1ZhWIs7qC7r3xXH+10kfJY+Q+5YMjmZjlL8SR1
+Ujqd02R9F2//6OeswyReachJZbZdtiEw3lPa4jVFYfhSe0M2ZPxMwvoXb25eyCNI
+1lYjSKq87wKBgHnLTNghjeDp4UKe6rNYPgRm0rDrhziJtX5JeUov1mALKb6dnmkh
+GfRK9g8sQqKDfXwfC6Z2gaMK9YaryujGaWYoCpoPXtmJ6oLPXH4XHuLh4mhUiP46
+xn8FEfSimuQS4/FMxH8A128GHQSI7AhGFFzlwfrBWcvXC+mNDsTvMmLxAoGARc+4
+upppfccETQZ7JsitMgD1TMwA2f2eEwoWTAitvlXFNT9PYSbYVHaAJbga6PLLCbYF
+FzAjHpxEOKYSdEyu7n/ayDL0/Z2V+qzc8KarDsg/0RgwppBbU/nUgeKb/U79qcYo
+y4ai3UKNCS70Ei1dTMvmdpnwXwlxfNIBufB6dy0CgYBMYq9Lc31GkC6PcGEEbx6W
+vjImOadWZbuOVnvEQjb5XCdcOsWsMcg96PtoeuyyHmhnEF1GsMzcIdQv/PHrvYpK
+Yp8D0aqsLEgwGrJQER26FPpKmyIwvcL+nm6q5W31PnU9AOC/WEkB6Zs58hsMzD2S
+kEJQcmfVew5mFXyxuEn3zA==
+-----END PRIVATE KEY-----]],
+                    project_id = "apisix",
+                    token_uri = "http://127.0.0.1:1980/token";,
+                    scopes = {
+                        "https://www.googleapis.com/auth/cloud-platform";
+                    },
+                },
+            }
+            local data, err = gcp.get(conf, "/apisix")
+            if err then
+                return ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+can't find main key, key: /apisix
+
+
+
+=== TEST 3: add secret  && consumer && check
+--- request
+GET /t
+--- config
+    location /t {
+        content_by_lua_block {
+            local conf = {
+                auth_config = {
+                    client_email = "[email protected]",
+                    private_key = [[
+-----BEGIN PRIVATE KEY-----
+MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDDzrFwnA3EvYyR
+aeMgaLD3hBjvxKrz10uox1X8q7YYhf2ViRtLRUMa2bEMYksE5hbhwpNf6mKAnLOC
+UuAT6cPPdUl/agKpJXviBPIR2LuzD17WsLJHp1HxUDssSkgfCaGcOGGNfLUhhIpF
+2JUctLmxiZoAZySlSjcwupSuDJ0aPm0XO8r9H8Qu5kF2Vkz5e5bFivLTmvzrQTe4
+v5V1UI6hThElCSeUmdNF3uG3wopxlvq4zXgLTnuLbrNf/Gc4mlpV+UDgTISj32Ep
+AB2vxKEbvQw4ti8YJnGXWjxLerhfrszFw+V8lpeduiDYA44ZFoVqvzxeIsVZNtcw
+Iu7PvEPNAgMBAAECggEAVpyN9m7A1F631/aLheFpLgMbeKt4puV7zQtnaJ2XrZ9P
+PR7pmNDpTu4uF3k/D8qrIm+L+uhVa+hkquf3wDct6w1JVnfQ93riImbnoKdK13ic
+DcEZCwLjByfjFMNCxZ/gAZca55fbExlqhFy6EHmMjhB8s2LsXcTHRuGxNI/Vyi49
+sxECibe0U53aqdJbVWrphIS67cpwl4TUkN6mrHsNuDYNJ9dgkpapoqp4FTFQsBqC
+afOK5qgJ68dWZ47FBUng+AZjdCncqAIuJxxItGVQP6YPsFs+OXcivIVHJr363TpC
+l85FfdvqWV5OGBbwSKhNwiTNUVvfSQVmtURGWG/HbQKBgQD4gZ1z9+Lx19kT9WTz
+lw93lxso++uhAPDTKviyWSRoEe5aN3LCd4My+/Aj+sk4ON/s2BV3ska5Im93j+vC
+rCv3uPn1n2jUhWuJ3bDqipeTW4n/CQA2m/8vd26TMk22yOkkqw2MIA8sjJ//SD7g
+tdG7up6DgGMP4hgbO89uGU7DAwKBgQDJtkKd0grh3u52Foeh9YaiAgYRwc65IE16
+UyD1OJxIuX/dYQDLlo5KyyngFa1ZhWIs7qC7r3xXH+10kfJY+Q+5YMjmZjlL8SR1
+Ujqd02R9F2//6OeswyReachJZbZdtiEw3lPa4jVFYfhSe0M2ZPxMwvoXb25eyCNI
+1lYjSKq87wKBgHnLTNghjeDp4UKe6rNYPgRm0rDrhziJtX5JeUov1mALKb6dnmkh
+GfRK9g8sQqKDfXwfC6Z2gaMK9YaryujGaWYoCpoPXtmJ6oLPXH4XHuLh4mhUiP46
+xn8FEfSimuQS4/FMxH8A128GHQSI7AhGFFzlwfrBWcvXC+mNDsTvMmLxAoGARc+4
+upppfccETQZ7JsitMgD1TMwA2f2eEwoWTAitvlXFNT9PYSbYVHaAJbga6PLLCbYF
+FzAjHpxEOKYSdEyu7n/ayDL0/Z2V+qzc8KarDsg/0RgwppBbU/nUgeKb/U79qcYo
+y4ai3UKNCS70Ei1dTMvmdpnwXwlxfNIBufB6dy0CgYBMYq9Lc31GkC6PcGEEbx6W
+vjImOadWZbuOVnvEQjb5XCdcOsWsMcg96PtoeuyyHmhnEF1GsMzcIdQv/PHrvYpK
+Yp8D0aqsLEgwGrJQER26FPpKmyIwvcL+nm6q5W31PnU9AOC/WEkB6Zs58hsMzD2S
+kEJQcmfVew5mFXyxuEn3zA==
+-----END PRIVATE KEY-----]],
+                    project_id = "apisix",
+                    token_uri = "http://127.0.0.1:1980/google/secret/token";,
+                    scopes = {
+                        "https://www.googleapis.com/auth/cloud-platform";
+                    },
+                    entries_uri = "http://127.0.0.1:1984";
+                },
+            }
+
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/secrets/gcp/mysecret', 
ngx.HTTP_PUT, conf)
+
+            if code >= 300 then
+                ngx.status = code
+                return ngx.say(body)
+            end
+
+            -- change consumer with secrets ref: gcp
+            code, body = t('/apisix/admin/consumers',
+                ngx.HTTP_PUT,
+                [[{
+                    "username": "jack",
+                    "plugins": {
+                          "key-auth": {
+                            "key": "$secret://gcp/mysecret/jack/key"
+                        }
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                return ngx.say(body)
+            end
+
+
+            local secret = require("apisix.secret")
+            local value = 
secret.fetch_by_uri("$secret://gcp/mysecret/jack/key")
+
+
+            local code, body = t('/apisix/admin/secrets/gcp/mysecret', 
ngx.HTTP_DELETE)
+            if code >= 300 then
+                ngx.status = code
+                return ngx.say(body)
+            end
+
+            code, body = t('/apisix/admin/consumers',
+                ngx.HTTP_PUT,
+                [[{
+                    "username": "jack",
+                    "plugins": {
+                          "key-auth": {
+                            "key": "$secret://gcp/mysecret/jack/key"
+                        }
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                return ngx.say(body)
+            end
+
+            local secret = require("apisix.secret")
+            local value = 
secret.fetch_by_uri("$secret://gcp/mysecret/jack/key")
+            if value then
+                ngx.say("secret value: ", value)
+            end
+            ngx.say("all done")
+        }
+    }
+--- response_body
+all done
+
+
+
+=== TEST 4: setup route
+--- 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": {
+                        "serverless-pre-function": {
+                            "phase": "rewrite",
+                            "functions": [
+                                "return function(conf, ctx)
+                                    
require('lib.server').google_secret_apisix_jack()
+                                end"
+                            ]
+                        }
+                    },
+                    "uri": 
"/projects/apisix/secrets/jack/versions/latest:access",
+                    "upstream": {
+                        "type": "roundrobin",
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        }
+                    }
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+
+
+
+=== TEST 5: setup route

Review Comment:
   `test 4`, `test 5` and `test 6`, they use the same title
   I can not catch what is the different



##########
apisix/secret/gcp.lua:
##########
@@ -0,0 +1,202 @@
+--
+-- 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.
+--
+
+--- GCP Tools.
+local core       = require("apisix.core")
+local http       = require("resty.http")
+local google_oauth = require("apisix.utils.google-cloud-oauth")
+
+local sub        = core.string.sub
+local find = core.string.find
+local decode_base64 = ngx.decode_base64
+
+local lrucache = core.lrucache.new({ttl = 300, count= 8})
+
+local schema = {
+    type = "object",
+    properties = {
+        auth_config = {
+            type = "object",
+            properties = {
+                client_email = { type = "string" },
+                private_key = { type = "string" },
+                project_id = { type = "string" },
+                token_uri = {
+                    type = "string",
+                    default = "https://oauth2.googleapis.com/token";
+                },
+                scopes = {
+                    type = "array",
+                    default = {
+                        "https://www.googleapis.com/auth/cloud-platform";
+                    }
+                },
+                entries_uri = {
+                    type = "string",
+                    default = "https://secretmanager.googleapis.com/v1";
+                },
+            },
+            required = { "client_email", "private_key", "project_id"}
+        },
+        ssl_verify = {
+            type = "boolean",
+            default = true
+        },
+        auth_file = { type = "string" },
+    },
+    oneOf = {
+        { required = { "auth_config" } },
+        { required = { "auth_file" } },
+    },
+    encrypt_fields = {"auth_config.private_key"},
+}
+
+local _M = {
+    schema = schema
+}
+
+local function fetch_oauth_conf(conf)
+    if conf.auth_config then
+        return conf.auth_config
+    end
+
+    local file_content, err = core.io.get_file(conf.auth_file)
+    if not file_content then
+        return nil, "failed to read configuration, file: " .. conf.auth_file 
.. ", err: " .. err
+    end
+
+    local config_tab, err = core.json.decode(file_content)
+    if not config_tab then
+        return nil, "config parse failure, data: " .. file_content .. ", err: 
" .. err
+    end
+
+    local config = {
+        auth_config = {
+            client_email = config_tab.client_email,
+            private_key = config_tab.private_key,
+            project_id = config_tab.project_id
+        }
+    }
+
+    local ok, err = core.schema.check(schema, config)
+    if not ok then
+        return nil, "config parse failure, file: " .. conf.auth_file .. ", 
err: " .. err
+    end
+
+    return config_tab
+end
+
+local function create_oauth_object(auth_config, ssl_verify)
+    return google_oauth:new(auth_config, ssl_verify)
+end
+
+local function get_secret(oauth, secrets_id)
+    local http_new = http.new()
+
+    local access_token = oauth:generate_access_token()
+    if not access_token then
+        return nil, "failed to get google oauth token"
+    end
+
+    local entries_uri = oauth.entries_uri .. "/projects/" .. oauth.project_id
+                            .. "/secrets/" .. secrets_id .. 
"/versions/latest:access"
+
+    local res, err = http_new:request_uri(entries_uri, {
+        ssl_verify = oauth.ssl_verify,
+        method = "GET",
+        headers = {
+            ["Content-Type"] = "application/json",
+            ["Authorization"] = (oauth.access_token_type or "Bearer") .. " " 
.. access_token,
+        },
+    })
+
+    if not res then
+        return nil, err
+    end
+
+    if res.status ~= 200 then
+        return nil, res.body
+    end
+
+    local body, err = core.json.decode(res.body)
+    if not body then
+        return nil, "failed to parse response data, " .. err
+    end
+
+    local payload = body.payload
+    if not payload then
+        return nil, "invalid payload"
+    end
+
+    return decode_base64(payload.data)
+end
+

Review Comment:
   two blank lines between different functions



##########
apisix/secret/gcp.lua:
##########
@@ -0,0 +1,202 @@
+--
+-- 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.
+--
+
+--- GCP Tools.
+local core       = require("apisix.core")
+local http       = require("resty.http")
+local google_oauth = require("apisix.utils.google-cloud-oauth")
+
+local sub        = core.string.sub
+local find = core.string.find
+local decode_base64 = ngx.decode_base64
+
+local lrucache = core.lrucache.new({ttl = 300, count= 8})
+
+local schema = {
+    type = "object",
+    properties = {
+        auth_config = {
+            type = "object",
+            properties = {
+                client_email = { type = "string" },
+                private_key = { type = "string" },
+                project_id = { type = "string" },
+                token_uri = {
+                    type = "string",
+                    default = "https://oauth2.googleapis.com/token";
+                },
+                scopes = {
+                    type = "array",
+                    default = {
+                        "https://www.googleapis.com/auth/cloud-platform";
+                    }
+                },
+                entries_uri = {
+                    type = "string",
+                    default = "https://secretmanager.googleapis.com/v1";
+                },
+            },
+            required = { "client_email", "private_key", "project_id"}
+        },
+        ssl_verify = {
+            type = "boolean",
+            default = true
+        },
+        auth_file = { type = "string" },
+    },
+    oneOf = {
+        { required = { "auth_config" } },
+        { required = { "auth_file" } },
+    },
+    encrypt_fields = {"auth_config.private_key"},
+}
+
+local _M = {
+    schema = schema
+}
+
+local function fetch_oauth_conf(conf)
+    if conf.auth_config then
+        return conf.auth_config
+    end
+
+    local file_content, err = core.io.get_file(conf.auth_file)
+    if not file_content then
+        return nil, "failed to read configuration, file: " .. conf.auth_file 
.. ", err: " .. err
+    end
+
+    local config_tab, err = core.json.decode(file_content)
+    if not config_tab then
+        return nil, "config parse failure, data: " .. file_content .. ", err: 
" .. err
+    end
+
+    local config = {
+        auth_config = {
+            client_email = config_tab.client_email,
+            private_key = config_tab.private_key,
+            project_id = config_tab.project_id
+        }
+    }
+
+    local ok, err = core.schema.check(schema, config)
+    if not ok then
+        return nil, "config parse failure, file: " .. conf.auth_file .. ", 
err: " .. err
+    end
+
+    return config_tab
+end
+
+local function create_oauth_object(auth_config, ssl_verify)
+    return google_oauth:new(auth_config, ssl_verify)
+end
+
+local function get_secret(oauth, secrets_id)
+    local http_new = http.new()
+
+    local access_token = oauth:generate_access_token()
+    if not access_token then
+        return nil, "failed to get google oauth token"
+    end
+
+    local entries_uri = oauth.entries_uri .. "/projects/" .. oauth.project_id
+                            .. "/secrets/" .. secrets_id .. 
"/versions/latest:access"
+
+    local res, err = http_new:request_uri(entries_uri, {
+        ssl_verify = oauth.ssl_verify,
+        method = "GET",
+        headers = {
+            ["Content-Type"] = "application/json",
+            ["Authorization"] = (oauth.access_token_type or "Bearer") .. " " 
.. access_token,
+        },
+    })
+
+    if not res then
+        return nil, err
+    end
+
+    if res.status ~= 200 then
+        return nil, res.body
+    end
+
+    local body, err = core.json.decode(res.body)
+    if not body then
+        return nil, "failed to parse response data, " .. err
+    end
+
+    local payload = body.payload
+    if not payload then
+        return nil, "invalid payload"
+    end
+
+    return decode_base64(payload.data)
+end
+
+local function make_request_to_gcp(conf, secrets_id)
+    local auth_config, err = fetch_oauth_conf(conf)
+    if not auth_config then
+        return nil, err
+    end
+
+    local lru_key =  auth_config.client_email .. "#" .. auth_config.project_id
+
+    local oauth, err = lrucache(lru_key, "gcp", create_oauth_object, 
auth_config, conf.ssl_verify)
+    if not oauth then
+        return nil, "failed to create oauth object, " .. err
+    end
+
+    local secret, err = get_secret(oauth, secrets_id)
+    if not secret then
+        return nil, err
+    end
+
+    return secret
+end
+
+function _M.get(conf, key)
+    core.log.info("fetching data from gcp for key: ", key)
+
+    local idx = find(key, '/')
+
+    local main_key = idx and sub(key, 1, idx - 1) or key
+    if main_key == "" then
+        return nil, "can't find main key, key: " .. key
+    end
+
+    local sub_key = idx and sub(key, idx + 1) or nil
+
+    core.log.info("main: ", main_key, sub_key and ", sub: " .. sub_key or "")
+
+    local res, err = make_request_to_gcp(conf, main_key)
+    if not res then
+        return nil, "failed to retrtive data from gcp secret manager: " .. err
+    end
+
+    if not sub_key then
+        return res
+    end
+
+    local data, err = core.json.decode(res)
+    if not data then
+        return nil, "failed to decode result, res: " .. res .. ", err: " .. err
+    end
+
+    return data[sub_key]
+end
+
+

Review Comment:
   two blank lines between different functions



##########
apisix/secret/gcp.lua:
##########
@@ -0,0 +1,202 @@
+--
+-- 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.
+--
+
+--- GCP Tools.
+local core       = require("apisix.core")
+local http       = require("resty.http")
+local google_oauth = require("apisix.utils.google-cloud-oauth")
+
+local sub        = core.string.sub
+local find = core.string.find
+local decode_base64 = ngx.decode_base64
+
+local lrucache = core.lrucache.new({ttl = 300, count= 8})
+
+local schema = {
+    type = "object",
+    properties = {
+        auth_config = {
+            type = "object",
+            properties = {
+                client_email = { type = "string" },
+                private_key = { type = "string" },
+                project_id = { type = "string" },
+                token_uri = {
+                    type = "string",
+                    default = "https://oauth2.googleapis.com/token";
+                },
+                scopes = {
+                    type = "array",
+                    default = {
+                        "https://www.googleapis.com/auth/cloud-platform";
+                    }
+                },
+                entries_uri = {
+                    type = "string",
+                    default = "https://secretmanager.googleapis.com/v1";
+                },
+            },
+            required = { "client_email", "private_key", "project_id"}
+        },
+        ssl_verify = {
+            type = "boolean",
+            default = true
+        },
+        auth_file = { type = "string" },
+    },
+    oneOf = {
+        { required = { "auth_config" } },
+        { required = { "auth_file" } },
+    },
+    encrypt_fields = {"auth_config.private_key"},
+}
+
+local _M = {
+    schema = schema
+}
+
+local function fetch_oauth_conf(conf)
+    if conf.auth_config then
+        return conf.auth_config
+    end
+
+    local file_content, err = core.io.get_file(conf.auth_file)
+    if not file_content then
+        return nil, "failed to read configuration, file: " .. conf.auth_file 
.. ", err: " .. err
+    end
+
+    local config_tab, err = core.json.decode(file_content)
+    if not config_tab then
+        return nil, "config parse failure, data: " .. file_content .. ", err: 
" .. err
+    end
+
+    local config = {
+        auth_config = {
+            client_email = config_tab.client_email,
+            private_key = config_tab.private_key,
+            project_id = config_tab.project_id
+        }
+    }
+
+    local ok, err = core.schema.check(schema, config)
+    if not ok then
+        return nil, "config parse failure, file: " .. conf.auth_file .. ", 
err: " .. err
+    end
+
+    return config_tab
+end
+
+local function create_oauth_object(auth_config, ssl_verify)

Review Comment:
   we do not need this function, if we can create `oauth object` by 
`google_oauth.new`



##########
apisix/secret/gcp.lua:
##########
@@ -0,0 +1,202 @@
+--
+-- 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.
+--
+
+--- GCP Tools.
+local core       = require("apisix.core")
+local http       = require("resty.http")
+local google_oauth = require("apisix.utils.google-cloud-oauth")
+
+local sub        = core.string.sub

Review Comment:
   ```suggestion
   local str_sub        = core.string.sub
   ```
   
   only use `sub` is not a good name, we should keep `str_` or `string_` as 
prefix



##########
apisix/secret/gcp.lua:
##########
@@ -0,0 +1,202 @@
+--
+-- 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.
+--
+
+--- GCP Tools.
+local core       = require("apisix.core")
+local http       = require("resty.http")
+local google_oauth = require("apisix.utils.google-cloud-oauth")
+
+local sub        = core.string.sub
+local find = core.string.find

Review Comment:
   ditto



-- 
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