Skip to content

Commit 6fa5ee7

Browse files
author
mtojek
committed
Fixes
1 parent d4dde20 commit 6fa5ee7

6 files changed

Lines changed: 92 additions & 0 deletions

File tree

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ func mustLoadRouter(config *Config, indexer Indexer) *mux.Router {
227227

228228
func getRouter(config *Config, indexer Indexer) (*mux.Router, error) {
229229
artifactsHandler := artifactsHandler(indexer, config.CacheTimeCatchAll)
230+
signaturesHandler := signaturesHandler(indexer, config.CacheTimeCatchAll)
230231
faviconHandleFunc, err := faviconHandler(config.CacheTimeCatchAll)
231232
if err != nil {
232233
return nil, err
@@ -248,6 +249,7 @@ func getRouter(config *Config, indexer Indexer) (*mux.Router, error) {
248249
router.HandleFunc("/health", healthHandler)
249250
router.HandleFunc("/favicon.ico", faviconHandleFunc)
250251
router.HandleFunc(artifactsRouterPath, artifactsHandler)
252+
router.HandleFunc(signaturesRouterPath, signaturesHandler)
251253
router.HandleFunc(packageIndexRouterPath, packageIndexHandler)
252254
router.HandleFunc(staticRouterPath, staticHandler)
253255
router.Use(loggingMiddleware)

main_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,31 @@ func TestArtifacts(t *testing.T) {
114114
}
115115
}
116116

117+
func TestSignatures(t *testing.T) {
118+
indexer := packages.NewZipFileSystemIndexer("./testdata/local-storage")
119+
120+
err := indexer.Init(context.Background())
121+
require.NoError(t, err)
122+
123+
signaturesHandler := signaturesHandler(indexer, testCacheTime)
124+
125+
tests := []struct {
126+
endpoint string
127+
path string
128+
file string
129+
handler func(w http.ResponseWriter, r *http.Request)
130+
}{
131+
{"/epr/example/example-1.0.1.zip.sig", signaturesRouterPath, "example-1.0.1.zip.sig", signaturesHandler},
132+
{"/epr/example/example-0.0.1.zip.sig", signaturesRouterPath, "missing-signature.txt", signaturesHandler},
133+
}
134+
135+
for _, test := range tests {
136+
t.Run(test.endpoint, func(t *testing.T) {
137+
runEndpoint(t, test.endpoint, test.path, test.file, test.handler)
138+
})
139+
}
140+
}
141+
117142
func TestStatics(t *testing.T) {
118143
packagesBasePaths := []string{"./testdata/package"}
119144
indexer := packages.NewFileSystemIndexer(packagesBasePaths...)

packages/http.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,7 @@ func ServeFile(w http.ResponseWriter, r *http.Request, p *Package, name string)
7979

8080
http.ServeContent(w, r, name, stat.ModTime(), f)
8181
}
82+
83+
func ServeSignature(w http.ResponseWriter, r *http.Request, p *Package) {
84+
http.ServeFile(w, r, p.BasePath+".sig")
85+
}

signatures.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package main
6+
7+
import (
8+
"log"
9+
"net/http"
10+
"time"
11+
12+
"github.com/Masterminds/semver/v3"
13+
"github.com/gorilla/mux"
14+
"github.com/pkg/errors"
15+
16+
"github.com/elastic/package-registry/packages"
17+
)
18+
19+
const signaturesRouterPath = "/epr/{packageName}/{packageName:[a-z0-9_]+}-{packageVersion}.zip.sig"
20+
21+
var errSignatureFileNotFound = errors.New("signature file not found")
22+
23+
func signaturesHandler(indexer Indexer, cacheTime time.Duration) func(w http.ResponseWriter, r *http.Request) {
24+
return func(w http.ResponseWriter, r *http.Request) {
25+
vars := mux.Vars(r)
26+
packageName, ok := vars["packageName"]
27+
if !ok {
28+
badRequest(w, "missing package name")
29+
return
30+
}
31+
32+
packageVersion, ok := vars["packageVersion"]
33+
if !ok {
34+
badRequest(w, "missing package version")
35+
return
36+
}
37+
38+
_, err := semver.StrictNewVersion(packageVersion)
39+
if err != nil {
40+
badRequest(w, "invalid package version")
41+
return
42+
}
43+
44+
opts := packages.NameVersionFilter(packageName, packageVersion)
45+
packageList, err := indexer.Get(r.Context(), &opts)
46+
if err != nil {
47+
log.Printf("getting package path failed: %v", err)
48+
http.Error(w, "internal server error", http.StatusInternalServerError)
49+
return
50+
}
51+
if len(packageList) == 0 {
52+
notFoundError(w, errSignatureFileNotFound)
53+
return
54+
}
55+
56+
cacheHeaders(w, cacheTime)
57+
packages.ServeSignature(w, r, packageList[0])
58+
}
59+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
e16ddaf4f91df524b27bf4f2e4b1ac09
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
signature file not found

0 commit comments

Comments
 (0)