From a2886115b8781a518451966b1b69b2c5b9270004 Mon Sep 17 00:00:00 2001 From: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Date: Fri, 12 Nov 2021 11:02:10 -0300 Subject: [PATCH] fix(custom-templates): avoid creation of template if the compose file does not exist EE-1470 (#6011) fix(custom-templates): avoid creation of template if the compose file does not exist EE-1470 --- .../customtemplates/customtemplate_create.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/api/http/handler/customtemplates/customtemplate_create.go b/api/http/handler/customtemplates/customtemplate_create.go index f79aac9e9..c4e375b43 100644 --- a/api/http/handler/customtemplates/customtemplate_create.go +++ b/api/http/handler/customtemplates/customtemplate_create.go @@ -2,6 +2,7 @@ package customtemplates import ( "errors" + "log" "net/http" "regexp" "strconv" @@ -270,6 +271,23 @@ func (handler *Handler) createCustomTemplateFromGitRepository(r *http.Request) ( return nil, err } + entryPath := filesystem.JoinPaths(projectPath, customTemplate.EntryPoint) + + exists, err := handler.FileService.FileExists(entryPath) + if err != nil || !exists { + if err := handler.FileService.RemoveDirectory(projectPath); err != nil { + log.Printf("[WARN] [http,customtemplate,git] [error: %s] [message: unable to remove git repository directory]", err) + } + } + + if err != nil { + return nil, err + } + + if !exists { + return nil, errors.New("Invalid Compose file, ensure that the Compose file path is correct") + } + return customTemplate, nil }