[MM-14521] New endpoint to validate site urls (#11944)
Этот коммит содержится в:
коммит произвёл
Ben Schumacher
родитель
451982f9d3
Коммит
f9d9dd8b60
@@ -27,6 +27,7 @@ func (api *API) InitSystem() {
|
|||||||
|
|
||||||
api.BaseRoutes.ApiRoot.Handle("/audits", api.ApiSessionRequired(getAudits)).Methods("GET")
|
api.BaseRoutes.ApiRoot.Handle("/audits", api.ApiSessionRequired(getAudits)).Methods("GET")
|
||||||
api.BaseRoutes.ApiRoot.Handle("/email/test", api.ApiSessionRequired(testEmail)).Methods("POST")
|
api.BaseRoutes.ApiRoot.Handle("/email/test", api.ApiSessionRequired(testEmail)).Methods("POST")
|
||||||
|
api.BaseRoutes.ApiRoot.Handle("/site_url/test", api.ApiSessionRequired(testSiteURL)).Methods("POST")
|
||||||
api.BaseRoutes.ApiRoot.Handle("/file/s3_test", api.ApiSessionRequired(testS3)).Methods("POST")
|
api.BaseRoutes.ApiRoot.Handle("/file/s3_test", api.ApiSessionRequired(testS3)).Methods("POST")
|
||||||
api.BaseRoutes.ApiRoot.Handle("/database/recycle", api.ApiSessionRequired(databaseRecycle)).Methods("POST")
|
api.BaseRoutes.ApiRoot.Handle("/database/recycle", api.ApiSessionRequired(databaseRecycle)).Methods("POST")
|
||||||
api.BaseRoutes.ApiRoot.Handle("/caches/invalidate", api.ApiSessionRequired(invalidateCaches)).Methods("POST")
|
api.BaseRoutes.ApiRoot.Handle("/caches/invalidate", api.ApiSessionRequired(invalidateCaches)).Methods("POST")
|
||||||
@@ -145,6 +146,32 @@ func testEmail(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
ReturnStatusOK(w)
|
ReturnStatusOK(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testSiteURL(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
|
||||||
|
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if *c.App.Config().ExperimentalSettings.RestrictSystemAdmin {
|
||||||
|
c.Err = model.NewAppError("testSiteURL", "api.restricted_system_admin", nil, "", http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
props := model.MapFromJson(r.Body)
|
||||||
|
siteURL := props["site_url"]
|
||||||
|
if siteURL == "" {
|
||||||
|
c.SetInvalidParam("site_url")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err := c.App.TestSiteURL(siteURL)
|
||||||
|
if err != nil {
|
||||||
|
c.Err = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnStatusOK(w)
|
||||||
|
}
|
||||||
|
|
||||||
func getAudits(c *Context, w http.ResponseWriter, r *http.Request) {
|
func getAudits(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
|
if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
|
||||||
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/mattermost/mattermost-server/mlog"
|
"github.com/mattermost/mattermost-server/mlog"
|
||||||
@@ -149,6 +150,47 @@ func TestEmailTest(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSiteURLTest(t *testing.T) {
|
||||||
|
th := Setup().InitBasic()
|
||||||
|
defer th.TearDown()
|
||||||
|
Client := th.Client
|
||||||
|
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if strings.HasSuffix(r.URL.Path, "/valid/api/v4/system/ping") {
|
||||||
|
w.WriteHeader(200)
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(400)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
validSiteURL := ts.URL + "/valid"
|
||||||
|
invalidSiteURL := ts.URL + "/invalid"
|
||||||
|
|
||||||
|
t.Run("as system admin", func(t *testing.T) {
|
||||||
|
_, resp := th.SystemAdminClient.TestSiteURL("")
|
||||||
|
CheckBadRequestStatus(t, resp)
|
||||||
|
|
||||||
|
_, resp = th.SystemAdminClient.TestSiteURL(invalidSiteURL)
|
||||||
|
CheckBadRequestStatus(t, resp)
|
||||||
|
|
||||||
|
_, resp = th.SystemAdminClient.TestSiteURL(validSiteURL)
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("as system user", func(t *testing.T) {
|
||||||
|
_, resp := Client.TestSiteURL(validSiteURL)
|
||||||
|
CheckForbiddenStatus(t, resp)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("as restricted system admin", func(t *testing.T) {
|
||||||
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ExperimentalSettings.RestrictSystemAdmin = true })
|
||||||
|
|
||||||
|
_, resp := Client.TestSiteURL(validSiteURL)
|
||||||
|
CheckForbiddenStatus(t, resp)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestDatabaseRecycle(t *testing.T) {
|
func TestDatabaseRecycle(t *testing.T) {
|
||||||
th := Setup().InitBasic()
|
th := Setup().InitBasic()
|
||||||
defer th.TearDown()
|
defer th.TearDown()
|
||||||
|
|||||||
11
app/admin.go
11
app/admin.go
@@ -4,6 +4,7 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@@ -174,6 +175,16 @@ func (a *App) RecycleDatabaseConnection() {
|
|||||||
mlog.Warn("Finished recycling the database connection.")
|
mlog.Warn("Finished recycling the database connection.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *App) TestSiteURL(siteURL string) *model.AppError {
|
||||||
|
url := fmt.Sprintf("%s/api/v4/system/ping", siteURL)
|
||||||
|
res, err := http.Get(url)
|
||||||
|
if err != nil || res.StatusCode != 200 {
|
||||||
|
return model.NewAppError("testSiteURL", "app.admin.test_site_url.failure", nil, "", http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (a *App) TestEmail(userId string, cfg *model.Config) *model.AppError {
|
func (a *App) TestEmail(userId string, cfg *model.Config) *model.AppError {
|
||||||
if len(*cfg.EmailSettings.SMTPServer) == 0 {
|
if len(*cfg.EmailSettings.SMTPServer) == 0 {
|
||||||
return model.NewAppError("testEmail", "api.admin.test_email.missing_server", nil, utils.T("api.context.invalid_param.app_error", map[string]interface{}{"Name": "SMTPServer"}), http.StatusBadRequest)
|
return model.NewAppError("testEmail", "api.admin.test_email.missing_server", nil, utils.T("api.context.invalid_param.app_error", map[string]interface{}{"Name": "SMTPServer"}), http.StatusBadRequest)
|
||||||
|
|||||||
@@ -2774,6 +2774,10 @@
|
|||||||
"id": "app.admin.test_email.failure",
|
"id": "app.admin.test_email.failure",
|
||||||
"translation": "Connection unsuccessful: {{.Error}}"
|
"translation": "Connection unsuccessful: {{.Error}}"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"id": "app.admin.test_site_url.failure",
|
||||||
|
"translation": "This is not a valid live URL"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"id": "app.channel.create_channel.no_team_id.app_error",
|
"id": "app.channel.create_channel.no_team_id.app_error",
|
||||||
"translation": "Must specify the team ID to create a channel"
|
"translation": "Must specify the team ID to create a channel"
|
||||||
|
|||||||
@@ -268,6 +268,10 @@ func (c *Client4) GetTestEmailRoute() string {
|
|||||||
return fmt.Sprintf("/email/test")
|
return fmt.Sprintf("/email/test")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client4) GetTestSiteURLRoute() string {
|
||||||
|
return fmt.Sprintf("/site_url/test")
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client4) GetTestS3Route() string {
|
func (c *Client4) GetTestS3Route() string {
|
||||||
return fmt.Sprintf("/file/s3_test")
|
return fmt.Sprintf("/file/s3_test")
|
||||||
}
|
}
|
||||||
@@ -2912,6 +2916,18 @@ func (c *Client4) TestEmail(config *Config) (bool, *Response) {
|
|||||||
return CheckStatusOK(r), BuildResponse(r)
|
return CheckStatusOK(r), BuildResponse(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestSiteURL will test the validity of a site URL.
|
||||||
|
func (c *Client4) TestSiteURL(siteURL string) (bool, *Response) {
|
||||||
|
requestBody := make(map[string]string)
|
||||||
|
requestBody["site_url"] = siteURL
|
||||||
|
r, err := c.DoApiPost(c.GetTestSiteURLRoute(), MapToJson(requestBody))
|
||||||
|
if err != nil {
|
||||||
|
return false, BuildErrorResponse(r, err)
|
||||||
|
}
|
||||||
|
defer closeBody(r)
|
||||||
|
return CheckStatusOK(r), BuildResponse(r)
|
||||||
|
}
|
||||||
|
|
||||||
// TestS3Connection will attempt to connect to the AWS S3.
|
// TestS3Connection will attempt to connect to the AWS S3.
|
||||||
func (c *Client4) TestS3Connection(config *Config) (bool, *Response) {
|
func (c *Client4) TestS3Connection(config *Config) (bool, *Response) {
|
||||||
r, err := c.DoApiPost(c.GetTestS3Route(), config.ToJson())
|
r, err := c.DoApiPost(c.GetTestS3Route(), config.ToJson())
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user