Files
mattermost/server/channels/app/plugin_install_test.go
Jesse Hallam ad142c958e MM-53355: install transitionally prepackaged plugins to filestore (#24225)
* move plugin signature verification to caller

The semantics for when plugin signature validation is required are unique to the caller, so move this logic there instead of masking it, thus simplifying some of the downstream code.

* support transitionally prepacked plugins

Transitionally prepackaged plugins are prepackaged plugins slated for unpackaging in some future release. Like prepackaged plugins, they automatically install or upgrade if the server is configured to enable that plugin, but unlike prepackaged plugins they don't add to the marketplace to allow for offline installs. In fact, if unlisted from the marketplace and not already enabled via `config.json`, a transitionally prepackaged plugin is essentially hidden.

To ensure a smooth transition in the future release when this plugin is no longer prepackaged at all, transitionally prepackaged plugins are persisted to the filestore as if they had been installed by the enduser. On the next restart, even while the plugin is still transitionally prepackaged, the version in the filestore will take priority. It remains possible for a transitionally prepackaged plugin to upgrade (and once again persist) if we ship a newer version before dropping it altogether.

Some complexity arises in a multi-server cluster, primarily because we don't want to deal with multiple servers writing the same object to the filestore. This is probably fine for S3, but has undefined semantics for regular filesystems, especially with some customers backing their files on any number of different fileshare technologies. To simplify the complexity, only the cluster leader persists transitionally prepackaged plugins.

Unfortunately, this too is complicated, since on upgrade to the first version with the transitionally prepackaged plugin, there is no guarantee that server will be the leader. In fact, as all nodes restart, there is no guarantee that any newly started server will start as the leader. So the persistence has to happen in a job-like fashion. The migration system might work, except we want the ability to run this repeatedly as we add to (or update) these transitionally prepackaged plugins. We also want to minimize the overhead required from the server to juggle any of this.

As a consequence, the persistence of transitionally prepackaged plugins occurs on every cluster leader change. Each server will try at most once to persist its collection of transitionally prepackaged plugins, and newly started servers will see the plugins in the filestore and skip this step altogether.

The current set of transitionally prepackaged plugins include the following, but this is expected to change:
* focalboard

* complete list of transitionally prepackaged plugins

* update plugin_install.go docs

* updated test plugins

* unit test transitionally prepackged plugins

* try restoring original working directory

* Apply suggestions from code review

Co-authored-by: Michael Kochell <6913320+mickmister@users.noreply.github.com>

* clarify processPrepackagedPlugins comment

---------

Co-authored-by: Michael Kochell <6913320+mickmister@users.noreply.github.com>
2023-08-17 12:46:57 -03:00

301 lines
9.2 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/json"
"io"
"os"
"path/filepath"
"sort"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/channels/utils/fileutils"
)
type nilReadSeeker struct {
}
func (r *nilReadSeeker) Read(p []byte) (int, error) {
return 0, io.EOF
}
func (r *nilReadSeeker) Seek(offset int64, whence int) (int64, error) {
return 0, nil
}
type testFile struct {
Name, Body string
}
func makeInMemoryGzipTarFile(t *testing.T, files []testFile) *bytes.Reader {
var buf bytes.Buffer
gzWriter := gzip.NewWriter(&buf)
tgz := tar.NewWriter(gzWriter)
for _, file := range files {
hdr := &tar.Header{
Name: file.Name,
Mode: 0600,
Size: int64(len(file.Body)),
}
err := tgz.WriteHeader(hdr)
require.NoError(t, err, "failed to write %s to in-memory tar file", file.Name)
_, err = tgz.Write([]byte(file.Body))
require.NoError(t, err, "failed to write body of %s to in-memory tar file", file.Name)
}
err := tgz.Close()
require.NoError(t, err, "failed to close in-memory tar file")
err = gzWriter.Close()
require.NoError(t, err, "failed to close in-memory tar.gz file")
return bytes.NewReader(buf.Bytes())
}
type byBundleInfoId []*model.BundleInfo
func (b byBundleInfoId) Len() int { return len(b) }
func (b byBundleInfoId) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byBundleInfoId) Less(i, j int) bool { return b[i].Manifest.Id < b[j].Manifest.Id }
func TestInstallPluginLocally(t *testing.T) {
t.Run("invalid tar", func(t *testing.T) {
th := Setup(t)
defer th.TearDown()
actualManifest, appErr := th.App.ch.installPluginLocally(&nilReadSeeker{}, installPluginLocallyOnlyIfNew)
require.NotNil(t, appErr)
assert.Equal(t, "app.plugin.extract.app_error", appErr.Id, appErr.Error())
require.Nil(t, actualManifest)
})
t.Run("missing manifest", func(t *testing.T) {
th := Setup(t)
defer th.TearDown()
reader := makeInMemoryGzipTarFile(t, []testFile{
{"test", "test file"},
})
actualManifest, appErr := th.App.ch.installPluginLocally(reader, installPluginLocallyOnlyIfNew)
require.NotNil(t, appErr)
assert.Equal(t, "app.plugin.manifest.app_error", appErr.Id, appErr.Error())
require.Nil(t, actualManifest)
})
installPlugin := func(t *testing.T, th *TestHelper, id, version string, installationStrategy pluginInstallationStrategy) (*model.Manifest, *model.AppError) {
t.Helper()
manifest := &model.Manifest{
Id: id,
Version: version,
}
manifestJSON, jsonErr := json.Marshal(manifest)
require.NoError(t, jsonErr)
reader := makeInMemoryGzipTarFile(t, []testFile{
{"plugin.json", string(manifestJSON)},
})
actualManifest, appError := th.App.ch.installPluginLocally(reader, installationStrategy)
if actualManifest != nil {
require.Equal(t, manifest, actualManifest)
}
return actualManifest, appError
}
t.Run("invalid plugin id", func(t *testing.T) {
th := Setup(t)
defer th.TearDown()
actualManifest, appErr := installPlugin(t, th, "invalid#plugin#id", "version", installPluginLocallyOnlyIfNew)
require.NotNil(t, appErr)
assert.Equal(t, "app.plugin.invalid_id.app_error", appErr.Id, appErr.Error())
require.Nil(t, actualManifest)
})
// The following tests fail mysteriously on CI due to an unexpected bundle being present.
// This exists to clean up manually until we figure out what test isn't cleaning up after
// itself.
cleanExistingBundles := func(t *testing.T, th *TestHelper) {
pluginsEnvironment := th.App.GetPluginsEnvironment()
require.NotNil(t, pluginsEnvironment)
bundleInfos, err := pluginsEnvironment.Available()
require.NoError(t, err)
for _, bundleInfo := range bundleInfos {
err := th.App.ch.removePluginLocally(bundleInfo.Manifest.Id)
require.Nilf(t, err, "failed to remove existing plugin %s", bundleInfo.Manifest.Id)
}
}
assertBundleInfoManifests := func(t *testing.T, th *TestHelper, manifests []*model.Manifest) {
pluginsEnvironment := th.App.GetPluginsEnvironment()
require.NotNil(t, pluginsEnvironment)
bundleInfos, err := pluginsEnvironment.Available()
require.NoError(t, err)
sort.Sort(byBundleInfoId(bundleInfos))
actualManifests := make([]*model.Manifest, 0, len(bundleInfos))
for _, bundleInfo := range bundleInfos {
actualManifests = append(actualManifests, bundleInfo.Manifest)
}
require.Equal(t, manifests, actualManifests)
}
t.Run("no plugins already installed", func(t *testing.T) {
th := Setup(t)
defer th.TearDown()
cleanExistingBundles(t, th)
manifest, appErr := installPlugin(t, th, "valid", "0.0.1", installPluginLocallyOnlyIfNew)
require.Nil(t, appErr)
require.NotNil(t, manifest)
assertBundleInfoManifests(t, th, []*model.Manifest{manifest})
})
t.Run("different plugin already installed", func(t *testing.T) {
th := Setup(t)
defer th.TearDown()
cleanExistingBundles(t, th)
otherManifest, appErr := installPlugin(t, th, "other", "0.0.1", installPluginLocallyOnlyIfNew)
require.Nil(t, appErr)
require.NotNil(t, otherManifest)
manifest, appErr := installPlugin(t, th, "valid", "0.0.1", installPluginLocallyOnlyIfNew)
require.Nil(t, appErr)
require.NotNil(t, manifest)
assertBundleInfoManifests(t, th, []*model.Manifest{otherManifest, manifest})
})
t.Run("same plugin already installed", func(t *testing.T) {
t.Run("install only if new", func(t *testing.T) {
th := Setup(t)
defer th.TearDown()
cleanExistingBundles(t, th)
existingManifest, appErr := installPlugin(t, th, "valid", "0.0.1", installPluginLocallyOnlyIfNew)
require.Nil(t, appErr)
require.NotNil(t, existingManifest)
manifest, appErr := installPlugin(t, th, "valid", "0.0.1", installPluginLocallyOnlyIfNew)
require.NotNil(t, appErr)
require.Equal(t, "app.plugin.install_id.app_error", appErr.Id, appErr.Error())
require.Nil(t, manifest)
assertBundleInfoManifests(t, th, []*model.Manifest{existingManifest})
})
t.Run("install if upgrade, but older", func(t *testing.T) {
th := Setup(t)
defer th.TearDown()
cleanExistingBundles(t, th)
existingManifest, appErr := installPlugin(t, th, "valid", "0.0.2", installPluginLocallyOnlyIfNewOrUpgrade)
require.Nil(t, appErr)
require.NotNil(t, existingManifest)
_, appErr = installPlugin(t, th, "valid", "0.0.1", installPluginLocallyOnlyIfNewOrUpgrade)
require.NotNil(t, appErr)
require.Equal(t, "app.plugin.skip_installation.app_error", appErr.Id)
assertBundleInfoManifests(t, th, []*model.Manifest{existingManifest})
})
t.Run("install if upgrade, but same version", func(t *testing.T) {
th := Setup(t)
defer th.TearDown()
cleanExistingBundles(t, th)
existingManifest, appErr := installPlugin(t, th, "valid", "0.0.2", installPluginLocallyOnlyIfNewOrUpgrade)
require.Nil(t, appErr)
require.NotNil(t, existingManifest)
_, appErr = installPlugin(t, th, "valid", "0.0.2", installPluginLocallyOnlyIfNewOrUpgrade)
require.NotNil(t, appErr)
require.Equal(t, "app.plugin.skip_installation.app_error", appErr.Id)
assertBundleInfoManifests(t, th, []*model.Manifest{existingManifest})
})
t.Run("install if upgrade, newer version", func(t *testing.T) {
th := Setup(t)
defer th.TearDown()
cleanExistingBundles(t, th)
existingManifest, appErr := installPlugin(t, th, "valid", "0.0.2", installPluginLocallyOnlyIfNewOrUpgrade)
require.Nil(t, appErr)
require.NotNil(t, existingManifest)
manifest, appErr := installPlugin(t, th, "valid", "0.0.3", installPluginLocallyOnlyIfNewOrUpgrade)
require.Nil(t, appErr)
require.NotNil(t, manifest)
assertBundleInfoManifests(t, th, []*model.Manifest{manifest})
})
t.Run("install always, old version", func(t *testing.T) {
th := Setup(t)
defer th.TearDown()
cleanExistingBundles(t, th)
existingManifest, appErr := installPlugin(t, th, "valid", "0.0.2", installPluginLocallyAlways)
require.Nil(t, appErr)
require.NotNil(t, existingManifest)
manifest, appErr := installPlugin(t, th, "valid", "0.0.1", installPluginLocallyAlways)
require.Nil(t, appErr)
require.NotNil(t, manifest)
assertBundleInfoManifests(t, th, []*model.Manifest{manifest})
})
})
}
func TestInstallPluginAlreadyActive(t *testing.T) {
th := Setup(t)
defer th.TearDown()
path, _ := fileutils.FindDir("tests")
reader, err := os.Open(filepath.Join(path, "testplugin.tar.gz"))
require.NoError(t, err)
actualManifest, appError := th.App.InstallPlugin(reader, true)
require.NotNil(t, actualManifest)
require.Nil(t, appError)
appError = th.App.EnablePlugin(actualManifest.Id)
require.Nil(t, appError)
pluginsEnvironment := th.App.GetPluginsEnvironment()
require.NotNil(t, pluginsEnvironment)
bundleInfos, err := pluginsEnvironment.Available()
require.NoError(t, err)
require.NotEmpty(t, bundleInfos)
for _, bundleInfo := range bundleInfos {
if bundleInfo.Manifest.Id == actualManifest.Id {
err := os.RemoveAll(bundleInfo.Path)
require.NoError(t, err)
}
}
actualManifest, appError = th.App.InstallPlugin(reader, true)
require.NotNil(t, appError)
require.Nil(t, actualManifest)
require.Equal(t, "app.plugin.restart.app_error", appError.Id)
}