summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2017-03-27 09:19:53 -0400
committerChristopher Speller <crspeller@gmail.com>2017-03-27 09:19:53 -0400
commita0d5c01dfd97b45478353ccff777de677f088d0f (patch)
treed4206ef53c44e5ec65fb5b5a01e68d3102e1adf6
parentd145c3583835766c1f200a413131e7d6bad82229 (diff)
downloadchat-a0d5c01dfd97b45478353ccff777de677f088d0f.tar.gz
chat-a0d5c01dfd97b45478353ccff777de677f088d0f.tar.bz2
chat-a0d5c01dfd97b45478353ccff777de677f088d0f.zip
Implement client config/license endpoints for APIv4 (#5867)
-rw-r--r--api4/system.go44
-rw-r--r--api4/system_test.go53
-rw-r--r--i18n/en.json8
-rw-r--r--model/client4.go27
4 files changed, 131 insertions, 1 deletions
diff --git a/api4/system.go b/api4/system.go
index 972d526da..098f7e4b7 100644
--- a/api4/system.go
+++ b/api4/system.go
@@ -17,8 +17,12 @@ func InitSystem() {
BaseRoutes.System.Handle("/ping", ApiHandler(getSystemPing)).Methods("GET")
BaseRoutes.ApiRoot.Handle("/config", ApiSessionRequired(getConfig)).Methods("GET")
- BaseRoutes.ApiRoot.Handle("/config/reload", ApiSessionRequired(configReload)).Methods("POST")
BaseRoutes.ApiRoot.Handle("/config", ApiSessionRequired(updateConfig)).Methods("PUT")
+ BaseRoutes.ApiRoot.Handle("/config/reload", ApiSessionRequired(configReload)).Methods("POST")
+ BaseRoutes.ApiRoot.Handle("/config/client", ApiHandler(getClientConfig)).Methods("GET")
+
+ BaseRoutes.ApiRoot.Handle("/license/client", ApiHandler(getClientLicense)).Methods("GET")
+
BaseRoutes.ApiRoot.Handle("/audits", ApiSessionRequired(getAudits)).Methods("GET")
BaseRoutes.ApiRoot.Handle("/email/test", ApiSessionRequired(testEmail)).Methods("POST")
BaseRoutes.ApiRoot.Handle("/database/recycle", ApiSessionRequired(databaseRecycle)).Methods("POST")
@@ -155,3 +159,41 @@ func getLogs(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(model.ArrayToJson(lines)))
}
+
+func getClientConfig(c *Context, w http.ResponseWriter, r *http.Request) {
+ format := r.URL.Query().Get("format")
+
+ if format == "" {
+ c.Err = model.NewAppError("getClientConfig", "api.config.client.old_format.app_error", nil, "", http.StatusNotImplemented)
+ return
+ }
+
+ if format != "old" {
+ c.SetInvalidParam("format")
+ return
+ }
+
+ w.Write([]byte(model.MapToJson(utils.ClientCfg)))
+}
+
+func getClientLicense(c *Context, w http.ResponseWriter, r *http.Request) {
+ format := r.URL.Query().Get("format")
+
+ if format == "" {
+ c.Err = model.NewAppError("getClientLicense", "api.license.client.old_format.app_error", nil, "", http.StatusNotImplemented)
+ return
+ }
+
+ if format != "old" {
+ c.SetInvalidParam("format")
+ return
+ }
+
+ etag := utils.GetClientLicenseEtag(true)
+ if HandleEtag(etag, "Get Client License", w, r) {
+ return
+ }
+
+ w.Header().Set(model.HEADER_ETAG_SERVER, etag)
+ w.Write([]byte(model.MapToJson(utils.GetSanitizedClientLicense())))
+}
diff --git a/api4/system_test.go b/api4/system_test.go
index 289a41907..b2b5d3f43 100644
--- a/api4/system_test.go
+++ b/api4/system_test.go
@@ -1,6 +1,7 @@
package api4
import (
+ "net/http"
"strings"
"testing"
@@ -129,6 +130,58 @@ func TestUpdateConfig(t *testing.T) {
}
}
+func TestGetOldClientConfig(t *testing.T) {
+ th := Setup().InitBasic().InitSystemAdmin()
+ defer TearDown()
+ Client := th.Client
+
+ config, resp := Client.GetOldClientConfig("")
+ CheckNoError(t, resp)
+
+ if len(config["Version"]) == 0 {
+ t.Fatal("config not returned correctly")
+ }
+
+ Client.Logout()
+
+ _, resp = Client.GetOldClientConfig("")
+ CheckNoError(t, resp)
+
+ if _, err := Client.DoApiGet("/config/client", ""); err == nil || err.StatusCode != http.StatusNotImplemented {
+ t.Fatal("should have errored with 501")
+ }
+
+ if _, err := Client.DoApiGet("/config/client?format=junk", ""); err == nil || err.StatusCode != http.StatusBadRequest {
+ t.Fatal("should have errored with 400")
+ }
+}
+
+func TestGetOldClientLicense(t *testing.T) {
+ th := Setup().InitBasic().InitSystemAdmin()
+ defer TearDown()
+ Client := th.Client
+
+ license, resp := Client.GetOldClientLicense("")
+ CheckNoError(t, resp)
+
+ if len(license["IsLicensed"]) == 0 {
+ t.Fatal("license not returned correctly")
+ }
+
+ Client.Logout()
+
+ _, resp = Client.GetOldClientLicense("")
+ CheckNoError(t, resp)
+
+ if _, err := Client.DoApiGet("/license/client", ""); err == nil || err.StatusCode != http.StatusNotImplemented {
+ t.Fatal("should have errored with 501")
+ }
+
+ if _, err := Client.DoApiGet("/license/client?format=junk", ""); err == nil || err.StatusCode != http.StatusBadRequest {
+ t.Fatal("should have errored with 400")
+ }
+}
+
func TestGetAudits(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
diff --git a/i18n/en.json b/i18n/en.json
index a48cc2cb0..0589d22e1 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -48,6 +48,14 @@
"translation": "September"
},
{
+ "id": "api.config.client.old_format.app_error",
+ "translation": "New format for the client configuration is not supported yet. Please specify format=old in the query string."
+ },
+ {
+ "id": "api.license.client.old_format.app_error",
+ "translation": "New format for the client license is not supported yet. Please specify format=old in the query string."
+ },
+ {
"id": "api.admin.add_certificate.no_file.app_error",
"translation": "No file under 'certificate' in request"
},
diff --git a/model/client4.go b/model/client4.go
index e1bd70878..6306039ff 100644
--- a/model/client4.go
+++ b/model/client4.go
@@ -130,6 +130,10 @@ func (c *Client4) GetConfigRoute() string {
return fmt.Sprintf("/config")
}
+func (c *Client4) GetLicenseRoute() string {
+ return fmt.Sprintf("/license")
+}
+
func (c *Client4) GetPostRoute(postId string) string {
return fmt.Sprintf(c.GetPostsRoute()+"/%v", postId)
}
@@ -1344,6 +1348,7 @@ func (c *Client4) GetPing() (bool, *Response) {
}
}
+// TestEmail will attempt to connect to the configured SMTP server.
func (c *Client4) TestEmail() (bool, *Response) {
if r, err := c.DoApiPost(c.GetTestEmailRoute(), ""); err != nil {
return false, &Response{StatusCode: r.StatusCode, Error: err}
@@ -1373,6 +1378,28 @@ func (c *Client4) ReloadConfig() (bool, *Response) {
}
}
+// GetOldClientConfig will retrieve the parts of the server configuration needed by the
+// client, formatted in the old format.
+func (c *Client4) GetOldClientConfig(etag string) (map[string]string, *Response) {
+ if r, err := c.DoApiGet(c.GetConfigRoute()+"/client?format=old", etag); err != nil {
+ return nil, &Response{StatusCode: r.StatusCode, Error: err}
+ } else {
+ defer closeBody(r)
+ return MapFromJson(r.Body), BuildResponse(r)
+ }
+}
+
+// GetOldClientLicense will retrieve the parts of the server license needed by the
+// client, formatted in the old format.
+func (c *Client4) GetOldClientLicense(etag string) (map[string]string, *Response) {
+ if r, err := c.DoApiGet(c.GetLicenseRoute()+"/client?format=old", etag); err != nil {
+ return nil, &Response{StatusCode: r.StatusCode, Error: err}
+ } else {
+ defer closeBody(r)
+ return MapFromJson(r.Body), BuildResponse(r)
+ }
+}
+
// DatabaseRecycle will recycle the connections. Discard current connection and get new one.
func (c *Client4) DatabaseRecycle() (bool, *Response) {
if r, err := c.DoApiPost(c.GetDatabaseRoute()+"/recycle", ""); err != nil {