Skip to content

Commit 6d62d67

Browse files
authored
Initial support for content packages (#2049)
Add archetype for `elastic-package create package`. Add test package, tests pass, but they do nothing. Package installation seems to work, we will need to review related code in Kibana.
1 parent 5379fbf commit 6d62d67

File tree

15 files changed

+200
-5
lines changed

15 files changed

+200
-5
lines changed

cmd/create_package.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func createPackageCommandAction(cmd *cobra.Command, args []string) error {
4949
Name: "type",
5050
Prompt: &survey.Select{
5151
Message: "Package type:",
52-
Options: []string{"input", "integration"},
52+
Options: []string{"input", "integration", "content"},
5353
Default: "integration",
5454
},
5555
Validate: survey.Required,

internal/packages/archetype/_static/package-manifest.yml.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ conditions:
1616
version: "{{.Manifest.Conditions.Kibana.Version}}"
1717
elastic:
1818
subscription: "{{.Manifest.Conditions.Elastic.Subscription}}"
19+
{{- if eq .Manifest.Type "content" }}
20+
discovery:
21+
fields: []
22+
{{- end }}
1923
screenshots:
2024
- src: /img/sample-screenshot.png
2125
title: Sample screenshot
@@ -26,6 +30,7 @@ icons:
2630
title: Sample logo
2731
size: 32x32
2832
type: image/svg+xml
33+
{{- if (or (eq .Manifest.Type "integration") (eq .Manifest.Type "input")) }}
2934
policy_templates:
3035
{{- if eq .Manifest.Type "integration" }}
3136
- name: sample
@@ -72,6 +77,7 @@ policy_templates:
7277
- localhost
7378
{{ end }}
7479
{{ end -}}
80+
{{ end -}}
7581
{{ if .Manifest.Elasticsearch }}
7682
elasticsearch:
7783
{{ if .Manifest.Elasticsearch.SourceMode }}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
errors:
2+
exclude_checks:
3+
{{- range $check := .ExcludeChecks }}
4+
- {{ $check }}
5+
{{- end }}

internal/packages/archetype/package.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type PackageDescriptor struct {
2020
Manifest packages.PackageManifest
2121

2222
InputDataStreamType string
23+
24+
ExcludeChecks []string
2325
}
2426

2527
// CreatePackage function bootstraps the new package based on the provided descriptor.
@@ -92,6 +94,14 @@ func createPackageInDir(packageDescriptor PackageDescriptor, cwd string) error {
9294

9395
}
9496

97+
if len(packageDescriptor.ExcludeChecks) > 0 {
98+
logger.Debugf("Write validation file")
99+
err = renderResourceFile(validationBaseTemplate, &packageDescriptor, filepath.Join(baseDir, "validation.yml"))
100+
if err != nil {
101+
return fmt.Errorf("can't render validation file")
102+
}
103+
}
104+
95105
logger.Debugf("Format the entire package")
96106
err = formatter.Format(baseDir, false)
97107
if err != nil {

internal/packages/archetype/package_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"testing"
1010

11+
"github.com/Masterminds/semver/v3"
1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
1314

@@ -29,6 +30,10 @@ func TestPackage(t *testing.T) {
2930
pd := createPackageDescriptorForTest("input", "^8.9.0")
3031
createAndCheckPackage(t, pd, true)
3132
})
33+
t.Run("content-package", func(t *testing.T) {
34+
pd := createPackageDescriptorForTest("content", "^8.16.0")
35+
createAndCheckPackage(t, pd, true)
36+
})
3237
}
3338

3439
func createAndCheckPackage(t *testing.T, pd PackageDescriptor, valid bool) {
@@ -52,17 +57,28 @@ func createPackageDescriptorForTest(packageType, kibanaVersion string) PackageDe
5257
},
5358
}
5459
}
60+
version := "1.2.3"
5561
specVersion, err := GetLatestStableSpecVersion()
62+
excludeChecks := []string{}
5663
if err != nil {
5764
panic(err)
5865
}
66+
if packageType == "content" {
67+
minSpecVersion := semver.MustParse("3.4.0")
68+
if !specVersion.LessThan(minSpecVersion) {
69+
panic("this code can be removed")
70+
}
71+
version = "0.1.0"
72+
specVersion = *minSpecVersion
73+
excludeChecks = append(excludeChecks, "PSR00002")
74+
}
5975
return PackageDescriptor{
6076
Manifest: packages.PackageManifest{
6177
SpecVersion: specVersion.String(),
6278
Name: "go_unit_test_package",
6379
Title: "Go Unit Test Package",
6480
Type: packageType,
65-
Version: "1.2.3",
81+
Version: version,
6682
Conditions: packages.Conditions{
6783
Kibana: packages.KibanaConditions{
6884
Version: kibanaVersion,
@@ -79,12 +95,13 @@ func createPackageDescriptorForTest(packageType, kibanaVersion string) PackageDe
7995
Categories: []string{"aws", "custom"},
8096
Elasticsearch: elasticsearch,
8197
},
98+
ExcludeChecks: excludeChecks,
8299
InputDataStreamType: inputDataStreamType,
83100
}
84101
}
85102

86103
func checkPackage(t *testing.T, packageRoot string, valid bool) {
87-
err := validation.ValidateFromPath(packageRoot)
104+
err, _ := validation.ValidateAndFilterFromPath(packageRoot)
88105
if !valid {
89106
assert.Error(t, err)
90107
return

internal/packages/archetype/resources.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ var packageDocsReadme string
2020
//go:embed _static/fields-base.yml.tmpl
2121
var fieldsBaseTemplate string
2222

23+
//go:embed _static/package-validation.yml.tmpl
24+
var validationBaseTemplate string
25+
2326
// Images (logo and screenshot)
2427

2528
//go:embed _static/sampleIcon.svg

internal/packages/packages.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"io/fs"
1313
"os"
1414
"path/filepath"
15+
"slices"
1516

1617
"github.com/elastic/go-ucfg"
1718
"github.com/elastic/go-ucfg/yaml"
@@ -96,6 +97,16 @@ type Conditions struct {
9697
Elastic ElasticConditions `config:"elastic" json:"elastic" yaml:"elastic"`
9798
}
9899

100+
// Discovery define indications for the data this package can be useful with.
101+
type Discovery struct {
102+
Fields []DiscoveryField `config:"fields" json:"fields" yaml:"fields"`
103+
}
104+
105+
// DiscoveryField defines a field used for discovery.
106+
type DiscoveryField struct {
107+
Name string `config:"name" json:"name" yaml:"name"`
108+
}
109+
99110
// PolicyTemplate is a configuration of inputs responsible for collecting log or metric data.
100111
type PolicyTemplate struct {
101112
Name string `config:"name" json:"name" yaml:"name"` // Name of policy template.
@@ -130,6 +141,7 @@ type PackageManifest struct {
130141
Version string `config:"version" json:"version" yaml:"version"`
131142
Source Source `config:"source" json:"source" yaml:"source"`
132143
Conditions Conditions `config:"conditions" json:"conditions" yaml:"conditions"`
144+
Discovery Discovery `config:"discovery" json:"discovery" yaml:"discovery"`
133145
PolicyTemplates []PolicyTemplate `config:"policy_templates" json:"policy_templates" yaml:"policy_templates"`
134146
Vars []Variable `config:"vars" json:"vars" yaml:"vars"`
135147
Owner Owner `config:"owner" json:"owner" yaml:"owner"`
@@ -450,7 +462,12 @@ func isPackageManifest(path string) (bool, error) {
450462
if err != nil {
451463
return false, fmt.Errorf("reading package manifest failed (path: %s): %w", path, err)
452464
}
453-
return (m.Type == "integration" || m.Type == "input") && m.Version != "", nil
465+
supportedTypes := []string{
466+
"content",
467+
"input",
468+
"integration",
469+
}
470+
return slices.Contains(supportedTypes, m.Type) && m.Version != "", nil
454471
}
455472

456473
func isDataStreamManifest(path string) (bool, error) {

internal/testrunner/testrunner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ func PackageHasDataStreams(manifest *packages.PackageManifest) (bool, error) {
503503
switch manifest.Type {
504504
case "integration":
505505
return true, nil
506-
case "input":
506+
case "input", "content":
507507
return false, nil
508508
default:
509509
return false, fmt.Errorf("unexpected package type %q", manifest.Type)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
Elastic License 2.0
2+
3+
URL: https://www.elastic.co/licensing/elastic-license
4+
5+
## Acceptance
6+
7+
By using the software, you agree to all of the terms and conditions below.
8+
9+
## Copyright License
10+
11+
The licensor grants you a non-exclusive, royalty-free, worldwide,
12+
non-sublicensable, non-transferable license to use, copy, distribute, make
13+
available, and prepare derivative works of the software, in each case subject to
14+
the limitations and conditions below.
15+
16+
## Limitations
17+
18+
You may not provide the software to third parties as a hosted or managed
19+
service, where the service provides users with access to any substantial set of
20+
the features or functionality of the software.
21+
22+
You may not move, change, disable, or circumvent the license key functionality
23+
in the software, and you may not remove or obscure any functionality in the
24+
software that is protected by the license key.
25+
26+
You may not alter, remove, or obscure any licensing, copyright, or other notices
27+
of the licensor in the software. Any use of the licensor’s trademarks is subject
28+
to applicable law.
29+
30+
## Patents
31+
32+
The licensor grants you a license, under any patent claims the licensor can
33+
license, or becomes able to license, to make, have made, use, sell, offer for
34+
sale, import and have imported the software, in each case subject to the
35+
limitations and conditions in this license. This license does not cover any
36+
patent claims that you cause to be infringed by modifications or additions to
37+
the software. If you or your company make any written claim that the software
38+
infringes or contributes to infringement of any patent, your patent license for
39+
the software granted under these terms ends immediately. If your company makes
40+
such a claim, your patent license ends immediately for work on behalf of your
41+
company.
42+
43+
## Notices
44+
45+
You must ensure that anyone who gets a copy of any part of the software from you
46+
also gets a copy of these terms.
47+
48+
If you modify the software, you must include in any modified copies of the
49+
software prominent notices stating that you have modified the software.
50+
51+
## No Other Rights
52+
53+
These terms do not imply any licenses other than those expressly granted in
54+
these terms.
55+
56+
## Termination
57+
58+
If you use the software in violation of these terms, such use is not licensed,
59+
and your licenses will automatically terminate. If the licensor provides you
60+
with a notice of your violation, and you cease all violation of this license no
61+
later than 30 days after you receive that notice, your licenses will be
62+
reinstated retroactively. However, if you violate these terms after such
63+
reinstatement, any additional violation of these terms will cause your licenses
64+
to terminate automatically and permanently.
65+
66+
## No Liability
67+
68+
*As far as the law allows, the software comes as is, without any warranty or
69+
condition, and the licensor will not be liable to you for any damages arising
70+
out of these terms or the use or nature of the software, under any kind of
71+
legal claim.*
72+
73+
## Definitions
74+
75+
The **licensor** is the entity offering these terms, and the **software** is the
76+
software the licensor makes available under these terms, including any portion
77+
of it.
78+
79+
**you** refers to the individual or entity agreeing to these terms.
80+
81+
**your company** is any legal entity, sole proprietorship, or other kind of
82+
organization that you work for, plus all organizations that have control over,
83+
are under the control of, or are under common control with that
84+
organization. **control** means ownership of substantially all the assets of an
85+
entity, or the power to direct its management and policies by vote, contract, or
86+
otherwise. Control can be direct or indirect.
87+
88+
**your licenses** are all the licenses granted to you for the software under
89+
these terms.
90+
91+
**use** means anything you do with the software requiring one of your licenses.
92+
93+
**trademark** means trademarks, service marks, and similar rights.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# newer versions go on top
2+
- version: "0.0.1"
3+
changes:
4+
- description: Initial draft of the package
5+
type: enhancement
6+
link: https://github.com/elastic/integrations/pull/1 # FIXME Replace with the real PR link

0 commit comments

Comments
 (0)