2021-05-13 02:05:16 +08:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2021-07-13 15:58:46 +08:00
|
|
|
"encoding/json"
|
2021-05-13 02:05:16 +08:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
|
|
"github.com/grafana/grafana/pkg/tests/testinfra"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
usernameAdmin = "admin"
|
|
|
|
usernameNonAdmin = "nonAdmin"
|
|
|
|
defaultPassword = "password"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPluginInstallAccess(t *testing.T) {
|
|
|
|
dir, cfgPath := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{
|
2021-05-20 16:42:26 +08:00
|
|
|
PluginAdminEnabled: true,
|
2021-05-13 02:05:16 +08:00
|
|
|
})
|
2021-08-25 21:11:22 +08:00
|
|
|
|
|
|
|
grafanaListedAddr, store := testinfra.StartGrafana(t, dir, cfgPath)
|
2021-05-13 02:05:16 +08:00
|
|
|
store.Bus = bus.GetBus() // in order to allow successful user auth
|
|
|
|
|
|
|
|
createUser(t, store, usernameNonAdmin, defaultPassword, false)
|
|
|
|
createUser(t, store, usernameAdmin, defaultPassword, true)
|
|
|
|
|
|
|
|
t.Run("Request is forbidden if not from an admin", func(t *testing.T) {
|
2021-07-13 15:58:46 +08:00
|
|
|
status, body := makePostRequest(t, grafanaAPIURL(usernameNonAdmin, grafanaListedAddr, "plugins/grafana-plugin/install"))
|
|
|
|
assert.Equal(t, 403, status)
|
|
|
|
assert.Equal(t, "Permission denied", body["message"])
|
2021-05-13 02:05:16 +08:00
|
|
|
|
2021-07-13 15:58:46 +08:00
|
|
|
status, body = makePostRequest(t, grafanaAPIURL(usernameNonAdmin, grafanaListedAddr, "plugins/grafana-plugin/uninstall"))
|
|
|
|
assert.Equal(t, 403, status)
|
|
|
|
assert.Equal(t, "Permission denied", body["message"])
|
2021-05-13 02:05:16 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Request is not forbidden if from an admin", func(t *testing.T) {
|
|
|
|
statusCode, body := makePostRequest(t, grafanaAPIURL(usernameAdmin, grafanaListedAddr, "plugins/test/install"))
|
2021-07-13 15:58:46 +08:00
|
|
|
|
2021-05-13 02:05:16 +08:00
|
|
|
assert.Equal(t, 404, statusCode)
|
2021-07-13 15:58:46 +08:00
|
|
|
assert.Equal(t, "Plugin not found", body["message"])
|
2021-05-13 02:05:16 +08:00
|
|
|
|
|
|
|
statusCode, body = makePostRequest(t, grafanaAPIURL(usernameAdmin, grafanaListedAddr, "plugins/test/uninstall"))
|
|
|
|
assert.Equal(t, 404, statusCode)
|
2021-07-13 15:58:46 +08:00
|
|
|
assert.Equal(t, "Plugin not installed", body["message"])
|
2021-05-13 02:05:16 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func createUser(t *testing.T, store *sqlstore.SQLStore, username, password string, isAdmin bool) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
cmd := models.CreateUserCommand{
|
|
|
|
Login: username,
|
|
|
|
Password: password,
|
|
|
|
IsAdmin: isAdmin,
|
|
|
|
}
|
|
|
|
_, err := store.CreateUser(context.Background(), cmd)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-07-13 15:58:46 +08:00
|
|
|
func makePostRequest(t *testing.T, URL string) (int, map[string]interface{}) {
|
2021-05-13 02:05:16 +08:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// nolint:gosec
|
|
|
|
resp, err := http.Post(URL, "application/json", bytes.NewBufferString(""))
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
log.Warn("Failed to close response body", "err", err)
|
|
|
|
})
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-13 15:58:46 +08:00
|
|
|
var body = make(map[string]interface{})
|
|
|
|
err = json.Unmarshal(b, &body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return resp.StatusCode, body
|
2021-05-13 02:05:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func grafanaAPIURL(username string, grafanaListedAddr string, path string) string {
|
|
|
|
return fmt.Sprintf("http://%s:%s@%s/api/%s", username, defaultPassword, grafanaListedAddr, path)
|
|
|
|
}
|