-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6b5890
commit 90fbfb8
Showing
9 changed files
with
211 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'grafana-infinity-datasource': minor | ||
--- | ||
|
||
Allow Grafana admins to set allowed hosts / denied hosts via grafana.ini file. [More details](https://grafana.com/docs/plugins/yesoreyeram-infinity-datasource/latest/references/url/) |
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,2 @@ | ||
[plugin.yesoreyeram-infinity-datasource] | ||
host_deny_list = "foo.com bar.com baz.com" |
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
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
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,81 @@ | ||
package infinity | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/grafana/grafana-infinity-datasource/pkg/models" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend" | ||
) | ||
|
||
// ValidateRequest is a basic implementation of request validator / interceptor | ||
func ValidateRequest(ctx context.Context, pCtx *backend.PluginContext, settings models.InfinitySettings, req *http.Request) error { | ||
hostName := req.URL.Hostname() | ||
if hostName == "" { | ||
return nil | ||
} | ||
|
||
//#region denied list of hosts from the grafana config / environment variable | ||
// if the host name of URL found in denied list of hosts grafana config, the URL should be blocked | ||
deniedHostsSettingFromGrafana := getHostNamesFromConfig(ctx, pCtx, "host_deny_list") | ||
if len(deniedHostsSettingFromGrafana) > 0 { | ||
deny := false | ||
for _, deniedHost := range deniedHostsSettingFromGrafana { | ||
if strings.EqualFold(hostName, strings.TrimSpace(deniedHost)) { | ||
deny = true | ||
} | ||
} | ||
if deny { | ||
return models.ErrHostNameDenied(hostName) | ||
} | ||
} | ||
//#endregion | ||
|
||
//#region allowed list of hosts from the grafana config / environment variable | ||
// if the host name of URL not found in allowed list of hosts grafana config, the URL should be blocked | ||
allowedHostsSettingFromGrafana := getHostNamesFromConfig(ctx, pCtx, "host_allow_list") | ||
if len(allowedHostsSettingFromGrafana) > 0 { | ||
allow := false | ||
for _, allowedHost := range allowedHostsSettingFromGrafana { | ||
if strings.EqualFold(hostName, strings.TrimSpace(allowedHost)) { | ||
allow = true | ||
} | ||
} | ||
if !allow { | ||
return models.ErrHostNameNotAllowed(hostName) | ||
} | ||
} | ||
//#endregion | ||
|
||
//#region allowed list of hosts from the grafana config / environment variable | ||
allowedHostsFromDsConfig := settings.AllowedHosts | ||
if len(allowedHostsFromDsConfig) > 0 { | ||
allow := false | ||
for _, host := range allowedHostsFromDsConfig { | ||
if strings.HasPrefix(req.URL.String(), host) { | ||
allow = true | ||
} | ||
} | ||
if !allow { | ||
return models.ErrURLNotAllowed | ||
} | ||
} | ||
//#endregion | ||
|
||
return nil | ||
} | ||
|
||
func getHostNamesFromConfig(ctx context.Context, pCtx *backend.PluginContext, key string) (out []string) { | ||
hostNamesConfig := strings.TrimSpace(models.GetGrafanaConfig(ctx, pCtx, key)) | ||
if hostNamesConfig == "" { | ||
return out | ||
} | ||
hostNames := strings.Split(hostNamesConfig, " ") | ||
for _, k := range hostNames { | ||
if key := strings.TrimSpace(k); key != "" { | ||
out = append(out, key) | ||
} | ||
} | ||
return out | ||
} |
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 |
---|---|---|
@@ -1,9 +1,21 @@ | ||
package models | ||
|
||
import "errors" | ||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var ( | ||
ErrUnsuccessfulHTTPResponseStatus error = errors.New("unsuccessful HTTP response") | ||
ErrParsingResponseBodyAsJson error = errors.New("unable to parse response body as JSON") | ||
ErrCreatingHTTPClient error = errors.New("error creating HTTP client") | ||
ErrURLNotAllowed error = errors.New("requested URL is not allowed. To allow this URL, update the datasource config Security -> Allowed Hosts section") | ||
) | ||
|
||
func ErrHostNameDenied(hostName string) error { | ||
return fmt.Errorf("hostname denied via grafana config. hostname %s", hostName) | ||
} | ||
|
||
func ErrHostNameNotAllowed(hostName string) error { | ||
return fmt.Errorf("hostname not allowed via grafana config. hostname %s", hostName) | ||
} |
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,37 @@ | ||
package models | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/grafana/grafana-plugin-sdk-go/backend" | ||
) | ||
|
||
// GetGrafanaConfig allow you to retrieve config set via grafana.ini file | ||
func GetGrafanaConfig(ctx context.Context, pCtx *backend.PluginContext, key string) (output string) { | ||
key = strings.TrimSpace(strings.ToUpper(key)) | ||
if v := strings.TrimSpace(os.Getenv(fmt.Sprintf("GF_PLUGIN_%s", key))); v != "" { | ||
output = v | ||
} | ||
// if pCtx == nil { | ||
// return output | ||
// } | ||
// pluginId := strings.TrimSpace(strings.ToUpper(pCtx.PluginID)) | ||
// if v := strings.TrimSpace(os.Getenv(fmt.Sprintf("GF_PLUGIN_%s_%s", pluginId, key))); v != "" && pluginId != "" { | ||
// output = v | ||
// } | ||
// if pCtx.DataSourceInstanceSettings == nil { | ||
// return output | ||
// } | ||
// dsUID := strings.TrimSpace(strings.ToUpper(pCtx.DataSourceInstanceSettings.UID)) | ||
// if v := strings.TrimSpace(os.Getenv(fmt.Sprintf("GF_DS_%s_%s", dsUID, key))); v != "" && dsUID != "" { | ||
// output = v | ||
// } | ||
// caseSensitiveDsUID := strings.TrimSpace(pCtx.DataSourceInstanceSettings.UID) | ||
// if v := strings.TrimSpace(os.Getenv(fmt.Sprintf("GF_DS_%s_%s", caseSensitiveDsUID, key))); v != "" && caseSensitiveDsUID != "" { | ||
// output = v | ||
// } | ||
return output | ||
} |