@@ -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
1920const 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
204225func createBuildDirectory (dirs ... string ) (string , error ) {
0 commit comments