local active = redis.call('GET', KEYS[1]) if not active or active == '' then return {'reject', 'CONFIG_MISSING', 'none', 'none', 'none', 'none', 'no_active_version'} end local source_ip = ARGV[1] local caller = ARGV[2] or '' local callee = ARGV[3] or '' local prefix = 'cfg:v:' .. active local gateway_id = redis.call('GET', prefix .. ':auth:ip:' .. source_ip) if not gateway_id then return {'reject', 'AUTH_MISSING', 'none', active, 'none', 'none', 'no_auth_ip'} end local gateway_json = redis.call('GET', prefix .. ':customer_gateway:' .. gateway_id) if not gateway_json then return {'reject', 'GATEWAY_MISSING', gateway_id, active, 'none', 'none', 'gateway_missing'} end if not string.find(gateway_json, '"status":"ENABLED"', 1, true) then return {'reject', 'GATEWAY_DISABLED', gateway_id, active, 'none', 'none', 'gateway_disabled'} end local customer_id = string.match(gateway_json, '"customerId":"([^"]+)"') if not customer_id then return {'reject', 'CUSTOMER_MISSING', gateway_id, active, 'none', 'none', 'customer_id_missing'} end local customer_json = redis.call('GET', prefix .. ':customer:' .. customer_id) if not customer_json then return {'reject', 'CUSTOMER_MISSING', gateway_id, active, 'none', 'none', 'customer_missing'} end if not string.find(customer_json, '"status":"ENABLED"', 1, true) then return {'reject', 'CUSTOMER_DISABLED', gateway_id, active, 'none', 'none', 'customer_disabled'} end local policies = redis.call('LRANGE', prefix .. ':customer_gateway:' .. gateway_id .. ':policies', 0, -1) for _, policy_json in ipairs(policies) do if string.find(policy_json, '"status":"ENABLED"', 1, true) then local caller_mode = string.match(policy_json, '"callerMode":"([^"]+)"') or 'ANY' local caller_value = string.match(policy_json, '"callerValue":"([^"]*)"') or '' local callee_mode = string.match(policy_json, '"calleeMode":"([^"]+)"') or 'ANY' local callee_value = string.match(policy_json, '"calleeValue":"([^"]*)"') or '' local caller_ok = caller_mode == 'ANY' or (caller_mode == 'EQUALS' and caller == caller_value) or (caller_mode == 'PREFIX' and string.sub(caller, 1, string.len(caller_value)) == caller_value) local callee_ok = callee_mode == 'ANY' or (callee_mode == 'EQUALS' and callee == callee_value) or (callee_mode == 'PREFIX' and string.sub(callee, 1, string.len(callee_value)) == callee_value) if caller_ok and callee_ok then local policy_id = string.match(policy_json, '"id":"([^"]+)"') or 'none' local line_group_id = string.match(policy_json, '"lineGroupId":"([^"]+)"') or 'none' return {'allow', 'OK', gateway_id, active, line_group_id, policy_id, customer_id} end end end return {'reject', 'NO_POLICY', gateway_id, active, 'none', 'none', 'no_policy_match'}