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

Package registry as proxy of package-storage #915

Merged
merged 18 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 18 additions & 8 deletions package_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func TestPackageStorage_Statics(t *testing.T) {

}

func TestPackageStorage_ResolverResponse(t *testing.T) {
func TestPackageStorage_ResolverHeadersResponse(t *testing.T) {
fs := storage.PrepareFakeServer(t, "./storage/testdata/search-index-all-full.json")
defer fs.Stop()

Expand All @@ -229,18 +229,24 @@ func TestPackageStorage_ResolverResponse(t *testing.T) {
staticHandler := staticHandler(indexer, testCacheTime)

tests := []struct {
endpoint string
path string
file string
headers map[string]string
handler func(w http.ResponseWriter, r *http.Request)
endpoint string
path string
file string
responseHeaders map[string]string
handler func(w http.ResponseWriter, r *http.Request)
}{
{"/package/1password/0.1.1/img/1password-logo-light-bg.svg", staticRouterPath, "1password-logo-light-bg.svg.response", map[string]string{"Last-Modified": "time"}, staticHandler},
{
endpoint: "/package/1password/0.1.1/img/1password-logo-light-bg.svg",
path: staticRouterPath,
file: "1password-logo-light-bg.svg.response",
responseHeaders: map[string]string{"Last-Modified": "time"},
handler: staticHandler,
},
}

for _, test := range tests {
t.Run(test.endpoint, func(t *testing.T) {
runEndpointWithHeaders(t, test.endpoint, test.path, test.file, test.headers, test.handler)
runEndpointWithStorageIndexerAndHeaders(t, test.endpoint, test.path, test.file, test.responseHeaders, test.handler)
})
}

Expand All @@ -249,3 +255,7 @@ func TestPackageStorage_ResolverResponse(t *testing.T) {
func runEndpointWithStorageIndexer(t *testing.T, endpoint, path, file string, handler func(w http.ResponseWriter, r *http.Request)) {
runEndpoint(t, endpoint, path, filepath.Join(storageIndexerGoldenDir, file), handler)
}

func runEndpointWithStorageIndexerAndHeaders(t *testing.T, endpoint, path, file string, headers map[string]string, handler func(w http.ResponseWriter, r *http.Request)) {
runEndpointWithHeaders(t, endpoint, path, filepath.Join(storageIndexerGoldenDir, file), headers, handler)
}
6 changes: 3 additions & 3 deletions packages/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func ServePackage(w http.ResponseWriter, r *http.Request, p *Package) {
defer span.End()

if p.RemoteResolver() != nil {
p.RemoteResolver().RedirectArtifactsHandler(w, r, p)
p.RemoteResolver().ForwardArtifactsHandler(w, r, p)
metrics.StorageRequestsTotal.With(
prometheus.Labels{"location": remoteLocationPrometheusLabel, "component": artifactComponentPrometheusLabel},
).Inc()
Expand All @@ -51,7 +51,7 @@ func ServePackageSignature(w http.ResponseWriter, r *http.Request, p *Package) {
defer span.End()

if p.RemoteResolver() != nil {
p.RemoteResolver().RedirectSignaturesHandler(w, r, p)
p.RemoteResolver().ForwardSignaturesHandler(w, r, p)
metrics.StorageRequestsTotal.With(
prometheus.Labels{"location": remoteLocationPrometheusLabel, "component": signatureComponentPrometheusLabel},
).Inc()
Expand Down Expand Up @@ -98,7 +98,7 @@ func ServePackageResource(w http.ResponseWriter, r *http.Request, p *Package, pa
defer span.End()

if p.RemoteResolver() != nil {
p.RemoteResolver().RedirectStaticHandler(w, r, p, packageFilePath)
p.RemoteResolver().ForwardStaticHandler(w, r, p, packageFilePath)
metrics.StorageRequestsTotal.With(
prometheus.Labels{"location": remoteLocationPrometheusLabel, "component": staticComponentPrometheusLabel},
).Inc()
Expand Down
6 changes: 3 additions & 3 deletions packages/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package packages
import "net/http"

type RemoteResolver interface {
RedirectArtifactsHandler(w http.ResponseWriter, r *http.Request, p *Package)
RedirectStaticHandler(w http.ResponseWriter, r *http.Request, p *Package, resourcePath string)
RedirectSignaturesHandler(w http.ResponseWriter, r *http.Request, p *Package)
ForwardArtifactsHandler(w http.ResponseWriter, r *http.Request, p *Package)
ForwardStaticHandler(w http.ResponseWriter, r *http.Request, p *Package, resourcePath string)
ForwardSignaturesHandler(w http.ResponseWriter, r *http.Request, p *Package)
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
}
6 changes: 3 additions & 3 deletions proxymode/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ func (pr proxyResolver) redirectRequest(w http.ResponseWriter, r *http.Request,
http.Redirect(w, r, remoteURL.String(), http.StatusMovedPermanently)
}

func (pr proxyResolver) RedirectArtifactsHandler(w http.ResponseWriter, r *http.Request, p *packages.Package) {
func (pr proxyResolver) ForwardArtifactsHandler(w http.ResponseWriter, r *http.Request, p *packages.Package) {
remotePath := fmt.Sprintf("/epr/package/%s-%s.zip", p.Name, p.Version)
pr.redirectRequest(w, r, remotePath)
}

func (pr proxyResolver) RedirectStaticHandler(w http.ResponseWriter, r *http.Request, p *packages.Package, resourcePath string) {
func (pr proxyResolver) ForwardStaticHandler(w http.ResponseWriter, r *http.Request, p *packages.Package, resourcePath string) {
remotePath := fmt.Sprintf("/package/%s/%s/%s", p.Name, p.Version, resourcePath)
pr.redirectRequest(w, r, remotePath)
}

func (pr proxyResolver) RedirectSignaturesHandler(w http.ResponseWriter, r *http.Request, p *packages.Package) {
func (pr proxyResolver) ForwardSignaturesHandler(w http.ResponseWriter, r *http.Request, p *packages.Package) {
remotePath := fmt.Sprintf("/epr/package/%s-%s.zip.sig", p.Name, p.Version)
pr.redirectRequest(w, r, remotePath)
}
Expand Down
6 changes: 3 additions & 3 deletions storage/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,21 @@ func addRequestHeadersToResponse(w http.ResponseWriter, resp *http.Response) {
}
}

func (resolver storageResolver) RedirectArtifactsHandler(w http.ResponseWriter, r *http.Request, p *packages.Package) {
func (resolver storageResolver) ForwardArtifactsHandler(w http.ResponseWriter, r *http.Request, p *packages.Package) {
nameVersionZip := fmt.Sprintf("%s-%s.zip", p.Name, p.Version)
artifactURL := resolver.artifactsPackagesURL.ResolveReference(&url.URL{Path: nameVersionZip})
resolver.pipeRequestProxy(w, r, artifactURL.String())
}

func (resolver storageResolver) RedirectStaticHandler(w http.ResponseWriter, r *http.Request, p *packages.Package, resourcePath string) {
func (resolver storageResolver) ForwardStaticHandler(w http.ResponseWriter, r *http.Request, p *packages.Package, resourcePath string) {
nameVersion := fmt.Sprintf("%s-%s/", p.Name, p.Version)
staticURL := resolver.artifactsStaticURL.
ResolveReference(&url.URL{Path: nameVersion}).
ResolveReference(&url.URL{Path: resourcePath})
resolver.pipeRequestProxy(w, r, staticURL.String())
}

func (resolver storageResolver) RedirectSignaturesHandler(w http.ResponseWriter, r *http.Request, p *packages.Package) {
func (resolver storageResolver) ForwardSignaturesHandler(w http.ResponseWriter, r *http.Request, p *packages.Package) {
nameVersionSigZip := fmt.Sprintf("%s-%s.zip.sig", p.Name, p.Version)
signatureURL := resolver.artifactsPackagesURL.ResolveReference(&url.URL{Path: nameVersionSigZip})
resolver.pipeRequestProxy(w, r, signatureURL.String())
Expand Down