Skip to content

Commit

Permalink
Validate built packages too (#720)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jsoriano authored Mar 14, 2022
1 parent a7d48cd commit 29d0efc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
31 changes: 21 additions & 10 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ func setupLintCommand() *cobraext.Command {
Use: "lint",
Short: "Lint the package",
Long: lintLongDescription,
RunE: lintCommandAction,
RunE: func(cmd *cobra.Command, args []string) error {
err := cobraext.ComposeCommandActions(cmd, args,
lintCommandAction,
validateSourceCommandAction,
)
if err != nil {
return err
}
cmd.Println("Done")
return nil
},
}

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

packageRootPath, found, err := packages.FindPackageRoot()
if !found {
return errors.New("package root not found")
}
if err != nil {
return errors.Wrap(err, "locating package root failed")
}

readmeFiles, err := docs.AreReadmesUpToDate()
if err != nil {
for _, f := range readmeFiles {
Expand All @@ -53,12 +55,21 @@ func lintCommandAction(cmd *cobra.Command, args []string) error {
}
return errors.Wrap(err, "checking readme files are up-to-date failed")
}
return nil
}

func validateSourceCommandAction(cmd *cobra.Command, args []string) error {
packageRootPath, found, err := packages.FindPackageRoot()
if !found {
return errors.New("package root not found")
}
if err != nil {
return errors.Wrap(err, "locating package root failed")
}
err = validator.ValidateFromPath(packageRootPath)
if err != nil {
return errors.Wrap(err, "linting package failed")
}

cmd.Println("Done")
return nil
}
39 changes: 30 additions & 9 deletions internal/builder/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"os"
"path/filepath"

"github.com/elastic/package-spec/code/go/pkg/validator"
"github.com/pkg/errors"

"github.com/elastic/elastic-package/internal/files"
"github.com/elastic/elastic-package/internal/logger"
"github.com/elastic/elastic-package/internal/packages"

"github.com/pkg/errors"
)

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

if !options.CreateZip {
return destinationDir, nil
if options.CreateZip {
return buildZippedPackage(options, destinationDir)
}

err = validator.ValidateFromPath(destinationDir)
if err != nil {
return "", errors.Wrap(err, "invalid content found in built package")
}
return destinationDir, nil
}

func buildZippedPackage(options BuildOptions, destinationDir string) (string, error) {
logger.Debug("Build zipped package")
zippedPackagePath, err := buildPackagesZipPath(options.PackageRoot)
if err != nil {
Expand All @@ -181,24 +190,36 @@ func BuildPackage(options BuildOptions) (string, error) {
return "", errors.Wrapf(err, "can't compress the built package (compressed file path: %s)", zippedPackagePath)
}

if !options.SignPackage {
return zippedPackagePath, nil
err = validator.ValidateFromZip(zippedPackagePath)
if err != nil {
return "", errors.Wrapf(err, "invalid content found in built zip package")
}

if options.SignPackage {
err := signZippedPackage(options, zippedPackagePath)
if err != nil {
return "", err
}
}

return zippedPackagePath, nil
}

func signZippedPackage(options BuildOptions, zippedPackagePath string) error {
logger.Debug("Sign the package")
m, err := packages.ReadPackageManifestFromPackageRoot(options.PackageRoot)
if err != nil {
return "", errors.Wrapf(err, "reading package manifest failed (path: %s)", options.PackageRoot)
return errors.Wrapf(err, "reading package manifest failed (path: %s)", options.PackageRoot)
}

err = files.Sign(zippedPackagePath, files.SignOptions{
PackageName: m.Name,
PackageVersion: m.Version,
})
if err != nil {
return "", errors.Wrapf(err, "can't sign the zipped package (path: %s)", zippedPackagePath)
return errors.Wrapf(err, "can't sign the zipped package (path: %s)", zippedPackagePath)
}
return zippedPackagePath, nil
return nil
}

func createBuildDirectory(dirs ...string) (string, error) {
Expand Down

0 comments on commit 29d0efc

Please sign in to comment.