Kong / kong

🦍 The Cloud-Native API Gateway and AI Gateway.
https://konghq.com/install/
Apache License 2.0
39.33k stars 4.82k forks source link

[Custom Plugin] Can't access config object from init_worker in handler.lua ?? #3001

Closed DanielTimLee closed 7 years ago

DanielTimLee commented 7 years ago

Summary

https://getkong.org/docs/0.11.x/plugin-development/custom-logic/#handler-lua-specifications

As I see here, Specification of handler.lua has config object from init_worker. But the Problem is, when I try to access config object from the init_worker it keeps returning me a nil value.

image

Steps To Reproduce

  1. Create a Kong-Vagrant Dev env. (with Custom Plugin) from here (https://github.com/Kong/kong-vagrant)
  2. Create a schema.lua
    return {
    no_consumer = false, -- this plugin is available on APIs as well as on Consumers,
    fields = {-- Describe your plugin's configuration's schema here.
    migration_url = {type = "string", required = true}, -- S3 or Any Storage URL
    },
    self_check = function(schema, plugin_t, dao, is_updating)
    -- perform any custom verification
    return true
    end
    }
  3. Create a Handler.lua function with init_worker.
    
    local plugin = require("kong.plugins.base_plugin"):extend()
    function plugin:new()
    plugin.super.new(self, "myPlugin")
    end

function plugin:init_worker(plugin_conf) plugin.super.access(self) ngx.log(ngx.ERR, plugin_conf) end

Result  
`2017/11/01 09:41:41 [error] 10578#0: *1 [lua] handler.lua:35: init_worker(): nil, context: init_worker_by_lua*`

4. **`Beware of Nil and PAAAAAAAAAAANIC !!!!!!!!`**
5. ** Actually, I'm not sure why that plugin not enabled error message pops out? ** (My Plugin is working fine.)

`./kong/cmd/start.lua:62: ./kong/cmd/start.lua:51: nginx: [error] init_by_lua error: ./kong/init.lua:154: myPlugin plugin is in use but not enabled`

### Additional Details & Logs

- Kong version (`$ kong version` == 0.11.1)
- Kong debug-level startup logs (`$ kong start --vv`)

```lua
2017/11/01 09:47:07 [verbose] Kong: 0.11.1
2017/11/01 09:47:07 [debug] ngx_lua: 10008
2017/11/01 09:47:07 [debug] nginx: 1011002
2017/11/01 09:47:07 [debug] Lua: LuaJIT 2.1.0-beta2
2017/11/01 09:47:07 [verbose] no config file found at /etc/kong/kong.conf
2017/11/01 09:47:07 [verbose] no config file found at /etc/kong.conf
2017/11/01 09:47:07 [verbose] no config file, skipping loading
2017/11/01 09:47:07 [debug] admin_access_log = "logs/admin_access.log"
2017/11/01 09:47:07 [debug] admin_error_log = "logs/error.log"
2017/11/01 09:47:07 [debug] admin_http2 = false
2017/11/01 09:47:07 [debug] admin_listen = "0.0.0.0:8001"
2017/11/01 09:47:07 [debug] admin_listen_ssl = "0.0.0.0:8444"
2017/11/01 09:47:07 [debug] admin_ssl = true
2017/11/01 09:47:07 [debug] anonymous_reports = true
2017/11/01 09:47:07 [debug] cassandra_consistency = "ONE"
2017/11/01 09:47:07 [debug] cassandra_contact_points = {"127.0.0.1"}
2017/11/01 09:47:07 [debug] cassandra_data_centers = {"dc1:2","dc2:3"}
2017/11/01 09:47:07 [debug] cassandra_keyspace = "kong"
2017/11/01 09:47:07 [debug] cassandra_lb_policy = "RoundRobin"
2017/11/01 09:47:07 [debug] cassandra_port = 9042
2017/11/01 09:47:07 [debug] cassandra_repl_factor = 1
2017/11/01 09:47:07 [debug] cassandra_repl_strategy = "SimpleStrategy"
2017/11/01 09:47:07 [debug] cassandra_schema_consensus_timeout = 10000
2017/11/01 09:47:07 [debug] cassandra_ssl = false
2017/11/01 09:47:07 [debug] cassandra_ssl_verify = false
2017/11/01 09:47:07 [debug] cassandra_timeout = 5000
2017/11/01 09:47:07 [debug] cassandra_username = "kong"
2017/11/01 09:47:07 [debug] client_body_buffer_size = "8k"
2017/11/01 09:47:07 [debug] client_max_body_size = "0"
2017/11/01 09:47:07 [debug] client_ssl = false
2017/11/01 09:47:07 [debug] custom_plugins = {}
2017/11/01 09:47:07 [debug] database = "postgres"
2017/11/01 09:47:07 [debug] db_cache_ttl = 3600
2017/11/01 09:47:07 [debug] db_update_frequency = 5
2017/11/01 09:47:07 [debug] db_update_propagation = 0
2017/11/01 09:47:07 [debug] dns_error_ttl = 1
2017/11/01 09:47:07 [debug] dns_hostsfile = "/etc/hosts"
2017/11/01 09:47:07 [debug] dns_no_sync = false
2017/11/01 09:47:07 [debug] dns_not_found_ttl = 30
2017/11/01 09:47:07 [debug] dns_order = {"LAST","SRV","A","CNAME"}
2017/11/01 09:47:07 [debug] dns_resolver = {}
2017/11/01 09:47:07 [debug] dns_stale_ttl = 4
2017/11/01 09:47:07 [debug] error_default_type = "text/plain"
2017/11/01 09:47:07 [debug] http2 = false
2017/11/01 09:47:07 [debug] latency_tokens = true
2017/11/01 09:47:07 [debug] log_level = "notice"
2017/11/01 09:47:07 [debug] lua_package_cpath = ""
2017/11/01 09:47:07 [debug] lua_package_path = "./?.lua;./?/init.lua;"
2017/11/01 09:47:07 [debug] lua_socket_pool_size = 30
2017/11/01 09:47:07 [debug] lua_ssl_verify_depth = 1
2017/11/01 09:47:07 [debug] mem_cache_size = "128m"
2017/11/01 09:47:07 [debug] nginx_daemon = "on"
2017/11/01 09:47:07 [debug] nginx_optimizations = true
2017/11/01 09:47:07 [debug] nginx_user = "nobody nobody"
2017/11/01 09:47:07 [debug] nginx_worker_processes = "auto"
2017/11/01 09:47:07 [debug] pg_database = "kong"
2017/11/01 09:47:07 [debug] pg_host = "127.0.0.1"
2017/11/01 09:47:07 [debug] pg_port = 5432
2017/11/01 09:47:07 [debug] pg_ssl = false
2017/11/01 09:47:07 [debug] pg_ssl_verify = false
2017/11/01 09:47:07 [debug] pg_user = "kong"
2017/11/01 09:47:07 [debug] prefix = "/usr/local/kong/"
2017/11/01 09:47:07 [debug] proxy_access_log = "logs/access.log"
2017/11/01 09:47:07 [debug] proxy_error_log = "logs/error.log"
2017/11/01 09:47:07 [debug] proxy_listen = "0.0.0.0:8000"
2017/11/01 09:47:07 [debug] proxy_listen_ssl = "0.0.0.0:8443"
2017/11/01 09:47:07 [debug] real_ip_header = "X-Real-IP"
2017/11/01 09:47:07 [debug] real_ip_recursive = "off"
2017/11/01 09:47:07 [debug] server_tokens = true
2017/11/01 09:47:07 [debug] ssl = true
2017/11/01 09:47:07 [debug] ssl_cipher_suite = "modern"
2017/11/01 09:47:07 [debug] ssl_ciphers = "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256"
2017/11/01 09:47:07 [debug] trusted_ips = {}
2017/11/01 09:47:07 [debug] upstream_keepalive = 60
2017/11/01 09:47:07 [verbose] prefix in use: /usr/local/kong
2017/11/01 09:47:07 [verbose] preparing nginx prefix directory at /usr/local/kong
2017/11/01 09:47:07 [verbose] SSL enabled, no custom certificate set: using default certificate
2017/11/01 09:47:07 [verbose] default SSL certificate found at /usr/local/kong/ssl/kong-default.crt
2017/11/01 09:47:07 [verbose] Admin SSL enabled, no custom certificate set: using default certificate
2017/11/01 09:47:07 [verbose] admin SSL certificate found at /usr/local/kong/ssl/admin-kong-default.crt
2017/11/01 09:47:07 [debug] searching for OpenResty 'nginx' executable
2017/11/01 09:47:07 [debug] /usr/local/openresty/nginx/sbin/nginx -v: 'nginx version: openresty/1.11.2.4'
2017/11/01 09:47:07 [debug] found OpenResty 'nginx' executable at /usr/local/openresty/nginx/sbin/nginx
2017/11/01 09:47:07 [debug] starting nginx: /usr/local/openresty/nginx/sbin/nginx -p /usr/local/kong -c nginx.conf
2017/11/01 09:47:08 [verbose] could not start Kong, stopping services
2017/11/01 09:47:08 [verbose] stopped services
Error: 
./kong/cmd/start.lua:62: ./kong/cmd/start.lua:51: nginx: [error] init_by_lua error: ./kong/init.lua:154: myPlugin plugin is in use but not enabled
stack traceback:
        [C]: in function 'assert'
        ./kong/init.lua:154: in function 'init'
        init_by_lua:3: in main chunk

stack traceback:
        [C]: in function 'error'
        ./kong/cmd/start.lua:62: in function 'cmd_exec'
        ./kong/cmd/init.lua:88: in function <./kong/cmd/init.lua:88>myPlugin
        [C]: in function 'xpcall'
        ./kong/cmd/init.lua:88: in function <./kong/cmd/init.lua:45>
        bin/kong:7: in function 'file_gen'
        init_worker_by_lua:38: in function <init_worker_by_lua:36>
        [C]: in function 'xpcall'
        init_worker_by_lua:45: in function <init_worker_by_lua:43>
p0pr0ck5 commented 7 years ago

The behavior you're seeing is correct. In the init worker phase, nonrequest context is available to load a specific instance of the plugin, and thus no specific configuration can be passed to the handler. The init_worker handler should be used for generic maintenance work and timer initialization logic, but not individual plugins entities. But do also note that you can manually load individual configuration from the data store via the dao. I believe the bot-detection plugin did this in 0.11.0 (though the logic was simplified in the latest release).

For any further questions on plugin development, please consider using the mailing list or Gitter, as GitHub issues should be reserved for actual bugs with Kong core. Thank you!