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

[Fleet] use @kbn/config-schema in Fleet API (Part 1) #192883

Merged
merged 16 commits into from
Sep 18, 2024

Conversation

juliaElastic
Copy link
Contributor

@juliaElastic juliaElastic commented Sep 13, 2024

Summary

Relates #184685

Readd #192447 with fixes and tests to validate that the response schemas are correct.

Checklist

juliaElastic and others added 3 commits September 13, 2024 15:26
## Summary

Part of elastic#184685

First set of changes, I'm planning to do more prs for the remaining
endpoints.

Will also look at making sure these schema definitions are included in
the final kibana `bundle.json`, related doc:
https://elasticco.atlassian.net/wiki/spaces/DOC/pages/450494532/API+reference+docs

When all schema definitions are moved to code, the fleet/openapi folder
can be deleted.

To check the result, add to `kibana.dev.yml`: `server.oas.enabled: true`
And then in kibana console, query:
```
GET kbn:/api/oas?pathStartsWith=/api/fleet/setup
GET kbn:/api/oas?pathStartsWith=/api/fleet/agents/setup

GET kbn:/api/oas?pathStartsWith=/api/fleet/package_policies
GET kbn:/api/oas?pathStartsWith=/api/fleet/package_policies/delete
GET kbn:/api/oas?pathStartsWith=/api/fleet/settings

GET kbn:/api/oas?pathStartsWith=/internal/fleet/settings/enrollment
```

To generate the bundle from code, run:
```
node scripts/capture_oas_snapshot --include-path /api/fleet --no-serverless --update

# writes to oas_docs/bundle.json
```

Response:
```
{
  "openapi": "3.0.0",
  "info": {
    "title": "Kibana HTTP APIs",
    "version": "0.0.0"
  },
  "servers": [
    {
      "url": "http://localhost:5603/julia"
    }
  ],
  "paths": {
    "/api/fleet/setup": {
      "post": {
        "summary": "",
        "tags": [],
        "description": "Initiate Fleet setup",
        "responses": {
          "200": {
            "content": {
              "application/json; Elastic-Api-Version=2023-10-31": {
                "schema": {
                  "type": "object",
                  "description": "A summary of the result of Fleet's `setup` lifecycle. If `isInitialized` is true, Fleet is ready to accept agent enrollment. `nonFatalErrors` may include useful insight into non-blocking issues with Fleet setup.",
                  "properties": {
                    "isInitialized": {
                      "type": "boolean"
                    },
                    "nonFatalErrors": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "name": {
                            "type": "string"
                          },
                          "message": {
                            "type": "string"
                          }
                        },
                        "additionalProperties": false,
                        "required": [
                          "name",
                          "message"
                        ]
                      }
                    }
                  },
                  "additionalProperties": false,
                  "required": [
                    "isInitialized",
                    "nonFatalErrors"
                  ]
                }
              }
            }
          },
          "400": {
            "content": {
              "application/json; Elastic-Api-Version=2023-10-31": {
                "schema": {
                  "type": "object",
                  "description": "Generic Error",
                  "properties": {
                    "statusCode": {
                      "type": "number"
                    },
                    "error": {
                      "type": "string"
                    },
                    "message": {
                      "type": "string"
                    }
                  },
                  "additionalProperties": false,
                  "required": [
                    "statusCode",
                    "error",
                    "message"
                  ]
                }
              }
            }
          },
          "500": {
            "content": {
              "application/json; Elastic-Api-Version=2023-10-31": {
                "schema": {
                  "type": "object",
                  "description": "Internal Server Error",
                  "properties": {
                    "message": {
                      "type": "string"
                    }
                  },
                  "additionalProperties": false,
                  "required": [
                    "message"
                  ]
                }
              }
            }
          }
        },
        "parameters": [
          {
            "in": "header",
            "name": "elastic-api-version",
            "description": "The version of the API to use",
            "schema": {
              "type": "string",
              "enum": [
                "2023-10-31"
              ],
              "default": "2023-10-31"
            }
          },
          {
            "description": "A required header to protect against CSRF attacks",
            "in": "header",
            "name": "kbn-xsrf",
            "required": true,
            "schema": {
              "example": "true",
              "type": "string"
            }
          }
        ],
        "operationId": "%2Fapi%2Ffleet%2Fsetup#0"
      }
    }
  },
  "components": {
    "schemas": {},
    "securitySchemes": {
      "basicAuth": {
        "type": "http",
        "scheme": "basic"
      },
      "apiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "Authorization"
      }
    }
  },
  "security": [
    {
      "basicAuth": []
    }
  ],
  "tags": []
}
```
@juliaElastic juliaElastic added the release_note:skip Skip the PR/issue when compiling release notes label Sep 13, 2024
@juliaElastic juliaElastic self-assigned this Sep 13, 2024
@obltmachine
Copy link

🤖 GitHub comments

Expand to view the GitHub comments

Just comment with:

  • /oblt-deploy : Deploy a Kibana instance using the Observability test environments.
  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)

});

const validationResp = PackagePolicyResponseSchema.validate(responseItem);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this way I'm making sure the response schema works, trying to fill all fields in objects, including optional ones

@juliaElastic juliaElastic added the backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) label Sep 16, 2024
@juliaElastic juliaElastic marked this pull request as ready for review September 16, 2024 12:16
@juliaElastic juliaElastic requested a review from a team as a code owner September 16, 2024 12:16
@botelastic botelastic bot added the Team:Fleet Team label for Observability Data Collection Fleet team label Sep 16, 2024
@elasticmachine
Copy link
Contributor

Pinging @elastic/fleet (Team:Fleet)

@criamico
Copy link
Contributor

criamico commented Sep 17, 2024

@juliaElastic I'm testing this branch locally. The specs are correctly generated from dev tools, but I found some failures on package_policies.

  • Tried to edit an existing package policy and failed with this error
    Screenshot 2024-09-17 at 12 17 56

  • Tried to create a new package policy:
    Screenshot 2024-09-17 at 12 19 50

Let me know if you need more info. I'll keep testing other paths.

Note: from package_policies I also deleted one integration policy, upgraded one that had older version and ran dryRun and they all worked for me. As of now the only two endpoints that had issues are the above.

@criamico
Copy link
Contributor

I found another error in a case of dryRun - however it seems pretty similar to the edit above:

Screenshot 2024-09-17 at 12 46 01

This was an integration that I had already installed and it fails on this branch.

@juliaElastic
Copy link
Contributor Author

Tried to create a new package policy

Fixed the issue with the create package policy.
I can't reproduce the edit/dryrun error, which integration have you tried? I tested with akamai and endpoint.

@criamico
Copy link
Contributor

I can't reproduce the edit/dryrun error, which integration have you tried? I tested with akamai and endpoint.

it happened with an old version of custom logs. I'll pull the latest version and see if I can reproduce it again.

@criamico
Copy link
Contributor

criamico commented Sep 17, 2024

With your latest changes, I tested the edit of package policies and it works fine now.

The dryRun worked in one case but I tried again with custom log:

  • Navigate to app/integrations/detail/log-2.2.0/overview and install it (it's an older version), it works fine for me
  • Once the integration is installed the "upgrade" button appears

Screenshot 2024-09-17 at 14 52 19

  • Try to upgrade. For me it fails with this error:

Screenshot 2024-09-17 at 12 46 01

@juliaElastic
Copy link
Contributor Author

Thanks, I found the issue comes from some differences the custom logs has that the model object was not covering.
I'm not sure how can we prevent further issues like this, we would have to test with all packages.

image

@criamico
Copy link
Contributor

criamico commented Sep 17, 2024

I'm not sure how can we prevent further issues like this, we would have to test with all packages.

I was going to ask the same thing. Is there any way we can automate this?

Do you think that it can happen only with the dryRun handler or also with other ones? We should have some degree of confidence that shipping this change won't break many integrations.

@juliaElastic
Copy link
Contributor Author

juliaElastic commented Sep 17, 2024

I'm not sure how can we prevent further issues like this, we would have to test with all packages.

I was going to ask the same thing. Is there any way we can automate this?

Do you think that it can happen only with the dryRun handler or also with other ones? We should have some degree of confidence that shipping this change won't break many integrations.

I could write a script that creates package policies for all packages in EPR, it's only tricky where there are required fields to fill out to create successfully. I suppose errors can happen in any API where the package policy has different fields than defined in the model types.

Copy link
Contributor

@criamico criamico left a comment

Choose a reason for hiding this comment

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

Code LGTM

I tested a bit more and didn't find any obvious error, however I am not sure how we can avoid breakages of the package policies apis.

@kibana-ci
Copy link
Collaborator

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] Jest Tests #13 / GlobalDataTagsTable should add new tags

Metrics [docs]

✅ unchanged

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @juliaElastic

@juliaElastic
Copy link
Contributor Author

I'm running cypress tests locally to create a package policy for each package and check that it's successful. It takes a while, not seeing any other issues so far.

@juliaElastic juliaElastic merged commit 406f073 into elastic:main Sep 18, 2024
23 checks passed
kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Sep 18, 2024
## Summary

Relates elastic#184685

Readd elastic#192447 with fixes and tests
to validate that the response schemas are correct.

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

(cherry picked from commit 406f073)
@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.x

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Sep 18, 2024
…193296)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Fleet] use @kbn/config-schema in Fleet API (Part 1)
(#192883)](#192883)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Julia
Bardi","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-09-18T13:13:21Z","message":"[Fleet]
use @kbn/config-schema in Fleet API (Part 1) (#192883)\n\n##
Summary\r\n\r\nRelates
https://github.com/elastic/kibana/issues/184685\r\n\r\nReadd
#192447 with fixes and tests\r\nto
validate that the response schemas are correct.\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"406f07386ca6b35f375e8f22e682e44c101b1182","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","v9.0.0","backport:prev-minor","v8.16.0"],"title":"[Fleet]
use @kbn/config-schema in Fleet API (Part
1)","number":192883,"url":"https://github.com/elastic/kibana/pull/192883","mergeCommit":{"message":"[Fleet]
use @kbn/config-schema in Fleet API (Part 1) (#192883)\n\n##
Summary\r\n\r\nRelates
https://github.com/elastic/kibana/issues/184685\r\n\r\nReadd
#192447 with fixes and tests\r\nto
validate that the response schemas are correct.\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"406f07386ca6b35f375e8f22e682e44c101b1182"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/192883","number":192883,"mergeCommit":{"message":"[Fleet]
use @kbn/config-schema in Fleet API (Part 1) (#192883)\n\n##
Summary\r\n\r\nRelates
https://github.com/elastic/kibana/issues/184685\r\n\r\nReadd
#192447 with fixes and tests\r\nto
validate that the response schemas are correct.\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"406f07386ca6b35f375e8f22e682e44c101b1182"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Julia Bardi <[email protected]>
juliaElastic added a commit to juliaElastic/kibana that referenced this pull request Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) release_note:skip Skip the PR/issue when compiling release notes Team:Fleet Team label for Observability Data Collection Fleet team v8.16.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants