2018-06-11 13:13:19 +00:00
|
|
|
package upload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2018-09-10 10:01:38 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/request"
|
|
|
|
"github.com/portainer/libhttp/response"
|
2021-02-23 03:21:39 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2020-07-07 21:57:52 +00:00
|
|
|
"github.com/portainer/portainer/api/filesystem"
|
2018-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// @id UploadTLS
|
|
|
|
// @summary Upload TLS files
|
2021-09-20 00:14:22 +00:00
|
|
|
// @description Use this environment(endpoint) to upload TLS files.
|
2021-02-23 03:21:39 +00:00
|
|
|
// @description **Access policy**: administrator
|
|
|
|
// @tags upload
|
2021-11-30 02:31:16 +00:00
|
|
|
// @security ApiKeyAuth
|
2021-02-23 03:21:39 +00:00
|
|
|
// @security jwt
|
|
|
|
// @accept multipart/form-data
|
|
|
|
// @produce json
|
|
|
|
// @param certificate path string true "TLS file type. Valid values are 'ca', 'cert' or 'key'." Enums(ca,cert,key)
|
|
|
|
// @param folder formData string true "Folder where the TLS file will be stored. Will be created if not existing"
|
|
|
|
// @param file formData file true "The file to upload"
|
|
|
|
// @success 204 "Success"
|
|
|
|
// @failure 400 "Invalid request"
|
|
|
|
// @failure 500 "Server error"
|
|
|
|
// @router /upload/tls/{certificate} [post]
|
2018-06-11 13:13:19 +00:00
|
|
|
func (handler *Handler) uploadTLS(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
certificate, err := request.RetrieveRouteVariableValue(r, "certificate")
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid certificate route variable", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
folder, err := request.RetrieveMultiPartFormValue(r, "folder", false)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid query parameter: folder", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 10:01:38 +00:00
|
|
|
file, _, err := request.RetrieveMultiPartFormFile(r, "file")
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid certificate file. Ensure that the certificate file is uploaded correctly", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var fileType portainer.TLSFileType
|
|
|
|
switch certificate {
|
|
|
|
case "ca":
|
|
|
|
fileType = portainer.TLSFileCA
|
|
|
|
case "cert":
|
|
|
|
fileType = portainer.TLSFileCert
|
|
|
|
case "key":
|
|
|
|
fileType = portainer.TLSFileKey
|
|
|
|
default:
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid certificate route value. Value must be one of: ca, cert or key", filesystem.ErrUndefinedTLSFileType)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = handler.FileService.StoreTLSFileFromBytes(folder, fileType, file)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to persist certificate file on disk", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.Empty(w)
|
|
|
|
}
|