From 282a8cf227849a25e306caff11597a0e4ba4cd01 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Sat, 8 Feb 2020 11:14:26 -0400 Subject: [PATCH] Allow make deploy using token (#79) * Allow make deploy using token Introduce support for `MM_ADMIN_TOKEN` as an alternative to defining `MM_ADMIN_USERNAME` and `MM_ADMIN_PASSWORD. * reverse README.md authentication schemes * wrap errors * Update build/deploy/main.go Co-Authored-By: Ben Schumacher * fix linting Co-authored-by: Ben Schumacher --- README.md | 9 ++++++++- build/deploy/main.go | 37 +++++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 1444e42..0a9d4df 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ This will produce a single plugin file (with support for multiple architectures) dist/com.example.my-plugin.tar.gz ``` -There is a build target to automate deploying and enabling the plugin to your server, but it requires configuration and [http](https://httpie.org/) to be installed: +There is a build target to automate deploying and enabling the plugin to your server, but it requires login credentials: ``` export MM_SERVICESETTINGS_SITEURL=http://localhost:8065 export MM_ADMIN_USERNAME=admin @@ -42,6 +42,13 @@ export MM_ADMIN_PASSWORD=password make deploy ``` +or configuration of a [personal access token](https://docs.mattermost.com/developer/personal-access-tokens.html): +``` +export MM_SERVICESETTINGS_SITEURL=http://localhost:8065 +export MM_ADMIN_TOKEN=j44acwd8obn78cdcx7koid4jkr +make deploy +``` + Alternatively, if you are running your `mattermost-server` out of a sibling directory by the same name, use the `deploy` target alone to unpack the files into the right directory. You will need to restart your server and manually enable your plugin. In production, deploy and upload your plugin via the [System Console](https://about.mattermost.com/default-plugin-uploads). diff --git a/build/deploy/main.go b/build/deploy/main.go index 7be98d2..1495720 100644 --- a/build/deploy/main.go +++ b/build/deploy/main.go @@ -34,18 +34,38 @@ func deploy() error { bundlePath := os.Args[2] siteURL := os.Getenv("MM_SERVICESETTINGS_SITEURL") + adminToken := os.Getenv("MM_ADMIN_TOKEN") adminUsername := os.Getenv("MM_ADMIN_USERNAME") adminPassword := os.Getenv("MM_ADMIN_PASSWORD") copyTargetDirectory, _ := filepath.Abs("../mattermost-server") - if siteURL != "" && adminUsername != "" && adminPassword != "" { - return uploadPlugin(pluginID, bundlePath, siteURL, adminUsername, adminPassword) + + if siteURL != "" { + client := model.NewAPIv4Client(siteURL) + + if adminToken != "" { + log.Printf("Authenticating using token against %s.", siteURL) + client.SetToken(adminToken) + + return uploadPlugin(client, pluginID, bundlePath) + } + + if adminUsername != "" && adminPassword != "" { + client := model.NewAPIv4Client(siteURL) + log.Printf("Authenticating as %s against %s.", adminUsername, siteURL) + _, resp := client.Login(adminUsername, adminPassword) + if resp.Error != nil { + return errors.Wrapf(resp.Error, "failed to login as %s: %s", adminUsername, resp.Error.Error()) + } + + return uploadPlugin(client, pluginID, bundlePath) + } } _, err := os.Stat(copyTargetDirectory) if os.IsNotExist(err) { return errors.New("no supported deployment method available, please install plugin manually") } else if err != nil { - return errors.Errorf("Failed to stat %s", copyTargetDirectory) + return errors.Wrapf(err, "failed to stat %s", copyTargetDirectory) } log.Printf("Installing plugin to mattermost-server found in %s.", copyTargetDirectory) @@ -55,14 +75,7 @@ func deploy() error { // uploadPlugin attempts to upload and enable a plugin via the Client4 API. // It will fail if plugin uploads are disabled. -func uploadPlugin(pluginID, bundlePath, siteURL, adminUsername, adminPassword string) error { - client := model.NewAPIv4Client(siteURL) - log.Printf("Authenticating as %s against %s.", adminUsername, siteURL) - _, resp := client.Login(adminUsername, adminPassword) - if resp.Error != nil { - return fmt.Errorf("Failed to login as %s: %s", adminUsername, resp.Error.Error()) - } - +func uploadPlugin(client *model.Client4, pluginID, bundlePath string) error { pluginBundle, err := os.Open(bundlePath) if err != nil { return errors.Wrapf(err, "failed to open %s", bundlePath) @@ -70,7 +83,7 @@ func uploadPlugin(pluginID, bundlePath, siteURL, adminUsername, adminPassword st defer pluginBundle.Close() log.Print("Uploading plugin via API.") - _, resp = client.UploadPluginForced(pluginBundle) + _, resp := client.UploadPluginForced(pluginBundle) if resp.Error != nil { return fmt.Errorf("Failed to upload plugin bundle: %s", resp.Error.Error()) }