2017-05-23 18:56:10 +00:00
|
|
|
package handler
|
2016-12-25 20:34:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/portainer/portainer"
|
2017-05-23 18:56:10 +00:00
|
|
|
httperror "github.com/portainer/portainer/http/error"
|
|
|
|
"github.com/portainer/portainer/http/security"
|
2016-12-25 20:34:02 +00:00
|
|
|
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UploadHandler represents an HTTP API handler for managing file uploads.
|
|
|
|
type UploadHandler struct {
|
|
|
|
*mux.Router
|
2017-03-12 16:24:15 +00:00
|
|
|
Logger *log.Logger
|
|
|
|
FileService portainer.FileService
|
2016-12-25 20:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUploadHandler returns a new instance of UploadHandler.
|
2017-05-23 18:56:10 +00:00
|
|
|
func NewUploadHandler(bouncer *security.RequestBouncer) *UploadHandler {
|
2016-12-25 20:34:02 +00:00
|
|
|
h := &UploadHandler{
|
2017-03-12 16:24:15 +00:00
|
|
|
Router: mux.NewRouter(),
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
2016-12-25 20:34:02 +00:00
|
|
|
}
|
2017-08-10 08:35:23 +00:00
|
|
|
h.Handle("/upload/tls/{certificate:(?:ca|cert|key)}",
|
2017-08-13 14:45:55 +00:00
|
|
|
bouncer.AdministratorAccess(http.HandlerFunc(h.handlePostUploadTLS))).Methods(http.MethodPost)
|
2016-12-25 20:34:02 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2017-10-15 17:24:40 +00:00
|
|
|
// handlePostUploadTLS handles POST requests on /upload/tls/{certificate:(?:ca|cert|key)}?folder=<folder>
|
2016-12-25 20:34:02 +00:00
|
|
|
func (handler *UploadHandler) handlePostUploadTLS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
certificate := vars["certificate"]
|
2017-08-10 08:35:23 +00:00
|
|
|
|
|
|
|
folder := r.FormValue("folder")
|
|
|
|
if folder == "" {
|
|
|
|
httperror.WriteErrorResponse(w, ErrInvalidQueryFormat, http.StatusBadRequest, handler.Logger)
|
2016-12-25 20:34:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file, _, err := r.FormFile("file")
|
|
|
|
defer file.Close()
|
|
|
|
if err != nil {
|
2017-05-23 18:56:10 +00:00
|
|
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
2016-12-25 20:34:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var fileType portainer.TLSFileType
|
|
|
|
switch certificate {
|
|
|
|
case "ca":
|
|
|
|
fileType = portainer.TLSFileCA
|
|
|
|
case "cert":
|
|
|
|
fileType = portainer.TLSFileCert
|
|
|
|
case "key":
|
|
|
|
fileType = portainer.TLSFileKey
|
|
|
|
default:
|
2017-05-23 18:56:10 +00:00
|
|
|
httperror.WriteErrorResponse(w, portainer.ErrUndefinedTLSFileType, http.StatusInternalServerError, handler.Logger)
|
2016-12-25 20:34:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-10 08:35:23 +00:00
|
|
|
err = handler.FileService.StoreTLSFile(folder, fileType, file)
|
2016-12-25 20:34:02 +00:00
|
|
|
if err != nil {
|
2017-05-23 18:56:10 +00:00
|
|
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
return
|
2016-12-25 20:34:02 +00:00
|
|
|
}
|
|
|
|
}
|