Kong Gateway Services are defined in the services
block in the decK file. Services support a number of configuration values including a protocol field which specifies the communication protocol used between the gateway and the upstream Service. To ensure this traffic is secure, you may want to validate that only https
protocols are used.
Here is a sample Ruleset file containing a single Rule that accomplishes this:
rules:
service-https-check:
description: "Ensure https usage in Kong GW Services"
given: $.services[*].protocol
severity: error
then:
function: pattern
functionOptions:
match: "^https$"
The JSONPath selector specified in given
reads the protocol
field in every Service under the services
key from the incoming file. With each of those values, the pattern
function is applied which evaluates the value against a regular expression pattern specified in the match
field. In this example, we assert that the string value in the protocol
field must match the string https
exactly.
Assume you have the following decK declarative configuration file (kong.yaml
) that defines a Service and a Route for a simple task tracking system:
_format_version: "3.0"
services:
- host: tasks.example.com
name: task-api
path: /
protocol: http
routes:
- methods:
- GET
name: task-api_gettasks
paths:
- ~/tasks$
Validating this configuration against the example ruleset, stored in ruleset.yaml
, results in the following violations:
deck file lint -s kong.yaml ruleset.yaml
Linting Violations: 1
Failures: 1
[error][7:15] Ensure https usage in Kong GW Services: `http` does not match the expression `^https$`
Modifying the declarative configuration as follows resolves this violation:
_format_version: "3.0"
services:
- host: tasks.example.com
name: task-api
path: /
protocol: https
routes:
- methods:
- GET
name: task-api_gettasks
paths:
- ~/tasks$
deck file lint -s kong.yaml ruleset.yaml; echo $?
Result:
The command results in a 0
(Success) return code. In situations where violations are detected, a non-zero return code is emitted allowing you to abort automated processes and help prevent problematic configurations from leaking into your production codebase and systems.