Created
November 15, 2023 06:19
-
-
Save suzuki-shunsuke/912e1da11ee3ebd38d6d9cf53d66b31d to your computer and use it in GitHub Desktop.
When I tried to download AWS CLI and calculate the checksum with Go, I faced a weird issue. https://github.com/aquaproj/aqua/issues/2467
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"github.com/codingsince1985/checksum" | |
) | |
func main() { | |
if err := core(); err != nil { | |
log.Fatal(err) | |
} | |
} | |
func core() error { | |
rc, err := download("http://awscli.amazonaws.com/AWSCLIV2-2.13.35.pkg") | |
if err != nil { | |
return err | |
} | |
defer rc.Close() | |
s, err := checksum.SHA256sumReader(rc) | |
if err != nil { | |
return err | |
} | |
fmt.Println(s) // The value is changed every time | |
return nil | |
} | |
func download(u string) (io.ReadCloser, error) { | |
client := http.DefaultClient | |
req, err := http.NewRequest(http.MethodGet, u, nil) | |
if err != nil { | |
return nil, fmt.Errorf("create a http request: %w", err) | |
} | |
resp, err := client.Do(req) | |
if err != nil { | |
return nil, fmt.Errorf("send http request: %w", err) | |
} | |
if resp.StatusCode >= http.StatusBadRequest { | |
return resp.Body, errors.New("status code >= 400") | |
} | |
log.Println("status code:", resp.StatusCode) // 200 | |
return resp.Body, nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
"github.com/codingsince1985/checksum" | |
) | |
func main() { | |
if err := core(); err != nil { | |
log.Fatal(err) | |
} | |
} | |
func core() error { | |
rc, err := download("http://awscli.amazonaws.com/AWSCLIV2-2.13.35.pkg") | |
if err != nil { | |
return err | |
} | |
defer rc.Close() | |
f, err := os.CreateTemp("", "") | |
if err != nil { | |
return err | |
} | |
defer func() { | |
f.Close() | |
if err := os.Remove(f.Name()); err != nil { | |
log.Println(err) | |
} | |
}() | |
if _, err := io.Copy(f, rc); err != nil { | |
return err | |
} | |
s, err := checksum.SHA256sumReader(f) | |
if err != nil { | |
return err | |
} | |
fmt.Println(s) // The value is unchanged but wrong. | |
return nil | |
} | |
func download(u string) (io.ReadCloser, error) { | |
client := http.DefaultClient | |
req, err := http.NewRequest(http.MethodGet, u, nil) | |
if err != nil { | |
return nil, fmt.Errorf("create a http request: %w", err) | |
} | |
resp, err := client.Do(req) | |
if err != nil { | |
return nil, fmt.Errorf("send http request: %w", err) | |
} | |
if resp.StatusCode >= http.StatusBadRequest { | |
return resp.Body, errors.New("status code >= 400") | |
} | |
log.Println("status code:", resp.StatusCode) | |
return resp.Body, nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
"github.com/codingsince1985/checksum" | |
) | |
func main() { | |
if err := core(); err != nil { | |
log.Fatal(err) | |
} | |
} | |
func core() error { | |
path, err := downloadAndCopy() | |
if err != nil { | |
return err | |
} | |
defer func() { | |
if err := os.Remove(path); err != nil { | |
log.Println(err) | |
} | |
}() | |
f, err := os.Open(path) | |
if err != nil { | |
return err | |
} | |
defer f.Close() | |
s, err := checksum.SHA256sumReader(f) | |
if err != nil { | |
return err | |
} | |
fmt.Println(s) | |
return nil | |
} | |
func downloadAndCopy() (string, error) { | |
rc, err := download("http://awscli.amazonaws.com/AWSCLIV2-2.13.35.pkg") | |
if err != nil { | |
return "", err | |
} | |
defer rc.Close() | |
f, err := os.CreateTemp("", "") | |
if err != nil { | |
return "", err | |
} | |
defer func() { | |
f.Close() | |
}() | |
if _, err := io.Copy(f, rc); err != nil { | |
return "", err | |
} | |
return f.Name(), nil | |
} | |
func download(u string) (io.ReadCloser, error) { | |
client := http.DefaultClient | |
req, err := http.NewRequest(http.MethodGet, u, nil) | |
if err != nil { | |
return nil, fmt.Errorf("create a http request: %w", err) | |
} | |
resp, err := client.Do(req) | |
if err != nil { | |
return nil, fmt.Errorf("send http request: %w", err) | |
} | |
if resp.StatusCode >= http.StatusBadRequest { | |
return resp.Body, errors.New("status code >= 400") | |
} | |
log.Println("status code:", resp.StatusCode) | |
return resp.Body, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bug1.go
The result is changed every time.
bug2.go
The result is unchanged but is wrong.
good1.go
The result is correct
Information
go.mod
I tried some URLs, but I couldn't reproduce the issue with URL other than the URL of AWS CLI.