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

Hardcoded timeout #665

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion examples/cli/tuf-client/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func GetCmd(target string) error {
}

// target is not present locally, so let's try to download it
path, _, err = up.DownloadTarget(targetInfo, "", "")
path, _, err = up.DownloadTarget(targetInfo, "", "",cfg.Timeout)
if err != nil {
return fmt.Errorf("failed to download target file %s - %w", target, err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/client/client_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func DownloadTarget(localMetadataDir, target string) error {
}

// target is not present locally, so let's try to download it
path, _, err = up.DownloadTarget(targetInfo, "", "")
path, _, err = up.DownloadTarget(targetInfo, "", "",cfg.Timeout)
if err != nil {
return fmt.Errorf("failed to download target file %s - %w", target, err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/multirepo/client/client_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func BootstrapTUF() ([]byte, map[string][]byte, error) {
}

// download targets (we don't have to actually store them other than for the sake of the example)
path, bytes, err := up.DownloadTarget(targetInfo, expectedTargetLocation, "")
path, bytes, err := up.DownloadTarget(targetInfo, expectedTargetLocation, "",cfg.Timeout)
if err != nil {
return nil, nil, fmt.Errorf("failed to download target file %s - %w", name, err)
}
Expand Down
3 changes: 3 additions & 0 deletions metadata/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package config
import (
"net/url"
"os"
"time"

"github.com/theupdateframework/go-tuf/v2/metadata/fetcher"
)
Expand All @@ -44,6 +45,7 @@ type UpdaterConfig struct {
// UnsafeLocalMode only uses the metadata as written on disk
// if the metadata is incomplete, calling updater.Refresh will fail
UnsafeLocalMode bool
Timeout time.Duration
}

// New creates a new UpdaterConfig instance used by the Updater to
Expand Down Expand Up @@ -71,6 +73,7 @@ func New(remoteURL string, rootBytes []byte) (*UpdaterConfig, error) {
DisableLocalCache: false, // enable local caching of trusted metadata
PrefixTargetsWithHash: true, // use hash-prefixed target files with consistent snapshots
UnsafeLocalMode: false,
Timeout: time.Second * 15,
}, nil
}

Expand Down
2 changes: 2 additions & 0 deletions metadata/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/theupdateframework/go-tuf/v2/metadata/fetcher"
Expand Down Expand Up @@ -55,6 +56,7 @@ func TestNewUpdaterConfig(t *testing.T) {
RemoteTargetsURL: "somepath/targets",
DisableLocalCache: false,
PrefixTargetsWithHash: true,
Timeout: time.Second*15,
},
wantErr: nil,
},
Expand Down
3 changes: 2 additions & 1 deletion metadata/multirepo/multirepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path/filepath"
"slices"
"time"

"github.com/theupdateframework/go-tuf/v2/metadata"
"github.com/theupdateframework/go-tuf/v2/metadata/config"
Expand Down Expand Up @@ -327,7 +328,7 @@ func (client *MultiRepoClient) DownloadTarget(repos []string, targetFile *metada
return targetPath, targetBytes, nil
}
// not present locally, so let's try to download it
targetPath, targetBytes, err = repoClient.DownloadTarget(targetFile, filePath, targetBaseURL)
targetPath, targetBytes, err = repoClient.DownloadTarget(targetFile, filePath, targetBaseURL, time.Second*15)
if err != nil {
// TODO: decide if we should error if one repository serves the expected target info, but we fail to download the actual target
// try downloading the target from the next available repository
Expand Down
7 changes: 5 additions & 2 deletions metadata/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (update *Updater) GetTargetInfo(targetPath string) (*metadata.TargetFiles,
}

// DownloadTarget downloads the target file specified by targetFile
func (update *Updater) DownloadTarget(targetFile *metadata.TargetFiles, filePath, targetBaseURL string) (string, []byte, error) {
func (update *Updater) DownloadTarget(targetFile *metadata.TargetFiles, filePath, targetBaseURL string,timeout time.Duration) (string, []byte, error) {
log := metadata.GetLogger()

var err error
Expand Down Expand Up @@ -244,8 +244,11 @@ func (update *Updater) DownloadTarget(targetFile *metadata.TargetFiles, filePath
targetRemotePath = fmt.Sprintf("%s/%s.%s", dirName, hashes, baseName)
}
}
if timeout!=0 {
update.cfg.Timeout = timeout
}
fullURL := fmt.Sprintf("%s%s", targetBaseURL, targetRemotePath)
data, err := update.cfg.Fetcher.DownloadFile(fullURL, targetFile.Length, time.Second*15)
data, err := update.cfg.Fetcher.DownloadFile(fullURL, targetFile.Length, update.cfg.Timeout)
if err != nil {
return "", nil, err
}
Expand Down