Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dbless): improve validation errors from /config #10161

Merged
merged 3 commits into from
Feb 1, 2023

Conversation

flrgh
Copy link
Contributor

@flrgh flrgh commented Jan 24, 2023

summary

This is a quality-of-life feature for the /config endpoint in dbless mode.

the problem

When the configuration passed into POST /config has validation errors, the raw structures from kong.db.schema are returned. Here's an example where a single service is missing the value for the host property and contains an invalid value for the port property:

{
  "message": "declarative config is invalid: {services={{host=\"required field missing\",port=\"value should be between 0 and 65535\"}}}",
  "code": 14,
  "fields": {
    "services": [
      {
        "host": "required field missing",
        "port": "value should be between 0 and 65535"
      }
    ]
  },
  "name": "invalid declarative configuration"
}

The errors are not correlated to the input entity in any meaningful way, so this gets very confusing once you have more than a couple entities--especially if they are nested underneath other entities (i.e. service.routes).

the solution

This PR adds an optional flatten_errors query param to the endpoint. When truth-y (e.g. POST /config?flatten_errors=1), errors are reshaped and attached to the response in a flattened_errors array at the top level (old fields are retained for backwards compatibility). Here's what the previous response looks like with flatten_errors turned on:

{
  "message": "declarative config is invalid: {services={{host=\"required field missing\",port=\"value should be between 0 and 65535\"}}}",
  "code": 14,
  "fields": {
    "services": [
      {
        "host": "required field missing",
        "port": "value should be between 0 and 65535"
      }
    ]
  },
  "name": "invalid declarative configuration",
  "flattened_errors": [
    {
      "entity_type": "service",
      "entity_name": "jeff",
      "entity_tags": [
        "jeff-is-cool"
      ],
      "entity": {
        "name": "jeff",
        "protocol": "http",
        "port": -1,
        "tags": [
          "jeff-is-cool"
        ]
      },
      "errors": [
        {
          "type": "field",
          "field": "host",
          "message": "required field missing"
        },
        {
          "type": "field",
          "field": "port",
          "message": "value should be between 0 and 65535"
        }
      ]
    }
  ]
}

PR status

This is ready for merge after an approving review or two.

todo

  • more input/response test cases
    • many-to-one
    • one-to-one
    • plugin entity test cases
  • changelog
  • fix tests broken by the changes made to kong.db.declarative
  • make the flattened array optional via query param (?)

kong/api/routes/config.lua Outdated Show resolved Hide resolved
@RobSerafini RobSerafini added this to the 3.2 milestone Jan 24, 2023
@flrgh flrgh requested a review from samugi January 26, 2023 00:40
Copy link
Member

@samugi samugi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great to me so far. I verified that the flattened output reflects what was discussed in the design. I understand it is still WIP so I am leaving the review as comment for now until it's ready

kong/db/errors.lua Outdated Show resolved Hide resolved
@flrgh flrgh force-pushed the feat/dbless-config-validation-errors branch from 142ce09 to 1723fdb Compare January 28, 2023 01:37
@flrgh flrgh marked this pull request as ready for review January 30, 2023 22:31
@flrgh flrgh force-pushed the feat/dbless-config-validation-errors branch from e679091 to 01a3045 Compare January 30, 2023 22:33
@flrgh
Copy link
Contributor Author

flrgh commented Jan 31, 2023

The initial goal was to use the processed input when generating the final error array.
This means updating kong.db.schema.others.declarative_config:flatten() to attach/return the processed config table when returning an error. I couldn't come up with a means of doing so without making lots of breaking and/or intrusive changes, so I compromised a bit.

kong/db/errors.lua Outdated Show resolved Hide resolved
@flrgh flrgh force-pushed the feat/dbless-config-validation-errors branch from 83af116 to e041d3c Compare January 31, 2023 18:04
@flrgh
Copy link
Contributor Author

flrgh commented Jan 31, 2023

Rebased onto master just now. I'm cherry-picking to EE to hopefully make sure there are no surprises over on that side.

Copy link
Contributor

@tyler-ball tyler-ball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few questions but overall I think its looking great. I would like to get 1 more review's eyes on this if possible, but the test cases look good

kong/api/routes/config.lua Show resolved Hide resolved
kong/db/declarative/init.lua Show resolved Hide resolved
@flrgh flrgh force-pushed the feat/dbless-config-validation-errors branch from 8b547f6 to 9191229 Compare January 31, 2023 22:41
@flrgh flrgh requested a review from samugi January 31, 2023 23:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants