Skip to content

Commit 2fc04d2

Browse files
authored
Merge pull request #371 from Prateeknandle/zip
fix for downloading policy-templete zip
2 parents 11d6d3e + 2693ba6 commit 2fc04d2

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ require (
4545
require (
4646
github.com/accuknox/auto-policy-discovery/src v0.0.0-20230707054448-845969c25277
4747
github.com/accuknox/auto-policy-discovery/src/protobuf v0.0.0-20230707054448-845969c25277
48-
github.com/cavaliergopher/grab/v3 v3.0.1
4948
github.com/charmbracelet/bubbles v0.15.0
5049
github.com/charmbracelet/bubbletea v0.23.2
5150
github.com/charmbracelet/lipgloss v0.7.1

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,6 @@ github.com/bytecodealliance/wasmtime-go v0.27.0 h1:b/mvyw1YJSwF5zNxqLH9V24ENkZGA
271271
github.com/bytecodealliance/wasmtime-go v0.27.0/go.mod h1:q320gUxqyI8yB+ZqRuaJOEnGkAnHh6WtJjMaT2CW4wI=
272272
github.com/bytecodealliance/wasmtime-go/v3 v3.0.2 h1:3uZCA/BLTIu+DqCfguByNMJa2HVHpXvjfy0Dy7g6fuA=
273273
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
274-
github.com/cavaliergopher/grab/v3 v3.0.1 h1:4z7TkBfmPjmLAAmkkAZNX/6QJ1nNFdv3SdIHXju0Fr4=
275-
github.com/cavaliergopher/grab/v3 v3.0.1/go.mod h1:1U/KNnD+Ft6JJiYoYBAimKH2XrYptb8Kl3DFGmsjpq4=
276274
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
277275
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
278276
github.com/cenkalti/backoff/v3 v3.2.2 h1:cfUAAO3yvKMYKPrvhDuHSwQnhZNk/RMHKdZqKTxfm6M=

recommend/policyTemplates.go

+39-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"archive/zip"
88
"context"
99
"fmt"
10+
"io"
11+
"net/http"
1012
"os"
1113
"path"
1214
"path/filepath"
1315
"runtime"
1416
"strings"
1517

16-
"github.com/cavaliergopher/grab/v3"
1718
"github.com/google/go-github/github"
1819
kg "github.com/kubearmor/KubeArmor/KubeArmor/log"
1920
pol "github.com/kubearmor/KubeArmor/pkg/KubeArmorController/api/security.kubearmor.com/v1"
@@ -95,6 +96,37 @@ func init() {
9596
CurrentVersion = CurrentRelease()
9697
}
9798

99+
func downloadZip(url string, destination string) error {
100+
req, err := http.NewRequest(http.MethodGet, url, nil)
101+
if err != nil {
102+
return err
103+
}
104+
105+
resp, err := http.DefaultClient.Do(req)
106+
if err != nil {
107+
return err
108+
}
109+
110+
defer resp.Body.Close()
111+
112+
out, err := os.Create(filepath.Clean(destination))
113+
if err != nil {
114+
return err
115+
}
116+
defer func() {
117+
if err := out.Close(); err != nil {
118+
kg.Warnf("Error closing os file %s\n", err)
119+
}
120+
}()
121+
122+
_, err = io.Copy(out, resp.Body)
123+
if err != nil {
124+
return err
125+
}
126+
127+
return nil
128+
}
129+
98130
// DownloadAndUnzipRelease downloads the latest version of policy-templates
99131
func DownloadAndUnzipRelease() (string, error) {
100132

@@ -106,20 +138,22 @@ func DownloadAndUnzipRelease() (string, error) {
106138
return "", err
107139
}
108140
downloadURL := fmt.Sprintf("%s%s.zip", url, LatestVersion)
109-
resp, err := grab.Get(getCachePath(), downloadURL)
141+
zipPath := getCachePath() + ".zip"
142+
err = downloadZip(downloadURL, zipPath)
110143
if err != nil {
111144
_ = removeData(getCachePath())
112145
return "", err
113146
}
114-
err = unZip(resp.Filename, getCachePath())
147+
148+
err = unZip(zipPath, getCachePath())
115149
if err != nil {
116150
return "", err
117151
}
118-
err = removeData(resp.Filename)
152+
err = removeData(zipPath)
119153
if err != nil {
120154
return "", err
121155
}
122-
_ = updatePolicyRules(strings.TrimSuffix(resp.Filename, ".zip"))
156+
_ = updatePolicyRules(strings.TrimSuffix(zipPath, ".zip"))
123157
CurrentVersion = CurrentRelease()
124158
return LatestVersion, nil
125159
}

recommend/recommend.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,12 @@ func Recommend(c *k8s.Client, o Options) error {
106106
}).Info("Found outdated version of policy-templates")
107107
log.Info("Downloading latest version [", LatestVersion, "]")
108108
if _, err := DownloadAndUnzipRelease(); err != nil {
109-
return err
109+
log.WithError(err).Error("could not download latest policy-templates version")
110+
} else {
111+
log.WithFields(log.Fields{
112+
"Updated Version": LatestVersion,
113+
}).Info("policy-templates updated")
110114
}
111-
log.WithFields(log.Fields{
112-
"Updated Version": LatestVersion,
113-
}).Info("policy-templates updated")
114115
}
115116

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

0 commit comments

Comments
 (0)