2020-07-06 23:18:39 +00:00
|
|
|
package customtemplates
|
|
|
|
|
|
|
|
import (
|
2022-05-31 10:00:47 +00:00
|
|
|
"encoding/json"
|
2020-07-06 23:18:39 +00:00
|
|
|
"errors"
|
2022-09-28 17:56:32 +00:00
|
|
|
"fmt"
|
2020-07-06 23:18:39 +00:00
|
|
|
"net/http"
|
2022-03-04 04:05:34 +00:00
|
|
|
"os"
|
2021-09-29 19:47:39 +00:00
|
|
|
"regexp"
|
2020-07-06 23:18:39 +00:00
|
|
|
"strconv"
|
|
|
|
|
2022-10-05 09:33:59 +00:00
|
|
|
"github.com/asaskevich/govalidator"
|
2020-07-06 23:18:39 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/request"
|
|
|
|
"github.com/portainer/libhttp/response"
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
"github.com/portainer/portainer/api/filesystem"
|
2022-10-05 09:33:59 +00:00
|
|
|
gittypes "github.com/portainer/portainer/api/git/types"
|
2020-07-06 23:18:39 +00:00
|
|
|
"github.com/portainer/portainer/api/http/security"
|
|
|
|
"github.com/portainer/portainer/api/internal/authorization"
|
2022-09-16 16:18:44 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-07-06 23:18:39 +00:00
|
|
|
)
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// @id CustomTemplateCreate
|
|
|
|
// @summary Create a custom template
|
|
|
|
// @description Create a custom template.
|
|
|
|
// @description **Access policy**: authenticated
|
|
|
|
// @tags custom_templates
|
2021-11-30 02:31:16 +00:00
|
|
|
// @security ApiKeyAuth
|
2021-02-23 03:21:39 +00:00
|
|
|
// @security jwt
|
2021-09-13 03:42:53 +00:00
|
|
|
// @accept json,multipart/form-data
|
2021-02-23 03:21:39 +00:00
|
|
|
// @produce json
|
|
|
|
// @param method query string true "method for creating template" Enums(string, file, repository)
|
|
|
|
// @param body_string body customTemplateFromFileContentPayload false "Required when using method=string"
|
|
|
|
// @param body_repository body customTemplateFromGitRepositoryPayload false "Required when using method=repository"
|
|
|
|
// @param Title formData string false "Title of the template. required when method is file"
|
|
|
|
// @param Description formData string false "Description of the template. required when method is file"
|
|
|
|
// @param Note formData string false "A note that will be displayed in the UI. Supports HTML content"
|
|
|
|
// @param Platform formData int false "Platform associated to the template (1 - 'linux', 2 - 'windows'). required when method is file" Enums(1,2)
|
|
|
|
// @param Type formData int false "Type of created stack (1 - swarm, 2 - compose), required when method is file" Enums(1,2)
|
|
|
|
// @param file formData file false "required when method is file"
|
|
|
|
// @success 200 {object} portainer.CustomTemplate
|
|
|
|
// @failure 400 "Invalid request"
|
|
|
|
// @failure 500 "Server error"
|
|
|
|
// @router /custom_templates [post]
|
2020-07-06 23:18:39 +00:00
|
|
|
func (handler *Handler) customTemplateCreate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
method, err := request.RetrieveQueryParameter(r, "method", false)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid query parameter: method", err)
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tokenData, err := security.RetrieveTokenData(r)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to retrieve user details from authentication token", err)
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
customTemplate, err := handler.createCustomTemplate(method, r)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to create custom template", err)
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
customTemplate.CreatedByUserID = tokenData.ID
|
|
|
|
|
|
|
|
customTemplates, err := handler.DataStore.CustomTemplate().CustomTemplates()
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to retrieve custom templates from the database", err)
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, existingTemplate := range customTemplates {
|
|
|
|
if existingTemplate.Title == customTemplate.Title {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Template name must be unique", errors.New("Template name must be unique"))
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
chore(store) EE-1981: Refactor/store/error checking, and other refactoring (#6173)
* use the Store interface IsErrObjectNotFound() to avoid revealing internal errors
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* what happens when you extract the datastore interfaces into their own package
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* Start renaming Storage methods
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* extract the boltdb specific code from the Portainer storage code (example, the others need the same)
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* more extract bolt.Tx from datastore code
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* minimise imports by putting moving the struct definition into the file that needs the Service imports
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* more extraction of boltdb.Tx
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* extract the use of bucket.SetSequence
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* almost done - just endpoint.Synchonise :/
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* so, endpoint.Synchonize looks hard, but i can't find where we use it, so 'delete first refactoring'
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* fix test compile errors
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* test compile fixes after rebase
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* fix a mis-remembering I had wrt deserialisation - last time i used AnyData - jsoniter's bindTo looks interesting for the same reason
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* set us up to make the connection an interface
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* make the db connection a datastore interface, and separate out our datastore services from the bolt ones
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* rename methods to something less oltdb internals specific
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* these errors are not boltdb secific
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* start using the db-backend factory method too
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* export boltdb raw in case we can't export from the service layer
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add a raw export from boltdb to yaml for broken db's, and an export services to yaml in backup
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add the version info by hand for now
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* actually, the export from services can be fully typed - its the import that needs to do more work
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* redo raw export, and make import capable of using it
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add DockerHub
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* migration from anything older than v1.21.0 has been broken for quite a while, deleting the un-tested code
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* fix go test ./... again
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* my goland wasn't setup to gofmt
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* move the two extremely dubious migration tests down into store, so they can use the test store code
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* the migrator is now free of boltdb
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* reverse goland overzealous replcement of internal with boltdb
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* more undo over-zealous goland internal->boltdb
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* yay, now bolt is only mentioned inside the api/database/ dir
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* and this might be the last of the boltdb references?
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add todo
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* extract the store code into a separate module too
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* don't need the fileService in boltdb anymore
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* use IsErrObjectNotFound()
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* use a string to select what database backend we use
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* make isNew store an ephemeral bool that doesn't stay true after we've initialised it
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* move the import.json wip to a separate file so its more obvious - we'll be using it for testing, emergency fixups, and in the next part of the store work, when we improve migrations and data model lifecycles
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* undo vscode formatting html
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* fix app templates symbol (#6221)
* feat(webhook) EE-2125 send registry auth haeder when update swarms service via webhook (#6220)
* feat(webhook) EE-2125 add some helpers to registry utils
* feat(webhook) EE-2125 persist registryID when creating a webhook
* feat(webhook) EE-2125 send registry auth header when executing a webhook
* feat(webhook) EE-2125 send registryID to backend when creating a service with webhook
* feat(webhook) EE-2125 use the initial registry ID to create webhook on editing service screen
* feat(webhook) EE-2125 update webhook when update registry
* feat(webhook) EE-2125 add endpoint of update webhook
* feat(webhook) EE-2125 code cleanup
* feat(webhook) EE-2125 fix a typo
* feat(webhook) EE-2125 fix circle import issue with unit test
Co-authored-by: Simon Meng <simon.meng@portainer.io>
* fix(kubeconfig): show kubeconfig download button for non admin users [EE-2123] (#6204)
Co-authored-by: Simon Meng <simon.meng@portainer.io>
* fix data-cy for k8s cluster menu (#6226)
LGTM
* feat(stack): make stack created from app template editable EE-1941 (#6104)
feat(stack): make stack from app template editable
* fix(container):disable Duplicate/Edit button when the container is portainer (#6223)
* fix/ee-1909/show-pull-image-error (#6195)
Co-authored-by: sunportainer <ericsun@SG1.local>
* feat(cy): add data-cy to helm install button (#6241)
* feat(cy): add data-cy to add registry button (#6242)
* refactor(app): convert root folder files to es6 (#4159)
* refactor(app): duplicate constants as es6 exports (#4158)
* fix(docker): provide workaround to save network name variable (#6080)
* fix/EE-1862/unable-to-stop-or-remove-stack workaround for var without default value in yaml file
* fix/EE-1862/unable-to-stop-or-remove-stack check yaml file
* fixed func and var names
* wrapper error and used bool for stringset
* UT case for createNetworkEnvFile
* UT case for %s=%s
* powerful StringSet
* wrapper error for extract network name
* wrapper all the return err
* store more env
* put to env file
* make default value None
* feat: gzip static resources (#6258)
* fix(ssl)//handle --sslcert and --sslkey ee-2106 (#6203)
* fix/ee-2106/handle-sslcert-sslkey
Co-authored-by: sunportainer <ericsun@SG1.local>
* fix(server):support disable https only ee-2068 (#6232)
* fix/ee-2068/disable-forcely-https
* feat(store): implement store tests EE-2112 (#6224)
* add store tests
* add some more tests
* Update missing helm user repo methods
* remove redundant comments
* add webhook export
* update webhooks
* use the Store interface IsErrObjectNotFound() to avoid revealing internal errors
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* what happens when you extract the datastore interfaces into their own package
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* Start renaming Storage methods
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* extract the boltdb specific code from the Portainer storage code (example, the others need the same)
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* more extract bolt.Tx from datastore code
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* minimise imports by putting moving the struct definition into the file that needs the Service imports
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* more extraction of boltdb.Tx
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* extract the use of bucket.SetSequence
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* almost done - just endpoint.Synchonise :/
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* so, endpoint.Synchonize looks hard, but i can't find where we use it, so 'delete first refactoring'
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* fix test compile errors
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* test compile fixes after rebase
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* fix a mis-remembering I had wrt deserialisation - last time i used AnyData - jsoniter's bindTo looks interesting for the same reason
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* set us up to make the connection an interface
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* make the db connection a datastore interface, and separate out our datastore services from the bolt ones
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* rename methods to something less oltdb internals specific
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* these errors are not boltdb secific
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* start using the db-backend factory method too
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* export boltdb raw in case we can't export from the service layer
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add a raw export from boltdb to yaml for broken db's, and an export services to yaml in backup
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add the version info by hand for now
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* actually, the export from services can be fully typed - its the import that needs to do more work
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* redo raw export, and make import capable of using it
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add DockerHub
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* migration from anything older than v1.21.0 has been broken for quite a while, deleting the un-tested code
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* fix go test ./... again
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* my goland wasn't setup to gofmt
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* move the two extremely dubious migration tests down into store, so they can use the test store code
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* the migrator is now free of boltdb
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* reverse goland overzealous replcement of internal with boltdb
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* more undo over-zealous goland internal->boltdb
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* yay, now bolt is only mentioned inside the api/database/ dir
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* and this might be the last of the boltdb references?
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add todo
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* extract the store code into a separate module too
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* don't need the fileService in boltdb anymore
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* use IsErrObjectNotFound()
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* use a string to select what database backend we use
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* make isNew store an ephemeral bool that doesn't stay true after we've initialised it
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* move the import.json wip to a separate file so its more obvious - we'll be using it for testing, emergency fixups, and in the next part of the store work, when we improve migrations and data model lifecycles
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* undo vscode formatting html
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* Update missing helm user repo methods
* feat(store): implement store tests EE-2112 (#6224)
* add store tests
* add some more tests
* remove redundant comments
* add webhook export
* update webhooks
* fix build issues after rebasing
* move migratorparams
* remove unneeded integer type conversions
* disable the db import/export for now
Co-authored-by: Richard Wei <54336863+WaysonWei@users.noreply.github.com>
Co-authored-by: cong meng <mcpacino@gmail.com>
Co-authored-by: Simon Meng <simon.meng@portainer.io>
Co-authored-by: Marcelo Rydel <marcelorydel26@gmail.com>
Co-authored-by: Hao Zhang <hao.zhang@portainer.io>
Co-authored-by: sunportainer <93502624+sunportainer@users.noreply.github.com>
Co-authored-by: sunportainer <ericsun@SG1.local>
Co-authored-by: wheresolivia <78844659+wheresolivia@users.noreply.github.com>
Co-authored-by: Chaim Lev-Ari <chiptus@users.noreply.github.com>
Co-authored-by: Chao Geng <93526589+chaogeng77977@users.noreply.github.com>
Co-authored-by: Dmitry Salakhov <to@dimasalakhov.com>
Co-authored-by: Matt Hook <hookenz@gmail.com>
2021-12-15 02:26:09 +00:00
|
|
|
err = handler.DataStore.CustomTemplate().Create(customTemplate)
|
2020-07-06 23:18:39 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to create custom template", err)
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resourceControl := authorization.NewPrivateResourceControl(strconv.Itoa(int(customTemplate.ID)), portainer.CustomTemplateResourceControl, tokenData.ID)
|
|
|
|
|
chore(store) EE-1981: Refactor/store/error checking, and other refactoring (#6173)
* use the Store interface IsErrObjectNotFound() to avoid revealing internal errors
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* what happens when you extract the datastore interfaces into their own package
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* Start renaming Storage methods
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* extract the boltdb specific code from the Portainer storage code (example, the others need the same)
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* more extract bolt.Tx from datastore code
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* minimise imports by putting moving the struct definition into the file that needs the Service imports
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* more extraction of boltdb.Tx
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* extract the use of bucket.SetSequence
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* almost done - just endpoint.Synchonise :/
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* so, endpoint.Synchonize looks hard, but i can't find where we use it, so 'delete first refactoring'
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* fix test compile errors
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* test compile fixes after rebase
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* fix a mis-remembering I had wrt deserialisation - last time i used AnyData - jsoniter's bindTo looks interesting for the same reason
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* set us up to make the connection an interface
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* make the db connection a datastore interface, and separate out our datastore services from the bolt ones
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* rename methods to something less oltdb internals specific
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* these errors are not boltdb secific
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* start using the db-backend factory method too
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* export boltdb raw in case we can't export from the service layer
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add a raw export from boltdb to yaml for broken db's, and an export services to yaml in backup
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add the version info by hand for now
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* actually, the export from services can be fully typed - its the import that needs to do more work
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* redo raw export, and make import capable of using it
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add DockerHub
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* migration from anything older than v1.21.0 has been broken for quite a while, deleting the un-tested code
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* fix go test ./... again
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* my goland wasn't setup to gofmt
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* move the two extremely dubious migration tests down into store, so they can use the test store code
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* the migrator is now free of boltdb
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* reverse goland overzealous replcement of internal with boltdb
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* more undo over-zealous goland internal->boltdb
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* yay, now bolt is only mentioned inside the api/database/ dir
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* and this might be the last of the boltdb references?
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add todo
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* extract the store code into a separate module too
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* don't need the fileService in boltdb anymore
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* use IsErrObjectNotFound()
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* use a string to select what database backend we use
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* make isNew store an ephemeral bool that doesn't stay true after we've initialised it
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* move the import.json wip to a separate file so its more obvious - we'll be using it for testing, emergency fixups, and in the next part of the store work, when we improve migrations and data model lifecycles
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* undo vscode formatting html
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* fix app templates symbol (#6221)
* feat(webhook) EE-2125 send registry auth haeder when update swarms service via webhook (#6220)
* feat(webhook) EE-2125 add some helpers to registry utils
* feat(webhook) EE-2125 persist registryID when creating a webhook
* feat(webhook) EE-2125 send registry auth header when executing a webhook
* feat(webhook) EE-2125 send registryID to backend when creating a service with webhook
* feat(webhook) EE-2125 use the initial registry ID to create webhook on editing service screen
* feat(webhook) EE-2125 update webhook when update registry
* feat(webhook) EE-2125 add endpoint of update webhook
* feat(webhook) EE-2125 code cleanup
* feat(webhook) EE-2125 fix a typo
* feat(webhook) EE-2125 fix circle import issue with unit test
Co-authored-by: Simon Meng <simon.meng@portainer.io>
* fix(kubeconfig): show kubeconfig download button for non admin users [EE-2123] (#6204)
Co-authored-by: Simon Meng <simon.meng@portainer.io>
* fix data-cy for k8s cluster menu (#6226)
LGTM
* feat(stack): make stack created from app template editable EE-1941 (#6104)
feat(stack): make stack from app template editable
* fix(container):disable Duplicate/Edit button when the container is portainer (#6223)
* fix/ee-1909/show-pull-image-error (#6195)
Co-authored-by: sunportainer <ericsun@SG1.local>
* feat(cy): add data-cy to helm install button (#6241)
* feat(cy): add data-cy to add registry button (#6242)
* refactor(app): convert root folder files to es6 (#4159)
* refactor(app): duplicate constants as es6 exports (#4158)
* fix(docker): provide workaround to save network name variable (#6080)
* fix/EE-1862/unable-to-stop-or-remove-stack workaround for var without default value in yaml file
* fix/EE-1862/unable-to-stop-or-remove-stack check yaml file
* fixed func and var names
* wrapper error and used bool for stringset
* UT case for createNetworkEnvFile
* UT case for %s=%s
* powerful StringSet
* wrapper error for extract network name
* wrapper all the return err
* store more env
* put to env file
* make default value None
* feat: gzip static resources (#6258)
* fix(ssl)//handle --sslcert and --sslkey ee-2106 (#6203)
* fix/ee-2106/handle-sslcert-sslkey
Co-authored-by: sunportainer <ericsun@SG1.local>
* fix(server):support disable https only ee-2068 (#6232)
* fix/ee-2068/disable-forcely-https
* feat(store): implement store tests EE-2112 (#6224)
* add store tests
* add some more tests
* Update missing helm user repo methods
* remove redundant comments
* add webhook export
* update webhooks
* use the Store interface IsErrObjectNotFound() to avoid revealing internal errors
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* what happens when you extract the datastore interfaces into their own package
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* Start renaming Storage methods
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* extract the boltdb specific code from the Portainer storage code (example, the others need the same)
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* more extract bolt.Tx from datastore code
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* minimise imports by putting moving the struct definition into the file that needs the Service imports
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* more extraction of boltdb.Tx
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* extract the use of bucket.SetSequence
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* almost done - just endpoint.Synchonise :/
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* so, endpoint.Synchonize looks hard, but i can't find where we use it, so 'delete first refactoring'
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* fix test compile errors
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* test compile fixes after rebase
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* fix a mis-remembering I had wrt deserialisation - last time i used AnyData - jsoniter's bindTo looks interesting for the same reason
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* set us up to make the connection an interface
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* make the db connection a datastore interface, and separate out our datastore services from the bolt ones
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* rename methods to something less oltdb internals specific
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* these errors are not boltdb secific
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* start using the db-backend factory method too
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* export boltdb raw in case we can't export from the service layer
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add a raw export from boltdb to yaml for broken db's, and an export services to yaml in backup
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add the version info by hand for now
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* actually, the export from services can be fully typed - its the import that needs to do more work
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* redo raw export, and make import capable of using it
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add DockerHub
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* migration from anything older than v1.21.0 has been broken for quite a while, deleting the un-tested code
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* fix go test ./... again
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* my goland wasn't setup to gofmt
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* move the two extremely dubious migration tests down into store, so they can use the test store code
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* the migrator is now free of boltdb
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* reverse goland overzealous replcement of internal with boltdb
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* more undo over-zealous goland internal->boltdb
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* yay, now bolt is only mentioned inside the api/database/ dir
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* and this might be the last of the boltdb references?
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* add todo
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* extract the store code into a separate module too
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* don't need the fileService in boltdb anymore
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* use IsErrObjectNotFound()
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* use a string to select what database backend we use
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* make isNew store an ephemeral bool that doesn't stay true after we've initialised it
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* move the import.json wip to a separate file so its more obvious - we'll be using it for testing, emergency fixups, and in the next part of the store work, when we improve migrations and data model lifecycles
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* undo vscode formatting html
Signed-off-by: Sven Dowideit <sven.dowideit@portainer.io>
* Update missing helm user repo methods
* feat(store): implement store tests EE-2112 (#6224)
* add store tests
* add some more tests
* remove redundant comments
* add webhook export
* update webhooks
* fix build issues after rebasing
* move migratorparams
* remove unneeded integer type conversions
* disable the db import/export for now
Co-authored-by: Richard Wei <54336863+WaysonWei@users.noreply.github.com>
Co-authored-by: cong meng <mcpacino@gmail.com>
Co-authored-by: Simon Meng <simon.meng@portainer.io>
Co-authored-by: Marcelo Rydel <marcelorydel26@gmail.com>
Co-authored-by: Hao Zhang <hao.zhang@portainer.io>
Co-authored-by: sunportainer <93502624+sunportainer@users.noreply.github.com>
Co-authored-by: sunportainer <ericsun@SG1.local>
Co-authored-by: wheresolivia <78844659+wheresolivia@users.noreply.github.com>
Co-authored-by: Chaim Lev-Ari <chiptus@users.noreply.github.com>
Co-authored-by: Chao Geng <93526589+chaogeng77977@users.noreply.github.com>
Co-authored-by: Dmitry Salakhov <to@dimasalakhov.com>
Co-authored-by: Matt Hook <hookenz@gmail.com>
2021-12-15 02:26:09 +00:00
|
|
|
err = handler.DataStore.ResourceControl().Create(resourceControl)
|
2020-07-06 23:18:39 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to persist resource control inside the database", err)
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
customTemplate.ResourceControl = resourceControl
|
|
|
|
|
|
|
|
return response.JSON(w, customTemplate)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) createCustomTemplate(method string, r *http.Request) (*portainer.CustomTemplate, error) {
|
|
|
|
switch method {
|
|
|
|
case "string":
|
|
|
|
return handler.createCustomTemplateFromFileContent(r)
|
|
|
|
case "repository":
|
|
|
|
return handler.createCustomTemplateFromGitRepository(r)
|
|
|
|
case "file":
|
|
|
|
return handler.createCustomTemplateFromFileUpload(r)
|
|
|
|
}
|
|
|
|
return nil, errors.New("Invalid value for query parameter: method. Value must be one of: string, repository or file")
|
|
|
|
}
|
|
|
|
|
|
|
|
type customTemplateFromFileContentPayload struct {
|
2021-02-23 03:21:39 +00:00
|
|
|
// URL of the template's logo
|
|
|
|
Logo string `example:"https://cloudinovasi.id/assets/img/logos/nginx.png"`
|
|
|
|
// Title of the template
|
|
|
|
Title string `example:"Nginx" validate:"required"`
|
|
|
|
// Description of the template
|
|
|
|
Description string `example:"High performance web server" validate:"required"`
|
|
|
|
// A note that will be displayed in the UI. Supports HTML content
|
|
|
|
Note string `example:"This is my <b>custom</b> template"`
|
|
|
|
// Platform associated to the template.
|
|
|
|
// Valid values are: 1 - 'linux', 2 - 'windows'
|
2021-09-02 05:28:51 +00:00
|
|
|
// Required for Docker stacks
|
|
|
|
Platform portainer.CustomTemplatePlatform `example:"1" enums:"1,2"`
|
|
|
|
// Type of created stack (1 - swarm, 2 - compose, 3 - kubernetes)
|
|
|
|
Type portainer.StackType `example:"1" enums:"1,2,3" validate:"required"`
|
2021-02-23 03:21:39 +00:00
|
|
|
// Content of stack file
|
|
|
|
FileContent string `validate:"required"`
|
2022-05-31 10:00:47 +00:00
|
|
|
// Definitions of variables in the stack file
|
|
|
|
Variables []portainer.CustomTemplateVariableDefinition
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *customTemplateFromFileContentPayload) Validate(r *http.Request) error {
|
|
|
|
if govalidator.IsNull(payload.Title) {
|
2020-07-08 00:25:37 +00:00
|
|
|
return errors.New("Invalid custom template title")
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
if govalidator.IsNull(payload.Description) {
|
2020-07-08 00:25:37 +00:00
|
|
|
return errors.New("Invalid custom template description")
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
if govalidator.IsNull(payload.FileContent) {
|
2020-07-08 00:25:37 +00:00
|
|
|
return errors.New("Invalid file content")
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
2021-09-02 05:28:51 +00:00
|
|
|
if payload.Type != portainer.KubernetesStack && payload.Platform != portainer.CustomTemplatePlatformLinux && payload.Platform != portainer.CustomTemplatePlatformWindows {
|
2020-07-08 00:25:37 +00:00
|
|
|
return errors.New("Invalid custom template platform")
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
2021-09-02 05:28:51 +00:00
|
|
|
if payload.Type != portainer.KubernetesStack && payload.Type != portainer.DockerSwarmStack && payload.Type != portainer.DockerComposeStack {
|
2020-07-08 00:25:37 +00:00
|
|
|
return errors.New("Invalid custom template type")
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
2021-09-29 19:47:39 +00:00
|
|
|
if !isValidNote(payload.Note) {
|
|
|
|
return errors.New("Invalid note. <img> tag is not supported")
|
|
|
|
}
|
2022-05-31 10:00:47 +00:00
|
|
|
|
2022-10-14 21:09:07 +00:00
|
|
|
return validateVariablesDefinitions(payload.Variables)
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 19:47:39 +00:00
|
|
|
func isValidNote(note string) bool {
|
|
|
|
if govalidator.IsNull(note) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
match, _ := regexp.MatchString("<img", note)
|
|
|
|
return !match
|
|
|
|
}
|
|
|
|
|
2020-07-06 23:18:39 +00:00
|
|
|
func (handler *Handler) createCustomTemplateFromFileContent(r *http.Request) (*portainer.CustomTemplate, error) {
|
|
|
|
var payload customTemplateFromFileContentPayload
|
|
|
|
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
customTemplateID := handler.DataStore.CustomTemplate().GetNextIdentifier()
|
|
|
|
customTemplate := &portainer.CustomTemplate{
|
|
|
|
ID: portainer.CustomTemplateID(customTemplateID),
|
|
|
|
Title: payload.Title,
|
|
|
|
EntryPoint: filesystem.ComposeFileDefaultName,
|
|
|
|
Description: payload.Description,
|
|
|
|
Note: payload.Note,
|
|
|
|
Platform: (payload.Platform),
|
|
|
|
Type: (payload.Type),
|
|
|
|
Logo: payload.Logo,
|
2022-05-31 10:00:47 +00:00
|
|
|
Variables: payload.Variables,
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
templateFolder := strconv.Itoa(customTemplateID)
|
|
|
|
projectPath, err := handler.FileService.StoreCustomTemplateFileFromBytes(templateFolder, customTemplate.EntryPoint, []byte(payload.FileContent))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
customTemplate.ProjectPath = projectPath
|
|
|
|
|
|
|
|
return customTemplate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type customTemplateFromGitRepositoryPayload struct {
|
2021-02-23 03:21:39 +00:00
|
|
|
// URL of the template's logo
|
|
|
|
Logo string `example:"https://cloudinovasi.id/assets/img/logos/nginx.png"`
|
|
|
|
// Title of the template
|
|
|
|
Title string `example:"Nginx" validate:"required"`
|
|
|
|
// Description of the template
|
|
|
|
Description string `example:"High performance web server" validate:"required"`
|
|
|
|
// A note that will be displayed in the UI. Supports HTML content
|
|
|
|
Note string `example:"This is my <b>custom</b> template"`
|
|
|
|
// Platform associated to the template.
|
|
|
|
// Valid values are: 1 - 'linux', 2 - 'windows'
|
2021-09-02 05:28:51 +00:00
|
|
|
// Required for Docker stacks
|
|
|
|
Platform portainer.CustomTemplatePlatform `example:"1" enums:"1,2"`
|
2021-02-23 03:21:39 +00:00
|
|
|
// Type of created stack (1 - swarm, 2 - compose)
|
|
|
|
Type portainer.StackType `example:"1" enums:"1,2" validate:"required"`
|
|
|
|
|
|
|
|
// URL of a Git repository hosting the Stack file
|
|
|
|
RepositoryURL string `example:"https://github.com/openfaas/faas" validate:"required"`
|
|
|
|
// Reference name of a Git repository hosting the Stack file
|
|
|
|
RepositoryReferenceName string `example:"refs/heads/master"`
|
|
|
|
// Use basic authentication to clone the Git repository
|
|
|
|
RepositoryAuthentication bool `example:"true"`
|
|
|
|
// Username used in basic authentication. Required when RepositoryAuthentication is true.
|
|
|
|
RepositoryUsername string `example:"myGitUsername"`
|
|
|
|
// Password used in basic authentication. Required when RepositoryAuthentication is true.
|
|
|
|
RepositoryPassword string `example:"myGitPassword"`
|
|
|
|
// Path to the Stack file inside the Git repository
|
|
|
|
ComposeFilePathInRepository string `example:"docker-compose.yml" default:"docker-compose.yml"`
|
2022-05-31 10:00:47 +00:00
|
|
|
// Definitions of variables in the stack file
|
|
|
|
Variables []portainer.CustomTemplateVariableDefinition
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *customTemplateFromGitRepositoryPayload) Validate(r *http.Request) error {
|
|
|
|
if govalidator.IsNull(payload.Title) {
|
2020-07-08 00:25:37 +00:00
|
|
|
return errors.New("Invalid custom template title")
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
if govalidator.IsNull(payload.Description) {
|
2020-07-08 00:25:37 +00:00
|
|
|
return errors.New("Invalid custom template description")
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
if govalidator.IsNull(payload.RepositoryURL) || !govalidator.IsURL(payload.RepositoryURL) {
|
2020-07-08 00:25:37 +00:00
|
|
|
return errors.New("Invalid repository URL. Must correspond to a valid URL format")
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
if payload.RepositoryAuthentication && (govalidator.IsNull(payload.RepositoryUsername) || govalidator.IsNull(payload.RepositoryPassword)) {
|
2020-07-08 00:25:37 +00:00
|
|
|
return errors.New("Invalid repository credentials. Username and password must be specified when authentication is enabled")
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
if govalidator.IsNull(payload.ComposeFilePathInRepository) {
|
|
|
|
payload.ComposeFilePathInRepository = filesystem.ComposeFileDefaultName
|
|
|
|
}
|
2021-09-02 05:28:51 +00:00
|
|
|
|
|
|
|
if payload.Type == portainer.KubernetesStack {
|
|
|
|
return errors.New("Creating a Kubernetes custom template from git is not supported")
|
|
|
|
}
|
|
|
|
|
2020-07-06 23:18:39 +00:00
|
|
|
if payload.Platform != portainer.CustomTemplatePlatformLinux && payload.Platform != portainer.CustomTemplatePlatformWindows {
|
2020-07-08 00:25:37 +00:00
|
|
|
return errors.New("Invalid custom template platform")
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
if payload.Type != portainer.DockerSwarmStack && payload.Type != portainer.DockerComposeStack {
|
2020-07-08 00:25:37 +00:00
|
|
|
return errors.New("Invalid custom template type")
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
2021-09-29 19:47:39 +00:00
|
|
|
if !isValidNote(payload.Note) {
|
|
|
|
return errors.New("Invalid note. <img> tag is not supported")
|
|
|
|
}
|
2022-05-31 10:00:47 +00:00
|
|
|
|
2022-10-14 21:09:07 +00:00
|
|
|
return validateVariablesDefinitions(payload.Variables)
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) createCustomTemplateFromGitRepository(r *http.Request) (*portainer.CustomTemplate, error) {
|
|
|
|
var payload customTemplateFromGitRepositoryPayload
|
|
|
|
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
customTemplateID := handler.DataStore.CustomTemplate().GetNextIdentifier()
|
|
|
|
customTemplate := &portainer.CustomTemplate{
|
|
|
|
ID: portainer.CustomTemplateID(customTemplateID),
|
|
|
|
Title: payload.Title,
|
|
|
|
EntryPoint: payload.ComposeFilePathInRepository,
|
|
|
|
Description: payload.Description,
|
|
|
|
Note: payload.Note,
|
|
|
|
Platform: payload.Platform,
|
|
|
|
Type: payload.Type,
|
|
|
|
Logo: payload.Logo,
|
2022-05-31 10:00:47 +00:00
|
|
|
Variables: payload.Variables,
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
projectPath := handler.FileService.GetCustomTemplateProjectPath(strconv.Itoa(customTemplateID))
|
|
|
|
customTemplate.ProjectPath = projectPath
|
|
|
|
|
2021-06-22 07:59:05 +00:00
|
|
|
repositoryUsername := payload.RepositoryUsername
|
|
|
|
repositoryPassword := payload.RepositoryPassword
|
|
|
|
if !payload.RepositoryAuthentication {
|
|
|
|
repositoryUsername = ""
|
|
|
|
repositoryPassword = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.GitService.CloneRepository(projectPath, payload.RepositoryURL, payload.RepositoryReferenceName, repositoryUsername, repositoryPassword)
|
2020-07-06 23:18:39 +00:00
|
|
|
if err != nil {
|
2022-10-05 09:33:59 +00:00
|
|
|
if err == gittypes.ErrAuthenticationFailure {
|
2022-09-28 17:56:32 +00:00
|
|
|
return nil, fmt.Errorf("invalid git credential")
|
|
|
|
}
|
2020-07-06 23:18:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-16 16:18:44 +00:00
|
|
|
|
2022-03-04 04:05:34 +00:00
|
|
|
isValidProject := true
|
|
|
|
defer func() {
|
|
|
|
if !isValidProject {
|
|
|
|
if err := handler.FileService.RemoveDirectory(projectPath); err != nil {
|
2022-09-16 16:18:44 +00:00
|
|
|
log.Warn().Err(err).Msg("unable to remove git repository directory")
|
2022-03-04 04:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-07-06 23:18:39 +00:00
|
|
|
|
2021-11-12 14:02:10 +00:00
|
|
|
entryPath := filesystem.JoinPaths(projectPath, customTemplate.EntryPoint)
|
2022-09-16 16:18:44 +00:00
|
|
|
|
2021-11-12 14:02:10 +00:00
|
|
|
exists, err := handler.FileService.FileExists(entryPath)
|
|
|
|
if err != nil || !exists {
|
2022-03-04 04:05:34 +00:00
|
|
|
isValidProject = false
|
2021-11-12 14:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return nil, errors.New("Invalid Compose file, ensure that the Compose file path is correct")
|
|
|
|
}
|
|
|
|
|
2022-03-04 04:05:34 +00:00
|
|
|
info, err := os.Lstat(entryPath)
|
|
|
|
if err != nil {
|
|
|
|
isValidProject = false
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if info.Mode()&os.ModeSymlink != 0 { // entry is a symlink
|
|
|
|
isValidProject = false
|
|
|
|
return nil, errors.New("Invalid Compose file, ensure that the Compose file is not a symbolic link")
|
|
|
|
}
|
|
|
|
|
2020-07-06 23:18:39 +00:00
|
|
|
return customTemplate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type customTemplateFromFileUploadPayload struct {
|
|
|
|
Logo string
|
|
|
|
Title string
|
|
|
|
Description string
|
|
|
|
Note string
|
|
|
|
Platform portainer.CustomTemplatePlatform
|
|
|
|
Type portainer.StackType
|
|
|
|
FileContent []byte
|
2022-05-31 10:00:47 +00:00
|
|
|
// Definitions of variables in the stack file
|
|
|
|
Variables []portainer.CustomTemplateVariableDefinition
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *customTemplateFromFileUploadPayload) Validate(r *http.Request) error {
|
|
|
|
title, err := request.RetrieveMultiPartFormValue(r, "Title", false)
|
|
|
|
if err != nil {
|
2020-07-08 00:25:37 +00:00
|
|
|
return errors.New("Invalid custom template title")
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
payload.Title = title
|
|
|
|
|
|
|
|
description, err := request.RetrieveMultiPartFormValue(r, "Description", false)
|
|
|
|
if err != nil {
|
2020-07-08 00:25:37 +00:00
|
|
|
return errors.New("Invalid custom template description")
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
payload.Description = description
|
|
|
|
|
2021-09-20 00:20:38 +00:00
|
|
|
logo, _ := request.RetrieveMultiPartFormValue(r, "Logo", true)
|
|
|
|
payload.Logo = logo
|
|
|
|
|
2020-07-06 23:18:39 +00:00
|
|
|
note, _ := request.RetrieveMultiPartFormValue(r, "Note", true)
|
2021-09-29 19:47:39 +00:00
|
|
|
if !isValidNote(note) {
|
|
|
|
return errors.New("Invalid note. <img> tag is not supported")
|
|
|
|
}
|
2020-07-06 23:18:39 +00:00
|
|
|
payload.Note = note
|
|
|
|
|
|
|
|
typeNumeral, _ := request.RetrieveNumericMultiPartFormValue(r, "Type", true)
|
|
|
|
templateType := portainer.StackType(typeNumeral)
|
2021-09-02 05:28:51 +00:00
|
|
|
if templateType != portainer.KubernetesStack && templateType != portainer.DockerSwarmStack && templateType != portainer.DockerComposeStack {
|
2020-07-08 00:25:37 +00:00
|
|
|
return errors.New("Invalid custom template type")
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
payload.Type = templateType
|
|
|
|
|
2021-09-02 05:28:51 +00:00
|
|
|
platform, _ := request.RetrieveNumericMultiPartFormValue(r, "Platform", true)
|
|
|
|
templatePlatform := portainer.CustomTemplatePlatform(platform)
|
|
|
|
if templateType != portainer.KubernetesStack && templatePlatform != portainer.CustomTemplatePlatformLinux && templatePlatform != portainer.CustomTemplatePlatformWindows {
|
|
|
|
return errors.New("Invalid custom template platform")
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.Platform = templatePlatform
|
|
|
|
|
2021-02-17 15:56:44 +00:00
|
|
|
composeFileContent, _, err := request.RetrieveMultiPartFormFile(r, "File")
|
2020-07-06 23:18:39 +00:00
|
|
|
if err != nil {
|
2020-07-08 00:25:37 +00:00
|
|
|
return errors.New("Invalid Compose file. Ensure that the Compose file is uploaded correctly")
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
payload.FileContent = composeFileContent
|
|
|
|
|
2022-05-31 10:00:47 +00:00
|
|
|
varsString, _ := request.RetrieveMultiPartFormValue(r, "Variables", true)
|
2022-10-20 08:42:49 +00:00
|
|
|
if varsString != "" {
|
|
|
|
err = json.Unmarshal([]byte(varsString), &payload.Variables)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("Invalid variables. Ensure that the variables are valid JSON")
|
|
|
|
}
|
|
|
|
return validateVariablesDefinitions(payload.Variables)
|
2022-05-31 10:00:47 +00:00
|
|
|
}
|
2022-10-20 08:42:49 +00:00
|
|
|
return nil
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) createCustomTemplateFromFileUpload(r *http.Request) (*portainer.CustomTemplate, error) {
|
|
|
|
payload := &customTemplateFromFileUploadPayload{}
|
|
|
|
err := payload.Validate(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
customTemplateID := handler.DataStore.CustomTemplate().GetNextIdentifier()
|
|
|
|
customTemplate := &portainer.CustomTemplate{
|
|
|
|
ID: portainer.CustomTemplateID(customTemplateID),
|
|
|
|
Title: payload.Title,
|
|
|
|
Description: payload.Description,
|
|
|
|
Note: payload.Note,
|
|
|
|
Platform: payload.Platform,
|
|
|
|
Type: payload.Type,
|
|
|
|
Logo: payload.Logo,
|
|
|
|
EntryPoint: filesystem.ComposeFileDefaultName,
|
2022-05-31 10:00:47 +00:00
|
|
|
Variables: payload.Variables,
|
2020-07-06 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
templateFolder := strconv.Itoa(customTemplateID)
|
|
|
|
projectPath, err := handler.FileService.StoreCustomTemplateFileFromBytes(templateFolder, customTemplate.EntryPoint, []byte(payload.FileContent))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
customTemplate.ProjectPath = projectPath
|
|
|
|
|
|
|
|
return customTemplate, nil
|
|
|
|
}
|