TykTechnologies / tyk

Tyk Open Source API Gateway written in Go, supporting REST, GraphQL, TCP and gRPC protocols
Other
9.65k stars 1.08k forks source link

Exception when using virtual endpoints #636

Closed mftaha closed 4 years ago

mftaha commented 7 years ago

*Do you want to request a feature or report a bug*? BUG

What is the current behavior? Getting the below exception when i call the corresponding api

What is the expected behavior? Virtual Endpoint JS should be called.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem

Logs

tyk_gateway_1 | 2017/03/24 21:58:22 http: panic serving 172.18.0.1:41590: interface conversion: interface is nil, not main.SessionState tyk_gateway_1 | goroutine 3071 [running]: tyk_gateway_1 | net/http.(*conn).serve.func1(0xc420f62800) tyk_gateway_1 | /usr/local/go/src/net/http/server.go:1491 +0x12a tyk_gateway_1 | panic(0xccdb20, 0xc420d6dac0) tyk_gateway_1 | /usr/local/go/src/runtime/panic.go:458 +0x243 tyk_gateway_1 | main.(*VirtualEndpoint).ServeHTTPForCache(0xc420a42d90, 0x12b3220, 0xc4202784e0, 0xc420b012c0, 0x0) tyk_gateway_1 | /src/github.com/TykTechnologies/tyk/middleware_virtual_endpoint.go:192 +0x19b6 tyk_gateway_1 | main.(*VirtualEndpoint).ProcessRequest(0xc420a42d90, 0x12b3220, 0xc4202784e0, 0xc420b012c0, 0xce8f00, 0x0, 0x18, 0xc420b51d70, 0x8) tyk_gateway_1 | /src/github.com/TykTechnologies/tyk/middleware_virtual_endpoint.go:290 +0x4d tyk_gateway_1 | main.CreateMiddleware.func1.1(0x12b3220, 0xc4202784e0, 0xc420b012c0) tyk_gateway_1 | /src/github.com/TykTechnologies/tyk/middleware.go:76 +0x637 tyk_gateway_1 | net/http.HandlerFunc.ServeHTTP(0xc420e740c0, 0x12b3220, 0xc4202784e0, 0xc420b012c0) tyk_gateway_1 | /usr/local/go/src/net/http/server.go:1726 +0x44 tyk_gateway_1 | main.CreateMiddleware.func1.1(0x12b3220, 0xc4202784e0, 0xc420b012c0) tyk_gateway_1 | /src/github.com/TykTechnologies/tyk/middleware.go:100 +0x83c tyk_gateway_1 | net/http.HandlerFunc.ServeHTTP(0xc420e74100, 0x12b3220, 0xc4202784e0, 0xc420b012c0) tyk_gateway_1 | /usr/local/go/src/net/http/server.go:1726 +0x44 tyk_gateway_1 | main.CreateMiddleware.func1.1(0x12b3220, 0xc4202784e0, 0xc420b012c0) tyk_gateway_1 | /src/github.com/TykTechnologies/tyk/middleware.go:100 +0x83c tyk_gateway_1 | net/http.HandlerFunc.ServeHTTP(0xc420e74140, 0x12b3220, 0xc4202784e0, 0xc420b012c0) tyk_gateway_1 | /usr/local/go/src/net/http/server.go:1726 +0x44 tyk_gateway_1 | main.CreateMiddleware.func1.1(0x12b3220, 0xc4202784e0, 0xc420b012c0) tyk_gateway_1 | /src/github.com/TykTechnologies/tyk/middleware.go:100 +0x83c tyk_gateway_1 | net/http.HandlerFunc.ServeHTTP(0xc420e74180, 0x12b3220, 0xc4202784e0, 0xc420b012c0) tyk_gateway_1 | /usr/local/go/src/net/http/server.go:1726 +0x44 tyk_gateway_1 | github.com/TykTechnologies/tyk/vendor/github.com/gorilla/mux.(*Router).ServeHTTP(0xc42039f090, 0x12b3220, 0xc4202784e0, 0xc420b012c0) tyk_gateway_1 | /src/github.com/TykTechnologies/tyk/vendor/github.com/gorilla/mux/mux.go:114 +0x10d tyk_gateway_1 | net/http.(*ServeMux).ServeHTTP(0xc420cd60f0, 0x12b3220, 0xc4202784e0, 0xc420b00b40) tyk_gateway_1 | /usr/local/go/src/net/http/server.go:2022 +0x7f tyk_gateway_1 | net/http.serverHandler.ServeHTTP(0xc420d8eb00, 0x12b3220, 0xc4202784e0, 0xc420b00b40) tyk_gateway_1 | /usr/local/go/src/net/http/server.go:2202 +0x7d tyk_gateway_1 | net/http.(*conn).serve(0xc420f62800, 0x12b42a0, 0xc420d6d580) tyk_gateway_1 | /usr/local/go/src/net/http/server.go:1579 +0x4b7 tyk_gateway_1 | created by net/http.(*Server).Serve tyk_gateway_1 | /usr/local/go/src/net/http/server.go:2293 +0x44d

MiddleWare Script

`function thisTest(request, session, config) { // Set up a reponse object log("Virtual Test running")

log("Request Body: ")
log(request.Body)

log("Session: ")
log(session)

log("Config:")
log(config)

log("param-1:")
log(request.Params["param1"])

var responseObject = {
    Body: "THIS IS A  VIRTUAL RESPONSE"
    Headers: {
        "test": "virtual",
        "test-2": "virtual"
    },
    Code: 200
}

return TykJsResponse(responseObject, session.meta_data)

} log("Virtual Test initialised") `

Tyk.conf { "listen_address": "", "listen_port": 8080, "secret": "####", "node_secret": "####", "template_path": "/opt/tyk-gateway/templates", "tyk_js_path": "/opt/tyk-gateway/js/tyk.js", "middleware_path": "/opt/tyk-gateway/middleware", "policies": { "policy_source": "service", "policy_connection_string": "http://tyk_dashboard:3000", "policy_record_name": "tyk_policies", "allow_explicit_policy_id": false }, "use_db_app_configs": true, "db_app_conf_options": { "connection_string": "http://tyk_dashboard:3000", "node_is_segmented": false, "tags": [ "test2" ] }, "disable_dashboard_zeroconf": false, "app_path": "/opt/tyk-gateway/apps/", "storage": { "type": "redis", "host": "redis", "port": 6379, "hosts": null, "username": "", "password": "", "database": 0, "optimisation_max_idle": 100, "optimisation_max_active": 0, "enable_cluster": false }, "enable_separate_cache_store": false, "cache_storage": { "type": "", "host": "", "port": 0, "hosts": null, "username": "", "password": "", "database": 0, "optimisation_max_idle": 0, "optimisation_max_active": 0, "enable_cluster": false }, "enable_analytics": true, "analytics_config": { "type": "mongo", "ignored_ips": [], "enable_detailed_recording": false, "enable_geo_ip": false, "geo_ip_db_path": "", "normalise_urls": { "enabled": false, "normalise_uuids": false, "normalise_numbers": false, "custom_patterns": null }, "pool_size": 0 }, "health_check": { "enable_health_checks": true, "health_check_value_timeouts": 60 }, "optimisations_use_async_session_write": true, "allow_master_keys": false, "hash_keys": true, "suppress_redis_signal_reload": false, "suppress_default_org_store": false, "use_redis_log": false, "sentry_code": "", "use_sentry": false, "use_syslog": false, "use_graylog": false, "use_logstash": false, "graylog_network_addr": "", "logstash_network_addr": "", "syslog_transport": "", "logstash_transport": "", "syslog_network_addr": "", "statsd_connection_string": "", "statsd_prefix": "", "enforce_org_data_age": false, "enforce_org_data_detail_logging": false, "enforce_org_quotas": false, "experimental_process_org_off_thread": false, "enable_non_transactional_rate_limiter": true, "enable_sentinel_rate_limiter": false, "enable_redis_rolling_limiter": false, "Monitor": { "enable_trigger_monitors": false, "configuration": { "method": "", "target_path": "", "template_path": "", "header_map": null, "event_timeout": 0 }, "global_trigger_limit": 0, "monitor_user_keys": false, "monitor_org_keys": false }, "oauth_refresh_token_expire": 0, "oauth_token_expire": 0, "oauth_redirect_uri_separator": "", "slave_options": { "use_rpc": false, "connection_string": "", "rpc_key": "", "api_key": "", "enable_rpc_cache": false, "bind_to_slugs": false, "disable_keyspace_sync": false, "group_id": "", "call_timeout": 30, "ping_timeout": 60 }, "disable_virtual_path_blobs": false, "local_session_cache": { "disable_cached_session_state": false, "cached_session_timeout": 0, "cached_session_eviction": 0 }, "http_server_options": { "override_defaults": false, "read_timeout": 0, "write_timeout": 0, "use_ssl": false, "use_ssl_le": false, "enable_websockets": false, "certificates": null, "server_name": "", "min_version": 0, "flush_interval": 0, "skip_url_cleaning": false }, "service_discovery": { "default_cache_timeout": 0 }, "close_connections": true, "auth_override": { "force_auth_provider": false, "auth_provider": { "name": "", "storage_engine": "", "meta": null }, "force_session_provider": false, "session_provider": { "name": "", "storage_engine": "", "meta": null } }, "uptime_tests": { "disable": false, "config": { "failure_trigger_sample_size": 0, "time_wait": 0, "checker_pool_size": 0, "enable_uptime_analytics": false } }, "hostname": "", "enable_api_segregation": false, "control_api_hostname": "", "enable_custom_domains": false, "enable_jsvm": true, "coprocess_options": { "enable_coprocess": true, "coprocess_grpc_server": "tcp://10.154.161.163:5555" }, "hide_generator_header": false, "event_handlers": { "events": null }, "event_trigers_defunct": {}, "pid_file_location": "", "allow_insecure_configs": true, "public_key_path": "", "close_idle_connections": false, "drl_notification_frequency": 0, "global_session_lifetime": 100, "force_global_session_lifetime": false, "bundle_base_url": "", "enable_bundle_downloader": false, "allow_remote_config": false, "legacy_enable_allowance_countdown": false, "max_idle_connections_per_host": 100, "reload_wait_time": 0 }

API Definition { "api_model": {}, "api_definition": { "id": "58d1da0b0572a10001741509", "name": "hala8", "slug": "hala8", "api_id": "9becc92419d54a834cfe0cafac78b737", "org_id": "58d1d9f20572a10001741502", "use_keyless": true, "use_oauth2": false, "use_openid": false, "openid_options": { "providers": [], "segregate_by_client": false }, "oauth_meta": { "allowed_access_types": [], "allowed_authorize_types": [], "auth_login_redirect": "" }, "auth": { "use_param": false, "param_name": "", "use_cookie": false, "cookie_name": "", "auth_header_name": "" }, "use_basic_auth": false, "enable_jwt": false, "use_standard_auth": false, "enable_coprocess_auth": false, "jwt_signing_method": "", "jwt_source": "", "jwt_identity_base_field": "", "jwt_client_base_field": "", "jwt_policy_field_name": "", "notifications": { "shared_secret": "", "oauth_on_keychange_url": "" }, "enable_signature_checking": false, "hmac_allowed_clock_skew": -1, "base_identity_provided_by": "", "definition": { "location": "header", "key": "x-api-version" }, "version_data": { "not_versioned": false, "versions": { "Default": { "name": "Default", "expires": "", "paths": { "ignored": [], "white_list": [], "black_list": [] }, "use_extended_paths": true, "extended_paths": { "virtual": [ { "response_function_name": "thisTest", "function_source_type": "file", "function_source_uri": "middleware/testVirtual.js", "path": "get-batch", "method": "GET", "use_session": true } ]}, "global_headers": {}, "global_headers_remove": [], "global_size_limit": 0, "override_target": "" } } }, "uptime_tests": { "check_list": [], "config": { "expire_utime_after": 0, "service_discovery": { "use_discovery_service": false, "query_endpoint": "", "use_nested_query": false, "parent_data_path": "", "data_path": "", "port_data_path": "", "target_path": "", "use_target_list": false, "cache_timeout": 60, "endpoint_returns_list": false }, "recheck_wait": 0 } }, "proxy": { "preserve_host_header": false, "listen_path": "/hala8/", "target_url": "http://httpbin.org", "strip_listen_path": true, "enable_load_balancing": false, "target_list": [], "check_host_against_uptime_tests": false, "service_discovery": { "use_discovery_service": false, "query_endpoint": "", "use_nested_query": false, "parent_data_path": "", "data_path": "hostname", "port_data_path": "port", "target_path": "/api-slug", "use_target_list": false, "cache_timeout": 60, "endpoint_returns_list": false } }, "disable_rate_limit": false, "disable_quota": false, "custom_middleware": { }, "custom_middleware_bundle": "", "cache_options": { "cache_timeout": 60, "enable_cache": false, "cache_all_safe_requests": false, "cache_response_codes": [], "enable_upstream_cache_control": false }, "session_lifetime": 0, "active": true, "auth_provider": { "name": "", "storage_engine": "", "meta": {} }, "session_provider": { "name": "", "storage_engine": "", "meta": null }, "event_handlers": { "events": {} }, "enable_batch_request_support": false, "enable_ip_whitelisting": false, "allowed_ips": [], "dont_set_quota_on_create": false, "expire_analytics_after": 0, "response_processors": [], "CORS": { "enable": false, "allowed_origins": [], "allowed_methods": [], "allowed_headers": [], "exposed_headers": [], "allow_credentials": false, "max_age": 24, "options_passthrough": false, "debug": false }, "domain": "", "do_not_track": false, "tags": [], "enable_context_vars": false }, "hook_references": [], "is_site": false, "sort_by": 0 }

Which versions of Tyk affected by this issue? Did this work in previous versions of Tyk?

latest docker images.

lonelycode commented 7 years ago

When you start the gateway - does it show the log message from the virtual endpoint script?

Virtual Test initialised

^ this message?

mftaha commented 7 years ago

Yes! could it be something in my configuration? or API JSON definition?

lonelycode commented 7 years ago

That basically means that the JS is ok. Is this api open or closed (uses an api token)?

nebolsin commented 7 years ago

Looks like there's syntax error in virtual endpoint example from Tyk documentation page (also appears in changelog).

@mftaha try adding comma after Body: "THIS IS A VIRTUAL RESPONSE" line.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, please add comments to this ticket if you would like it to stay open. Thank you for your contributions.