forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: Evaluate if an app is disabled for API requests (grafana#79564)
- Loading branch information
1 parent
3e4abde
commit 1324186
Showing
4 changed files
with
128 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package api | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
|
||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" | ||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginsettings" | ||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore" | ||
"github.com/grafana/grafana/pkg/web" | ||
) | ||
|
||
func checkAppEnabled(pluginStore pluginstore.Store, pluginSettings pluginsettings.Service) func(c *contextmodel.ReqContext) { | ||
return func(c *contextmodel.ReqContext) { | ||
pluginID := web.Params(c.Req)[":pluginId"] | ||
p, exists := pluginStore.Plugin(c.Req.Context(), pluginID) | ||
if !exists { | ||
c.JsonApiErr(http.StatusNotFound, "Plugin not found", nil) | ||
return | ||
} | ||
if !p.IsApp() { | ||
return | ||
} | ||
|
||
ps, err := pluginSettings.GetPluginSettingByPluginID(c.Req.Context(), &pluginsettings.GetByPluginIDArgs{ | ||
OrgID: c.OrgID, | ||
PluginID: pluginID, | ||
}) | ||
if err != nil { | ||
if errors.Is(err, pluginsettings.ErrPluginSettingNotFound) { | ||
c.JsonApiErr(http.StatusNotFound, "Plugin not found", nil) | ||
return | ||
} | ||
c.JsonApiErr(http.StatusInternalServerError, "Failed to get plugin settings", err) | ||
return | ||
} | ||
|
||
if !ps.Enabled { | ||
c.JsonApiErr(http.StatusNotFound, "Plugin is not enabled", nil) | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/grafana/grafana/pkg/plugins" | ||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" | ||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginsettings" | ||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore" | ||
"github.com/grafana/grafana/pkg/services/user" | ||
"github.com/grafana/grafana/pkg/web" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestHTTPServer_CheckEnabled(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
pluginID string | ||
expectedCode int | ||
}{ | ||
{ | ||
name: "should return a 404 if the plugin doesn't exist", | ||
pluginID: "missing", | ||
expectedCode: 404, | ||
}, | ||
{ | ||
name: "should not set an error code if the plugin is not an app", | ||
pluginID: "mysql", | ||
expectedCode: 0, // unset | ||
}, | ||
{ | ||
name: "should not set an error code if the plugin is enabled", | ||
pluginID: "grafana-test-app", | ||
expectedCode: 0, // unset | ||
}, | ||
{ | ||
name: "should return a 404 if the plugin is disabled", | ||
pluginID: "grafana-test-app_disabled", | ||
expectedCode: 404, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
hs := &HTTPServer{} | ||
hs.pluginStore = &pluginstore.FakePluginStore{ | ||
PluginList: []pluginstore.Plugin{ | ||
{JSONData: plugins.JSONData{ID: "mysql"}}, | ||
{JSONData: plugins.JSONData{Type: plugins.TypeApp, ID: "grafana-test-app"}}, | ||
{JSONData: plugins.JSONData{Type: plugins.TypeApp, ID: "grafana-test-app_disabled"}}, | ||
}, | ||
} | ||
hs.PluginSettings = &pluginsettings.FakePluginSettings{Plugins: map[string]*pluginsettings.DTO{ | ||
"grafana-test-app": {ID: 0, OrgID: 1, PluginID: "grafana-test-app", PluginVersion: "1.0.0", Enabled: true}, | ||
"grafana-test-app_disabled": {ID: 0, OrgID: 1, PluginID: "grafana-test-app_disabled", PluginVersion: "1.0.0", Enabled: false}, | ||
}} | ||
httpReq, err := http.NewRequest(http.MethodGet, "", nil) | ||
httpReq = web.SetURLParams(httpReq, map[string]string{":pluginId": tt.pluginID}) | ||
require.NoError(t, err) | ||
|
||
responseWriter := web.NewResponseWriter("GET", httptest.NewRecorder()) | ||
c := &contextmodel.ReqContext{ | ||
Context: &web.Context{Req: httpReq, Resp: responseWriter}, | ||
SignedInUser: &user.SignedInUser{OrgID: 1}, | ||
} | ||
checkAppEnabled(hs.pluginStore, hs.PluginSettings)(c) | ||
assert.Equal(t, tt.expectedCode, c.Resp.Status()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters