From 6beace0f6e6bb10690333a6bc17295337a48a025 Mon Sep 17 00:00:00 2001 From: Yvonnick Esnault Date: Tue, 18 Aug 2020 10:50:18 +0200 Subject: [PATCH] feat(api): validate yml plugin before import it (#5378) * feat(api): validate yml plugin before import it close #5329 Signed-off-by: Yvonnick Esnault --- engine/api/grpc_plugin.go | 6 ++++++ sdk/plugin.go | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/engine/api/grpc_plugin.go b/engine/api/grpc_plugin.go index e93be60621..7e29a64f0e 100644 --- a/engine/api/grpc_plugin.go +++ b/engine/api/grpc_plugin.go @@ -26,6 +26,9 @@ func (api *API) postGRPCluginHandler() service.Handler { if err := service.UnmarshalBody(r, &p); err != nil { return sdk.WithStack(err) } + if err := p.Validate(); err != nil { + return sdk.WithStack(err) + } p.Binaries = nil tx, err := db.Begin() @@ -103,6 +106,9 @@ func (api *API) putGRPCluginHandler() service.Handler { if err := service.UnmarshalBody(r, &p); err != nil { return sdk.WithStack(err) } + if err := p.Validate(); err != nil { + return sdk.WithStack(err) + } var name = mux.Vars(r)["name"] old, err := plugin.LoadByName(api.mustDB(), name) diff --git a/sdk/plugin.go b/sdk/plugin.go index d5ccecf7d6..64b2e73231 100644 --- a/sdk/plugin.go +++ b/sdk/plugin.go @@ -19,6 +19,13 @@ type GRPCPlugin struct { Integration string `json:"integration" db:"-" yaml:"integration" cli:"integration"` } +func (p *GRPCPlugin) Validate() error { + if p.Name == "" || p.Type == "" || p.Author == "" || p.Description == "" { + return NewErrorFrom(ErrPluginInvalid, "Invalid plugin: name, type, author and description are mandatory") + } + return nil +} + // GetBinary returns the binary for a specific os and arch func (p GRPCPlugin) GetBinary(os, arch string) *GRPCPluginBinary { for _, b := range p.Binaries {