forked from elastic/package-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signatures.go
64 lines (53 loc) · 1.75 KB
/
signatures.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package main
import (
"net/http"
"time"
"github.com/Masterminds/semver/v3"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/elastic/package-registry/packages"
"github.com/elastic/package-registry/util"
)
const signaturesRouterPath = "/epr/{packageName}/{packageName:[a-z0-9_]+}-{packageVersion}.zip.sig"
var errSignatureFileNotFound = errors.New("signature file not found")
func signaturesHandler(indexer Indexer, cacheTime time.Duration) func(w http.ResponseWriter, r *http.Request) {
logger := util.Logger()
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
packageName, ok := vars["packageName"]
if !ok {
badRequest(w, "missing package name")
return
}
packageVersion, ok := vars["packageVersion"]
if !ok {
badRequest(w, "missing package version")
return
}
_, err := semver.StrictNewVersion(packageVersion)
if err != nil {
badRequest(w, "invalid package version")
return
}
opts := packages.NameVersionFilter(packageName, packageVersion)
packageList, err := indexer.Get(r.Context(), &opts)
if err != nil {
logger.Error("getting package path failed",
zap.String("package.name", packageName),
zap.String("package.version", packageVersion),
zap.Error(err))
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
if len(packageList) == 0 {
notFoundError(w, errSignatureFileNotFound)
return
}
cacheHeaders(w, cacheTime)
packages.ServePackageSignature(w, r, packageList[0])
}
}