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"
2019-03-21 01:20:14 +00:00
"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
)
// POST request on /api/upload/tls/{certificate:(?:ca|cert|key)}?folder=<folder>
func ( handler * Handler ) uploadTLS ( w http . ResponseWriter , r * http . Request ) * httperror . HandlerError {
certificate , err := request . RetrieveRouteVariableValue ( r , "certificate" )
if err != nil {
return & httperror . HandlerError { http . StatusBadRequest , "Invalid certificate route variable" , err }
}
folder , err := request . RetrieveMultiPartFormValue ( r , "folder" , false )
if err != nil {
return & httperror . HandlerError { http . StatusBadRequest , "Invalid query parameter: folder" , err }
}
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 {
return & httperror . HandlerError { http . StatusBadRequest , "Invalid certificate file. Ensure that the certificate file is uploaded correctly" , err }
}
var fileType portainer . TLSFileType
switch certificate {
case "ca" :
fileType = portainer . TLSFileCA
case "cert" :
fileType = portainer . TLSFileCert
case "key" :
fileType = portainer . TLSFileKey
default :
2020-07-07 21:57:52 +00:00
return & httperror . HandlerError { http . StatusBadRequest , "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 {
return & httperror . HandlerError { http . StatusInternalServerError , "Unable to persist certificate file on disk" , err }
}
return response . Empty ( w )
}