Skip to content

Commit

Permalink
golint
Browse files Browse the repository at this point in the history
  • Loading branch information
michalpristas committed Jun 21, 2022
1 parent 2c325d6 commit 6d39843
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
30 changes: 22 additions & 8 deletions dev-tools/mage/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ import (
)

const (
inlineTemplate = "inline"
inlineTemplate = "inline"
xpackDirName = "x-pack"
windowsBinarySuffix = ".exe"
)

// Expand expands the given Go text/template string.
Expand Down Expand Up @@ -131,6 +133,7 @@ func expandFile(src, dst string, args ...map[string]interface{}) error {
return err
}

//nolint:gosec // 0644 is required
if err = ioutil.WriteFile(createDir(dst), []byte(output), 0644); err != nil {
return errors.Wrap(err, "failed to write rendered template")
}
Expand Down Expand Up @@ -276,6 +279,7 @@ func MustFindReplace(file string, re *regexp.Regexp, repl string) {
func DownloadFile(url, destinationDir string) (string, error) {
log.Println("Downloading", url)

//nolint:gosec,noctx // url is not user input
resp, err := http.Get(url)
if err != nil {
return "", errors.Wrap(err, "http get failed")
Expand Down Expand Up @@ -331,6 +335,7 @@ func unzip(sourceFile, destinationDir string) error {
}
defer innerFile.Close()

//nolint:gosec // G305 zip traversal, no user input
path := filepath.Join(destinationDir, f.Name)
if !strings.HasPrefix(path, destinationDir) {
return errors.Errorf("illegal file path in zip: %v", f.Name)
Expand All @@ -350,6 +355,7 @@ func unzip(sourceFile, destinationDir string) error {
}
defer out.Close()

//nolint:gosec // DoS vulnerability, no user input
if _, err = io.Copy(out, innerFile); err != nil {
return err
}
Expand All @@ -369,6 +375,7 @@ func unzip(sourceFile, destinationDir string) error {

// Tar compress a directory using tar + gzip algorithms
func Tar(src string, targetFile string) error {
//nolint:forbidigo // pattern forbidden but we want it here
fmt.Printf(">> creating TAR file from directory: %s, target: %s\n", src, targetFile)

f, err := os.Create(targetFile)
Expand All @@ -389,6 +396,7 @@ func Tar(src string, targetFile string) error {

// if a symlink, skip file
if fi.Mode().Type() == os.ModeSymlink {
//nolint:forbidigo // pattern forbidden but we want it here
fmt.Printf(">> skipping symlink: %s\n", file)
return nil
}
Expand Down Expand Up @@ -464,6 +472,7 @@ func untar(sourceFile, destinationDir string) error {
return err
}

//nolint:gosec // G305: file traversal, no user input
path := filepath.Join(destinationDir, header.Name)
if !strings.HasPrefix(path, destinationDir) {
return errors.Errorf("illegal file path in tar: %v", header.Name)
Expand All @@ -484,6 +493,7 @@ func untar(sourceFile, destinationDir string) error {
return err
}

//nolint:gosec // decompression bomb, no user input
if _, err = io.Copy(writer, tarReader); err != nil {
return err
}
Expand Down Expand Up @@ -560,7 +570,7 @@ func numParallel() int {
// based on GOMAXPROCS. The provided ctx is passed to the functions (if they
// accept it as a param).
func ParallelCtx(ctx context.Context, fns ...interface{}) {
var fnWrappers []func(context.Context) error
fnWrappers := make([]func(context.Context) error, 0, len(fns))
for _, f := range fns {
fnWrapper := funcTypeWrap(f)
if fnWrapper == nil {
Expand Down Expand Up @@ -758,6 +768,7 @@ func CreateSHA512File(file string) error {
computedHash := hex.EncodeToString(sum.Sum(nil))
out := fmt.Sprintf("%v %v", computedHash, filepath.Base(file))

//nolint:gosec // permissions are correct
return ioutil.WriteFile(file+".sha512", []byte(out), 0644)
}

Expand Down Expand Up @@ -785,7 +796,7 @@ func IsUpToDate(dst string, sources ...string) bool {

var files []string
for _, s := range sources {
filepath.Walk(s, func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(s, func(path string, info os.FileInfo, err error) error {
if err != nil {
if os.IsNotExist(err) {
return nil
Expand All @@ -799,6 +810,9 @@ func IsUpToDate(dst string, sources ...string) bool {

return nil
})
if err != nil {
panic(err)
}
}

execute, err := target.Path(dst, files...)
Expand All @@ -811,7 +825,7 @@ func OSSBeatDir(path ...string) string {
ossDir := CWD()

// Check if we need to correct ossDir because it's in x-pack.
if parentDir := filepath.Base(filepath.Dir(ossDir)); parentDir == "x-pack" {
if parentDir := filepath.Base(filepath.Dir(ossDir)); parentDir == xpackDirName {
// If the OSS version of the beat exists.
tmp := filepath.Join(ossDir, "../..", BeatName)
if _, err := os.Stat(tmp); !os.IsNotExist(err) {
Expand All @@ -828,7 +842,7 @@ func XPackBeatDir(path ...string) string {
// Check if we have an X-Pack only beats
cur := CWD()

if parentDir := filepath.Base(filepath.Dir(cur)); parentDir == "x-pack" {
if parentDir := filepath.Base(filepath.Dir(cur)); parentDir == xpackDirName {
tmp := filepath.Join(filepath.Dir(cur), BeatName)
return filepath.Join(append([]string{tmp}, path...)...)
}
Expand Down Expand Up @@ -856,7 +870,7 @@ func CreateDir(file string) string {
// binaryExtension returns the appropriate file extension based on GOOS.
func binaryExtension(goos string) string {
if goos == "windows" {
return ".exe"
return windowsBinarySuffix
}
return ""
}
Expand All @@ -880,7 +894,7 @@ func ParseVersion(version string) (major, minor, patch int, err error) {
major, _ = strconv.Atoi(data["major"])
minor, _ = strconv.Atoi(data["minor"])
patch, _ = strconv.Atoi(data["patch"])
return
return major, minor, patch, nil
}

// ListMatchingEnvVars returns all of the environment variables names that begin
Expand Down Expand Up @@ -957,7 +971,7 @@ func ReadGLIBCRequirement(elfFile string) (*SemanticVersion, error) {
return nil, errors.New("no GLIBC symbols found in binary (is this a static binary?)")
}

var versions []SemanticVersion
versions := make([]SemanticVersion, 0, len(versionSet))
for ver := range versionSet {
versions = append(versions, ver)
}
Expand Down
1 change: 1 addition & 0 deletions dev-tools/mage/pkgtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

//nolint:goconst // avoiding const check for Deb/Zip
package mage

import (
Expand Down
4 changes: 0 additions & 4 deletions internal/pkg/core/plugin/service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ import (
"github.com/elastic/elastic-agent/pkg/core/server"
)

const (
maxTimeout = 2 * time.Minute
)

var (
// ErrAppNotInstalled is returned when configuration is performed on not installed application.
ErrAppNotInstalled = errors.New("application is not installed", errors.TypeApplication)
Expand Down

0 comments on commit 6d39843

Please sign in to comment.