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

Add validation rule to disallow dangling object IDs #589

Merged
merged 9 commits into from
Sep 12, 2023

Conversation

mrodm
Copy link
Contributor

@mrodm mrodm commented Sep 11, 2023

What does this PR do?

This PR adds a new validation rule to be applied in v3 to check dangling object IDs.

Why is it important?

This new validation rule ensures that all references listed in dashboards or other assets are valid ones. That means that there is an asset file with the same ID and type in the package.

Example of the error reported using elastic-package:

 $ elastic-package check 
Format the package
Done
Lint the package
2023/09/11 16:40:52 Warning: package using an unreleased version of the spec (3.0.0-next)
Error: checking package failed: linting package failed: found 1 validation error:
   1. file "/home/user/Coding/work/package-spec/test/packages/bad_dangling_object_ids/kibana/dashboard/bad_dangling_object_ids-82273ffe-6acc-4f2f-bbee-c1004abba63d.json" is invalid: dangling reference found: bad_dangling_object_ids-8287a5d5-1576-4f3a-83c4-444e9058439c (search)

IMPORTANT: Excluded index-pattern references from this check.

Errors found if this validation rule would be applied to all packages (mainly dangling reference of tags):

  • Package gcp:
     1. file "/home/mariorodriguez/Coding/work/integrations/packages/gcp/kibana/dashboard/gcp-1ae960c0-f9f8-11eb-bc38-79936db7c106.json" is invalid: dangling reference found: gcp-fleet-managed-default (tag)
     2. file "/home/mariorodriguez/Coding/work/integrations/packages/gcp/kibana/dashboard/gcp-1ae960c0-f9f8-11eb-bc38-79936db7c106.json" is invalid: dangling reference found: gcp-fleet-pkg-gcp-default (tag)
     3. file "/home/mariorodriguez/Coding/work/integrations/packages/gcp/kibana/dashboard/gcp-1ae960c0-f9f8-11eb-bc38-79936db7c106.json" is invalid: dangling reference found: gcp-fleet-managed-default (tag)
     4. file "/home/mariorodriguez/Coding/work/integrations/packages/gcp/kibana/dashboard/gcp-1ae960c0-f9f8-11eb-bc38-79936db7c106.json" is invalid: dangling reference found: gcp-fleet-pkg-gcp-default (tag)
     5. file "/home/mariorodriguez/Coding/work/integrations/packages/gcp/kibana/dashboard/gcp-2b0fd7b0-feac-11ea-b032-d59f894a5072.json" is invalid: dangling reference found: gcp-fleet-managed-default (tag)
     6. file "/home/mariorodriguez/Coding/work/integrations/packages/gcp/kibana/dashboard/gcp-2b0fd7b0-feac-11ea-b032-d59f894a5072.json" is invalid: dangling reference found: gcp-fleet-pkg-gcp-default (tag)
     7. file "/home/mariorodriguez/Coding/work/integrations/packages/gcp/kibana/dashboard/gcp-ca401040-8e52-11ea-9fa6-4d675d5290dc.json" is invalid: dangling reference found: gcp-fleet-managed-default (tag)
     8. file "/home/mariorodriguez/Coding/work/integrations/packages/gcp/kibana/dashboard/gcp-ca401040-8e52-11ea-9fa6-4d675d5290dc.json" is invalid: dangling reference found: gcp-fleet-pkg-gcp-default (tag)
     9. file "/home/mariorodriguez/Coding/work/integrations/packages/gcp/kibana/dashboard/gcp-f40ee870-5e4a-11ea-a4f6-717338406083.json" is invalid: dangling reference found: gcp-fleet-managed-default (tag)
    10. file "/home/mariorodriguez/Coding/work/integrations/packages/gcp/kibana/dashboard/gcp-f40ee870-5e4a-11ea-a4f6-717338406083.json" is invalid: dangling reference found: gcp-fleet-pkg-gcp-default (tag)
    11. file "/home/mariorodriguez/Coding/work/integrations/packages/gcp/kibana/dashboard/gcp-f40ee870-5e4a-11ea-a4f6-717338406083.json" is invalid: dangling reference found: gcp-fleet-managed-default (tag)
    12. file "/home/mariorodriguez/Coding/work/integrations/packages/gcp/kibana/dashboard/gcp-f40ee870-5e4a-11ea-a4f6-717338406083.json" is invalid: dangling reference found: gcp-fleet-pkg-gcp-default (tag)
    
  • Package istio:
    1. file "/home/mariorodriguez/Coding/work/integrations/packages/istio/kibana/dashboard/istio-fed6f500-5941-11ed-bfb8-bbfe5b338339.json" is invalid: dangling reference found: fleet-managed-default (tag)
    2. file "/home/mariorodriguez/Coding/work/integrations/packages/istio/kibana/dashboard/istio-fed6f500-5941-11ed-bfb8-bbfe5b338339.json" is invalid: dangling reference found: fleet-pkg-istio-default (tag)
    
  • Package system:
     1. file "/home/mariorodriguez/Coding/work/integrations/packages/system/kibana/dashboard/system-0d3f2380-fa78-11e6-ae9b-81e5311e8cab.json" is invalid: dangling reference found: system-fleet-pkg-system-default (tag)
     2. file "/home/mariorodriguez/Coding/work/integrations/packages/system/kibana/dashboard/system-277876d0-fa2c-11e6-bbd3-29c986c96e5a.json" is invalid: dangling reference found: system-fleet-pkg-system-default (tag)
     4. file "/home/mariorodriguez/Coding/work/integrations/packages/system/kibana/dashboard/system-5517a150-f9ce-11e6-8115-a7c18106d86a.json" is invalid: dangling reference found: system-fleet-pkg-system-default (tag)
     5. file "/home/mariorodriguez/Coding/work/integrations/packages/system/kibana/dashboard/system-79ffd6e0-faa0-11e6-947f-177f697178b8.json" is invalid: dangling reference found: system-fleet-pkg-system-default (tag)
     6. file "/home/mariorodriguez/Coding/work/integrations/packages/system/kibana/dashboard/system-Logs-syslog-dashboard.json" is invalid: dangling reference found: system-fleet-pkg-system-default (tag)
     7. file "/home/mariorodriguez/Coding/work/integrations/packages/system/kibana/dashboard/system-Metrics-system-overview.json" is invalid: dangling reference found: system-fleet-pkg-system-default (tag)
     8. file "/home/mariorodriguez/Coding/work/integrations/packages/system/kibana/search/system-62439dc0-f9c9-11e6-a747-6121780e0414.json" is invalid: dangling reference found: system-fleet-pkg-system-default (tag)
     9. file "/home/mariorodriguez/Coding/work/integrations/packages/system/kibana/search/system-8030c1b0-fa77-11e6-ae9b-81e5311e8cab.json" is invalid: dangling reference found: system-fleet-pkg-system-default (tag)
    10. file "/home/mariorodriguez/Coding/work/integrations/packages/system/kibana/search/system-Syslog-system-logs.json" is invalid: dangling reference found: system-fleet-pkg-system-default (tag)
    11. file "/home/mariorodriguez/Coding/work/integrations/packages/system/kibana/search/system-b6f321e0-fa25-11e6-bbd3-29c986c96e5a.json" is invalid: dangling reference found: system-fleet-pkg-system-default (tag)
    12. file "/home/mariorodriguez/Coding/work/integrations/packages/system/kibana/search/system-eb0039f0-fa7f-11e6-a1df-a78bd7504d38.json" is invalid: dangling reference found: system-fleet-pkg-system-default (tag)
    
  • Package airflow:
    1. file "/home/mariorodriguez/Coding/work/integrations/packages/airflow/kibana/dashboard/airflow-a3aa42d0-a465-11ed-9ff0-ab4dd59e4c75.json" is invalid: dangling reference found: airflow-fleet-managed-default (tag)
    2. file "/home/mariorodriguez/Coding/work/integrations/packages/airflow/kibana/dashboard/airflow-a3aa42d0-a465-11ed-9ff0-ab4dd59e4c75.json" is invalid: dangling reference found: airflow-fleet-pkg-airflow-default (tag)
    

Checklist

Related issues

Update error message to follow the same pattern as other validation
errors
@mrodm mrodm self-assigned this Sep 11, 2023
@mrodm
Copy link
Contributor Author

mrodm commented Sep 11, 2023

test integrations

@elasticmachine
Copy link

Created or updated PR in integrations repostiory to test this vesrion. Check elastic/integrations#7758

@mrodm mrodm marked this pull request as ready for review September 11, 2023 17:40
@mrodm mrodm requested a review from a team as a code owner September 11, 2023 17:40
@jsoriano
Copy link
Member

Is it possible that it is ok to reference these fleet managed tags?

@mrodm mrodm changed the title Add validation dangling objects Add validation rule to disallow dangling object IDs Sep 11, 2023
@@ -133,6 +133,7 @@ func (s Spec) rules(pkgType string, rootSpec spectypes.ItemSpec) validationRules
{fn: semantic.ValidateProfilingNonGA, types: []string{"integration"}},
{fn: semantic.ValidateKibanaObjectIDs, types: []string{"integration"}},
{fn: semantic.ValidateRoutingRulesAndDataset, types: []string{"integration"}, since: semver.MustParse("2.9.0")},
{fn: semantic.ValidateKibanaNoDanglingObjectIDs, since: semver.MustParse("3.0.0")},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to be applied since v3.0.0

@mrodm mrodm mentioned this pull request Sep 12, 2023
@mrodm
Copy link
Contributor Author

mrodm commented Sep 12, 2023

Is it possible that it is ok to reference these fleet managed tags?

@jsoriano Not sure about this, in case of elastic-package when it exports dashboards it was set to remove those references from the dashboard too in this PR elastic/elastic-package#1206.

Currently, if it is tried to export those dashboards (for instance from airflow package), it is raised this error (as a note the installation does not fail):

 $ elastic-package-v0.86.1 export dashboards --allow-snapshot
Export Kibana dashboards
Warning: exporting dashboards from a SNAPSHOT version of Kibana (8.10.0-SNAPSHOT) is discouraged. It could lead to invalid dashboards (for example if they use features that are reverted or modified before the final release)
? Which dashboards would you like to export? [Metrics Airflow] Overview (ID: airflow-a3aa42d0-a465-11ed-9ff0-ab4dd59e4c75)
Error: dashboards export failed: exporting dashboards using Kibana client failed: at least Kibana object returned an error: [0] Saved object [tag/airflow-fleet-managed-default] not found
[1] Saved object [tag/airflow-fleet-pkg-airflow-default] not found

Probably because elastic-package also adds the package name as part of one of their transform operations: https://github.com/elastic/elastic-package/blob/e1157add2447f28f135fdb055aa396fce8f9c3b5/internal/export/transform_standardize.go#L128-L141

Those fleet tags are always created when the package is installed ("managed" and "<package_name>").

jsoriano
jsoriano previously approved these changes Sep 12, 2023
Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

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

👍

Comment on lines 69 to 79
found := false
for _, installed := range installedIDs {
if reference.objectID != installed.objectID {
continue
}
if reference.objectType != installed.objectType {
continue
}

found = true
}
Copy link
Member

Choose a reason for hiding this comment

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

Nit, as the struct seems to be comparable, maybe you can use slices.Contains.

Suggested change
found := false
for _, installed := range installedIDs {
if reference.objectID != installed.objectID {
continue
}
if reference.objectType != installed.objectType {
continue
}
found = true
}
found := slices.Contains(installedIDs, reference)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll check it, but there is a third parameter filePath in the struct that has not the same value between installedIDs and referencedIDs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to use slice.ContainsFunc to use those specific conditions.

Using slice.Conditions make the tests fail.

}

var references []reference
for _, reference := range allReferences {
Copy link
Member

Choose a reason for hiding this comment

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

Nit. Also here maybe you can use some slices helper.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to use slices.Contains 👍

@@ -1,3 +1,4 @@
{
"id": "good-dashboard-abc-1"
"id": "good-dashboard-abc-1",
"type": "dashboard"
Copy link
Member

Choose a reason for hiding this comment

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

Why these files didn't have types? Were they crafted files for tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I think those assets were crafted with the minimum values for the tests. At that moment, it was just the id field.

@mrodm mrodm requested a review from jsoriano September 12, 2023 14:34
jsoriano
jsoriano previously approved these changes Sep 12, 2023
Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

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

👍

@elasticmachine
Copy link

💚 Build Succeeded

History

cc @mrodm

@mrodm mrodm merged commit 0839924 into elastic:main Sep 12, 2023
@mrodm mrodm deleted the add_validation_dangling_objects branch September 12, 2023 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants