-
Notifications
You must be signed in to change notification settings - Fork 67
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
Implement remote resolver for storage indexer #823
Merged
Merged
Changes from 27 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
46fcb06
WIP
8852fb7
Merge branch 'main' into 767-enable-storage-indexer
382f1c6
Missing policy_templates
946bf7f
Fix: mage format
edd94cf
Regenerate
0efc2e5
Remove not relevant tests
cfbee35
Package indices
58f2807
Signatures
3cd046d
Fix
468cafc
WIP
5c62893
Package location
99a0996
WIP SetFileSystemReference
2056f7d
WIP
598969f
Make interfaces private
00ab65a
Refactor handlers
2e51265
Artifacts
335de19
WIP
759df56
Remove all changes:
8eb72ca
Fix: function naming
c262da1
WIP
a69be52
Packages
9fd79a1
Fix
b9dfb09
Fix
8813d9c
Fixes
c13245a
CHANGELOG
cba877c
SetIndexerRef
d337610
Fix: changelog
94d7b82
Use remote resolver
1a1997b
Fix
5f8a95e
CHANGELOG
56c3f80
Fix: interfaces
82848e8
ServePackage
8fac495
Revert to ServeFile
9eb177a
User http.StatusMovedPermanently
3abef88
Fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
// 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" | ||
"net/http" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/elastic/package-registry/storage" | ||
) | ||
|
||
const storageIndexerGoldenDir = "storage-indexer" | ||
|
||
func TestPackageStorage_Endpoints(t *testing.T) { | ||
fs := storage.PrepareFakeServer(t, "./storage/testdata/search-index-all-full.json") | ||
defer fs.Stop() | ||
indexer := storage.NewIndexer(fs.Client(), storage.FakeIndexerOptions) | ||
|
||
err := indexer.Init(context.Background()) | ||
require.NoError(t, err) | ||
|
||
tests := []struct { | ||
endpoint string | ||
path string | ||
file string | ||
handler func(w http.ResponseWriter, r *http.Request) | ||
}{ | ||
{"/search", "/search", "search.json", searchHandler(indexer, testCacheTime)}, | ||
{"/search?all=true", "/search", "search-all.json", searchHandler(indexer, testCacheTime)}, | ||
{"/categories", "/categories", "categories.json", categoriesHandler(indexer, testCacheTime)}, | ||
{"/categories?experimental=true", "/categories", "categories-experimental.json", categoriesHandler(indexer, testCacheTime)}, | ||
{"/categories?experimental=foo", "/categories", "categories-experimental-error.txt", categoriesHandler(indexer, testCacheTime)}, | ||
{"/categories?experimental=true&kibana.version=6.5.2", "/categories", "categories-kibana652.json", categoriesHandler(indexer, testCacheTime)}, | ||
{"/categories?prerelease=true", "/categories", "categories-prerelease.json", categoriesHandler(indexer, testCacheTime)}, | ||
{"/categories?prerelease=foo", "/categories", "categories-prerelease-error.txt", categoriesHandler(indexer, testCacheTime)}, | ||
{"/categories?prerelease=true&kibana.version=6.5.2", "/categories", "categories-prerelease-kibana652.json", categoriesHandler(indexer, testCacheTime)}, | ||
{"/categories?include_policy_templates=true", "/categories", "categories-include-policy-templates.json", categoriesHandler(indexer, testCacheTime)}, | ||
{"/categories?include_policy_templates=foo", "/categories", "categories-include-policy-templates-error.txt", categoriesHandler(indexer, testCacheTime)}, | ||
{"/search?kibana.version=6.5.2", "/search", "search-kibana652.json", searchHandler(indexer, testCacheTime)}, | ||
{"/search?kibana.version=7.2.1", "/search", "search-kibana721.json", searchHandler(indexer, testCacheTime)}, | ||
{"/search?kibana.version=8.0.0", "/search", "search-kibana800.json", searchHandler(indexer, testCacheTime)}, | ||
{"/search?category=web", "/search", "search-category-web.json", searchHandler(indexer, testCacheTime)}, | ||
{"/search?category=web&all=true", "/search", "search-category-web-all.json", searchHandler(indexer, testCacheTime)}, | ||
{"/search?category=custom", "/search", "search-category-custom.json", searchHandler(indexer, testCacheTime)}, | ||
{"/search?experimental=true", "/search", "search-package-experimental.json", searchHandler(indexer, testCacheTime)}, | ||
{"/search?experimental=foo", "/search", "search-package-experimental-error.txt", searchHandler(indexer, testCacheTime)}, | ||
{"/search?category=datastore&experimental=true", "/search", "search-category-datastore.json", searchHandler(indexer, testCacheTime)}, | ||
{"/search?prerelease=true", "/search", "search-package-prerelease.json", searchHandler(indexer, testCacheTime)}, | ||
{"/search?prerelease=foo", "/search", "search-package-prerelease-error.txt", searchHandler(indexer, testCacheTime)}, | ||
{"/search?category=datastore&prerelease=true", "/search", "search-category-datastore-prerelease.json", searchHandler(indexer, testCacheTime)}, | ||
|
||
// Removed flags, kept ensure that they don't break requests from old versions. | ||
{"/search?internal=true", "/search", "search-package-internal.json", searchHandler(indexer, testCacheTime)}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.endpoint, func(t *testing.T) { | ||
runEndpointWithStorageIndexer(t, test.endpoint, test.path, test.file, test.handler) | ||
}) | ||
} | ||
} | ||
|
||
func TestPackageStorage_PackageIndex(t *testing.T) { | ||
fs := storage.PrepareFakeServer(t, "./storage/testdata/search-index-all-full.json") | ||
defer fs.Stop() | ||
indexer := storage.NewIndexer(fs.Client(), storage.FakeIndexerOptions) | ||
|
||
err := indexer.Init(context.Background()) | ||
require.NoError(t, err) | ||
|
||
packageIndexHandler := packageIndexHandler(indexer, testCacheTime) | ||
|
||
tests := []struct { | ||
endpoint string | ||
path string | ||
file string | ||
handler func(w http.ResponseWriter, r *http.Request) | ||
}{ | ||
{"/package/1password/0.1.1/", packageIndexRouterPath, "1password-0.1.1.json", packageIndexHandler}, | ||
{"/package/kubernetes/0.3.0/", packageIndexRouterPath, "kubernetes-0.3.0.json", packageIndexHandler}, | ||
{"/package/osquery/1.0.3/", packageIndexRouterPath, "osquery-1.0.3.json", packageIndexHandler}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.endpoint, func(t *testing.T) { | ||
runEndpointWithStorageIndexer(t, test.endpoint, test.path, test.file, test.handler) | ||
}) | ||
} | ||
} | ||
|
||
func TestPackageStorage_Artifacts(t *testing.T) { | ||
fs := storage.PrepareFakeServer(t, "./storage/testdata/search-index-all-full.json") | ||
defer fs.Stop() | ||
indexer := storage.NewIndexer(fs.Client(), storage.FakeIndexerOptions) | ||
|
||
err := indexer.Init(context.Background()) | ||
require.NoError(t, err) | ||
|
||
artifactsHandler := artifactsHandler(indexer, testCacheTime) | ||
|
||
tests := []struct { | ||
endpoint string | ||
path string | ||
file string | ||
handler func(w http.ResponseWriter, r *http.Request) | ||
}{ | ||
{"/epr/1password/1password-0.1.1.zip", artifactsRouterPath, "1password-0.1.1.zip.txt", artifactsHandler}, | ||
{"/epr/kubernetes/kubernetes-999.999.999.zip", artifactsRouterPath, "artifact-package-version-not-found.txt", artifactsHandler}, | ||
{"/epr/missing/missing-1.0.3.zip", artifactsRouterPath, "artifact-package-not-found.txt", artifactsHandler}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.endpoint, func(t *testing.T) { | ||
runEndpointWithStorageIndexer(t, test.endpoint, test.path, test.file, test.handler) | ||
}) | ||
} | ||
} | ||
|
||
func TestPackageStorage_Signatures(t *testing.T) { | ||
fs := storage.PrepareFakeServer(t, "./storage/testdata/search-index-all-full.json") | ||
defer fs.Stop() | ||
indexer := storage.NewIndexer(fs.Client(), storage.FakeIndexerOptions) | ||
|
||
err := indexer.Init(context.Background()) | ||
require.NoError(t, err) | ||
|
||
signaturesHandler := signaturesHandler(indexer, testCacheTime) | ||
|
||
tests := []struct { | ||
endpoint string | ||
path string | ||
file string | ||
handler func(w http.ResponseWriter, r *http.Request) | ||
}{ | ||
{"/epr/1password/1password-0.1.1.zip.sig", signaturesRouterPath, "1password-0.1.1.zip.sig", signaturesHandler}, | ||
{"/epr/checkpoint/checkpoint-0.5.2.zip.sig", signaturesRouterPath, "checkpoint-0.5.2.zip.sig", signaturesHandler}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.endpoint, func(t *testing.T) { | ||
runEndpoint(t, test.endpoint, test.path, test.file, test.handler) | ||
}) | ||
} | ||
} | ||
|
||
func TestPackageStorage_Statics(t *testing.T) { | ||
fs := storage.PrepareFakeServer(t, "./storage/testdata/search-index-all-full.json") | ||
defer fs.Stop() | ||
indexer := storage.NewIndexer(fs.Client(), storage.FakeIndexerOptions) | ||
|
||
err := indexer.Init(context.Background()) | ||
require.NoError(t, err) | ||
|
||
staticHandler := staticHandler(indexer, testCacheTime) | ||
|
||
tests := []struct { | ||
endpoint string | ||
path string | ||
file 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", staticHandler}, | ||
{"/package/cassandra/1.1.0/img/[Logs Cassandra] System Logs.jpg", staticRouterPath, "logs-cassandra-system-logs.jpg", staticHandler}, | ||
{"/package/cef/0.1.0/docs/README.md", staticRouterPath, "cef-readme.md", staticHandler}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.endpoint, func(t *testing.T) { | ||
runEndpoint(t, test.endpoint, test.path, test.file, test.handler) | ||
}) | ||
} | ||
|
||
} | ||
|
||
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) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't call it "Hijack", here we are doing a legit redirect 🙂
Some suggestions:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can apply those renamings, not a problem :)
I found an analogy with http.Hijacker, which lets a handler take over the TCP connection. In this case, we let Storage Indexer handlers process requests, which include a redirection.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, let's keep this terminology if already present in the standard library :)