Skip to content

Commit 29d0efc

Browse files
authored
Validate built packages too (#720)
Built packages can have content that was not in their source files, such as imported ECS fields. Validate final packages to ensure that content included during build is also valid.
1 parent a7d48cd commit 29d0efc

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

cmd/lint.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@ func setupLintCommand() *cobraext.Command {
2424
Use: "lint",
2525
Short: "Lint the package",
2626
Long: lintLongDescription,
27-
RunE: lintCommandAction,
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
err := cobraext.ComposeCommandActions(cmd, args,
29+
lintCommandAction,
30+
validateSourceCommandAction,
31+
)
32+
if err != nil {
33+
return err
34+
}
35+
cmd.Println("Done")
36+
return nil
37+
},
2838
}
2939

3040
return cobraext.NewCommand(cmd, cobraext.ContextPackage)
@@ -33,14 +43,6 @@ func setupLintCommand() *cobraext.Command {
3343
func lintCommandAction(cmd *cobra.Command, args []string) error {
3444
cmd.Println("Lint the package")
3545

36-
packageRootPath, found, err := packages.FindPackageRoot()
37-
if !found {
38-
return errors.New("package root not found")
39-
}
40-
if err != nil {
41-
return errors.Wrap(err, "locating package root failed")
42-
}
43-
4446
readmeFiles, err := docs.AreReadmesUpToDate()
4547
if err != nil {
4648
for _, f := range readmeFiles {
@@ -53,12 +55,21 @@ func lintCommandAction(cmd *cobra.Command, args []string) error {
5355
}
5456
return errors.Wrap(err, "checking readme files are up-to-date failed")
5557
}
58+
return nil
59+
}
5660

61+
func validateSourceCommandAction(cmd *cobra.Command, args []string) error {
62+
packageRootPath, found, err := packages.FindPackageRoot()
63+
if !found {
64+
return errors.New("package root not found")
65+
}
66+
if err != nil {
67+
return errors.Wrap(err, "locating package root failed")
68+
}
5769
err = validator.ValidateFromPath(packageRootPath)
5870
if err != nil {
5971
return errors.Wrap(err, "linting package failed")
6072
}
6173

62-
cmd.Println("Done")
6374
return nil
6475
}

internal/builder/packages.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import (
99
"os"
1010
"path/filepath"
1111

12+
"github.com/elastic/package-spec/code/go/pkg/validator"
13+
"github.com/pkg/errors"
14+
1215
"github.com/elastic/elastic-package/internal/files"
1316
"github.com/elastic/elastic-package/internal/logger"
1417
"github.com/elastic/elastic-package/internal/packages"
15-
16-
"github.com/pkg/errors"
1718
)
1819

1920
const buildIntegrationsFolder = "integrations"
@@ -166,10 +167,18 @@ func BuildPackage(options BuildOptions) (string, error) {
166167
return "", errors.Wrap(err, "resolving external fields failed")
167168
}
168169

169-
if !options.CreateZip {
170-
return destinationDir, nil
170+
if options.CreateZip {
171+
return buildZippedPackage(options, destinationDir)
171172
}
172173

174+
err = validator.ValidateFromPath(destinationDir)
175+
if err != nil {
176+
return "", errors.Wrap(err, "invalid content found in built package")
177+
}
178+
return destinationDir, nil
179+
}
180+
181+
func buildZippedPackage(options BuildOptions, destinationDir string) (string, error) {
173182
logger.Debug("Build zipped package")
174183
zippedPackagePath, err := buildPackagesZipPath(options.PackageRoot)
175184
if err != nil {
@@ -181,24 +190,36 @@ func BuildPackage(options BuildOptions) (string, error) {
181190
return "", errors.Wrapf(err, "can't compress the built package (compressed file path: %s)", zippedPackagePath)
182191
}
183192

184-
if !options.SignPackage {
185-
return zippedPackagePath, nil
193+
err = validator.ValidateFromZip(zippedPackagePath)
194+
if err != nil {
195+
return "", errors.Wrapf(err, "invalid content found in built zip package")
186196
}
187197

198+
if options.SignPackage {
199+
err := signZippedPackage(options, zippedPackagePath)
200+
if err != nil {
201+
return "", err
202+
}
203+
}
204+
205+
return zippedPackagePath, nil
206+
}
207+
208+
func signZippedPackage(options BuildOptions, zippedPackagePath string) error {
188209
logger.Debug("Sign the package")
189210
m, err := packages.ReadPackageManifestFromPackageRoot(options.PackageRoot)
190211
if err != nil {
191-
return "", errors.Wrapf(err, "reading package manifest failed (path: %s)", options.PackageRoot)
212+
return errors.Wrapf(err, "reading package manifest failed (path: %s)", options.PackageRoot)
192213
}
193214

194215
err = files.Sign(zippedPackagePath, files.SignOptions{
195216
PackageName: m.Name,
196217
PackageVersion: m.Version,
197218
})
198219
if err != nil {
199-
return "", errors.Wrapf(err, "can't sign the zipped package (path: %s)", zippedPackagePath)
220+
return errors.Wrapf(err, "can't sign the zipped package (path: %s)", zippedPackagePath)
200221
}
201-
return zippedPackagePath, nil
222+
return nil
202223
}
203224

204225
func createBuildDirectory(dirs ...string) (string, error) {

0 commit comments

Comments
 (0)