DAO for custom plugins not working

I’m using kong 1.2.1.
I wrote a custom plugin which creates 2 tables in Postgres database.
The plugin is installed in custom path, and I applied the patch package.path = conf.lua_package_path .. ";" .. package.path to /usr/local/share/lua/5.1/kong/cmd/migrations.lua, as described in https://github.com/Kong/kong/issues/4514#issuecomment-483791787.

The custom migrations in my plugin run successfully and kong started normally.

The problem is, DAO is not working for my plugin.

Output when I start kong, kong started normally:

2019/08/07 17:22:29 [verbose] Kong: 1.2.1
2019/08/07 17:22:29 [debug] ngx_lua: 10013
2019/08/07 17:22:29 [debug] nginx: 1013006
2019/08/07 17:22:29 [debug] Lua: LuaJIT 2.1.0-beta3
2019/08/07 17:22:29 [verbose] reading config file at kong.conf
2019/08/07 17:22:29 [debug] reading environment variables
2019/08/07 17:22:29 [debug] admin_access_log = "logs/admin_access.log"
2019/08/07 17:22:29 [debug] admin_error_log = "logs/error.log"
2019/08/07 17:22:29 [debug] admin_listen = {"127.0.0.1:8001","127.0.0.1:8444 ssl"}
2019/08/07 17:22:29 [debug] anonymous_reports = false
2019/08/07 17:22:29 [debug] cassandra_consistency = "ONE"
2019/08/07 17:22:29 [debug] cassandra_contact_points = {"127.0.0.1"}
2019/08/07 17:22:29 [debug] cassandra_data_centers = {"dc1:2","dc2:3"}
2019/08/07 17:22:29 [debug] cassandra_keyspace = "kong"
2019/08/07 17:22:29 [debug] cassandra_lb_policy = "RequestRoundRobin"
2019/08/07 17:22:29 [debug] cassandra_port = 9042
2019/08/07 17:22:29 [debug] cassandra_repl_factor = 1
2019/08/07 17:22:29 [debug] cassandra_repl_strategy = "SimpleStrategy"
2019/08/07 17:22:29 [debug] cassandra_schema_consensus_timeout = 10000
2019/08/07 17:22:29 [debug] cassandra_ssl = false
2019/08/07 17:22:29 [debug] cassandra_ssl_verify = false
2019/08/07 17:22:29 [debug] cassandra_timeout = 5000
2019/08/07 17:22:29 [debug] cassandra_username = "kong"
2019/08/07 17:22:29 [debug] client_body_buffer_size = "8k"
2019/08/07 17:22:29 [debug] client_max_body_size = "0"
2019/08/07 17:22:29 [debug] client_ssl = false
2019/08/07 17:22:29 [debug] database = "postgres"
2019/08/07 17:22:29 [debug] db_cache_ttl = 0
2019/08/07 17:22:29 [debug] db_cache_warmup_entities = {"services","plugins"}
2019/08/07 17:22:29 [debug] db_resurrect_ttl = 30
2019/08/07 17:22:29 [debug] db_update_frequency = 5
2019/08/07 17:22:29 [debug] db_update_propagation = 0
2019/08/07 17:22:29 [debug] dns_error_ttl = 1
2019/08/07 17:22:29 [debug] dns_hostsfile = "/etc/hosts"
2019/08/07 17:22:29 [debug] dns_no_sync = false
2019/08/07 17:22:29 [debug] dns_not_found_ttl = 30
2019/08/07 17:22:29 [debug] dns_order = {"LAST","SRV","A","CNAME"}
2019/08/07 17:22:29 [debug] dns_resolver = {}
2019/08/07 17:22:29 [debug] dns_stale_ttl = 4
2019/08/07 17:22:29 [debug] error_default_type = "text/plain"
2019/08/07 17:22:29 [debug] headers = {"server_tokens","latency_tokens"}
2019/08/07 17:22:29 [debug] log_level = "debug"
2019/08/07 17:22:29 [debug] lua_package_cpath = ""
2019/08/07 17:22:29 [debug] lua_package_path = "/home/leslie/workspace/lua/kong/kong-plugin-udesk-cs-canary/?.lua;/home/leslie/workspace/lua/kong/kong-plugin-udesk-cs-canary/?/init.lua;./?.lua;./?/init.lua;"
2019/08/07 17:22:29 [debug] lua_socket_pool_size = 30
2019/08/07 17:22:29 [debug] lua_ssl_verify_depth = 1
2019/08/07 17:22:29 [debug] mem_cache_size = "128m"
2019/08/07 17:22:29 [debug] nginx_admin_directives = {}
2019/08/07 17:22:29 [debug] nginx_daemon = "on"
2019/08/07 17:22:29 [debug] nginx_http_directives = {}
2019/08/07 17:22:29 [debug] nginx_optimizations = true
2019/08/07 17:22:29 [debug] nginx_proxy_directives = {}
2019/08/07 17:22:29 [debug] nginx_sproxy_directives = {}
2019/08/07 17:22:29 [debug] nginx_stream_directives = {}
2019/08/07 17:22:29 [debug] nginx_user = "nobody nobody"
2019/08/07 17:22:29 [debug] nginx_worker_processes = "auto"
2019/08/07 17:22:29 [debug] origins = {}
2019/08/07 17:22:29 [debug] pg_database = "kong"
2019/08/07 17:22:29 [debug] pg_host = "127.0.0.1"
2019/08/07 17:22:29 [debug] pg_max_concurrent_queries = 0
2019/08/07 17:22:29 [debug] pg_password = "******"
2019/08/07 17:22:29 [debug] pg_port = 5432
2019/08/07 17:22:29 [debug] pg_semaphore_timeout = 60000
2019/08/07 17:22:29 [debug] pg_ssl = false
2019/08/07 17:22:29 [debug] pg_ssl_verify = false
2019/08/07 17:22:29 [debug] pg_timeout = 5000
2019/08/07 17:22:29 [debug] pg_user = "kong"
2019/08/07 17:22:29 [debug] plugins = {"udesk-cs-canary"}
2019/08/07 17:22:29 [debug] prefix = "/home/leslie/workspace/lua/kong/kong-demo"
2019/08/07 17:22:29 [debug] proxy_access_log = "logs/access.log"
2019/08/07 17:22:29 [debug] proxy_error_log = "logs/error.log"
2019/08/07 17:22:29 [debug] proxy_listen = {"0.0.0.0:8000","0.0.0.0:8443 ssl"}
2019/08/07 17:22:29 [debug] real_ip_header = "X-Real-IP"
2019/08/07 17:22:29 [debug] real_ip_recursive = "off"
2019/08/07 17:22:29 [debug] router_consistency = "strict"
2019/08/07 17:22:29 [debug] ssl_cipher_suite = "modern"
2019/08/07 17:22:29 [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"
2019/08/07 17:22:29 [debug] stream_listen = {"off"}
2019/08/07 17:22:29 [debug] trusted_ips = {}
2019/08/07 17:22:29 [debug] upstream_keepalive = 60
2019/08/07 17:22:29 [verbose] prefix in use: /home/leslie/workspace/lua/kong/kong-demo
2019/08/07 17:22:29 [debug] loading subsystems migrations...
2019/08/07 17:22:29 [verbose] retrieving database schema state...
2019/08/07 17:22:29 [verbose] schema state retrieved
2019/08/07 17:22:29 [verbose] preparing nginx prefix directory at /home/leslie/workspace/lua/kong/kong-demo
2019/08/07 17:22:29 [verbose] SSL enabled, no custom certificate set: using default certificate
2019/08/07 17:22:29 [verbose] default SSL certificate found at /home/leslie/workspace/lua/kong/kong-demo/ssl/kong-default.crt
2019/08/07 17:22:29 [verbose] Admin SSL enabled, no custom certificate set: using default certificate
2019/08/07 17:22:29 [verbose] admin SSL certificate found at /home/leslie/workspace/lua/kong/kong-demo/ssl/admin-kong-default.crt
2019/08/07 17:22:29 [debug] searching for OpenResty 'nginx' executable
2019/08/07 17:22:29 [debug] /usr/local/openresty/nginx/sbin/nginx -v: 'nginx version: openresty/1.13.6.2'
2019/08/07 17:22:29 [debug] found OpenResty 'nginx' executable at /usr/local/openresty/nginx/sbin/nginx
2019/08/07 17:22:29 [debug] testing nginx configuration: KONG_NGINX_CONF_CHECK=true /usr/local/openresty/nginx/sbin/nginx -t -p /home/leslie/workspace/lua/kong/kong-demo -c nginx.conf
2019/08/07 17:22:29 [debug] searching for OpenResty 'nginx' executable
2019/08/07 17:22:29 [debug] /usr/local/openresty/nginx/sbin/nginx -v: 'nginx version: openresty/1.13.6.2'
2019/08/07 17:22:29 [debug] found OpenResty 'nginx' executable at /usr/local/openresty/nginx/sbin/nginx
2019/08/07 17:22:29 [debug] sending signal to pid at: /home/leslie/workspace/lua/kong/kong-demo/pids/nginx.pid
2019/08/07 17:22:29 [debug] kill -0 `cat /home/leslie/workspace/lua/kong/kong-demo/pids/nginx.pid` >/dev/null 2>&1
2019/08/07 17:22:29 [debug] starting nginx: /usr/local/openresty/nginx/sbin/nginx -p /home/leslie/workspace/lua/kong/kong-demo -c nginx.conf
2019/08/07 17:22:29 [debug] nginx started
2019/08/07 17:22:29 [info] Kong started

Output when I send a request to API gateway, and the DAO is not working:

2019/08/07 17:24:14 [error] 17920#0: *1342 lua coroutine: runtime error: ...-udesk-cs-canary/kong/plugins/udesk-cs-canary/access.lua:25: attempt to call method 'count' (a nil value)
stack traceback:
coroutine 0:
	...-udesk-cs-canary/kong/plugins/udesk-cs-canary/access.lua: in function 'load_company_locator'
	...-udesk-cs-canary/kong/plugins/udesk-cs-canary/access.lua:59: in function 'is_cs_canary_request_judge_by_subdomain'
	...-udesk-cs-canary/kong/plugins/udesk-cs-canary/access.lua:71: in function 'is_cs_canary_request'
	...-udesk-cs-canary/kong/plugins/udesk-cs-canary/access.lua:81: in function 'execute'
	...udesk-cs-canary/kong/plugins/udesk-cs-canary/handler.lua:5: in function <...udesk-cs-canary/kong/plugins/udesk-cs-canary/handler.lua:4>
coroutine 1:
	[C]: in function 'resume'
	coroutine.wrap:21: in function <coroutine.wrap:21>
	/usr/local/share/lua/5.1/kong/init.lua:663: in function 'access'
	access_by_lua(nginx-kong.conf:98):2: in function <access_by_lua(nginx-kong.conf:98):1>, client: 127.0.0.1, server: kong, request: "GET / HTTP/1.1", host: "example.com"

daos.lua file of my plugin:

local typedefs = require "kong.db.schema.typedefs"

local cs_canary_subdomain_company_locators = {
  name = "cs_canary_subdomain_company_locators",
  primary_key = { "id" },
  fields = {
    { id = typedefs.uuid },
    { company_id = { type = "integer" } },
    { subdomain = { type = "string" } },
    { created_at = typedefs.auto_timestamp_s },
    { updated_at = typedefs.auto_timestamp_s },
  }
}

local cs_canary_whitelist_company_ids = {
  name = "cs_canary_whitelist_company_ids",
  primary_key = { "id" },
  fields = {
    { id = typedefs.uuid },
    { company_ids = { type = "integer" } },
    { is_canary = { type = "boolean" } },
  }
}

return {
  cs_canary_subdomain_company_locators = cs_canary_subdomain_company_locators,
  cs_canary_whitelist_company_ids = cs_canary_whitelist_company_ids,
}

And some functions in access.lua:

local function load_company_locator(subdomain)
  kong.log.debug("count: " .. kong.db.cs_canary_subdomain_company_locators:count())

  local entity, err = kong.db.cs_canary_subdomain_company_locators:select_by_field('subdomain', subdomain)

  if err then
    kong.log.err("Error when query cs_canary_subdomain_company_locators: " .. err)
    return nil
  end

  if not entity then
    kong.log.err("subdomain " .. subdomain .. " not found in cs_canary_subdomain_company_locators")
    return nil
  end

  return entity
end

Both the function calls kong.db.cs_canary_subdomain_company_locators:count() and kong.db.cs_canary_subdomain_company_locators:select_by_field('subdomain', subdomain) throw the same error:

attempt to call method 'count' (a nil value)
attempt to call method 'select_by_field' (a nil value)

I’m new to Lua and new to Kong, would somebody please give me some hints to debug this error?
Thanks in advance!


© 2018 Kong Inc.    Terms  •  Privacy  •  FAQ