@@ -7,13 +7,14 @@ import (
7
7
"archive/zip"
8
8
"context"
9
9
"fmt"
10
+ "io"
11
+ "net/http"
10
12
"os"
11
13
"path"
12
14
"path/filepath"
13
15
"runtime"
14
16
"strings"
15
17
16
- "github.com/cavaliergopher/grab/v3"
17
18
"github.com/google/go-github/github"
18
19
kg "github.com/kubearmor/KubeArmor/KubeArmor/log"
19
20
pol "github.com/kubearmor/KubeArmor/pkg/KubeArmorController/api/security.kubearmor.com/v1"
@@ -95,6 +96,37 @@ func init() {
95
96
CurrentVersion = CurrentRelease ()
96
97
}
97
98
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
+
98
130
// DownloadAndUnzipRelease downloads the latest version of policy-templates
99
131
func DownloadAndUnzipRelease () (string , error ) {
100
132
@@ -106,20 +138,22 @@ func DownloadAndUnzipRelease() (string, error) {
106
138
return "" , err
107
139
}
108
140
downloadURL := fmt .Sprintf ("%s%s.zip" , url , LatestVersion )
109
- resp , err := grab .Get (getCachePath (), downloadURL )
141
+ zipPath := getCachePath () + ".zip"
142
+ err = downloadZip (downloadURL , zipPath )
110
143
if err != nil {
111
144
_ = removeData (getCachePath ())
112
145
return "" , err
113
146
}
114
- err = unZip (resp .Filename , getCachePath ())
147
+
148
+ err = unZip (zipPath , getCachePath ())
115
149
if err != nil {
116
150
return "" , err
117
151
}
118
- err = removeData (resp . Filename )
152
+ err = removeData (zipPath )
119
153
if err != nil {
120
154
return "" , err
121
155
}
122
- _ = updatePolicyRules (strings .TrimSuffix (resp . Filename , ".zip" ))
156
+ _ = updatePolicyRules (strings .TrimSuffix (zipPath , ".zip" ))
123
157
CurrentVersion = CurrentRelease ()
124
158
return LatestVersion , nil
125
159
}
0 commit comments