@@ -17,10 +17,12 @@ limitations under the License.
17
17
package controllers
18
18
19
19
import (
20
+ "bytes"
20
21
"context"
22
+ "crypto/sha1"
23
+ "crypto/sha256"
21
24
"fmt"
22
25
"io"
23
- "io/ioutil"
24
26
"net/http"
25
27
"net/url"
26
28
"os"
@@ -94,7 +96,7 @@ func (r *HelmReleaseReconciler) getHelmChart(ctx context.Context, hr *v2.HelmRel
94
96
// loads it into a chart.Chart, and removes the downloaded artifact.
95
97
// It returns the loaded chart.Chart on success, or an error.
96
98
func (r * HelmReleaseReconciler ) loadHelmChart (source * sourcev1.HelmChart ) (* chart.Chart , error ) {
97
- f , err := ioutil . TempFile ("" , fmt .Sprintf ("%s-%s-*.tgz" , source .GetNamespace (), source .GetName ()))
99
+ f , err := os . CreateTemp ("" , fmt .Sprintf ("%s-%s-*.tgz" , source .GetNamespace (), source .GetName ()))
98
100
if err != nil {
99
101
return nil , err
100
102
}
@@ -126,13 +128,41 @@ func (r *HelmReleaseReconciler) loadHelmChart(source *sourcev1.HelmChart) (*char
126
128
return nil , fmt .Errorf ("artifact '%s' download failed (status code: %s)" , source .GetArtifact ().URL , resp .Status )
127
129
}
128
130
129
- if _ , err = io .Copy (f , resp .Body ); err != nil {
131
+ var buf bytes.Buffer
132
+ if _ , err = io .Copy (f , & buf ); err != nil {
133
+ return nil , err
134
+ }
135
+
136
+ // verify checksum matches origin
137
+ if err := r .verifyArtifact (source .GetArtifact (), f , resp .Body ); err != nil {
130
138
return nil , err
131
139
}
132
140
133
141
return loader .Load (f .Name ())
134
142
}
135
143
144
+ func (r * HelmReleaseReconciler ) verifyArtifact (artifact * sourcev1.Artifact , writer io.Writer , reader io.Reader ) error {
145
+ hasher := sha256 .New ()
146
+
147
+ // for backwards compatibility with source-controller v0.17.2 and older
148
+ if len (artifact .Checksum ) == 40 {
149
+ hasher = sha1 .New ()
150
+ }
151
+
152
+ // compute checksum
153
+ mw := io .MultiWriter (hasher , writer )
154
+ if _ , err := io .Copy (mw , reader ); err != nil {
155
+ return err
156
+ }
157
+
158
+ if checksum := fmt .Sprintf ("%x" , hasher .Sum (nil )); checksum != artifact .Checksum {
159
+ return fmt .Errorf ("failed to verify artifact: computed checksum '%s' doesn't match advertised '%s'" ,
160
+ checksum , artifact .Checksum )
161
+ }
162
+
163
+ return nil
164
+ }
165
+
136
166
// deleteHelmChart deletes the v1beta1.HelmChart of the v2beta1.HelmRelease.
137
167
func (r * HelmReleaseReconciler ) deleteHelmChart (ctx context.Context , hr * v2.HelmRelease ) error {
138
168
if hr .Status .HelmChart == "" {
0 commit comments