Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ResponseChecker in download #289

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions pkg/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (

type RequestOption = func(*http.Request)

type ResponseChecker = func(*http.Response) error

func ApplyURLTransformer(urlTransformer URLTransformer, baseURLs ...string) ([]string, error) {
transformedURLs := make([]string, len(baseURLs))
for index, baseURL := range baseURLs {
Expand All @@ -42,7 +44,7 @@ func ApplyURLTransformer(urlTransformer URLTransformer, baseURLs ...string) ([]s
return transformedURLs, nil
}

func Bytes(ctx context.Context, url string, display func(string), requestOptions ...RequestOption) ([]byte, error) {
func Bytes(ctx context.Context, url string, display func(string), checker ResponseChecker, requestOptions ...RequestOption) ([]byte, error) {
display("Downloading " + url)

request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
Expand All @@ -60,11 +62,15 @@ func Bytes(ctx context.Context, url string, display func(string), requestOptions
}
defer response.Body.Close()

if err = checker(response); err != nil {
return nil, err
}

return io.ReadAll(response.Body)
}

func JSON(ctx context.Context, url string, display func(string), requestOptions ...RequestOption) (any, error) {
data, err := Bytes(ctx, url, display, requestOptions...)
func JSON(ctx context.Context, url string, display func(string), checker ResponseChecker, requestOptions ...RequestOption) (any, error) {
data, err := Bytes(ctx, url, display, checker, requestOptions...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -103,3 +109,7 @@ func WithBasicAuth(username string, password string) RequestOption {
func NoTransform(value string) (string, error) {
return value, nil
}

func NoCheck(*http.Response) error {
return nil
}
36 changes: 2 additions & 34 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ package github

import (
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"strconv"

"github.com/tofuutils/tenv/v3/pkg/apimsg"
"github.com/tofuutils/tenv/v3/pkg/download"
versionfinder "github.com/tofuutils/tenv/v3/versionmanager/semantic/finder"
)

Expand Down Expand Up @@ -113,28 +113,13 @@ func ListReleases(ctx context.Context, githubReleaseURL string, githubToken stri
}

func apiGetRequest(ctx context.Context, callURL string, authorizationHeader string) (any, error) {
resp, err := downloadWithHeaders(ctx, callURL, func(request *http.Request) {
return download.JSON(ctx, callURL, download.NoDisplay, checkRateLimit, func(request *http.Request) {
request.Header.Set("Accept", "application/vnd.github+json")
if authorizationHeader != "" {
request.Header.Set("Authorization", authorizationHeader)
}
request.Header.Set("X-GitHub-Api-Version", "2022-11-28") //nolint
})
if err != nil {
return nil, err
}
defer resp.Body.Close()

if err := checkRateLimit(resp); err != nil {
return nil, err
}

var result any
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, apimsg.ErrReturn
}

return result, nil
}

func checkRateLimit(resp *http.Response) error {
Expand All @@ -146,23 +131,6 @@ func checkRateLimit(resp *http.Response) error {
return nil
}

func downloadWithHeaders(ctx context.Context, url string, modifyRequest func(*http.Request)) (*http.Response, error) {
client := &http.Client{}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
if modifyRequest != nil {
modifyRequest(req)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}

return resp, nil
}

func buildAuthorizationHeader(token string) string {
if token == "" {
return ""
Expand Down
2 changes: 1 addition & 1 deletion pkg/htmlquery/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

func Request(ctx context.Context, callURL string, selector string, extractor func(*goquery.Selection) string, ro ...download.RequestOption) ([]string, error) {
data, err := download.Bytes(ctx, callURL, download.NoDisplay, ro...)
data, err := download.Bytes(ctx, callURL, download.NoDisplay, download.NoCheck, ro...)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions versionmanager/retriever/atmos/atmosretriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ func (r AtmosRetriever) Install(ctx context.Context, versionStr string, targetPa
}

requestOptions := config.GetBasicAuthOption(r.conf.Getenv, config.AtmosRemoteUserEnvName, config.AtmosRemotePassEnvName)
data, err := download.Bytes(ctx, assetURLs[0], r.conf.Displayer.Display, requestOptions...)
data, err := download.Bytes(ctx, assetURLs[0], r.conf.Displayer.Display, download.NoCheck, requestOptions...)
if err != nil {
return err
}

dataSums, err := download.Bytes(ctx, assetURLs[1], r.conf.Displayer.Display, requestOptions...)
dataSums, err := download.Bytes(ctx, assetURLs[1], r.conf.Displayer.Display, download.NoCheck, requestOptions...)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions versionmanager/retriever/terraform/terraformretriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (r TerraformRetriever) Install(ctx context.Context, version string, targetP

r.conf.Displayer.Display(apimsg.MsgFetchRelease + versionURL)

value, err := download.JSON(ctx, versionURL, download.NoDisplay, requestOptions...)
value, err := download.JSON(ctx, versionURL, download.NoDisplay, download.NoCheck, requestOptions...)
if err != nil {
return err
}
Expand Down Expand Up @@ -124,7 +124,7 @@ func (r TerraformRetriever) Install(ctx context.Context, version string, targetP
return err
}

data, err := download.Bytes(ctx, assetURLs[0], r.conf.Displayer.Display, requestOptions...)
data, err := download.Bytes(ctx, assetURLs[0], r.conf.Displayer.Display, download.NoCheck, requestOptions...)
if err != nil {
return err
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func (r TerraformRetriever) ListVersions(ctx context.Context) ([]string, error)

r.conf.Displayer.Display(apimsg.MsgFetchAllReleases + releasesURL)

value, err := download.JSON(ctx, releasesURL, download.NoDisplay, requestOptions...)
value, err := download.JSON(ctx, releasesURL, download.NoDisplay, download.NoCheck, requestOptions...)
if err != nil {
return nil, err
}
Expand All @@ -174,7 +174,7 @@ func (r TerraformRetriever) ListVersions(ctx context.Context) ([]string, error)
}

func (r TerraformRetriever) checkSumAndSig(ctx context.Context, fileName string, data []byte, downloadSumsURL string, downloadSumsSigURL string, options []download.RequestOption) error {
dataSums, err := download.Bytes(ctx, downloadSumsURL, r.conf.Displayer.Display, options...)
dataSums, err := download.Bytes(ctx, downloadSumsURL, r.conf.Displayer.Display, download.NoCheck, options...)
if err != nil {
return err
}
Expand All @@ -187,14 +187,14 @@ func (r TerraformRetriever) checkSumAndSig(ctx context.Context, fileName string,
return nil
}

dataSumsSig, err := download.Bytes(ctx, downloadSumsSigURL, r.conf.Displayer.Display, options...)
dataSumsSig, err := download.Bytes(ctx, downloadSumsSigURL, r.conf.Displayer.Display, download.NoCheck, options...)
if err != nil {
return err
}

var dataPublicKey []byte
if r.conf.TfKeyPath == "" {
dataPublicKey, err = download.Bytes(ctx, publicKeyURL, r.conf.Displayer.Display)
dataPublicKey, err = download.Bytes(ctx, publicKeyURL, r.conf.Displayer.Display, download.NoCheck)
} else {
dataPublicKey, err = os.ReadFile(r.conf.TfKeyPath)
}
Expand Down
4 changes: 2 additions & 2 deletions versionmanager/retriever/terragrunt/terragruntretriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ func (r TerragruntRetriever) Install(ctx context.Context, versionStr string, tar
}

requestOptions := config.GetBasicAuthOption(r.conf.Getenv, config.TgRemoteUserEnvName, config.TgRemotePassEnvName)
data, err := download.Bytes(ctx, assetURLs[0], r.conf.Displayer.Display, requestOptions...)
data, err := download.Bytes(ctx, assetURLs[0], r.conf.Displayer.Display, download.NoCheck, requestOptions...)
if err != nil {
return err
}

dataSums, err := download.Bytes(ctx, assetURLs[1], r.conf.Displayer.Display, requestOptions...)
dataSums, err := download.Bytes(ctx, assetURLs[1], r.conf.Displayer.Display, download.NoCheck, requestOptions...)
if err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions versionmanager/retriever/tofu/tofuretriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (r TofuRetriever) Install(ctx context.Context, versionStr string, targetPat
}

requestOptions := config.GetBasicAuthOption(r.conf.Getenv, config.TofuRemoteUserEnvName, config.TofuRemotePassEnvName)
data, err := download.Bytes(ctx, assetURLs[0], r.conf.Displayer.Display, requestOptions...)
data, err := download.Bytes(ctx, assetURLs[0], r.conf.Displayer.Display, download.NoCheck, requestOptions...)
if err != nil {
return err
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func (r TofuRetriever) ListVersions(ctx context.Context) ([]string, error) {

r.conf.Displayer.Display(apimsg.MsgFetchAllReleases + listURL)

value, err := download.JSON(ctx, listURL, download.NoDisplay, requestOptions...)
value, err := download.JSON(ctx, listURL, download.NoDisplay, download.NoCheck, requestOptions...)
if err != nil {
return nil, err
}
Expand All @@ -185,7 +185,7 @@ func (r TofuRetriever) ListVersions(ctx context.Context) ([]string, error) {
}

func (r TofuRetriever) checkSumAndSig(ctx context.Context, version *version.Version, stable bool, data []byte, fileName string, assetURLs []string, options []download.RequestOption) error {
dataSums, err := download.Bytes(ctx, assetURLs[1], r.conf.Displayer.Display, options...)
dataSums, err := download.Bytes(ctx, assetURLs[1], r.conf.Displayer.Display, download.NoCheck, options...)
if err != nil {
return err
}
Expand All @@ -198,12 +198,12 @@ func (r TofuRetriever) checkSumAndSig(ctx context.Context, version *version.Vers
return nil
}

dataSumsSig, err := download.Bytes(ctx, assetURLs[3], r.conf.Displayer.Display, options...)
dataSumsSig, err := download.Bytes(ctx, assetURLs[3], r.conf.Displayer.Display, download.NoCheck, options...)
if err != nil {
return err
}

dataSumsCert, err := download.Bytes(ctx, assetURLs[2], r.conf.Displayer.Display, options...)
dataSumsCert, err := download.Bytes(ctx, assetURLs[2], r.conf.Displayer.Display, download.NoCheck, options...)
if err != nil {
return err
}
Expand All @@ -222,14 +222,14 @@ func (r TofuRetriever) checkSumAndSig(ctx context.Context, version *version.Vers

r.conf.Displayer.Display("cosign executable not found, fallback to pgp check")

dataSumsSig, err = download.Bytes(ctx, assetURLs[4], r.conf.Displayer.Display, options...)
dataSumsSig, err = download.Bytes(ctx, assetURLs[4], r.conf.Displayer.Display, download.NoCheck, options...)
if err != nil {
return err
}

var dataPublicKey []byte
if r.conf.TofuKeyPath == "" {
dataPublicKey, err = download.Bytes(ctx, publicKeyURL, r.conf.Displayer.Display)
dataPublicKey, err = download.Bytes(ctx, publicKeyURL, r.conf.Displayer.Display, download.NoCheck)
} else {
dataPublicKey, err = os.ReadFile(r.conf.TofuKeyPath)
}
Expand Down