2018-06-11 13:13:19 +00:00
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/asaskevich/govalidator"
|
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"
|
|
|
|
"github.com/portainer/portainer/api/filesystem"
|
2018-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type settingsUpdatePayload struct {
|
2018-07-11 08:39:20 +00:00
|
|
|
LogoURL *string
|
2018-06-11 13:13:19 +00:00
|
|
|
BlackListedLabels []portainer.Pair
|
2018-07-11 08:39:20 +00:00
|
|
|
AuthenticationMethod *int
|
|
|
|
LDAPSettings *portainer.LDAPSettings
|
2018-12-30 16:02:22 +00:00
|
|
|
OAuthSettings *portainer.OAuthSettings
|
2018-07-11 08:39:20 +00:00
|
|
|
AllowBindMountsForRegularUsers *bool
|
|
|
|
AllowPrivilegedModeForRegularUsers *bool
|
2018-12-05 22:36:25 +00:00
|
|
|
EnableHostManagementFeatures *bool
|
2018-07-11 08:39:20 +00:00
|
|
|
SnapshotInterval *string
|
2018-08-07 15:43:36 +00:00
|
|
|
TemplatesURL *string
|
2019-07-25 22:38:07 +00:00
|
|
|
EdgeAgentCheckinInterval *int
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *settingsUpdatePayload) Validate(r *http.Request) error {
|
2018-12-30 16:02:22 +00:00
|
|
|
if *payload.AuthenticationMethod != 1 && *payload.AuthenticationMethod != 2 && *payload.AuthenticationMethod != 3 {
|
2019-01-16 15:53:10 +00:00
|
|
|
return portainer.Error("Invalid authentication method value. Value must be one of: 1 (internal), 2 (LDAP/AD) or 3 (OAuth)")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
2018-07-11 08:39:20 +00:00
|
|
|
if payload.LogoURL != nil && *payload.LogoURL != "" && !govalidator.IsURL(*payload.LogoURL) {
|
2018-06-11 13:13:19 +00:00
|
|
|
return portainer.Error("Invalid logo URL. Must correspond to a valid URL format")
|
|
|
|
}
|
2018-08-07 15:43:36 +00:00
|
|
|
if payload.TemplatesURL != nil && *payload.TemplatesURL != "" && !govalidator.IsURL(*payload.TemplatesURL) {
|
|
|
|
return portainer.Error("Invalid external templates URL. Must correspond to a valid URL format")
|
|
|
|
}
|
2018-06-11 13:13:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PUT request on /api/settings
|
|
|
|
func (handler *Handler) settingsUpdate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
var payload settingsUpdatePayload
|
|
|
|
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err}
|
|
|
|
}
|
|
|
|
|
2018-07-11 08:39:20 +00:00
|
|
|
settings, err := handler.SettingsService.Settings()
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve the settings from the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.AuthenticationMethod != nil {
|
|
|
|
settings.AuthenticationMethod = portainer.AuthenticationMethod(*payload.AuthenticationMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.LogoURL != nil {
|
|
|
|
settings.LogoURL = *payload.LogoURL
|
|
|
|
}
|
|
|
|
|
2018-08-07 15:43:36 +00:00
|
|
|
if payload.TemplatesURL != nil {
|
|
|
|
settings.TemplatesURL = *payload.TemplatesURL
|
|
|
|
}
|
|
|
|
|
2018-07-11 08:39:20 +00:00
|
|
|
if payload.BlackListedLabels != nil {
|
|
|
|
settings.BlackListedLabels = payload.BlackListedLabels
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.LDAPSettings != nil {
|
2019-01-30 01:53:14 +00:00
|
|
|
ldapPassword := settings.LDAPSettings.Password
|
|
|
|
if payload.LDAPSettings.Password != "" {
|
|
|
|
ldapPassword = payload.LDAPSettings.Password
|
|
|
|
}
|
2018-07-11 08:39:20 +00:00
|
|
|
settings.LDAPSettings = *payload.LDAPSettings
|
2019-01-30 01:53:14 +00:00
|
|
|
settings.LDAPSettings.Password = ldapPassword
|
2018-07-11 08:39:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 16:02:22 +00:00
|
|
|
if payload.OAuthSettings != nil {
|
2018-12-30 16:39:16 +00:00
|
|
|
clientSecret := payload.OAuthSettings.ClientSecret
|
|
|
|
if clientSecret == "" {
|
|
|
|
clientSecret = settings.OAuthSettings.ClientSecret
|
|
|
|
}
|
2018-12-30 16:02:22 +00:00
|
|
|
settings.OAuthSettings = *payload.OAuthSettings
|
2018-12-30 16:39:16 +00:00
|
|
|
settings.OAuthSettings.ClientSecret = clientSecret
|
2018-12-30 16:02:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 08:39:20 +00:00
|
|
|
if payload.AllowBindMountsForRegularUsers != nil {
|
|
|
|
settings.AllowBindMountsForRegularUsers = *payload.AllowBindMountsForRegularUsers
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.AllowPrivilegedModeForRegularUsers != nil {
|
|
|
|
settings.AllowPrivilegedModeForRegularUsers = *payload.AllowPrivilegedModeForRegularUsers
|
|
|
|
}
|
|
|
|
|
2018-12-05 22:36:25 +00:00
|
|
|
if payload.EnableHostManagementFeatures != nil {
|
|
|
|
settings.EnableHostManagementFeatures = *payload.EnableHostManagementFeatures
|
|
|
|
}
|
|
|
|
|
2018-07-11 08:39:20 +00:00
|
|
|
if payload.SnapshotInterval != nil && *payload.SnapshotInterval != settings.SnapshotInterval {
|
2018-11-06 09:49:48 +00:00
|
|
|
err := handler.updateSnapshotInterval(settings, *payload.SnapshotInterval)
|
2018-11-05 20:58:15 +00:00
|
|
|
if err != nil {
|
2018-11-06 09:49:48 +00:00
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to update snapshot interval", err}
|
2018-11-05 20:58:15 +00:00
|
|
|
}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 22:38:07 +00:00
|
|
|
if payload.EdgeAgentCheckinInterval != nil {
|
|
|
|
settings.EdgeAgentCheckinInterval = *payload.EdgeAgentCheckinInterval
|
|
|
|
}
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
tlsError := handler.updateTLS(settings)
|
|
|
|
if tlsError != nil {
|
|
|
|
return tlsError
|
|
|
|
}
|
|
|
|
|
2018-06-19 11:15:10 +00:00
|
|
|
err = handler.SettingsService.UpdateSettings(settings)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist settings changes inside the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(w, settings)
|
|
|
|
}
|
|
|
|
|
2018-11-06 09:49:48 +00:00
|
|
|
func (handler *Handler) updateSnapshotInterval(settings *portainer.Settings, snapshotInterval string) error {
|
|
|
|
settings.SnapshotInterval = snapshotInterval
|
|
|
|
|
|
|
|
schedules, err := handler.ScheduleService.SchedulesByJobType(portainer.SnapshotJobType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(schedules) != 0 {
|
|
|
|
snapshotSchedule := schedules[0]
|
|
|
|
snapshotSchedule.CronExpression = "@every " + snapshotInterval
|
|
|
|
|
2018-11-13 01:39:26 +00:00
|
|
|
err := handler.JobScheduler.UpdateSystemJobSchedule(portainer.SnapshotJobType, snapshotSchedule.CronExpression)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.ScheduleService.UpdateSchedule(snapshotSchedule.ID, &snapshotSchedule)
|
2018-11-06 09:49:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
func (handler *Handler) updateTLS(settings *portainer.Settings) *httperror.HandlerError {
|
|
|
|
if (settings.LDAPSettings.TLSConfig.TLS || settings.LDAPSettings.StartTLS) && !settings.LDAPSettings.TLSConfig.TLSSkipVerify {
|
|
|
|
caCertPath, _ := handler.FileService.GetPathForTLSFile(filesystem.LDAPStorePath, portainer.TLSFileCA)
|
|
|
|
settings.LDAPSettings.TLSConfig.TLSCACertPath = caCertPath
|
|
|
|
} else {
|
|
|
|
settings.LDAPSettings.TLSConfig.TLSCACertPath = ""
|
|
|
|
err := handler.FileService.DeleteTLSFiles(filesystem.LDAPStorePath)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove TLS files from disk", err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|