From 75003f1d5f0c188efb37d8a90f72b05576c3594d Mon Sep 17 00:00:00 2001 From: Julien Salleyron Date: Fri, 6 Nov 2026 13:00:23 +0100 Subject: [PATCH 1/4] feat: add plugin states endpoint and partial update for the scraper Piceus can load the state of all plugins in one call and update only stars, hidden or tagsETag without sending the whole plugin. --- cmd/serve/serve.go | 8 ++ pkg/db/db.go | 26 ++++++ pkg/db/mongodb/bootstrap.go | 2 +- pkg/db/mongodb/plugindb.go | 84 ++++++++++++++++- pkg/db/mongodb/plugindb_test.go | 132 ++++++++++++++++++++++++++- pkg/handlers/dbmock_test.go | 10 +++ pkg/handlers/handlers.go | 1 + pkg/handlers/state.go | 116 ++++++++++++++++++++++++ pkg/handlers/state_test.go | 155 ++++++++++++++++++++++++++++++++ 9 files changed, 528 insertions(+), 6 deletions(-) create mode 100644 pkg/handlers/state.go create mode 100644 pkg/handlers/state_test.go diff --git a/cmd/serve/serve.go b/cmd/serve/serve.go index cd29bf9..50758b3 100644 --- a/cmd/serve/serve.go +++ b/cmd/serve/serve.go @@ -57,6 +57,7 @@ func run(ctx context.Context, cfg Config) error { r.Handle("/external/", buildExternalRouter(handler)) registerInternalBlacklistRoutes(r, handler) + registerInternalStatesRoute(r, handler) r.HandleFunc("/live", healthChecker.Live) r.HandleFunc("/ready", healthChecker.Ready) @@ -82,6 +83,7 @@ func buildInternalRouter(handler handlers.Handlers) http.Handler { r.Handler(http.MethodGet, "/", otelhttp.NewHandler(http.HandlerFunc(handler.List), "internal_list")) r.Handler(http.MethodPost, "/", otelhttp.NewHandler(http.HandlerFunc(handler.Create), "internal_create")) r.Handler(http.MethodPut, "/:uuid", otelhttp.NewHandler(http.HandlerFunc(handler.Update), "internal_update")) + r.Handler(http.MethodPatch, "/:uuid", otelhttp.NewHandler(http.HandlerFunc(handler.Patch), "internal_patch")) r.Handler(http.MethodDelete, "/:uuid", otelhttp.NewHandler(http.HandlerFunc(handler.Delete), "internal_delete")) r.NotFound = http.HandlerFunc(handlers.NotFound) @@ -99,6 +101,12 @@ func registerInternalBlacklistRoutes(r *http.ServeMux, handler handlers.Handlers r.Handle("DELETE /internal/blacklist/{owner}/{repo}", otelhttp.NewHandler(http.HandlerFunc(handler.DeleteFromBlacklist), "internal_delete_from_blacklist")) } +// registerInternalStatesRoute registers the plugin states endpoint directly on +// the ServeMux, like the blacklist endpoints. +func registerInternalStatesRoute(r *http.ServeMux, handler handlers.Handlers) { + r.Handle("GET /internal/states", otelhttp.NewHandler(http.HandlerFunc(handler.ListStates), "internal_list_states")) +} + func buildExternalRouter(handler handlers.Handlers) http.Handler { r := httprouter.New() diff --git a/pkg/db/db.go b/pkg/db/db.go index 54d3bf8..8db1e21 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -25,6 +25,32 @@ type Plugin struct { Disabled bool `json:"disabled,omitempty" bson:"disabled"` Hidden bool `json:"hidden,omitempty" bson:"hidden"` UseUnsafe bool `json:"useUnsafe,omitempty" bson:"useUnsafe"` + // TagsETag is the ETag of the last GitHub tags response seen by the scraper. + TagsETag string `json:"tagsETag,omitempty" bson:"tagsETag"` +} + +// PluginState is the part of a plugin the scraper needs to know if a plugin changed. +type PluginState struct { + ID string `json:"id" bson:"id"` + Name string `json:"name" bson:"name"` + LatestVersion string `json:"latestVersion" bson:"latestVersion"` + Stars int `json:"stars" bson:"stars"` + Hidden bool `json:"hidden" bson:"hidden"` + Disabled bool `json:"disabled" bson:"disabled"` + TagsETag string `json:"tagsETag" bson:"tagsETag"` +} + +// PluginPatch holds the plugin fields that can be updated alone. +// A nil field is not updated. +type PluginPatch struct { + Stars *int `json:"stars,omitempty"` + Hidden *bool `json:"hidden,omitempty"` + TagsETag *string `json:"tagsETag,omitempty"` +} + +// IsEmpty returns true when the patch updates nothing. +func (p PluginPatch) IsEmpty() bool { + return p.Stars == nil && p.Hidden == nil && p.TagsETag == nil } // BlacklistEntry represents a repository excluded from the plugin scraping. diff --git a/pkg/db/mongodb/bootstrap.go b/pkg/db/mongodb/bootstrap.go index 860abb8..4e5d8d6 100644 --- a/pkg/db/mongodb/bootstrap.go +++ b/pkg/db/mongodb/bootstrap.go @@ -30,7 +30,7 @@ func (m *MongoDB) Bootstrap() error { Options: &options.IndexOptions{ Name: new("_by_stars"), }, - Keys: bson.D{{Key: "stars", Value: 1}}, + Keys: bson.D{{Key: fieldStars, Value: 1}}, }, { Options: &options.IndexOptions{ diff --git a/pkg/db/mongodb/plugindb.go b/pkg/db/mongodb/plugindb.go index 5441002..1c35452 100644 --- a/pkg/db/mongodb/plugindb.go +++ b/pkg/db/mongodb/plugindb.go @@ -24,6 +24,8 @@ const ( fieldHashes = "hashes" fieldDisabled = "disabled" fieldHidden = "hidden" + fieldTagsETag = "tagsETag" + fieldStars = "stars" ) // MongoDB is a mongoDB client. @@ -160,7 +162,7 @@ func (m *MongoDB) List(ctx context.Context, page db.Pagination) ([]db.Plugin, st opts := &options.FindOptions{} opts.SetLimit(int64(page.Size + 1)) opts.SetProjection(bson.D{{Key: fieldHashes, Value: 0}}) - opts.SetSort(bson.D{{Key: "stars", Value: -1}}) + opts.SetSort(bson.D{{Key: fieldStars, Value: -1}}) cursor, err := m.client.Collection(m.collName).Find(ctx, criteria, opts) if err != nil { @@ -326,6 +328,86 @@ func (m *MongoDB) Update(ctx context.Context, id string, plugin db.Plugin) (db.P return updated, nil } +// ListStates returns the state of all plugins, hidden and disabled plugins included. +func (m *MongoDB) ListStates(ctx context.Context) ([]db.PluginState, error) { + ctx, span := m.tracer.Start(ctx, "db_list_states") + defer span.End() + + opts := &options.FindOptions{} + opts.SetProjection(bson.D{ + {Key: "_id", Value: 0}, + {Key: "id", Value: 1}, + {Key: "name", Value: 1}, + {Key: "latestVersion", Value: 1}, + {Key: fieldStars, Value: 1}, + {Key: fieldHidden, Value: 1}, + {Key: fieldDisabled, Value: 1}, + {Key: fieldTagsETag, Value: 1}, + }) + + cursor, err := m.client.Collection(m.collName).Find(ctx, bson.D{}, opts) + if err != nil { + span.RecordError(err) + + return nil, fmt.Errorf("unable to find plugin states: %w", err) + } + + states := make([]db.PluginState, 0) + + if err = cursor.All(ctx, &states); err != nil { + span.RecordError(err) + + return nil, fmt.Errorf("unable to unmarshal plugin states: %w", err) + } + + return states, nil +} + +// Patch updates only the non-nil fields of the patch. +func (m *MongoDB) Patch(ctx context.Context, id string, patch db.PluginPatch) (db.Plugin, error) { + ctx, span := m.tracer.Start(ctx, "db_patch") + defer span.End() + + set := bson.D{} + + if patch.Stars != nil { + set = append(set, bson.E{Key: fieldStars, Value: *patch.Stars}) + } + + if patch.Hidden != nil { + set = append(set, bson.E{Key: fieldHidden, Value: *patch.Hidden}) + } + + if patch.TagsETag != nil { + set = append(set, bson.E{Key: fieldTagsETag, Value: *patch.TagsETag}) + } + + if len(set) == 0 { + return db.Plugin{}, errors.New("nothing to update") + } + + filter := bson.D{{Key: "id", Value: id}} + update := bson.D{{Key: "$set", Value: set}} + + opts := &options.FindOneAndUpdateOptions{} + opts.SetReturnDocument(options.After) + opts.SetProjection(bson.D{{Key: fieldHashes, Value: 0}}) + + var updated db.Plugin + + if err := m.client.Collection(m.collName).FindOneAndUpdate(ctx, filter, update, opts).Decode(&updated); err != nil { + span.RecordError(err) + + if errors.Is(err, mongo.ErrNoDocuments) { + return db.Plugin{}, db.NotFoundError{Err: err} + } + + return db.Plugin{}, fmt.Errorf("unable to patch plugin: %w", err) + } + + return updated, nil +} + // CreateHash creates a new plugin hash. func (m *MongoDB) CreateHash(ctx context.Context, module, version, hash string) (db.PluginHash, error) { ctx, span := m.tracer.Start(ctx, "db_create_hash") diff --git a/pkg/db/mongodb/plugindb_test.go b/pkg/db/mongodb/plugindb_test.go index 03077fb..429495d 100644 --- a/pkg/db/mongodb/plugindb_test.go +++ b/pkg/db/mongodb/plugindb_test.go @@ -43,6 +43,7 @@ func TestMongoDB_Create(t *testing.T) { "something": "there", }, CreatedAt: time.Now().Add(-2 * time.Hour), + TagsETag: `W/"abc"`, } got, err := store.Create(ctx, plugin) @@ -650,8 +651,9 @@ func TestMongoDB_Update(t *testing.T) { Versions: []string{ "v1.1.1", }, - Stars: 10, - Snippet: nil, + Stars: 10, + Snippet: nil, + TagsETag: `W/"old"`, }, Hashes: []db.PluginHash{ {Name: "plugin@v1.1.1", Hash: "123"}, @@ -676,14 +678,16 @@ func TestMongoDB_Update(t *testing.T) { Versions: []string{ "v1.1.1", }, - Stars: 10, - Snippet: nil, + Stars: 10, + Snippet: nil, + TagsETag: `W/"new"`, }) require.NoError(t, err) want := fixtures["plugin"].Plugin want.Name = "New Name" want.Author = "New Author" + want.TagsETag = `W/"new"` assert.Equal(t, want, got) @@ -987,3 +991,123 @@ func buildNextID(t *testing.T, next db.Plugin) string { return base64.RawStdEncoding.EncodeToString(b) } + +func TestMongoDB_ListStates(t *testing.T) { + ctx := context.Background() + + store, _ := createDatabase(t, []fixture{ + { + key: "active", + plugin: pluginDocument{ + Plugin: db.Plugin{ + ID: "123", + Name: "active", + Readme: "readme", + LatestVersion: "v1.0.0", + Stars: 10, + Snippet: map[string]any{"something": "there"}, + TagsETag: `W/"abc"`, + }, + Hashes: []db.PluginHash{{Name: "active@v1.0.0", Hash: "123"}}, + }, + }, + { + key: "hidden", + plugin: pluginDocument{ + Plugin: db.Plugin{ID: "234", Name: "hidden", LatestVersion: "v2.0.0", Stars: 3, Hidden: true}, + }, + }, + { + key: "disabled", + plugin: pluginDocument{ + Plugin: db.Plugin{ID: "345", Name: "disabled", LatestVersion: "v3.0.0", Disabled: true}, + }, + }, + }) + + states, err := store.ListStates(ctx) + require.NoError(t, err) + + assert.ElementsMatch(t, []db.PluginState{ + {ID: "123", Name: "active", LatestVersion: "v1.0.0", Stars: 10, TagsETag: `W/"abc"`}, + {ID: "234", Name: "hidden", LatestVersion: "v2.0.0", Stars: 3, Hidden: true}, + {ID: "345", Name: "disabled", LatestVersion: "v3.0.0", Disabled: true}, + }, states) +} + +func TestMongoDB_ListStates_empty(t *testing.T) { + store, _ := createDatabase(t, nil) + + states, err := store.ListStates(context.Background()) + require.NoError(t, err) + + assert.NotNil(t, states) + assert.Empty(t, states) +} + +func TestMongoDB_Patch(t *testing.T) { + ctx := context.Background() + + store, fixtures := createDatabase(t, []fixture{ + { + key: "plugin", + plugin: pluginDocument{ + Plugin: db.Plugin{ + ID: "123", + Name: "plugin", + DisplayName: "plugin", + Author: "author", + Readme: "readme", + LatestVersion: "v1.1.1", + Versions: []string{"v1.1.1"}, + Stars: 10, + Snippet: map[string]any{"something": "there"}, + Disabled: true, + TagsETag: `W/"old"`, + }, + Hashes: []db.PluginHash{ + {Name: "plugin@v1.1.1", Hash: "123"}, + }, + }, + }, + }) + + want := fixtures["plugin"].Plugin + + // Only stars. + got, err := store.Patch(ctx, "123", db.PluginPatch{Stars: new(42)}) + require.NoError(t, err) + + want.Stars = 42 + assert.Equal(t, want, toUTCPlugin(got)) + + // Only hidden. + got, err = store.Patch(ctx, "123", db.PluginPatch{Hidden: new(true)}) + require.NoError(t, err) + + want.Hidden = true + assert.Equal(t, want, toUTCPlugin(got)) + + // Only ETag, zero values are written. + got, err = store.Patch(ctx, "123", db.PluginPatch{Stars: new(0), TagsETag: new(`W/"new"`)}) + require.NoError(t, err) + + want.Stars = 0 + want.TagsETag = `W/"new"` + assert.Equal(t, want, toUTCPlugin(got)) + + // Check the stored document, hashes included, is not changed in other fields. + stored, ok := getPlugin(t, store, "123") + require.True(t, ok) + + assert.Equal(t, want, toUTCPlugin(stored.Plugin)) + assert.Equal(t, fixtures["plugin"].Hashes, stored.Hashes) + + // Empty patch. + _, err = store.Patch(ctx, "123", db.PluginPatch{}) + require.Error(t, err) + + // Unknown plugin. + _, err = store.Patch(ctx, "456", db.PluginPatch{Stars: new(1)}) + require.ErrorAs(t, err, &db.NotFoundError{}) +} diff --git a/pkg/handlers/dbmock_test.go b/pkg/handlers/dbmock_test.go index 0bc162b..ff0afe8 100644 --- a/pkg/handlers/dbmock_test.go +++ b/pkg/handlers/dbmock_test.go @@ -14,6 +14,8 @@ type mockDB struct { getByNameFn func(context.Context, string, bool) (db.Plugin, error) searchByNameFn func(context.Context, string, db.Pagination) ([]db.Plugin, string, error) updateFn func(context.Context, string, db.Plugin) (db.Plugin, error) + listStatesFn func(ctx context.Context) ([]db.PluginState, error) + patchFn func(ctx context.Context, id string, patch db.PluginPatch) (db.Plugin, error) deleteHashFn func(ctx context.Context, id string) error createHashFn func(ctx context.Context, module, version, hash string) (db.PluginHash, error) @@ -53,6 +55,14 @@ func (m mockDB) Update(ctx context.Context, id string, plugin db.Plugin) (db.Plu return m.updateFn(ctx, id, plugin) } +func (m mockDB) ListStates(ctx context.Context) ([]db.PluginState, error) { + return m.listStatesFn(ctx) +} + +func (m mockDB) Patch(ctx context.Context, id string, patch db.PluginPatch) (db.Plugin, error) { + return m.patchFn(ctx, id, patch) +} + func (m mockDB) DeleteHash(ctx context.Context, id string) error { return m.deleteHashFn(ctx, id) } diff --git a/pkg/handlers/handlers.go b/pkg/handlers/handlers.go index c63a147..7867973 100644 --- a/pkg/handlers/handlers.go +++ b/pkg/handlers/handlers.go @@ -43,6 +43,7 @@ type PluginStorer interface { // Storer is capable of storing plugins and the plugin blacklist. type Storer interface { PluginStorer + PluginStateStorer BlacklistStorer } diff --git a/pkg/handlers/state.go b/pkg/handlers/state.go new file mode 100644 index 0000000..05d2acb --- /dev/null +++ b/pkg/handlers/state.go @@ -0,0 +1,116 @@ +package handlers + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "io" + "net/http" + + "github.com/rs/zerolog/log" + "github.com/traefik/plugin-service/pkg/db" +) + +// PluginStateStorer is capable of reading and updating the plugin state used by the scraper. +type PluginStateStorer interface { + ListStates(ctx context.Context) ([]db.PluginState, error) + Patch(ctx context.Context, id string, patch db.PluginPatch) (db.Plugin, error) +} + +// ListStates returns the state of all plugins, hidden and disabled plugins included, without pagination. +func (h Handlers) ListStates(rw http.ResponseWriter, req *http.Request) { + ctx, span := h.tracer.Start(req.Context(), "handler_list_states") + defer span.End() + + rw.Header().Set("Content-Type", "application/json") + + states, err := h.store.ListStates(ctx) + if err != nil { + span.RecordError(err) + log.Error().Err(err).Msg("Error fetching plugin states") + JSONInternalServerError(rw) + + return + } + + if err := json.NewEncoder(rw).Encode(states); err != nil { + span.RecordError(err) + log.Error().Err(err).Msg("Failed to encode plugin states") + JSONInternalServerError(rw) + + return + } +} + +// Patch updates only the given fields of a plugin (stars, hidden, tagsETag). +func (h Handlers) Patch(rw http.ResponseWriter, req *http.Request) { + ctx, span := h.tracer.Start(req.Context(), "handler_patch") + defer span.End() + + rw.Header().Set("Content-Type", "application/json") + + id, err := getPathParam(req.URL) + if err != nil { + span.RecordError(err) + JSONError(rw, http.StatusBadRequest, "Missing plugin id") + + return + } + + logger := log.With().Str("plugin_id", id).Logger() + + body, err := io.ReadAll(req.Body) + if err != nil { + span.RecordError(err) + logger.Error().Err(err).Msg("Unable to read body for patch") + JSONError(rw, http.StatusBadRequest, err.Error()) + + return + } + + var patch db.PluginPatch + + if len(body) > 0 { + dec := json.NewDecoder(bytes.NewReader(body)) + dec.DisallowUnknownFields() + + if err = dec.Decode(&patch); err != nil { + span.RecordError(err) + logger.Error().Err(err).Msg("Unable to decode plugin patch") + JSONError(rw, http.StatusBadRequest, err.Error()) + + return + } + } + + if patch.IsEmpty() { + JSONError(rw, http.StatusBadRequest, "nothing to update") + + return + } + + updated, err := h.store.Patch(ctx, id, patch) + if err != nil { + span.RecordError(err) + + if errors.As(err, &db.NotFoundError{}) { + NotFound(rw, req) + + return + } + + logger.Error().Err(err).Msg("Error patching plugin") + JSONInternalServerError(rw) + + return + } + + if err := json.NewEncoder(rw).Encode(updated); err != nil { + span.RecordError(err) + logger.Error().Err(err).Msg("Failed to encode patched plugin") + JSONInternalServerError(rw) + + return + } +} diff --git a/pkg/handlers/state_test.go b/pkg/handlers/state_test.go new file mode 100644 index 0000000..9b10193 --- /dev/null +++ b/pkg/handlers/state_test.go @@ -0,0 +1,155 @@ +package handlers + +import ( + "context" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/traefik/plugin-service/pkg/db" +) + +func TestHandlers_ListStates(t *testing.T) { + testDB := mockDB{ + listStatesFn: func(_ context.Context) ([]db.PluginState, error) { + return []db.PluginState{ + {ID: "123", Name: "github.com/traefik/plugindemo", LatestVersion: "v0.2.1", Stars: 22, TagsETag: `W/"abc"`}, + {ID: "456", Name: "github.com/traefik/hidden", LatestVersion: "v1.0.0", Hidden: true, Disabled: true}, + }, nil + }, + } + + rw := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/internal/states", http.NoBody) + + New(testDB, nil, nil, "").ListStates(rw, req) + + require.Equal(t, http.StatusOK, rw.Code) + assert.JSONEq(t, `[ + {"id":"123","name":"github.com/traefik/plugindemo","latestVersion":"v0.2.1","stars":22,"hidden":false,"disabled":false,"tagsETag":"W/\"abc\""}, + {"id":"456","name":"github.com/traefik/hidden","latestVersion":"v1.0.0","stars":0,"hidden":true,"disabled":true,"tagsETag":""} + ]`, rw.Body.String()) +} + +func TestHandlers_ListStates_empty(t *testing.T) { + testDB := mockDB{ + listStatesFn: func(_ context.Context) ([]db.PluginState, error) { + return []db.PluginState{}, nil + }, + } + + rw := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/internal/states", http.NoBody) + + New(testDB, nil, nil, "").ListStates(rw, req) + + require.Equal(t, http.StatusOK, rw.Code) + assert.JSONEq(t, `[]`, rw.Body.String()) +} + +func TestHandlers_Patch(t *testing.T) { + testCases := []struct { + desc string + body string + wantPatch db.PluginPatch + }{ + { + desc: "stars only", + body: `{"stars":12}`, + wantPatch: db.PluginPatch{Stars: new(12)}, + }, + { + desc: "hidden only", + body: `{"hidden":false}`, + wantPatch: db.PluginPatch{Hidden: new(false)}, + }, + { + desc: "empty ETag", + body: `{"tagsETag":""}`, + wantPatch: db.PluginPatch{TagsETag: new("")}, + }, + { + desc: "all fields", + body: `{"stars":0,"hidden":true,"tagsETag":"W/\"abc\""}`, + wantPatch: db.PluginPatch{Stars: new(0), Hidden: new(true), TagsETag: new(`W/"abc"`)}, + }, + } + + for _, test := range testCases { + t.Run(test.desc, func(t *testing.T) { + var ( + gotID string + gotPatch db.PluginPatch + ) + + testDB := mockDB{ + patchFn: func(_ context.Context, id string, patch db.PluginPatch) (db.Plugin, error) { + gotID = id + gotPatch = patch + + return db.Plugin{ID: id, Name: "github.com/traefik/plugindemo"}, nil + }, + } + + rw := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPatch, "/123", strings.NewReader(test.body)) + + New(testDB, nil, nil, "").Patch(rw, req) + + require.Equal(t, http.StatusOK, rw.Code) + assert.Equal(t, "123", gotID) + assert.Equal(t, test.wantPatch, gotPatch) + }) + } +} + +func TestHandlers_Patch_badRequest(t *testing.T) { + testDB := mockDB{ + patchFn: func(_ context.Context, _ string, _ db.PluginPatch) (db.Plugin, error) { + t.Fatal("store should not be called on invalid input") + return db.Plugin{}, nil + }, + } + + testCases := []struct { + desc string + path string + body string + }{ + {desc: "empty body", path: "/123", body: ""}, + {desc: "empty object", path: "/123", body: `{}`}, + {desc: "null values", path: "/123", body: `{"stars":null}`}, + {desc: "unknown field", path: "/123", body: `{"name":"other"}`}, + {desc: "invalid JSON", path: "/123", body: `{`}, + {desc: "missing id", path: "/", body: `{"stars":1}`}, + } + + for _, test := range testCases { + t.Run(test.desc, func(t *testing.T) { + rw := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPatch, test.path, strings.NewReader(test.body)) + + New(testDB, nil, nil, "").Patch(rw, req) + + assert.Equal(t, http.StatusBadRequest, rw.Code) + }) + } +} + +func TestHandlers_Patch_notFound(t *testing.T) { + testDB := mockDB{ + patchFn: func(_ context.Context, _ string, _ db.PluginPatch) (db.Plugin, error) { + return db.Plugin{}, db.NotFoundError{} + }, + } + + rw := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPatch, "/123", strings.NewReader(`{"stars":1}`)) + + New(testDB, nil, nil, "").Patch(rw, req) + + assert.Equal(t, http.StatusNotFound, rw.Code) +} From c0499f2864bc9cec5621249d15e26fcbd990e08b Mon Sep 17 00:00:00 2001 From: mmatur Date: Fri, 20 Nov 2026 15:54:15 +0100 Subject: [PATCH 2/4] fix: keep the stored tags ETag on an update without it --- pkg/db/db.go | 3 ++- pkg/db/mongodb/plugindb_test.go | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/db/db.go b/pkg/db/db.go index 8db1e21..b0bd864 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -26,7 +26,8 @@ type Plugin struct { Hidden bool `json:"hidden,omitempty" bson:"hidden"` UseUnsafe bool `json:"useUnsafe,omitempty" bson:"useUnsafe"` // TagsETag is the ETag of the last GitHub tags response seen by the scraper. - TagsETag string `json:"tagsETag,omitempty" bson:"tagsETag"` + // omitempty: an update without the ETag (an older scraper) does not erase it. + TagsETag string `json:"tagsETag,omitempty" bson:"tagsETag,omitempty"` } // PluginState is the part of a plugin the scraper needs to know if a plugin changed. diff --git a/pkg/db/mongodb/plugindb_test.go b/pkg/db/mongodb/plugindb_test.go index 429495d..62498ca 100644 --- a/pkg/db/mongodb/plugindb_test.go +++ b/pkg/db/mongodb/plugindb_test.go @@ -707,6 +707,15 @@ func TestMongoDB_Update(t *testing.T) { assert.Equal(t, want, got) + // An update without the ETag (an older scraper) keeps the stored one. + withoutETag := got + withoutETag.TagsETag = "" + + got, err = store.Update(ctx, "123", withoutETag) + require.NoError(t, err) + + assert.Equal(t, want, got) + // Check that we get a db.NotFound when no plugin have the given id. _, err = store.Update(ctx, "456", got) require.ErrorAs(t, err, &db.NotFoundError{}) From 479bb7a15cd491d6d17b075bb3011462b16c6275 Mon Sep 17 00:00:00 2001 From: mmatur Date: Fri, 20 Nov 2026 16:00:44 +0100 Subject: [PATCH 3/4] fix: route the states endpoint in the internal router and harden the patch decoding --- cmd/serve/serve.go | 9 ++------- pkg/handlers/state.go | 36 ++++++++++++++++++++---------------- pkg/handlers/state_test.go | 4 ++++ 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/cmd/serve/serve.go b/cmd/serve/serve.go index 50758b3..8020bbb 100644 --- a/cmd/serve/serve.go +++ b/cmd/serve/serve.go @@ -57,7 +57,6 @@ func run(ctx context.Context, cfg Config) error { r.Handle("/external/", buildExternalRouter(handler)) registerInternalBlacklistRoutes(r, handler) - registerInternalStatesRoute(r, handler) r.HandleFunc("/live", healthChecker.Live) r.HandleFunc("/ready", healthChecker.Ready) @@ -84,6 +83,8 @@ func buildInternalRouter(handler handlers.Handlers) http.Handler { r.Handler(http.MethodPost, "/", otelhttp.NewHandler(http.HandlerFunc(handler.Create), "internal_create")) r.Handler(http.MethodPut, "/:uuid", otelhttp.NewHandler(http.HandlerFunc(handler.Update), "internal_update")) r.Handler(http.MethodPatch, "/:uuid", otelhttp.NewHandler(http.HandlerFunc(handler.Patch), "internal_patch")) + // No conflict with "/:uuid": httprouter has one tree per method, and no GET route has a wildcard. + r.Handler(http.MethodGet, "/states", otelhttp.NewHandler(http.HandlerFunc(handler.ListStates), "internal_list_states")) r.Handler(http.MethodDelete, "/:uuid", otelhttp.NewHandler(http.HandlerFunc(handler.Delete), "internal_delete")) r.NotFound = http.HandlerFunc(handlers.NotFound) @@ -101,12 +102,6 @@ func registerInternalBlacklistRoutes(r *http.ServeMux, handler handlers.Handlers r.Handle("DELETE /internal/blacklist/{owner}/{repo}", otelhttp.NewHandler(http.HandlerFunc(handler.DeleteFromBlacklist), "internal_delete_from_blacklist")) } -// registerInternalStatesRoute registers the plugin states endpoint directly on -// the ServeMux, like the blacklist endpoints. -func registerInternalStatesRoute(r *http.ServeMux, handler handlers.Handlers) { - r.Handle("GET /internal/states", otelhttp.NewHandler(http.HandlerFunc(handler.ListStates), "internal_list_states")) -} - func buildExternalRouter(handler handlers.Handlers) http.Handler { r := httprouter.New() diff --git a/pkg/handlers/state.go b/pkg/handlers/state.go index 05d2acb..3f6bba4 100644 --- a/pkg/handlers/state.go +++ b/pkg/handlers/state.go @@ -1,7 +1,6 @@ package handlers import ( - "bytes" "context" "encoding/json" "errors" @@ -12,6 +11,9 @@ import ( "github.com/traefik/plugin-service/pkg/db" ) +// maxPatchSize is the maximum size of a patch body. +const maxPatchSize = 1 << 20 + // PluginStateStorer is capable of reading and updating the plugin state used by the scraper. type PluginStateStorer interface { ListStates(ctx context.Context) ([]db.PluginState, error) @@ -60,28 +62,24 @@ func (h Handlers) Patch(rw http.ResponseWriter, req *http.Request) { logger := log.With().Str("plugin_id", id).Logger() - body, err := io.ReadAll(req.Body) - if err != nil { + var patch db.PluginPatch + + dec := json.NewDecoder(http.MaxBytesReader(rw, req.Body, maxPatchSize)) + dec.DisallowUnknownFields() + + // An empty body is an empty patch. + if err = dec.Decode(&patch); err != nil && !errors.Is(err, io.EOF) { span.RecordError(err) - logger.Error().Err(err).Msg("Unable to read body for patch") + logger.Error().Err(err).Msg("Unable to decode plugin patch") JSONError(rw, http.StatusBadRequest, err.Error()) return } - var patch db.PluginPatch - - if len(body) > 0 { - dec := json.NewDecoder(bytes.NewReader(body)) - dec.DisallowUnknownFields() - - if err = dec.Decode(&patch); err != nil { - span.RecordError(err) - logger.Error().Err(err).Msg("Unable to decode plugin patch") - JSONError(rw, http.StatusBadRequest, err.Error()) + if dec.More() { + JSONError(rw, http.StatusBadRequest, "body must contain a single JSON object") - return - } + return } if patch.IsEmpty() { @@ -90,6 +88,12 @@ func (h Handlers) Patch(rw http.ResponseWriter, req *http.Request) { return } + if patch.Stars != nil && *patch.Stars < 0 { + JSONError(rw, http.StatusBadRequest, "stars must not be negative") + + return + } + updated, err := h.store.Patch(ctx, id, patch) if err != nil { span.RecordError(err) diff --git a/pkg/handlers/state_test.go b/pkg/handlers/state_test.go index 9b10193..ef4244d 100644 --- a/pkg/handlers/state_test.go +++ b/pkg/handlers/state_test.go @@ -125,6 +125,10 @@ func TestHandlers_Patch_badRequest(t *testing.T) { {desc: "unknown field", path: "/123", body: `{"name":"other"}`}, {desc: "invalid JSON", path: "/123", body: `{`}, {desc: "missing id", path: "/", body: `{"stars":1}`}, + {desc: "two JSON objects", path: "/123", body: `{"stars":1}{"hidden":true}`}, + {desc: "trailing data", path: "/123", body: `{"stars":1} garbage`}, + {desc: "negative stars", path: "/123", body: `{"stars":-5}`}, + {desc: "too large", path: "/123", body: `{"tagsETag":"` + strings.Repeat("a", maxPatchSize) + `"}`}, } for _, test := range testCases { From ec2afca59e6a3f77b8344a6978811a064802e184 Mon Sep 17 00:00:00 2001 From: mmatur Date: Tue, 24 Nov 2026 09:58:00 +0100 Subject: [PATCH 4/4] fix: address review on the states and patch endpoints --- cmd/serve/serve.go | 46 ++++++------ cmd/serve/serve_test.go | 128 ++++++++++++++++++++++++++++++++ pkg/db/db.go | 12 +-- pkg/db/errors.go | 11 ++- pkg/db/mongodb/plugindb.go | 60 ++++++++++----- pkg/db/mongodb/plugindb_test.go | 107 ++++++++++++++++---------- pkg/handlers/dbmock_test.go | 4 +- pkg/handlers/handlers.go | 19 +++++ pkg/handlers/handlers_test.go | 16 ++++ pkg/handlers/state.go | 74 +++++++++--------- pkg/handlers/state_test.go | 68 ++++++++++------- 11 files changed, 393 insertions(+), 152 deletions(-) create mode 100644 cmd/serve/serve_test.go diff --git a/cmd/serve/serve.go b/cmd/serve/serve.go index 8020bbb..3257082 100644 --- a/cmd/serve/serve.go +++ b/cmd/serve/serve.go @@ -56,7 +56,6 @@ func run(ctx context.Context, cfg Config) error { r.Handle("/internal/", buildInternalRouter(handler)) r.Handle("/external/", buildExternalRouter(handler)) - registerInternalBlacklistRoutes(r, handler) r.HandleFunc("/live", healthChecker.Live) r.HandleFunc("/ready", healthChecker.Ready) @@ -76,30 +75,35 @@ func buildPublicRouter(handler handlers.Handlers) http.Handler { return http.StripPrefix("/public", r) } +// buildInternalRouter uses a ServeMux: a static segment ("states", "blacklist") takes precedence over "{uuid}". func buildInternalRouter(handler handlers.Handlers) http.Handler { - r := httprouter.New() - - r.Handler(http.MethodGet, "/", otelhttp.NewHandler(http.HandlerFunc(handler.List), "internal_list")) - r.Handler(http.MethodPost, "/", otelhttp.NewHandler(http.HandlerFunc(handler.Create), "internal_create")) - r.Handler(http.MethodPut, "/:uuid", otelhttp.NewHandler(http.HandlerFunc(handler.Update), "internal_update")) - r.Handler(http.MethodPatch, "/:uuid", otelhttp.NewHandler(http.HandlerFunc(handler.Patch), "internal_patch")) - // No conflict with "/:uuid": httprouter has one tree per method, and no GET route has a wildcard. - r.Handler(http.MethodGet, "/states", otelhttp.NewHandler(http.HandlerFunc(handler.ListStates), "internal_list_states")) - r.Handler(http.MethodDelete, "/:uuid", otelhttp.NewHandler(http.HandlerFunc(handler.Delete), "internal_delete")) - - r.NotFound = http.HandlerFunc(handlers.NotFound) - r.PanicHandler = handlers.PanicHandler + r := http.NewServeMux() - return http.StripPrefix("/internal", r) + r.Handle("GET /{$}", otelhttp.NewHandler(http.HandlerFunc(handler.List), "internal_list")) + r.Handle("POST /{$}", otelhttp.NewHandler(http.HandlerFunc(handler.Create), "internal_create")) + r.Handle("GET /states", otelhttp.NewHandler(http.HandlerFunc(handler.ListStates), "internal_list_states")) + r.Handle("PUT /{uuid}", otelhttp.NewHandler(http.HandlerFunc(handler.Update), "internal_update")) + r.Handle("PATCH /{uuid}", otelhttp.NewHandler(http.HandlerFunc(handler.Patch), "internal_patch")) + r.Handle("DELETE /{uuid}", otelhttp.NewHandler(http.HandlerFunc(handler.Delete), "internal_delete")) + r.Handle("GET /blacklist", otelhttp.NewHandler(http.HandlerFunc(handler.ListBlacklist), "internal_list_blacklist")) + r.Handle("POST /blacklist", otelhttp.NewHandler(http.HandlerFunc(handler.AddToBlacklist), "internal_add_to_blacklist")) + r.Handle("DELETE /blacklist/{owner}/{repo}", otelhttp.NewHandler(http.HandlerFunc(handler.DeleteFromBlacklist), "internal_delete_from_blacklist")) + r.HandleFunc("/", handlers.NotFound) + + return http.StripPrefix("/internal", recoverPanic(r)) } -// registerInternalBlacklistRoutes registers the blacklist endpoints directly on -// the ServeMux: they cannot live in the internal httprouter because the static -// "blacklist" segment conflicts with its "/:uuid" wildcard routes. -func registerInternalBlacklistRoutes(r *http.ServeMux, handler handlers.Handlers) { - r.Handle("GET /internal/blacklist", otelhttp.NewHandler(http.HandlerFunc(handler.ListBlacklist), "internal_list_blacklist")) - r.Handle("POST /internal/blacklist", otelhttp.NewHandler(http.HandlerFunc(handler.AddToBlacklist), "internal_add_to_blacklist")) - r.Handle("DELETE /internal/blacklist/{owner}/{repo}", otelhttp.NewHandler(http.HandlerFunc(handler.DeleteFromBlacklist), "internal_delete_from_blacklist")) +// recoverPanic answers a panic with handlers.PanicHandler, as httprouter does. +func recoverPanic(next http.Handler) http.Handler { + return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + defer func() { + if err := recover(); err != nil { + handlers.PanicHandler(rw, req, err) + } + }() + + next.ServeHTTP(rw, req) + }) } func buildExternalRouter(handler handlers.Handlers) http.Handler { diff --git a/cmd/serve/serve_test.go b/cmd/serve/serve_test.go new file mode 100644 index 0000000..f6b0fc0 --- /dev/null +++ b/cmd/serve/serve_test.go @@ -0,0 +1,128 @@ +package serve + +import ( + "context" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/traefik/plugin-service/pkg/db" + "github.com/traefik/plugin-service/pkg/handlers" +) + +// routeStore records the store method a route reaches. +// The embedded Storer is nil: any other method panics, and the router answers 500. +type routeStore struct { + handlers.Storer + + called string +} + +func (s *routeStore) List(context.Context, db.Pagination) ([]db.Plugin, string, error) { + s.called = "List" + return nil, "", nil +} + +func (s *routeStore) Create(_ context.Context, plugin db.Plugin) (db.Plugin, error) { + s.called = "Create " + plugin.Name + return plugin, nil +} + +func (s *routeStore) Update(_ context.Context, id string, plugin db.Plugin) (db.Plugin, error) { + s.called = "Update " + id + return plugin, nil +} + +func (s *routeStore) Get(_ context.Context, id string) (db.Plugin, error) { + return db.Plugin{ID: id}, nil +} + +func (s *routeStore) Delete(_ context.Context, id string) error { + s.called = "Delete " + id + return nil +} + +func (s *routeStore) ListStates(context.Context) ([]db.PluginState, error) { + s.called = "ListStates" + return []db.PluginState{}, nil +} + +func (s *routeStore) Patch(_ context.Context, id string, _ db.PluginPatch) error { + s.called = "Patch " + id + return nil +} + +func (s *routeStore) ListBlacklist(context.Context) ([]db.BlacklistEntry, error) { + s.called = "ListBlacklist" + return nil, nil +} + +func (s *routeStore) UpsertBlacklist(_ context.Context, entry db.BlacklistEntry) (db.BlacklistEntry, error) { + s.called = "UpsertBlacklist " + entry.Repository + return entry, nil +} + +func (s *routeStore) DeleteBlacklist(_ context.Context, repository string) error { + s.called = "DeleteBlacklist " + repository + return nil +} + +func Test_buildInternalRouter(t *testing.T) { + testCases := []struct { + method string + target string + body string + wantCode int + wantCall string + }{ + {method: http.MethodGet, target: "/internal/", wantCode: http.StatusOK, wantCall: "List"}, + {method: http.MethodPost, target: "/internal/", body: `{"name":"plugin"}`, wantCode: http.StatusCreated, wantCall: "Create plugin"}, + {method: http.MethodGet, target: "/internal/states", wantCode: http.StatusOK, wantCall: "ListStates"}, + {method: http.MethodPut, target: "/internal/123", body: `{}`, wantCode: http.StatusOK, wantCall: "Update 123"}, + {method: http.MethodPatch, target: "/internal/123", body: `{"stars":1}`, wantCode: http.StatusNoContent, wantCall: "Patch 123"}, + {method: http.MethodDelete, target: "/internal/123", wantCode: http.StatusOK, wantCall: "Delete 123"}, + {method: http.MethodGet, target: "/internal/blacklist", wantCode: http.StatusOK, wantCall: "ListBlacklist"}, + {method: http.MethodPost, target: "/internal/blacklist", body: `{"repository":"owner/repo"}`, wantCode: http.StatusOK, wantCall: "UpsertBlacklist owner/repo"}, + {method: http.MethodDelete, target: "/internal/blacklist/owner/repo", wantCode: http.StatusNoContent, wantCall: "DeleteBlacklist owner/repo"}, + // No redirect to a path without the /internal prefix. + {method: http.MethodGet, target: "/internal/states/", wantCode: http.StatusNotFound}, + {method: http.MethodPatch, target: "/internal/123/", body: `{"stars":1}`, wantCode: http.StatusNotFound}, + {method: http.MethodGet, target: "/internal/123", wantCode: http.StatusNotFound}, + {method: http.MethodPost, target: "/internal/", body: `{"name":"plugin","stars":-1}`, wantCode: http.StatusBadRequest}, + } + + for _, test := range testCases { + t.Run(test.method+" "+test.target, func(t *testing.T) { + t.Parallel() + + store := &routeStore{} + + rw := httptest.NewRecorder() + req := httptest.NewRequest(test.method, test.target, strings.NewReader(test.body)) + + buildInternalRouter(handlers.New(store, nil, nil, "")).ServeHTTP(rw, req) + + assert.Equal(t, test.wantCode, rw.Code, rw.Body.String()) + assert.Equal(t, test.wantCall, store.called) + + if test.wantCode == http.StatusNotFound { + assert.JSONEq(t, `{"error":"Not Found"}`, rw.Body.String()) + } + }) + } +} + +func Test_buildInternalRouter_panic(t *testing.T) { + store := &routeStore{} + + rw := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/internal/?name=plugin", http.NoBody) + + // GetByName is not implemented by routeStore: it panics. + buildInternalRouter(handlers.New(store, nil, nil, "")).ServeHTTP(rw, req) + + assert.Equal(t, http.StatusInternalServerError, rw.Code) + assert.JSONEq(t, `{"error":"panic"}`, rw.Body.String()) +} diff --git a/pkg/db/db.go b/pkg/db/db.go index b0bd864..0a0785a 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -25,9 +25,6 @@ type Plugin struct { Disabled bool `json:"disabled,omitempty" bson:"disabled"` Hidden bool `json:"hidden,omitempty" bson:"hidden"` UseUnsafe bool `json:"useUnsafe,omitempty" bson:"useUnsafe"` - // TagsETag is the ETag of the last GitHub tags response seen by the scraper. - // omitempty: an update without the ETag (an older scraper) does not erase it. - TagsETag string `json:"tagsETag,omitempty" bson:"tagsETag,omitempty"` } // PluginState is the part of a plugin the scraper needs to know if a plugin changed. @@ -38,7 +35,9 @@ type PluginState struct { Stars int `json:"stars" bson:"stars"` Hidden bool `json:"hidden" bson:"hidden"` Disabled bool `json:"disabled" bson:"disabled"` - TagsETag string `json:"tagsETag" bson:"tagsETag"` + // TagsETag is the ETag of the last GitHub tags response seen by the scraper. + // It is not a Plugin field: only a PluginPatch writes it. + TagsETag string `json:"tagsETag" bson:"tagsETag"` } // PluginPatch holds the plugin fields that can be updated alone. @@ -49,11 +48,6 @@ type PluginPatch struct { TagsETag *string `json:"tagsETag,omitempty"` } -// IsEmpty returns true when the patch updates nothing. -func (p PluginPatch) IsEmpty() bool { - return p.Stars == nil && p.Hidden == nil && p.TagsETag == nil -} - // BlacklistEntry represents a repository excluded from the plugin scraping. // The Repository is the GitHub full name (owner/repo) and is the unique key. type BlacklistEntry struct { diff --git a/pkg/db/errors.go b/pkg/db/errors.go index bb78130..d8b42aa 100644 --- a/pkg/db/errors.go +++ b/pkg/db/errors.go @@ -1,6 +1,15 @@ package db -import "fmt" +import ( + "errors" + "fmt" +) + +// ErrEmptyPatch is returned for a patch that updates nothing. +var ErrEmptyPatch = errors.New("nothing to update") + +// ErrAlreadyExists is returned when a plugin with the same name already exists, disabled or hidden included. +var ErrAlreadyExists = errors.New("a plugin with this name already exists") // NotFoundError represents a document not found error. type NotFoundError struct { diff --git a/pkg/db/mongodb/plugindb.go b/pkg/db/mongodb/plugindb.go index 1c35452..0400b42 100644 --- a/pkg/db/mongodb/plugindb.go +++ b/pkg/db/mongodb/plugindb.go @@ -9,6 +9,7 @@ import ( "regexp" "time" + "github.com/rs/zerolog/log" "github.com/traefik/plugin-service/pkg/db" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" @@ -106,6 +107,18 @@ func (m *MongoDB) Create(ctx context.Context, plugin db.Plugin) (db.Plugin, erro ctx, span := m.tracer.Start(ctx, "db_create") defer span.End() + // GetByName does not return a disabled plugin: without this check, the scraper would create it again. + count, err := m.client.Collection(m.collName).CountDocuments(ctx, bson.D{{Key: "name", Value: plugin.Name}}, options.Count().SetLimit(1)) + if err != nil { + span.RecordError(err) + + return db.Plugin{}, fmt.Errorf("unable to count plugins by name: %w", err) + } + + if count > 0 { + return db.Plugin{}, db.ErrAlreadyExists + } + id := primitive.NewObjectID() plugin.ID = id.Hex() plugin.CreatedAt = time.Now().Truncate(time.Millisecond) @@ -116,7 +129,7 @@ func (m *MongoDB) Create(ctx context.Context, plugin db.Plugin) (db.Plugin, erro Hashes: []db.PluginHash{}, } - _, err := m.client.Collection(m.collName).InsertOne(ctx, doc) + _, err = m.client.Collection(m.collName).InsertOne(ctx, doc) if err != nil { span.RecordError(err) @@ -352,19 +365,37 @@ func (m *MongoDB) ListStates(ctx context.Context) ([]db.PluginState, error) { return nil, fmt.Errorf("unable to find plugin states: %w", err) } + defer func() { _ = cursor.Close(ctx) }() + states := make([]db.PluginState, 0) - if err = cursor.All(ctx, &states); err != nil { + for cursor.Next(ctx) { + var state db.PluginState + + // One malformed document must not hide the state of all the others. + if err = cursor.Decode(&state); err != nil { + span.RecordError(err) + + id, _ := cursor.Current.Lookup("id").StringValueOK() + log.Error().Err(err).Str("plugin_id", id).Msg("Unable to decode a plugin state, skipped") + + continue + } + + states = append(states, state) + } + + if err = cursor.Err(); err != nil { span.RecordError(err) - return nil, fmt.Errorf("unable to unmarshal plugin states: %w", err) + return nil, fmt.Errorf("unable to read plugin states: %w", err) } return states, nil } // Patch updates only the non-nil fields of the patch. -func (m *MongoDB) Patch(ctx context.Context, id string, patch db.PluginPatch) (db.Plugin, error) { +func (m *MongoDB) Patch(ctx context.Context, id string, patch db.PluginPatch) error { ctx, span := m.tracer.Start(ctx, "db_patch") defer span.End() @@ -383,29 +414,24 @@ func (m *MongoDB) Patch(ctx context.Context, id string, patch db.PluginPatch) (d } if len(set) == 0 { - return db.Plugin{}, errors.New("nothing to update") + return db.ErrEmptyPatch } filter := bson.D{{Key: "id", Value: id}} update := bson.D{{Key: "$set", Value: set}} - opts := &options.FindOneAndUpdateOptions{} - opts.SetReturnDocument(options.After) - opts.SetProjection(bson.D{{Key: fieldHashes, Value: 0}}) - - var updated db.Plugin - - if err := m.client.Collection(m.collName).FindOneAndUpdate(ctx, filter, update, opts).Decode(&updated); err != nil { + res, err := m.client.Collection(m.collName).UpdateOne(ctx, filter, update) + if err != nil { span.RecordError(err) - if errors.Is(err, mongo.ErrNoDocuments) { - return db.Plugin{}, db.NotFoundError{Err: err} - } + return fmt.Errorf("unable to patch plugin: %w", err) + } - return db.Plugin{}, fmt.Errorf("unable to patch plugin: %w", err) + if res.MatchedCount == 0 { + return db.NotFoundError{Err: mongo.ErrNoDocuments} } - return updated, nil + return nil } // CreateHash creates a new plugin hash. diff --git a/pkg/db/mongodb/plugindb_test.go b/pkg/db/mongodb/plugindb_test.go index 62498ca..0125801 100644 --- a/pkg/db/mongodb/plugindb_test.go +++ b/pkg/db/mongodb/plugindb_test.go @@ -43,7 +43,6 @@ func TestMongoDB_Create(t *testing.T) { "something": "there", }, CreatedAt: time.Now().Add(-2 * time.Hour), - TagsETag: `W/"abc"`, } got, err := store.Create(ctx, plugin) @@ -69,6 +68,23 @@ func TestMongoDB_Create(t *testing.T) { assert.Empty(t, stored.Hashes) } +func TestMongoDB_Create_alreadyExists(t *testing.T) { + store, _ := createDatabase(t, []fixture{ + { + key: "disabled", + plugin: pluginDocument{Plugin: db.Plugin{ID: "123", Name: "plugin", Disabled: true}}, + }, + }) + + _, err := store.Create(context.Background(), db.Plugin{Name: "plugin"}) + require.ErrorIs(t, err, db.ErrAlreadyExists) + + count, err := store.client.Collection(store.collName).CountDocuments(context.Background(), bson.D{{Key: "name", Value: "plugin"}}) + require.NoError(t, err) + + assert.EqualValues(t, 1, count) +} + func TestMongoDB_Get(t *testing.T) { ctx := context.Background() store, fixtures := createDatabase(t, []fixture{ @@ -651,9 +667,8 @@ func TestMongoDB_Update(t *testing.T) { Versions: []string{ "v1.1.1", }, - Stars: 10, - Snippet: nil, - TagsETag: `W/"old"`, + Stars: 10, + Snippet: nil, }, Hashes: []db.PluginHash{ {Name: "plugin@v1.1.1", Hash: "123"}, @@ -678,16 +693,14 @@ func TestMongoDB_Update(t *testing.T) { Versions: []string{ "v1.1.1", }, - Stars: 10, - Snippet: nil, - TagsETag: `W/"new"`, + Stars: 10, + Snippet: nil, }) require.NoError(t, err) want := fixtures["plugin"].Plugin want.Name = "New Name" want.Author = "New Author" - want.TagsETag = `W/"new"` assert.Equal(t, want, got) @@ -707,15 +720,6 @@ func TestMongoDB_Update(t *testing.T) { assert.Equal(t, want, got) - // An update without the ETag (an older scraper) keeps the stored one. - withoutETag := got - withoutETag.TagsETag = "" - - got, err = store.Update(ctx, "123", withoutETag) - require.NoError(t, err) - - assert.Equal(t, want, got) - // Check that we get a db.NotFound when no plugin have the given id. _, err = store.Update(ctx, "456", got) require.ErrorAs(t, err, &db.NotFoundError{}) @@ -1015,7 +1019,6 @@ func TestMongoDB_ListStates(t *testing.T) { LatestVersion: "v1.0.0", Stars: 10, Snippet: map[string]any{"something": "there"}, - TagsETag: `W/"abc"`, }, Hashes: []db.PluginHash{{Name: "active@v1.0.0", Hash: "123"}}, }, @@ -1034,6 +1037,17 @@ func TestMongoDB_ListStates(t *testing.T) { }, }) + require.NoError(t, store.Patch(ctx, "123", db.PluginPatch{TagsETag: new(`W/"abc"`)})) + + // A malformed legacy document is skipped, not an error for all the others. + _, err := store.client.Collection(store.collName).InsertOne(ctx, bson.D{ + {Key: "id", Value: "456"}, + {Key: "name", Value: 42}, + {Key: "stars", Value: 1.5}, + {Key: "disabled", Value: true}, + }) + require.NoError(t, err) + states, err := store.ListStates(ctx) require.NoError(t, err) @@ -1072,7 +1086,6 @@ func TestMongoDB_Patch(t *testing.T) { Stars: 10, Snippet: map[string]any{"something": "there"}, Disabled: true, - TagsETag: `W/"old"`, }, Hashes: []db.PluginHash{ {Name: "plugin@v1.1.1", Hash: "123"}, @@ -1082,41 +1095,57 @@ func TestMongoDB_Patch(t *testing.T) { }) want := fixtures["plugin"].Plugin + wantETag := "" + + check := func(t *testing.T) { + t.Helper() + + stored, ok := getPlugin(t, store, "123") + require.True(t, ok) + + assert.Equal(t, want, toUTCPlugin(stored.Plugin)) + assert.Equal(t, fixtures["plugin"].Hashes, stored.Hashes) + + states, err := store.ListStates(ctx) + require.NoError(t, err) + require.Len(t, states, 1) + + assert.Equal(t, wantETag, states[0].TagsETag) + } // Only stars. - got, err := store.Patch(ctx, "123", db.PluginPatch{Stars: new(42)}) - require.NoError(t, err) + require.NoError(t, store.Patch(ctx, "123", db.PluginPatch{Stars: new(42)})) want.Stars = 42 - assert.Equal(t, want, toUTCPlugin(got)) + check(t) // Only hidden. - got, err = store.Patch(ctx, "123", db.PluginPatch{Hidden: new(true)}) - require.NoError(t, err) + require.NoError(t, store.Patch(ctx, "123", db.PluginPatch{Hidden: new(true)})) want.Hidden = true - assert.Equal(t, want, toUTCPlugin(got)) + check(t) - // Only ETag, zero values are written. - got, err = store.Patch(ctx, "123", db.PluginPatch{Stars: new(0), TagsETag: new(`W/"new"`)}) - require.NoError(t, err) + // Only ETag. + require.NoError(t, store.Patch(ctx, "123", db.PluginPatch{TagsETag: new(`W/"new"`)})) + + wantETag = `W/"new"` + check(t) + + // Zero values are written. + require.NoError(t, store.Patch(ctx, "123", db.PluginPatch{Stars: new(0)})) want.Stars = 0 - want.TagsETag = `W/"new"` - assert.Equal(t, want, toUTCPlugin(got)) + check(t) - // Check the stored document, hashes included, is not changed in other fields. - stored, ok := getPlugin(t, store, "123") - require.True(t, ok) + // An update does not touch the ETag. + _, err := store.Update(ctx, "123", want) + require.NoError(t, err) - assert.Equal(t, want, toUTCPlugin(stored.Plugin)) - assert.Equal(t, fixtures["plugin"].Hashes, stored.Hashes) + check(t) // Empty patch. - _, err = store.Patch(ctx, "123", db.PluginPatch{}) - require.Error(t, err) + require.ErrorIs(t, store.Patch(ctx, "123", db.PluginPatch{}), db.ErrEmptyPatch) // Unknown plugin. - _, err = store.Patch(ctx, "456", db.PluginPatch{Stars: new(1)}) - require.ErrorAs(t, err, &db.NotFoundError{}) + require.ErrorAs(t, store.Patch(ctx, "456", db.PluginPatch{Stars: new(1)}), &db.NotFoundError{}) } diff --git a/pkg/handlers/dbmock_test.go b/pkg/handlers/dbmock_test.go index ff0afe8..a8e35c6 100644 --- a/pkg/handlers/dbmock_test.go +++ b/pkg/handlers/dbmock_test.go @@ -15,7 +15,7 @@ type mockDB struct { searchByNameFn func(context.Context, string, db.Pagination) ([]db.Plugin, string, error) updateFn func(context.Context, string, db.Plugin) (db.Plugin, error) listStatesFn func(ctx context.Context) ([]db.PluginState, error) - patchFn func(ctx context.Context, id string, patch db.PluginPatch) (db.Plugin, error) + patchFn func(ctx context.Context, id string, patch db.PluginPatch) error deleteHashFn func(ctx context.Context, id string) error createHashFn func(ctx context.Context, module, version, hash string) (db.PluginHash, error) @@ -59,7 +59,7 @@ func (m mockDB) ListStates(ctx context.Context) ([]db.PluginState, error) { return m.listStatesFn(ctx) } -func (m mockDB) Patch(ctx context.Context, id string, patch db.PluginPatch) (db.Plugin, error) { +func (m mockDB) Patch(ctx context.Context, id string, patch db.PluginPatch) error { return m.patchFn(ctx, id, patch) } diff --git a/pkg/handlers/handlers.go b/pkg/handlers/handlers.go index 7867973..e632c80 100644 --- a/pkg/handlers/handlers.go +++ b/pkg/handlers/handlers.go @@ -170,11 +170,24 @@ func (h Handlers) Create(rw http.ResponseWriter, req *http.Request) { return } + if pl.Stars < 0 { + JSONError(rw, http.StatusBadRequest, errNegativeStars) + + return + } + logger := log.With().Str("module_name", pl.Name).Logger() created, err := h.store.Create(ctx, pl) if err != nil { span.RecordError(err) + + if errors.Is(err, db.ErrAlreadyExists) { + JSONError(rw, http.StatusConflict, err.Error()) + + return + } + logger.Error().Err(err).Msg("Error persisting plugin") JSONError(rw, http.StatusInternalServerError, "Could not persist data") @@ -221,6 +234,12 @@ func (h Handlers) Update(rw http.ResponseWriter, req *http.Request) { return } + if input.Stars < 0 { + JSONError(rw, http.StatusBadRequest, errNegativeStars) + + return + } + pg, err := h.store.Update(ctx, id, input) if err != nil { span.RecordError(err) diff --git a/pkg/handlers/handlers_test.go b/pkg/handlers/handlers_test.go index 5c6a5e4..c342cca 100644 --- a/pkg/handlers/handlers_test.go +++ b/pkg/handlers/handlers_test.go @@ -5,6 +5,7 @@ import ( "net/http" "net/http/httptest" "os" + "strings" "testing" "time" @@ -195,3 +196,18 @@ func TestHandlers_List_SearchByName(t *testing.T) { assert.JSONEq(t, string(file), rw.Body.String()) } + +func TestHandlers_Create_alreadyExists(t *testing.T) { + testDB := mockDB{ + createFn: func(_ context.Context, _ db.Plugin) (db.Plugin, error) { + return db.Plugin{}, db.ErrAlreadyExists + }, + } + + rw := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"name":"github.com/traefik/plugindemo"}`)) + + New(testDB, nil, nil, "").Create(rw, req) + + assert.Equal(t, http.StatusConflict, rw.Code) +} diff --git a/pkg/handlers/state.go b/pkg/handlers/state.go index 3f6bba4..fffddb5 100644 --- a/pkg/handlers/state.go +++ b/pkg/handlers/state.go @@ -14,10 +14,13 @@ import ( // maxPatchSize is the maximum size of a patch body. const maxPatchSize = 1 << 20 +// errNegativeStars is the message returned for a plugin with negative stars, whatever the endpoint. +const errNegativeStars = "stars must not be negative" + // PluginStateStorer is capable of reading and updating the plugin state used by the scraper. type PluginStateStorer interface { ListStates(ctx context.Context) ([]db.PluginState, error) - Patch(ctx context.Context, id string, patch db.PluginPatch) (db.Plugin, error) + Patch(ctx context.Context, id string, patch db.PluginPatch) error } // ListStates returns the state of all plugins, hidden and disabled plugins included, without pagination. @@ -50,8 +53,6 @@ func (h Handlers) Patch(rw http.ResponseWriter, req *http.Request) { ctx, span := h.tracer.Start(req.Context(), "handler_patch") defer span.End() - rw.Header().Set("Content-Type", "application/json") - id, err := getPathParam(req.URL) if err != nil { span.RecordError(err) @@ -62,59 +63,60 @@ func (h Handlers) Patch(rw http.ResponseWriter, req *http.Request) { logger := log.With().Str("plugin_id", id).Logger() - var patch db.PluginPatch - - dec := json.NewDecoder(http.MaxBytesReader(rw, req.Body, maxPatchSize)) - dec.DisallowUnknownFields() - - // An empty body is an empty patch. - if err = dec.Decode(&patch); err != nil && !errors.Is(err, io.EOF) { + patch, err := decodePatch(rw, req) + if err != nil { span.RecordError(err) logger.Error().Err(err).Msg("Unable to decode plugin patch") - JSONError(rw, http.StatusBadRequest, err.Error()) - return - } - - if dec.More() { - JSONError(rw, http.StatusBadRequest, "body must contain a single JSON object") - - return - } - - if patch.IsEmpty() { - JSONError(rw, http.StatusBadRequest, "nothing to update") + if errors.As(err, new(*http.MaxBytesError)) { + JSONError(rw, http.StatusRequestEntityTooLarge, err.Error()) + } else { + JSONError(rw, http.StatusBadRequest, err.Error()) + } return } if patch.Stars != nil && *patch.Stars < 0 { - JSONError(rw, http.StatusBadRequest, "stars must not be negative") + JSONError(rw, http.StatusBadRequest, errNegativeStars) return } - updated, err := h.store.Patch(ctx, id, patch) - if err != nil { + if err = h.store.Patch(ctx, id, patch); err != nil { span.RecordError(err) - if errors.As(err, &db.NotFoundError{}) { + switch { + case errors.Is(err, db.ErrEmptyPatch): + JSONError(rw, http.StatusBadRequest, err.Error()) + case errors.As(err, &db.NotFoundError{}): NotFound(rw, req) - - return + default: + logger.Error().Err(err).Msg("Error patching plugin") + JSONInternalServerError(rw) } - logger.Error().Err(err).Msg("Error patching plugin") - JSONInternalServerError(rw) - return } - if err := json.NewEncoder(rw).Encode(updated); err != nil { - span.RecordError(err) - logger.Error().Err(err).Msg("Failed to encode patched plugin") - JSONInternalServerError(rw) + rw.WriteHeader(http.StatusNoContent) +} - return +// decodePatch decodes a body holding a single JSON object. An empty body is an empty patch. +func decodePatch(rw http.ResponseWriter, req *http.Request) (db.PluginPatch, error) { + var patch db.PluginPatch + + dec := json.NewDecoder(http.MaxBytesReader(rw, req.Body, maxPatchSize)) + dec.DisallowUnknownFields() + + if err := dec.Decode(&patch); err != nil && !errors.Is(err, io.EOF) { + return db.PluginPatch{}, err + } + + // Anything after the object, even a lone "}" or "]", is rejected. + if err := dec.Decode(&struct{}{}); !errors.Is(err, io.EOF) { + return db.PluginPatch{}, errors.Join(errors.New("body must contain a single JSON object"), err) } + + return patch, nil } diff --git a/pkg/handlers/state_test.go b/pkg/handlers/state_test.go index ef4244d..d0817dd 100644 --- a/pkg/handlers/state_test.go +++ b/pkg/handlers/state_test.go @@ -80,17 +80,19 @@ func TestHandlers_Patch(t *testing.T) { for _, test := range testCases { t.Run(test.desc, func(t *testing.T) { + t.Parallel() + var ( gotID string gotPatch db.PluginPatch ) testDB := mockDB{ - patchFn: func(_ context.Context, id string, patch db.PluginPatch) (db.Plugin, error) { + patchFn: func(_ context.Context, id string, patch db.PluginPatch) error { gotID = id gotPatch = patch - return db.Plugin{ID: id, Name: "github.com/traefik/plugindemo"}, nil + return nil }, } @@ -99,54 +101,66 @@ func TestHandlers_Patch(t *testing.T) { New(testDB, nil, nil, "").Patch(rw, req) - require.Equal(t, http.StatusOK, rw.Code) + require.Equal(t, http.StatusNoContent, rw.Code) + assert.Empty(t, rw.Body.String()) assert.Equal(t, "123", gotID) assert.Equal(t, test.wantPatch, gotPatch) }) } } -func TestHandlers_Patch_badRequest(t *testing.T) { - testDB := mockDB{ - patchFn: func(_ context.Context, _ string, _ db.PluginPatch) (db.Plugin, error) { - t.Fatal("store should not be called on invalid input") - return db.Plugin{}, nil - }, - } - +func TestHandlers_Patch_invalid(t *testing.T) { testCases := []struct { - desc string - path string - body string + desc string + path string + body string + wantCode int }{ - {desc: "empty body", path: "/123", body: ""}, - {desc: "empty object", path: "/123", body: `{}`}, - {desc: "null values", path: "/123", body: `{"stars":null}`}, - {desc: "unknown field", path: "/123", body: `{"name":"other"}`}, - {desc: "invalid JSON", path: "/123", body: `{`}, - {desc: "missing id", path: "/", body: `{"stars":1}`}, - {desc: "two JSON objects", path: "/123", body: `{"stars":1}{"hidden":true}`}, - {desc: "trailing data", path: "/123", body: `{"stars":1} garbage`}, - {desc: "negative stars", path: "/123", body: `{"stars":-5}`}, - {desc: "too large", path: "/123", body: `{"tagsETag":"` + strings.Repeat("a", maxPatchSize) + `"}`}, + {desc: "empty body", path: "/123", body: "", wantCode: http.StatusBadRequest}, + {desc: "empty object", path: "/123", body: `{}`, wantCode: http.StatusBadRequest}, + {desc: "null values", path: "/123", body: `{"stars":null}`, wantCode: http.StatusBadRequest}, + {desc: "unknown field", path: "/123", body: `{"name":"other"}`, wantCode: http.StatusBadRequest}, + {desc: "invalid JSON", path: "/123", body: `{`, wantCode: http.StatusBadRequest}, + {desc: "missing id", path: "/", body: `{"stars":1}`, wantCode: http.StatusBadRequest}, + {desc: "two JSON objects", path: "/123", body: `{"stars":1}{"hidden":true}`, wantCode: http.StatusBadRequest}, + {desc: "trailing data", path: "/123", body: `{"stars":1} garbage`, wantCode: http.StatusBadRequest}, + {desc: "trailing brace", path: "/123", body: `{"stars":1}}`, wantCode: http.StatusBadRequest}, + {desc: "trailing bracket", path: "/123", body: `{"stars":1}]`, wantCode: http.StatusBadRequest}, + {desc: "negative stars", path: "/123", body: `{"stars":-5}`, wantCode: http.StatusBadRequest}, + {desc: "too large", path: "/123", body: `{"tagsETag":"` + strings.Repeat("a", maxPatchSize) + `"}`, wantCode: http.StatusRequestEntityTooLarge}, } for _, test := range testCases { t.Run(test.desc, func(t *testing.T) { + t.Parallel() + + testDB := mockDB{ + patchFn: func(_ context.Context, _ string, patch db.PluginPatch) error { + // The store is the one that rejects an empty patch. + if patch == (db.PluginPatch{}) { + return db.ErrEmptyPatch + } + + t.Error("store should not be called on invalid input") + + return nil + }, + } + rw := httptest.NewRecorder() req := httptest.NewRequest(http.MethodPatch, test.path, strings.NewReader(test.body)) New(testDB, nil, nil, "").Patch(rw, req) - assert.Equal(t, http.StatusBadRequest, rw.Code) + assert.Equal(t, test.wantCode, rw.Code) }) } } func TestHandlers_Patch_notFound(t *testing.T) { testDB := mockDB{ - patchFn: func(_ context.Context, _ string, _ db.PluginPatch) (db.Plugin, error) { - return db.Plugin{}, db.NotFoundError{} + patchFn: func(_ context.Context, _ string, _ db.PluginPatch) error { + return db.NotFoundError{} }, }