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] Fixes to preconfigure API #96094

Merged
merged 6 commits into from
Apr 7, 2021

Conversation

Zacqary
Copy link
Contributor

@Zacqary Zacqary commented Apr 1, 2021

Summary

Closes #95788

  • Fixes passing the is_managed flag when creating a new policy
  • Fixes passing the is_default and is_default_fleet_server flags
  • Adds the force flag to the packages schema instead of passing it by default; this restores the ability for the package registry to throw an error when trying to install and outdated package, unless the user passes force: true intentionally
  • Reconciles the preconfiguration API's naming convention for versioned packages, splitting the package name with - instead of :

Testing examples

Send a PUT to /api/fleet/preconfiguration with the following payloads to test different scenarios:

force flag

{
   "packages": [
      {
         "name": "apache",
         "version": "0.3.4",
         "force": true
      }
   ]
}

Also remove force: true and ensure that this request fails.

is_managed

{
    "agentPolicies": [
        {
            "name": "Preconfigured Managed Policy",
            "id": 1,
            "namespace": "test",
            "is_managed": true,
            "package_policies": [
                {
                    "package": {
                        "name": "system"
                    },
                    "name": "System Integration"
                }
            ]
        }
    ]
}

After the policy is created, go to the policy in the Fleet UI and try to add an integration. Ensure that it throws an error related to being a managed policy.

is_default/is_default_fleet_server

Before running this, restart yarn es snapshot so that you're working with a fresh cluster. Then run this request BEFORE opening the Fleet app for the first time.

{
    "agentPolicies": [
        {
            "name": "Preconfigured Default Policy",
            "id": 1,
            "namespace": "test",
            "is_default": true,
            "package_policies": [
                {
                    "package": {
                        "name": "system"
                    },
                    "name": "System Integration"
                }
            ]
        },
        {
          "name": "Preconfigured Default Fleet Policy",
          "id": 1,
          "namespace": "test",
          "is_default_fleet_server": true,
          "package_policies": [
              {
                  "package": {
                      "name": "system"
                  },
                  "name": "System Integration"
              }
          ]
      }
    ]
}

After running this, open Fleet and ensure that these are the only two policies created, instead of Fleet creating new Default policies.

Confirm by running this test scenario with only one of the Default or Default Fleet Server policies. When Fleet initializes, it should create a default policy for whichever one you did not preconfigure.

Checklist

@Zacqary Zacqary added v8.0.0 release_note:skip Skip the PR/issue when compiling release notes Feature:Fleet Fleet team's agent central management project Team:Fleet Team label for Observability Data Collection Fleet team v7.13.0 labels Apr 1, 2021
@Zacqary Zacqary requested a review from a team as a code owner April 1, 2021 19:58
@elasticmachine
Copy link
Contributor

Pinging @elastic/fleet (Feature:Fleet)

@elasticmachine
Copy link
Contributor

Pinging @elastic/fleet (Team:Fleet)

@Zacqary Zacqary self-assigned this Apr 1, 2021
Copy link
Contributor

@ruflin ruflin left a comment

Choose a reason for hiding this comment

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

Can you share a full json example with all the flags in the PR description? This will help to have it directly documented and makes testing easier.

@@ -45,7 +45,7 @@ export async function ensurePreconfiguredPackagesAndPolicies(
// If there are multiple packages with duplicate versions, separate them with semicolons, e.g
// package-a:1.0.0, package-a:2.0.0; package-b:1.0.0, package-b:2.0.0
const duplicateList = duplicatePackages
.map(([, versions]) => versions.map((v) => `${v.name}:${v.version}`).join(', '))
.map(([, versions]) => versions.map((v) => `${v.name}-${v.version}`).join(', '))
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm starting to wonder if what you have with the : is actually better. I know I used the - in some of my tests but : looks pretty intuitive. What is the reason you changed it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

- is used everywhere else in the codebase, such as this example in EPM's package registry: https://github.com/elastic/kibana/blob/master/x-pack/plugins/fleet/server/services/epm/registry/index.ts#L65

I like : better too but I think consistency is the most important.

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we mix two things here. The part you linked is the name of the .zip file I think. The reason we have - there is because it is pretty uncomment to have : in a file name. So it is apache-1.2.3.zip instead of apache:1.2.3.zip as file name. We also copied here what is used for the other stack downloads. I would think for configuration we could use the :. But as packages are not allowed to have any - in the name it is also something we could change later on and adjust all the places where it is used. So good to move forward with your change 👍

Copy link
Contributor

@jfsiii jfsiii left a comment

Choose a reason for hiding this comment

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

Haven't run locally, but it reads well & has tests. Left two comments but they can be added anytime. Giving a 🚢 if you need it.

@@ -45,7 +45,7 @@ export async function ensurePreconfiguredPackagesAndPolicies(
// If there are multiple packages with duplicate versions, separate them with semicolons, e.g
// package-a:1.0.0, package-a:2.0.0; package-b:1.0.0, package-b:2.0.0
const duplicateList = duplicatePackages
.map(([, versions]) => versions.map((v) => `${v.name}:${v.version}`).join(', '))
.map(([, versions]) => versions.map((v) => `${v.name}-${v.version}`).join(', '))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can use pkgToPkgKey

export const pkgToPkgKey = ({ name, version }: { name: string; version: string }) =>
to ensure the correct value and avoid the duplication

@@ -123,7 +137,7 @@ export async function ensurePreconfiguredPackagesAndPolicies(
id: p.policy.id,
updated_at: p.policy.updated_at,
})),
packages: preconfiguredPackages.map((pkg) => `${pkg.name}:${pkg.version}`),
packages: preconfiguredPackages.map((pkg) => `${pkg.name}-${pkg.version}`),
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment re: pkgToPkgKey

export const pkgToPkgKey = ({ name, version }: { name: string; version: string }) =>

Copy link
Contributor

@ruflin ruflin left a comment

Choose a reason for hiding this comment

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

I tested a few scenarios and it does what it should.

One thing I stumbled over this that I set is_managed but then was still able to delete the policy. Not sure if this is related to this PR or something still being worked on. @jfsiii probably knows.

@jfsiii
Copy link
Contributor

jfsiii commented Apr 7, 2021

@ruflin managed policies should not be able to be deleted

describe('POST /api/fleet/agent_policies/delete', () => {

Can you add more detail about the steps you took so Zacqary or I can confirm/fix?

@ruflin
Copy link
Contributor

ruflin commented Apr 7, 2021

@jfsiii Tried to take it out of my copy / paste history and did not run it yet again. But I think that is the command I used:

curl -X PUT http://elastic:changeme@localhost:5601/api/fleet/setup/preconfiguration -H "kbn-xsrf: foo" -H "Content-Type:application/json" -d'
{
    "agentPolicies": [
        {
            "name": "Fun is fun",
            "id": "foo-bar2",
            "namespace": "test",
            "is_managed": true,
            "package_policies": [
                {
                    "package": {
                        "name": "system"
                    },
                    "name": "System Integration"
                }
            ]
        }
    ]
}'

@jfsiii
Copy link
Contributor

jfsiii commented Apr 7, 2021

@ruflin thanks. That would create the policy. How did you delete it? Via the UI or another curl command?

@ruflin
Copy link
Contributor

ruflin commented Apr 7, 2021

I used the UI.

@Zacqary
Copy link
Contributor Author

Zacqary commented Apr 7, 2021

@ruflin Even though you were able to delete the policy, did it still prevent you from adding a new integration from the UI? I just want to make sure the is_managed flag is actually being applied.

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

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

cc @Zacqary

@Zacqary
Copy link
Contributor Author

Zacqary commented Apr 7, 2021

@ruflin Just tested and I wasn't able to reproduce your issue. Deleting a managed preconfigured policy threw a Cannot delete managed policy <uuid> error.

@Zacqary Zacqary merged commit c8e23ad into elastic:master Apr 7, 2021
@Zacqary Zacqary deleted the 95788-preconfigure-fixes branch April 7, 2021 18:07
Zacqary added a commit to Zacqary/kibana that referenced this pull request Apr 7, 2021
@ruflin
Copy link
Contributor

ruflin commented Apr 7, 2021

@Zacqary Probably an error on my end. I'll try again on master.

@ruflin
Copy link
Contributor

ruflin commented Apr 7, 2021

I tested again and indeed I get the error. What confused me was that the Delete button was available. In contrast on the Default policy is greyed out. We should update the UI to also grey out the button.

@Zacqary
Copy link
Contributor Author

Zacqary commented Apr 7, 2021

@ruflin Tracking that here: #96492

@jfsiii
Copy link
Contributor

jfsiii commented Apr 7, 2021

@ruflin I am working on that right now #96492 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:Fleet Fleet team's agent central management project release_note:skip Skip the PR/issue when compiling release notes Team:Fleet Team label for Observability Data Collection Fleet team v7.13.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Fleet] Follow up to preconfigure API
5 participants