99 "time"
1010)
1111
12+ const pluralDigestHeader = "x-plrl-digest"
13+
1214var (
1315 timeout = 60 * time .Second
1416 client = & http.Client {
2022)
2123
2224func getBody (url , token string ) (string , error ) {
23- resp , err := getReader (url , token )
25+ resp , _ , err := getReader (url , token )
2426 if err != nil {
2527 return "" , err
2628 }
@@ -35,33 +37,33 @@ func getBody(url, token string) (string, error) {
3537 return string (body ), nil
3638}
3739
38- func getReader (url , token string ) (io.ReadCloser , error ) {
40+ func getReader (url , token string ) (io.ReadCloser , http. Header , error ) {
3941 req , err := http .NewRequest (http .MethodGet , url , nil )
4042 if err != nil {
41- return nil , err
43+ return nil , nil , err
4244 }
4345 req .Header .Add ("Authorization" , "Token " + token )
4446
4547 for i := 0 ; i < 3 ; i ++ {
46- resp , retriable , err := doRequest (req )
48+ resp , header , retriable , err := doRequest (req )
4749 if err != nil {
4850 if ! retriable {
49- return nil , err
51+ return nil , nil , err
5052 }
5153
5254 time .Sleep (time .Duration (50 * (i + 1 )) * time .Millisecond )
5355 continue
5456 }
5557
56- return resp , nil
58+ return resp , header , nil
5759 }
58- return nil , fmt .Errorf ("could not fetch manifest, retries exhaused: %w" , err )
60+ return nil , nil , fmt .Errorf ("could not fetch manifest, retries exhaused: %w" , err )
5961}
6062
61- func doRequest (req * http.Request ) (io.ReadCloser , bool , error ) {
63+ func doRequest (req * http.Request ) (io.ReadCloser , http. Header , bool , error ) {
6264 resp , err := client .Do (req )
6365 if err != nil {
64- return nil , false , err
66+ return nil , nil , false , err
6567 }
6668
6769 if resp .StatusCode != http .StatusOK {
@@ -71,13 +73,13 @@ func doRequest(req *http.Request) (io.ReadCloser, bool, error) {
7173 err := fmt .Errorf ("could not fetch manifest, error code %d" , resp .StatusCode )
7274
7375 if resp .StatusCode == http .StatusTooManyRequests {
74- return nil , true , err
76+ return nil , nil , true , err
7577 }
7678
77- return nil , false , err
79+ return nil , nil , false , err
7880 }
7981
80- return resp .Body , false , nil
82+ return resp .Body , resp . Header , false , nil
8183}
8284
8385func fetchSha (consoleURL , token , serviceID string ) (string , error ) {
@@ -89,18 +91,21 @@ func fetchSha(consoleURL, token, serviceID string) (string, error) {
8991 return getBody (url , token )
9092}
9193
92- func fetch (url , token string ) (string , error ) {
94+ func fetch (url , token , sha string ) (string , error ) {
9395 dir , err := os .MkdirTemp ("" , "manifests" )
9496 if err != nil {
9597 return "" , err
9698 }
9799
98- resp , err := getReader (url , token )
100+ resp , header , err := getReader (url , token )
99101 if err != nil {
100102 return "" , err
101103 }
102-
103104 defer resp .Close ()
105+ tarballSha := header .Get (pluralDigestHeader )
106+ if tarballSha != "" && sha != tarballSha {
107+ return "" , fmt .Errorf ("tarball sha expected %s actual %s" , sha , tarballSha )
108+ }
104109
105110 log .V (1 ).Info ("finished request to" , "url" , url )
106111
0 commit comments