Skip to content
Kong Logo | Kong Docs Logo
search
  • We're Hiring!
  • Docs
    • Kong Gateway
    • Kong Konnect
    • Kong Mesh
    • Plugin Hub
    • decK
    • Kubernetes Ingress Controller
    • Insomnia
    • Kuma

    • Docs contribution guidelines
  • Plugin Hub
  • Support
  • Community
  • Kong Academy
Get a Demo Start Free Trial
  • Kong Gateway
  • Kong Konnect
  • Kong Mesh
  • Plugin Hub
  • decK
  • Kubernetes Ingress Controller
  • Insomnia
  • Kuma

  • Docs contribution guidelines
  • 3.2.x (latest)
  • 3.1.x
  • 3.0.x
  • 2.8.x
  • 2.7.x
  • 2.6.x
  • Older Enterprise versions (2.1-2.5)
  • Older OSS versions (2.1-2.5)
  • Archive (pre-2.1)
    • Overview of Kong Gateway
      • Version Support Policy
      • Supported Installation Options
      • Supported Linux Distributions
    • Stability
    • Release Notes
      • Services
        • Overview
        • Configure Routes with Expressions
      • Upstreams
      • Plugins
      • Routing Traffic
      • Load Balancing
      • Health Checks and Circuit Breakers
      • Kong Performance Testing
    • Glossary
    • Get Kong
    • Services and Routes
    • Rate Limiting
    • Proxy Caching
    • Key Authentication
    • Load-Balancing
      • Overview
        • Overview
        • Deploy Kong Gateway in Hybrid mode
      • DB-less Deployment
      • Traditional
      • Overview
        • Helm
        • OpenShift with Helm
        • kubectl apply
        • Kubernetes Deployment Options
        • Using docker run
        • Build your own Docker images
        • Amazon Linux
        • Debian
        • Red Hat
        • Ubuntu
      • Running Kong as a non-root user
      • Securing the Admin API
      • Using systemd
      • Start Kong Gateway Securely
      • Programatically Creating Admins
      • Enabling RBAC
      • Overview
      • Download your License
      • Deploy Enterprise License
      • Using the License API
      • Monitor Licenses Usage
      • Default Ports
      • DNS Considerations
      • Network and Firewall
      • CP/DP Communication through a Forward Proxy
    • Kong Configuration File
    • Environment Variables
    • Serving a Website and APIs from Kong
      • Overview
      • Prometheus
      • StatsD
      • Datadog
      • Overview
      • Writing a Custom Trace Exporter
      • Tracing API Reference
    • Resource Sizing Guidelines
    • Security Update Process
    • Blue-Green Deployments
    • Canary Deployments
    • Clustering Reference
      • Log Reference
      • Dynamic log level updates
      • Customize Gateway Logs
      • Upgrade Kong Gateway 3.1.x
      • Migrate from OSS to Enterprise
    • Overview
      • Overview
      • Metrics
      • Analytics with InfluxDB
      • Analytics with Prometheus
      • Estimate Analytics Storage in PostgreSQL
      • Overview
      • Getting Started
      • Advanced Usage
        • Overview
        • Environment Variables
        • AWS Secrets Manager
        • Google Secrets Manager
        • Hashicorp Vault
        • Securing the Database with AWS Secrets Manager
      • Reference Format
      • Overview
      • Get Started with Dynamic Plugin Ordering
      • Overview
      • Enable the Dev Portal
      • Publish an OpenAPI Spec
      • Structure and File Types
      • Themes Files
      • Working with Templates
      • Using the Editor
        • Basic Auth
        • Key Auth
        • OIDC
        • Sessions
        • Adding Custom Registration Fields
        • Manage Developers
        • Developer Roles and Content Permissions
        • Authorization Provider Strategy
        • Enable Application Registration
        • Enable Key Authentication for Application Registration
          • External OAuth2 Support
          • Set up Okta and Kong for External Oauth
          • Set up Azure AD and Kong for External Authentication
        • Manage Applications
        • Theme Editing
        • Migrating Templates Between Workspaces
        • Markdown Rendering Module
        • Customizing Portal Emails
        • Adding and Using JavaScript Assets
        • Single Page App in Dev Portal
        • Alternate OpenAPI Renderer
      • SMTP
      • Workspaces
      • Helpers CLI
      • Portal API Documentation
    • Audit Logging
    • Keyring and Data Encryption
    • Workspaces
    • Consumer Groups
    • Event Hooks
    • FIPS 140-2
    • Overview
    • Enable Kong Manager
      • Services and Routes
      • Rate Limiting
      • Proxy Caching
      • Authentication with Consumers
      • Load Balancing
      • Overview
      • Create a Super Admin
      • Workspaces and Teams
      • Reset Passwords and RBAC Tokens
      • Basic Auth
        • Configure LDAP
        • LDAP Service Directory Mapping
        • Configure OIDC
        • OIDC Authenticated Group Mapping
      • Sessions
        • Overview
        • Enable RBAC
        • Add a Role and Permissions
        • Create a User
        • Create an Admin
    • Networking Configuration
    • Workspaces
    • Create Consumer Groups
    • Sending Email
    • Overview
    • File Structure
    • Implementing Custom Logic
    • Plugin Configuration
    • Accessing the Data Store
    • Storing Custom Entities
    • Caching Custom Entities
    • Extending the Admin API
    • Writing Tests
    • (un)Installing your Plugin
      • Overview
      • kong.client
      • kong.client.tls
      • kong.cluster
      • kong.ctx
      • kong.ip
      • kong.jwe
      • kong.log
      • kong.nginx
      • kong.node
      • kong.request
      • kong.response
      • kong.router
      • kong.service
      • kong.service.request
      • kong.service.response
      • kong.table
      • kong.tracing
      • kong.vault
      • kong.websocket.client
      • kong.websocket.upstream
      • Go
      • Javascript
      • Python
      • Running Plugins in Containers
      • External Plugin Performance
    • Overview
        • Overview
        • OpenID Connect with Curity
        • OpenID Connect with Azure AD
        • OpenID Connect with Google
        • OpenID Connect with Okta
        • OpenID Connect with Auth0
        • OpenID Connect with Cognito
      • Authentication Reference
      • Allow Multiple Authentication Plugins
    • Rate Limiting Plugin
      • Add a Body Value
    • GraphQL
      • gRPC Plugins
      • Configure a gRPC service
    • Overview
    • Information Routes
    • Health Routes
    • Tags
    • Debug Routes
    • Services
    • Routes
    • Consumers
    • Plugins
    • Certificates
    • CA Certificates
    • SNIs
    • Upstreams
    • Targets
    • Vaults
    • Keys
    • Licenses
    • Workspaces
    • RBAC
    • Admins
    • Developers
    • Consumer Groups
    • Event Hooks
    • Keyring and Data Encryption
    • Audit Logs
    • kong.conf
    • Injecting Nginx Directives
    • CLI
    • File Permissions Reference
    • Key Management
    • Performance Testing Framework
    • Router Expressions Language
    • FAQ

github-edit-pageEdit this page

report-issueReport an issue

enterprise-switcher-iconSwitch to OSS

On this page
  • kong.service.set_upstream(host)
  • kong.service.set_target(host, port)
  • kong.service.set_tls_cert_key(chain, key)
  • kong.service.set_tls_verify(on)
  • kong.service.set_tls_verify_depth(depth)
  • kong.service.set_tls_verify_store(store)
Kong Gateway
3.1.x
  • Home
  • Kong Gateway
  • Plugin Development
  • PDK
  • kong.service
You are browsing documentation for an outdated version. See the latest documentation here.

kong.service

The service module contains a set of functions to manipulate the connection aspect of the request to the Service, such as connecting to a given host, IP address/port, or choosing a given Upstream entity for load-balancing and healthchecking.

kong.service.set_upstream(host)

Sets the desired Upstream entity to handle the load-balancing step for this request. Using this method is equivalent to creating a Service with a host property equal to that of an Upstream entity (in which case, the request would be proxied to one of the Targets associated with that Upstream).

The host argument should receive a string equal to the name of one of the Upstream entities currently configured.

Phases

  • access

Parameters

  • host (string):

Returns

  1. boolean|nil: true on success, or nil if no upstream entities where found

  2. string|nil: An error message describing the error if there was one.

Usage

local ok, err = kong.service.set_upstream("service.prod")
if not ok then
  kong.log.err(err)
  return
end

kong.service.set_target(host, port)

Sets the host and port on which to connect to for proxying the request. Using this method is equivalent to ask Kong to not run the load-balancing phase for this request, and consider it manually overridden. Load-balancing components such as retries and health-checks will also be ignored for this request.

The host argument expects the hostname or IP address of the upstream server, and the port expects a port number.

Phases

  • access

Parameters

  • host (string):
  • port (number):

Usage

kong.service.set_target("service.local", 443)
kong.service.set_target("192.168.130.1", 80)

kong.service.set_tls_cert_key(chain, key)

Sets the client certificate used while handshaking with the Service.

The chain argument is the client certificate and intermediate chain (if any) returned by functions such as ngx.ssl.parse_pem_cert.

The key argument is the private key corresponding to the client certificate returned by functions such as ngx.ssl.parse_pem_priv_key.

Phases

  • rewrite, access, balancer

Parameters

  • chain (cdata): The client certificate chain
  • key (cdata): The client certificate private key

Returns

  1. boolean|nil: true if the operation succeeded, nil if an error occurred

  2. string|nil: An error message describing the error if there was one

Usage

local chain = assert(ssl.parse_pem_cert(cert_data))
local key = assert(ssl.parse_pem_priv_key(key_data))

local ok, err = kong.service.set_tls_cert_key(chain, key)
if not ok then
  -- do something with error
end

kong.service.set_tls_verify(on)

Sets whether TLS verification is enabled while handshaking with the Service.

The on argument is a boolean flag, where true means upstream verification is enabled and false disables it.

This call affects only the current request. If the trusted certificate store is not set already (via proxy_ssl_trusted_certificate or kong.service.set_upstream_ssl_trusted_store), then TLS verification will always fail with “unable to get local issuer certificate” error.

Phases

  • rewrite, access, balancer

Parameters

  • on (boolean): Whether to enable TLS certificate verification for the current request

Returns

  1. boolean|nil: true if the operation succeeded, nil if an error occurred

  2. string|nil: An error message describing the error if there was one

Usage

local ok, err = kong.service.set_tls_verify(true)
if not ok then
  -- do something with error
end

kong.service.set_tls_verify_depth(depth)

Sets the maximum depth of verification when validating upstream server’s TLS certificate.

This call affects only the current request. For the depth to be actually used the verification has to be enabled with either the proxy_ssl_verify directive or using the kong.service.set_tls_verify function.

Phases

  • rewrite, access, balancer

Parameters

  • depth (number): Depth to use when validating. Must be non-negative

Returns

  1. boolean|nil: true if the operation succeeded, nil if an error occurred

  2. string|nil: An error message describing the error if there was one

Usage

local ok, err = kong.service.set_tls_verify_depth(3)
if not ok then
  -- do something with error
end

kong.service.set_tls_verify_store(store)

Sets the CA trust store to use when validating upstream server’s TLS certificate.

This call affects only the current request. For the store to be actually used the verification has to be enabled with either the proxy_ssl_verify directive or using the kong.service.set_tls_verify function.

The resty.openssl.x509.store object can be created by following examples from the Kong/lua-kong-nginx-module repo.

Phases

  • rewrite, access, balancer

Parameters

  • store (table): resty.openssl.x509.store object to use

Returns

  1. boolean|nil: true if the operation succeeded, nil if an error occurred

  2. string|nil: An error message describing the error if there was one

Usage

local store = require("resty.openssl.x509.store")
local st = assert(store.new())
-- st:add(...certificate)

local ok, err = kong.service.set_tls_verify_store(st)
if not ok then
  -- do something with error
end
Thank you for your feedback.
Was this page useful?
  • Kong
    THE CLOUD CONNECTIVITY COMPANY

    Kong powers reliable digital connections across APIs, hybrid and multi-cloud environments.

    • Company
    • Customers
    • Events
    • Investors
    • Careers Hiring!
    • Partners
    • Press
    • Contact
  • Products
    • Kong Konnect
    • Kong Gateway
    • Kong Mesh
    • Get Started
    • Pricing
  • Resources
    • eBooks
    • Webinars
    • Briefs
    • Blog
    • API Gateway
    • Microservices
  • Open Source
    • Install Kong Gateway
    • Kong Community
    • Kubernetes Ingress
    • Kuma
    • Insomnia
  • Solutions
    • Decentralize
    • Secure & Govern
    • Create a Dev Platform
    • API Gateway
    • Kubernetes
    • Service Mesh
Star
  • Terms•Privacy
© Kong Inc. 2023