Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/export/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func applyTransformations(ctx *transformationContext, objects []common.MapStr) (
decodeObject,
stripObjectProperties,
standardizeObjectProperties,
removeFleetManagedTags,
removeFleetTags,
standardizeObjectID).
transform(objects)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ package export

import (
"fmt"
"strings"

"github.com/elastic/elastic-package/internal/common"
)

func removeFleetManagedTags(ctx *transformationContext, object common.MapStr) (common.MapStr, error) {
// removeFleetTags removes fleet managed and shared tags from the given Kibana object.
func removeFleetTags(ctx *transformationContext, object common.MapStr) (common.MapStr, error) {
aType, err := object.GetValue("type")
if err != nil {
return nil, fmt.Errorf("failed to read type field: %w", err)
Expand Down Expand Up @@ -38,6 +40,11 @@ func removeTagReferences(ctx *transformationContext, object common.MapStr) (comm
return nil, err
}

newReferences, err = filterOutSharedTags(ctx, newReferences)
if err != nil {
return nil, err
}

_, err = object.Put("references", newReferences)
if err != nil {
return nil, fmt.Errorf("can't update references: %w", err)
Expand All @@ -64,6 +71,9 @@ func removeTagObjects(ctx *transformationContext, object common.MapStr) (common.
if isTagFleetManaged(aIdString, ctx.packageName) {
return nil, nil
}
if isSharedTag(aIdString, ctx.packageName) {
return nil, nil
}
return object, nil
}

Expand Down Expand Up @@ -111,3 +121,40 @@ func filterOutFleetManagedTags(ctx *transformationContext, references []interfac
}
return newReferences, nil
}

// isSharedTag checks if the given tag ID is a shared tag for the specified package.
// Shared tags ids are created by fleet form the tags.yml file in the package.
// https://github.com/elastic/kibana/blob/5385f96a132114362b2542e6a44c96a697b66c28/x-pack/platform/plugins/shared/fleet/server/services/epm/kibana/assets/tag_assets.ts#L67
func isSharedTag(aId string, packageName string) bool {
defaultSharedTagTemplate := "fleet-shared-tag-%s"
securitySolutionTagTemplate := "%s-security-solution"

return strings.Contains(aId, fmt.Sprintf(defaultSharedTagTemplate, packageName)) ||
strings.Contains(aId, fmt.Sprintf(securitySolutionTagTemplate, packageName))
}

func filterOutSharedTags(ctx *transformationContext, references []interface{}) ([]interface{}, error) {
newReferences := make([]interface{}, 0)
for _, r := range references {
reference := r.(map[string]interface{})

aType, ok := reference["type"]
if !ok {
continue
}
if aType != "tag" {
newReferences = append(newReferences, r)
continue
}

aId, ok := reference["id"].(string)
if !ok {
return nil, fmt.Errorf("failed to assert name as a string: %v", reference["id"])
}
if isSharedTag(aId, ctx.packageName) {
continue
}
newReferences = append(newReferences, r)
}
return newReferences, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestRemoveFleetTagsDashboard(t *testing.T) {
packageName: "elastic_package_registry",
}

result, err := removeFleetManagedTags(ctx, given)
result, err := removeFleetTags(ctx, given)
require.NoError(t, err)

resultJson, err := json.MarshalIndent(&result, "", " ")
Expand Down Expand Up @@ -55,6 +55,16 @@ func TestRemoveFleetTagsObjects(t *testing.T) {
objectFile: "./testdata/elastic_package_registry.random_tag.json",
expectedRemoved: false,
},
{
title: "Shared tag - default",
objectFile: "./testdata/elastic_package_registry.shared_tag_default.json",
expectedRemoved: true,
},
{
title: "Shared tag - security solution",
objectFile: "./testdata/elastic_package_registry.shared_tag_security_solution.json",
expectedRemoved: true,
},
}

for _, c := range cases {
Expand All @@ -70,7 +80,7 @@ func TestRemoveFleetTagsObjects(t *testing.T) {
packageName: "elastic_package_registry",
}

result, err := removeFleetManagedTags(ctx, given)
result, err := removeFleetTags(ctx, given)
require.NoError(t, err)

if c.expectedRemoved {
Expand Down
12 changes: 11 additions & 1 deletion internal/export/testdata/elastic_package_registry.dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,17 @@
"id": "elastic_package_registry-54252400-d309-11ed-97de-b7062e02194f",
"name": "tag-ref-54252400-d309-11ed-97de-b7062e02194f",
"type": "tag"
}
},
{
"id": "elastic_package_registry-fleet-shared-tag-elastic_package_registry",
"name": "tag-ref-fleet-shared-tag-default",
"type": "tag"
},
{
"id": "elastic_package_registry-security-solution-default",
"name": "tag-ref-security-solution-default",
"type": "tag"
}
],
"type": "dashboard"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"attributes": {
"color": "#FFFFFF",
"description": "",
"name": "Managed"
},
"coreMigrationVersion": "8.7.0",
"created_at": "2023-04-04T16:50:46.882Z",
"id": "elastic_package_registry-fleet-shared-tag-elastic_package_registry-default",
"migrationVersion": {
"tag": "8.0.0"
},
"references": [],
"type": "tag"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"attributes": {
"color": "#FFFFFF",
"description": "",
"name": "Managed"
},
"coreMigrationVersion": "8.7.0",
"created_at": "2023-04-04T16:50:46.882Z",
"id": "elastic_package_registry-security-solution-default",
"migrationVersion": {
"tag": "8.0.0"
},
"references": [],
"type": "tag"
}