[MM-25657] api4/config: add config migrate endpoint (#14928)

* api4/config: add config migrate endpoint

* api4/config: reflect review comments

* fix i18n lint error

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
This commit is contained in:
Ibrahim Serdar Acikgoz
2020-08-14 18:26:23 +03:00
committed by GitHub
parent bda24c8fe0
commit 16ffb6712f
5 changed files with 76 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ func (api *API) InitConfig() {
api.BaseRoutes.ApiRoot.Handle("/config/reload", api.ApiSessionRequired(configReload)).Methods("POST")
api.BaseRoutes.ApiRoot.Handle("/config/client", api.ApiHandler(getClientConfig)).Methods("GET")
api.BaseRoutes.ApiRoot.Handle("/config/environment", api.ApiSessionRequired(getEnvironmentConfig)).Methods("GET")
api.BaseRoutes.ApiRoot.Handle("/config/migrate", api.ApiSessionRequired(migrateConfig)).Methods("POST")
}
func getConfig(c *Context, w http.ResponseWriter, r *http.Request) {
@@ -227,3 +228,36 @@ func patchConfig(c *Context, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Write([]byte(c.App.GetSanitizedConfig().ToJson()))
}
func migrateConfig(c *Context, w http.ResponseWriter, r *http.Request) {
props := model.StringInterfaceFromJson(r.Body)
from, ok := props["from"].(string)
if !ok {
c.SetInvalidParam("from")
return
}
to, ok := props["to"].(string)
if !ok {
c.SetInvalidParam("to")
return
}
auditRec := c.MakeAuditRecord("migrateConfig", audit.Fail)
auditRec.AddMeta("from", from)
auditRec.AddMeta("to", to)
defer c.LogAuditRec(auditRec)
if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
return
}
err := config.Migrate(from, to)
if err != nil {
c.Err = model.NewAppError("migrateConfig", "api.config.migrate_config.app_error", nil, err.Error(), http.StatusInternalServerError)
return
}
auditRec.Success()
ReturnStatusOK(w)
}

View File

@@ -17,6 +17,7 @@ func (api *API) InitConfigLocal() {
api.BaseRoutes.ApiRoot.Handle("/config", api.ApiLocal(getConfig)).Methods("GET")
api.BaseRoutes.ApiRoot.Handle("/config", api.ApiLocal(localUpdateConfig)).Methods("PUT")
api.BaseRoutes.ApiRoot.Handle("/config/patch", api.ApiLocal(localPatchConfig)).Methods("PUT")
api.BaseRoutes.ApiRoot.Handle("/config/migrate", api.ApiLocal(migrateConfig)).Methods("POST")
}
func localUpdateConfig(c *Context, w http.ResponseWriter, r *http.Request) {

View File

@@ -9,6 +9,7 @@ import (
"strings"
"testing"
"github.com/mattermost/mattermost-server/v5/config"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -551,3 +552,26 @@ func TestPatchConfig(t *testing.T) {
require.Equal(t, nonEmptyURL, *cfg.ServiceSettings.SiteURL)
})
}
func TestMigrateConfig(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
t.Run("user is not system admin", func(t *testing.T) {
_, response := th.Client.MigrateConfig("from", "to")
CheckForbiddenStatus(t, response)
})
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
f, err := config.NewStore("from.json", false)
require.NoError(t, err)
defer f.RemoveFile("from.json")
_, err = config.NewStore("to.json", false)
require.NoError(t, err)
defer f.RemoveFile("to.json")
_, response := client.MigrateConfig("from.json", "to.json")
CheckNoError(t, response)
})
}