From d4a9044033afaf8765fc8854c20ee53355549adf Mon Sep 17 00:00:00 2001 From: sureshkrishnan-v Date: Wed, 18 Dec 2024 00:44:04 +0530 Subject: [PATCH 1/2] Fix: Made the timeout in updater.DownloadTarget configurable with a default value of 15s. Signed-off-by: sureshkrishnan-v --- examples/cli/tuf-client/cmd/get.go | 2 +- examples/client/client_example.go | 2 +- examples/multirepo/client/client_example.go | 2 +- metadata/config/config.go | 3 +++ metadata/multirepo/multirepo.go | 3 ++- metadata/updater/updater.go | 7 +++++-- 6 files changed, 13 insertions(+), 6 deletions(-) diff --git a/examples/cli/tuf-client/cmd/get.go b/examples/cli/tuf-client/cmd/get.go index 9b695d96..fea88cfa 100644 --- a/examples/cli/tuf-client/cmd/get.go +++ b/examples/cli/tuf-client/cmd/get.go @@ -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) } diff --git a/examples/client/client_example.go b/examples/client/client_example.go index 7a0f29cd..3477a8ea 100644 --- a/examples/client/client_example.go +++ b/examples/client/client_example.go @@ -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) } diff --git a/examples/multirepo/client/client_example.go b/examples/multirepo/client/client_example.go index 87f10468..1673ed63 100644 --- a/examples/multirepo/client/client_example.go +++ b/examples/multirepo/client/client_example.go @@ -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) } diff --git a/metadata/config/config.go b/metadata/config/config.go index cd8cc98a..98005455 100644 --- a/metadata/config/config.go +++ b/metadata/config/config.go @@ -20,6 +20,7 @@ package config import ( "net/url" "os" + "time" "github.com/theupdateframework/go-tuf/v2/metadata/fetcher" ) @@ -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 @@ -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 } diff --git a/metadata/multirepo/multirepo.go b/metadata/multirepo/multirepo.go index d6802beb..231c8e89 100644 --- a/metadata/multirepo/multirepo.go +++ b/metadata/multirepo/multirepo.go @@ -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" @@ -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 diff --git a/metadata/updater/updater.go b/metadata/updater/updater.go index 1b9d21b8..675f66e1 100644 --- a/metadata/updater/updater.go +++ b/metadata/updater/updater.go @@ -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 @@ -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 } From 7d8cf5d4f1d986224f0c72baf6405941847745c8 Mon Sep 17 00:00:00 2001 From: sureshkrishnan-v Date: Wed, 18 Dec 2024 00:45:40 +0530 Subject: [PATCH 2/2] Fix: Made the timeout in updater.DownloadTarget configurable with a default value of 15s. Signed-off-by: sureshkrishnan-v --- metadata/config/config_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/metadata/config/config_test.go b/metadata/config/config_test.go index a5d48746..35d58d24 100644 --- a/metadata/config/config_test.go +++ b/metadata/config/config_test.go @@ -22,6 +22,7 @@ import ( "os" "path/filepath" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/theupdateframework/go-tuf/v2/metadata/fetcher" @@ -55,6 +56,7 @@ func TestNewUpdaterConfig(t *testing.T) { RemoteTargetsURL: "somepath/targets", DisableLocalCache: false, PrefixTargetsWithHash: true, + Timeout: time.Second*15, }, wantErr: nil, },