From ff5a1f6f2626250b33580c62ad0d22efe6ef8f2f Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 30 Nov 2022 23:24:31 +0100 Subject: [PATCH 1/2] Add support for excluded paths in packaging (#33894) * Add support for excluded paths in packaging After an update in heartbeat we have have a path like `usr/share/heartbeat/.node/node/lib/node_modules/@elastic/synthetics/templates/lightweight/heartbeat.yml` which matches the `configFilePattern` which causes a permission check which the file does not satisfy. It has nothing to do with the actual heartbeat configuration file. * Fix linting issues * Fix one more linting issue (cherry picked from commit 3198a686db03c76ef387e0b76e0ad941c4212cca) # Conflicts: # dev-tools/packaging/package_test.go --- dev-tools/packaging/package_test.go | 70 +++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/dev-tools/packaging/package_test.go b/dev-tools/packaging/package_test.go index 249bd0bb6db..d34c278eb06 100644 --- a/dev-tools/packaging/package_test.go +++ b/dev-tools/packaging/package_test.go @@ -36,6 +36,8 @@ import ( "strings" "testing" + "errors" + "github.com/blakesmith/ar" rpm "github.com/cavaliercoder/go-rpm" ) @@ -48,7 +50,12 @@ const ( ) var ( +<<<<<<< HEAD configFilePattern = regexp.MustCompile(`.*beat\.yml$|apm-server\.yml|elastic-agent\.yml$`) +======= + excludedPathsPattern = regexp.MustCompile(`node_modules`) + configFilePattern = regexp.MustCompile(`/(\w+beat\.yml|apm-server\.yml|elastic-agent\.yml)$`) +>>>>>>> 3198a686db (Add support for excluded paths in packaging (#33894)) manifestFilePattern = regexp.MustCompile(`manifest.yml`) modulesDirPattern = regexp.MustCompile(`module/.+`) modulesDDirPattern = regexp.MustCompile(`modules.d/$`) @@ -183,6 +190,50 @@ func checkZip(t *testing.T, file string) { checkLicensesPresent(t, "", p) } +<<<<<<< HEAD +======= +const ( + npcapLicense = `Dependency : Npcap \(https://nmap.org/npcap/\)` + libpcapLicense = `Dependency : Libpcap \(http://www.tcpdump.org/\)` + winpcapLicense = `Dependency : Winpcap \(https://www.winpcap.org/\)` + radiotapLicense = `Dependency : ieee80211_radiotap.h Header File` +) + +// This reflects the order that the licenses and notices appear in the relevant files. +var npcapLicensePattern = regexp.MustCompile( + "(?s)" + npcapLicense + + ".*" + libpcapLicense + + ".*" + winpcapLicense + + ".*" + radiotapLicense, +) + +func checkNpcapNotices(pkg, file string, contents io.Reader) error { + if !strings.Contains(pkg, "packetbeat") { + return nil + } + + wantNotices := strings.Contains(pkg, "windows") && !strings.Contains(pkg, "oss") + + // If the packetbeat README.md is made to be generated + // conditionally then it should also be checked here. + pkg = filepath.Base(pkg) + file, err := filepath.Rel(pkg[:len(pkg)-len(filepath.Ext(pkg))], file) + if err != nil { + return err + } + switch file { + case "NOTICE.txt": + if npcapLicensePattern.MatchReader(bufio.NewReader(contents)) != wantNotices { + if wantNotices { + return fmt.Errorf("Npcap license section not found in %s file in %s", file, pkg) + } + return fmt.Errorf("unexpected Npcap license section found in %s file in %s", file, pkg) + } + } + return nil +} + +>>>>>>> 3198a686db (Add support for excluded paths in packaging (#33894)) func checkDocker(t *testing.T, file string) { p, info, err := readDocker(file) if err != nil { @@ -528,6 +579,9 @@ func readRPM(rpmFile string) (*packageFile, *rpm.PackageFile, error) { pf := &packageFile{Name: filepath.Base(rpmFile), Contents: map[string]packageEntry{}} for _, file := range contents { + if excludedPathsPattern.MatchString(file.Name()) { + continue + } pe := packageEntry{ File: file.Name(), Mode: file.Mode(), @@ -555,7 +609,7 @@ func readDeb(debFile string, dataBuffer *bytes.Buffer) (*packageFile, error) { for { header, err := arReader.Next() if err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { break } return nil, err @@ -606,12 +660,16 @@ func readTarContents(tarName string, data io.Reader) (*packageFile, error) { for { header, err := tarReader.Next() if err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { break } return nil, err } + if excludedPathsPattern.MatchString(header.Name) { + continue + } + p.Contents[header.Name] = packageEntry{ File: header.Name, UID: header.Uid, @@ -632,6 +690,9 @@ func readZip(zipFile string) (*packageFile, error) { p := &packageFile{Name: filepath.Base(zipFile), Contents: map[string]packageEntry{}} for _, f := range r.File { + if excludedPathsPattern.MatchString(f.Name) { + continue + } p.Contents[f.Name] = packageEntry{ File: f.Name, Mode: f.Mode(), @@ -662,7 +723,7 @@ func readDocker(dockerFile string) (*packageFile, *dockerInfo, error) { for { header, err := tarReader.Next() if err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { break } return nil, nil, err @@ -708,6 +769,9 @@ func readDocker(dockerFile string) (*packageFile, *dockerInfo, error) { if strings.HasPrefix("/"+name, workingDir) || "/"+name == entrypoint { p.Contents[name] = entry } + if excludedPathsPattern.MatchString(name) { + continue + } // Add also licenses for _, licenseFile := range licenseFiles { if strings.Contains(name, licenseFile) { From e1d192a03749e04e443cf38491b9443fe48babae Mon Sep 17 00:00:00 2001 From: Craig MacKenzie Date: Wed, 30 Nov 2022 17:29:23 -0500 Subject: [PATCH 2/2] Resolve merge conflicts. --- dev-tools/packaging/package_test.go | 50 +---------------------------- 1 file changed, 1 insertion(+), 49 deletions(-) diff --git a/dev-tools/packaging/package_test.go b/dev-tools/packaging/package_test.go index d34c278eb06..b094859d660 100644 --- a/dev-tools/packaging/package_test.go +++ b/dev-tools/packaging/package_test.go @@ -50,12 +50,8 @@ const ( ) var ( -<<<<<<< HEAD - configFilePattern = regexp.MustCompile(`.*beat\.yml$|apm-server\.yml|elastic-agent\.yml$`) -======= excludedPathsPattern = regexp.MustCompile(`node_modules`) - configFilePattern = regexp.MustCompile(`/(\w+beat\.yml|apm-server\.yml|elastic-agent\.yml)$`) ->>>>>>> 3198a686db (Add support for excluded paths in packaging (#33894)) + configFilePattern = regexp.MustCompile(`.*beat\.yml$|apm-server\.yml|elastic-agent\.yml$`) manifestFilePattern = regexp.MustCompile(`manifest.yml`) modulesDirPattern = regexp.MustCompile(`module/.+`) modulesDDirPattern = regexp.MustCompile(`modules.d/$`) @@ -190,50 +186,6 @@ func checkZip(t *testing.T, file string) { checkLicensesPresent(t, "", p) } -<<<<<<< HEAD -======= -const ( - npcapLicense = `Dependency : Npcap \(https://nmap.org/npcap/\)` - libpcapLicense = `Dependency : Libpcap \(http://www.tcpdump.org/\)` - winpcapLicense = `Dependency : Winpcap \(https://www.winpcap.org/\)` - radiotapLicense = `Dependency : ieee80211_radiotap.h Header File` -) - -// This reflects the order that the licenses and notices appear in the relevant files. -var npcapLicensePattern = regexp.MustCompile( - "(?s)" + npcapLicense + - ".*" + libpcapLicense + - ".*" + winpcapLicense + - ".*" + radiotapLicense, -) - -func checkNpcapNotices(pkg, file string, contents io.Reader) error { - if !strings.Contains(pkg, "packetbeat") { - return nil - } - - wantNotices := strings.Contains(pkg, "windows") && !strings.Contains(pkg, "oss") - - // If the packetbeat README.md is made to be generated - // conditionally then it should also be checked here. - pkg = filepath.Base(pkg) - file, err := filepath.Rel(pkg[:len(pkg)-len(filepath.Ext(pkg))], file) - if err != nil { - return err - } - switch file { - case "NOTICE.txt": - if npcapLicensePattern.MatchReader(bufio.NewReader(contents)) != wantNotices { - if wantNotices { - return fmt.Errorf("Npcap license section not found in %s file in %s", file, pkg) - } - return fmt.Errorf("unexpected Npcap license section found in %s file in %s", file, pkg) - } - } - return nil -} - ->>>>>>> 3198a686db (Add support for excluded paths in packaging (#33894)) func checkDocker(t *testing.T, file string) { p, info, err := readDocker(file) if err != nil {