Skip to content

Commit

Permalink
Merge pull request #371 from Prateeknandle/zip
Browse files Browse the repository at this point in the history
fix for downloading policy-templete zip
  • Loading branch information
daemon1024 committed Sep 8, 2023
2 parents 11d6d3e + 2693ba6 commit 2fc04d2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ require (
require (
github.com/accuknox/auto-policy-discovery/src v0.0.0-20230707054448-845969c25277
github.com/accuknox/auto-policy-discovery/src/protobuf v0.0.0-20230707054448-845969c25277
github.com/cavaliergopher/grab/v3 v3.0.1
github.com/charmbracelet/bubbles v0.15.0
github.com/charmbracelet/bubbletea v0.23.2
github.com/charmbracelet/lipgloss v0.7.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,6 @@ github.com/bytecodealliance/wasmtime-go v0.27.0 h1:b/mvyw1YJSwF5zNxqLH9V24ENkZGA
github.com/bytecodealliance/wasmtime-go v0.27.0/go.mod h1:q320gUxqyI8yB+ZqRuaJOEnGkAnHh6WtJjMaT2CW4wI=
github.com/bytecodealliance/wasmtime-go/v3 v3.0.2 h1:3uZCA/BLTIu+DqCfguByNMJa2HVHpXvjfy0Dy7g6fuA=
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/cavaliergopher/grab/v3 v3.0.1 h1:4z7TkBfmPjmLAAmkkAZNX/6QJ1nNFdv3SdIHXju0Fr4=
github.com/cavaliergopher/grab/v3 v3.0.1/go.mod h1:1U/KNnD+Ft6JJiYoYBAimKH2XrYptb8Kl3DFGmsjpq4=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v3 v3.2.2 h1:cfUAAO3yvKMYKPrvhDuHSwQnhZNk/RMHKdZqKTxfm6M=
Expand Down
44 changes: 39 additions & 5 deletions recommend/policyTemplates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"archive/zip"
"context"
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"strings"

"github.com/cavaliergopher/grab/v3"
"github.com/google/go-github/github"
kg "github.com/kubearmor/KubeArmor/KubeArmor/log"
pol "github.com/kubearmor/KubeArmor/pkg/KubeArmorController/api/security.kubearmor.com/v1"
Expand Down Expand Up @@ -95,6 +96,37 @@ func init() {
CurrentVersion = CurrentRelease()
}

func downloadZip(url string, destination string) error {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}

defer resp.Body.Close()

out, err := os.Create(filepath.Clean(destination))
if err != nil {
return err
}
defer func() {
if err := out.Close(); err != nil {
kg.Warnf("Error closing os file %s\n", err)
}
}()

_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}

return nil
}

// DownloadAndUnzipRelease downloads the latest version of policy-templates
func DownloadAndUnzipRelease() (string, error) {

Expand All @@ -106,20 +138,22 @@ func DownloadAndUnzipRelease() (string, error) {
return "", err
}
downloadURL := fmt.Sprintf("%s%s.zip", url, LatestVersion)
resp, err := grab.Get(getCachePath(), downloadURL)
zipPath := getCachePath() + ".zip"
err = downloadZip(downloadURL, zipPath)
if err != nil {
_ = removeData(getCachePath())
return "", err
}
err = unZip(resp.Filename, getCachePath())

err = unZip(zipPath, getCachePath())
if err != nil {
return "", err
}
err = removeData(resp.Filename)
err = removeData(zipPath)
if err != nil {
return "", err
}
_ = updatePolicyRules(strings.TrimSuffix(resp.Filename, ".zip"))
_ = updatePolicyRules(strings.TrimSuffix(zipPath, ".zip"))
CurrentVersion = CurrentRelease()
return LatestVersion, nil
}
Expand Down
9 changes: 5 additions & 4 deletions recommend/recommend.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ func Recommend(c *k8s.Client, o Options) error {
}).Info("Found outdated version of policy-templates")
log.Info("Downloading latest version [", LatestVersion, "]")
if _, err := DownloadAndUnzipRelease(); err != nil {
return err
log.WithError(err).Error("could not download latest policy-templates version")
} else {
log.WithFields(log.Fields{
"Updated Version": LatestVersion,
}).Info("policy-templates updated")
}
log.WithFields(log.Fields{
"Updated Version": LatestVersion,
}).Info("policy-templates updated")
}

if err = createOutDir(o.OutDir); err != nil {
Expand Down

0 comments on commit 2fc04d2

Please sign in to comment.