Files
mostlymatter/app/plugin_install_test.go
Ali Farooq a6e992ae74 MM-16368 - Plugin Signing (#13017)
* [MM-18757] POST handler for `/plugins/marketplace` (#12372)

* Implement installMarketplacePlugin

* Add InstallMarketplacePlugin endpoint

* Fix go.mod

* merge with master

* Fix go.mod

* Fix plugin tests

* Move get plugin to marketplace client

* Fix stylistic concerns

* Add trailing newline to the go.mod

* [MM-16586] Add plugin signature settings (#12390)

* MM-17149 - Extend config.json for marketplace settings (#11933)

* MM-17149 - Extend config.json for marketplace settings

* Renamed MarketplaceUrl, tracking default marketplace url

* Added EnableMarketplace to the client config

* Revert "Added EnableMarketplace to the client config"

This reverts commit 0f982c4c661c2cd9bb96264e9a01a2363c40d9c5.

* MM-17149 - Added EnableMarketplace to the client config (#11958)

* Added EnableMarketplace to the client config

* Moved EnableMarketplace setting out of limited client configuration

* Add public key settings to the config.json

* Rename PublicKeys to SignaturePublicKeyFiles

* Change filepath.Split to Base

* Remove additional prints

* Force extention of a public key file

* Remove config validation

* Remove error on delete

* Remove config cloning

* Add error messages

* Add plugin public key tests

* Rename extension to PluginSignaturePublicKeyFileExtention

* Remove EnforceVerification

* Change []*PublicKeyDescription to []string

* Change .asc extension to .plugin.asc

* Change ordering of public methods

* Change plugin key commands

* Update examples in the plugin key commands

* Remove forcing extention

* Add verify signature in settings

* Fix tabbing

* Fix naming

* Remove unused text

* Remove unused text

* Update command examples

* Fix unit tests

* Change errors.New to errors.Wrap

* Fix verbose flag

* Change .asc to .gpg

* Fix }

* Change AddPublicKey signature

* Change public.key extension

* Add plugin public key command tests

* Update en.json

* Bootstrap the public keys

* Update en.json

* Fix en.json

* Fix en.json

* Bootstrap hard-coded public key

* Remove unused texts in en.json

* Change file to name

* Add license header

* Update development public key

* Remove writeFile method

* Remove .plugin.asc extension

* Rename publiKey to mattermostPublicKey

* Remove init_public_keys string

* GolangCI

* Closing file handlers

* Fixed test that was installing nps plugin

* [MM-19798] Implement plugin signature verification (#12768)

* MM-17149 - Extend config.json for marketplace settings (#11933)

* MM-17149 - Extend config.json for marketplace settings

* Renamed MarketplaceUrl, tracking default marketplace url

* Added EnableMarketplace to the client config

* Revert "Added EnableMarketplace to the client config"

This reverts commit 0f982c4c661c2cd9bb96264e9a01a2363c40d9c5.

* MM-17149 - Added EnableMarketplace to the client config (#11958)

* Added EnableMarketplace to the client config

* Moved EnableMarketplace setting out of limited client configuration

* Add public key settings to the config.json

* Rename PublicKeys to SignaturePublicKeyFiles

* Change filepath.Split to Base

* Remove additional prints

* Force extention of a public key file

* Remove config validation

* Remove error on delete

* Remove config cloning

* Add error messages

* Add plugin public key tests

* Rename extension to PluginSignaturePublicKeyFileExtention

* Remove EnforceVerification

* Change []*PublicKeyDescription to []string

* Change .asc extension to .plugin.asc

* Change ordering of public methods

* Change plugin key commands

* Update examples in the plugin key commands

* Remove forcing extention

* Add verify signature in settings

* Fix tabbing

* Fix naming

* Remove unused text

* Remove unused text

* Update command examples

* Fix unit tests

* Change errors.New to errors.Wrap

* Fix verbose flag

* Change .asc to .gpg

* Fix }

* Change AddPublicKey signature

* Change public.key extension

* Add plugin public key command tests

* Update en.json

* Bootstrap the public keys

* Update en.json

* Fix en.json

* Fix en.json

* Bootstrap hard-coded public key

* Remove unused texts in en.json

* Change file to name

* Add license header

* Implement plugin signature verification

* Remove benburker openpgp

* Update en.json

* Update development public key

* Add support of multiple signatures in filestore

* Update en.json

* Run go mod vendor

* Fix style

* Remove writeFile method

* Remove .plugin.asc extension

* Rename publiKey to mattermostPublicKey

* Verify plugin with mattermost public key

* Remove init_public_keys string

* Add InstallPluginWithSignature method and  Refactor

* Add signature verification on claster notification

* Remove armored signature headers

* Add error strings

* Fix en.json

* Change signatureStorePath

* Implement minor fixes

* Refactor plugin install methods

* Add installPlugin method to uploadPlugin

* Update en.json

* Refactor installPlugin

* Limit number of signatures

* Close signatures

* Fix helper function

* Fix fromReadCloseSeekerToReadSeeker

* Cleaned up ReadCloseSeeker for signatures

* Remove signature truncation on FS

* GolangCI

* Add tests for armored signatures and plugin uploads

* Fix nil slice issue

* Fix TestPluginSync

* Fixed tests

* Return io.ReadSeeker from downloadFromUrl

* Add log for the found plugins in the file store

* Remove logging plugin detection info

* [MM-20134] Consume and store single-signature for each plugin (#13081)

* Consume and store single-signature for each plugin

* Fix en.json

* Remove saveSignature method

* Remove public key hash

* PR Feedback

* refactored config

* PR feedback
2019-11-18 19:02:41 -05:00

259 строки
8.0 KiB
Go

package app
import (
"archive/tar"
"bytes"
"compress/gzip"
"io"
"sort"
"testing"
"github.com/mattermost/mattermost-server/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
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).InitBasic()
defer th.TearDown()
actualManifest, appErr := th.App.installPluginLocally(&nilReadSeeker{}, nil, 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).InitBasic()
defer th.TearDown()
reader := makeInMemoryGzipTarFile(t, []testFile{
{"test", "test file"},
})
actualManifest, appErr := th.App.installPluginLocally(reader, nil, 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,
}
reader := makeInMemoryGzipTarFile(t, []testFile{
{"plugin.json", manifest.ToJson()},
})
actualManifest, appError := th.App.installPluginLocally(reader, nil, installationStrategy)
if actualManifest != nil {
require.Equal(t, manifest, actualManifest)
}
return actualManifest, appError
}
t.Run("invalid plugin id", func(t *testing.T) {
th := Setup(t).InitBasic()
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.Nil(t, err)
for _, bundleInfo := range bundleInfos {
err := th.App.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.Nil(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).InitBasic()
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).InitBasic()
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).InitBasic()
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).InitBasic()
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.1", installPluginLocallyOnlyIfNewOrUpgrade)
require.Nil(t, appErr)
require.Nil(t, manifest)
assertBundleInfoManifests(t, th, []*model.Manifest{existingManifest})
})
t.Run("install if upgrade, but same version", func(t *testing.T) {
th := Setup(t).InitBasic()
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.2", installPluginLocallyOnlyIfNewOrUpgrade)
require.Nil(t, appErr)
require.Nil(t, manifest)
assertBundleInfoManifests(t, th, []*model.Manifest{existingManifest})
})
t.Run("install if upgrade, newer version", func(t *testing.T) {
th := Setup(t).InitBasic()
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).InitBasic()
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})
})
})
}