-
Notifications
You must be signed in to change notification settings - Fork 68
/
search.go
188 lines (156 loc) · 4.85 KB
/
search.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// 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 (
"context"
"fmt"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
"github.com/Masterminds/semver/v3"
"go.elastic.co/apm/module/apmzap/v2"
"go.elastic.co/apm/v2"
"go.uber.org/zap"
"github.com/elastic/package-registry/internal/util"
"github.com/elastic/package-registry/packages"
"github.com/elastic/package-registry/proxymode"
)
func searchHandler(logger *zap.Logger, indexer Indexer, cacheTime time.Duration) func(w http.ResponseWriter, r *http.Request) {
return searchHandlerWithProxyMode(logger, indexer, proxymode.NoProxy(logger), cacheTime)
}
func searchHandlerWithProxyMode(logger *zap.Logger, indexer Indexer, proxyMode *proxymode.ProxyMode, cacheTime time.Duration) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
logger := logger.With(apmzap.TraceContext(r.Context())...)
filter, err := newSearchFilterFromQuery(r.URL.Query())
if err != nil {
badRequest(w, err.Error())
return
}
opts := packages.GetOptions{
Filter: filter,
}
packages, err := indexer.Get(r.Context(), &opts)
if err != nil {
notFoundError(w, fmt.Errorf("fetching package failed: %w", err))
return
}
if proxyMode.Enabled() {
proxiedPackages, err := proxyMode.Search(r)
if err != nil {
logger.Error("proxy mode: search failed", zap.Error(err))
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
packages = packages.Join(proxiedPackages)
if !opts.Filter.AllVersions {
packages = latestPackagesVersion(packages)
}
}
data, err := getPackageOutput(r.Context(), packages)
if err != nil {
notFoundError(w, err)
return
}
cacheHeaders(w, cacheTime)
jsonHeader(w)
fmt.Fprint(w, string(data))
}
}
func newSearchFilterFromQuery(query url.Values) (*packages.Filter, error) {
var filter packages.Filter
if len(query) == 0 {
return &filter, nil
}
var err error
if v := query.Get("kibana.version"); v != "" {
filter.KibanaVersion, err = semver.NewVersion(v)
if err != nil {
return nil, fmt.Errorf("invalid Kibana version '%s': %w", v, err)
}
}
if v := query.Get("category"); v != "" {
filter.Category = v
}
if v := query.Get("package"); v != "" {
filter.PackageName = v
}
if v := query.Get("type"); v != "" {
filter.PackageType = v
}
if v := query.Get("capabilities"); v != "" {
filter.Capabilities = strings.Split(v, ",")
}
if v := query.Get("spec.min"); v != "" {
filter.SpecMin, err = getSpecVersion(v)
if err != nil {
return nil, fmt.Errorf("invalid 'spec.min' version: %w", err)
}
}
if v := query.Get("spec.max"); v != "" {
filter.SpecMax, err = getSpecVersion(v)
if err != nil {
return nil, fmt.Errorf("invalid 'spec.max' version: %w", err)
}
}
if v := query.Get("all"); v != "" {
// Default is false, also on error
filter.AllVersions, err = strconv.ParseBool(v)
if err != nil {
return nil, fmt.Errorf("invalid 'all' query param: '%s'", v)
}
}
// Deprecated: release tags to be removed.
if v := query.Get("experimental"); v != "" {
// In case of error, keep it false
filter.Experimental, err = strconv.ParseBool(v)
if err != nil {
return nil, fmt.Errorf("invalid 'experimental' query param: '%s'", v)
}
}
if v := query.Get("prerelease"); v != "" {
// In case of error, keep it false
filter.Prerelease, err = strconv.ParseBool(v)
if err != nil {
return nil, fmt.Errorf("invalid 'prerelease' query param: '%s'", v)
}
}
if v := query.Get("discovery"); v != "" {
discovery, err := packages.NewDiscoveryFilter(v)
if err != nil {
return nil, fmt.Errorf("invalid 'discovery' query param: '%s': %w", v, err)
}
filter.Discovery = discovery
}
return &filter, nil
}
func getSpecVersion(version string) (*semver.Version, error) {
// version must cointain just <major.minor>
if len(strings.Split(version, ".")) != 2 {
return nil, fmt.Errorf("invalid version '%s': it should be <major.version>", version)
}
specVersion, err := semver.NewVersion(version)
if err != nil {
return nil, fmt.Errorf("invalid spec version '%s': %w", version, err)
}
return specVersion, nil
}
func getPackageOutput(ctx context.Context, packageList packages.Packages) ([]byte, error) {
span, _ := apm.StartSpan(ctx, "GetPackageOutput", "app")
defer span.End()
// Packages need to be sorted to be always outputted in the same order
sort.Sort(packageList)
var output []packages.BasePackage
for _, p := range packageList {
data := p.BasePackage
output = append(output, data)
}
// Instead of return `null` in case of an empty array, return []
if len(output) == 0 {
return []byte("[]"), nil
}
return util.MarshalJSONPretty(output)
}