From 059ee8926c9ba1e5049c32e75b5f5494b1ae34ab Mon Sep 17 00:00:00 2001 From: mrjoelkamp Date: Wed, 14 Aug 2024 15:27:02 -0500 Subject: [PATCH] refactor: move fullURL only needed for DefaultFetcher --- pkg/tuf/tuf.go | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/pkg/tuf/tuf.go b/pkg/tuf/tuf.go index 71c949c..cfcc913 100644 --- a/pkg/tuf/tuf.go +++ b/pkg/tuf/tuf.go @@ -127,28 +127,26 @@ func NewClient(initialRoot []byte, tufPath, metadataSource, targetsSource string } func (t *Client) generateTargetURI(target *metadata.TargetFiles, digest string) (string, error) { - targetBaseURL := ensureTrailingSlash(t.cfg.RemoteTargetsURL) - targetRemotePath := target.Path - // if PrefixTargetsWithHash is set, we need to prefix the target name with the hash and handle subdirectories - // similar logic to https://github.com/theupdateframework/go-tuf/blob/f95222bdd22d2ac4e5b8ed6fe912b645e213c3b5/metadata/updater/updater.go#L227-L247 - if t.cfg.PrefixTargetsWithHash { - baseName := filepath.Base(targetRemotePath) - dirName, ok := strings.CutSuffix(targetRemotePath, "/"+baseName) - if !ok { - // . - targetRemotePath = fmt.Sprintf("%s.%s", digest, baseName) - } else { - // /. - targetRemotePath = fmt.Sprintf("%s/%s.%s", dirName, digest, baseName) - } - } - fullURL := fmt.Sprintf("%s%s", targetBaseURL, targetRemotePath) - switch fetcher := t.cfg.Fetcher.(type) { case *RegistryFetcher: return fmt.Sprintf("%s@sha256:%s", t.cfg.RemoteTargetsURL, digest), nil case *fetcher.DefaultFetcher: - return fullURL, nil + targetBaseURL := ensureTrailingSlash(t.cfg.RemoteTargetsURL) + targetRemotePath := target.Path + // if PrefixTargetsWithHash is set, we need to prefix the target name with the hash and handle subdirectories + // similar logic to https://github.com/theupdateframework/go-tuf/blob/f95222bdd22d2ac4e5b8ed6fe912b645e213c3b5/metadata/updater/updater.go#L227-L247 + if t.cfg.PrefixTargetsWithHash { + baseName := filepath.Base(targetRemotePath) + dirName, ok := strings.CutSuffix(targetRemotePath, "/"+baseName) + if !ok { + // . + targetRemotePath = fmt.Sprintf("%s.%s", digest, baseName) + } else { + // /. + targetRemotePath = fmt.Sprintf("%s/%s.%s", dirName, digest, baseName) + } + } + return fmt.Sprintf("%s%s", targetBaseURL, targetRemotePath), nil default: return "", fmt.Errorf("unsupported fetcher type: %T", fetcher) }