fix(aeec): enforce non-empty EdgeIDs for global key environment retrieval EE-3013 (#6808)

pull/6819/head
andres-portainer 2022-04-25 11:35:14 -03:00 committed by GitHub
parent bcec6a8915
commit 5b77edb76d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package endpoints
import (
"errors"
"net/http"
httperror "github.com/portainer/libhttp/error"
@ -21,6 +22,9 @@ type endpointCreateGlobalKeyResponse struct {
// @router /endpoints/global-key [post]
func (handler *Handler) endpointCreateGlobalKey(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
edgeID := r.Header.Get(portainer.PortainerAgentEdgeIDHeader)
if edgeID == "" {
return httperror.BadRequest("Invalid Edge ID", errors.New("the Edge ID cannot be empty"))
}
// Search for existing endpoints for the given edgeID

View File

@ -0,0 +1,30 @@
package endpoints
import (
"net/http"
"net/http/httptest"
"testing"
portainer "github.com/portainer/portainer/api"
helper "github.com/portainer/portainer/api/internal/testhelpers"
)
func TestEmptyGlobalKey(t *testing.T) {
handler := NewHandler(
helper.NewTestRequestBouncer(),
)
req, err := http.NewRequest(http.MethodPost, "https://portainer.io:9443/endpoints/global-key", nil)
if err != nil {
t.Fatal("request error:", err)
}
req.Header.Set(portainer.PortainerAgentEdgeIDHeader, "")
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatal("expected a 400 response, found:", rec.Code)
}
}