Skip to content

Commit

Permalink
fix: Ensure configured token is fetched during perms check
Browse files Browse the repository at this point in the history
* Seems like auto provisioning does not work in multi org settings. A workaround is to turn off externalServiceAccounts feature flag and manually provision the plugin with a service account in each Org.

* This commit fixes on how we fetch the token between automatically provisioned and manually provided ones during permission checks.

Signed-off-by: Mahendra Paipuri <[email protected]>
  • Loading branch information
mahendrapaipuri committed Oct 27, 2024
1 parent e1f1558 commit b190aae
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pids
*.seed
*.pid.lock
.bra.toml
*.db

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
Expand Down
9 changes: 4 additions & 5 deletions pkg/plugin/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,11 @@ func (app *App) GetAuthZClient(req *http.Request) (authz.EnforcementClient, erro
return nil, err
}

// Bail we cannot get token provisioned by externalServiceAccount and no token
// has been manually configured. In this case we cannot check permissions and moreover
// we cannot make API requests to Grafana
saToken, err := grafanaConfig.PluginAppClientSecret()
if err != nil || (saToken == "" && app.conf.Token == "") {
if err == nil {
err = errors.New("neither service account token nor configured token found")
}

if err != nil && app.conf.Token == "" {
ctxLogger.Error("failed to fetch service account and configured token", "error", err)

return nil, err
Expand Down
37 changes: 35 additions & 2 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,47 @@ extra configuration to get an API token from Grafana.
Grafana to make API requests to Grafana. This can be done automatically by enabling
feature flag `externalServiceAccounts`, which will create a service account and
provision a service account token automatically for the plugin. Please consult
[Local Installation](#local-installation) on how to configure the feature flags on
[Installation](#installation) on how to configure the feature flags on
Grafana server.

> [!NOTE]
> If the operators do not wish or cannot use `externalServiceAccounts` feature flag on
their Grafana deployment, it is possible to manually create an API token and set it in
the [plugin configuration options](#authentication-settings).

### Multiple Orgs

Grafana does not support yet automatically provisioning the plugins with service tokens
using `externalServiceAccounts`. More details can be found in this [GH issue](https://github.com/grafana/grafana/issues/91844).
A workaround in this case is to turn off the feature flag `externalServiceAccounts` and
manually create service account token for each Org. and setting it in the plugin
configuration file. In this case, the provisioned config for the plugin will look like this:

```yaml
apps:
- type: mahendrapaipuri-dashboardreporter-app
org_id: 1
org_name: Main Org.
disabled: false
secureJsonData:
saToken: <ServiceAccountTokenForMainOrg>
jsonData:
appUrl: http://localhost:3000

- type: mahendrapaipuri-dashboardreporter-app
org_id: 2
org_name: Test Org.
disabled: false
secureJsonData:
saToken: <ServiceAccountTokenForTestOrg>
jsonData:
appUrl: http://localhost:3000
```
> [!IMPORTANT]
> It is compulsory to disable `externalServiceAccounts` feature flag in multiple Org. setting
as plugin wont work as expected with this feature flag.

## Using plugin

### Using Grafana web UI
Expand Down Expand Up @@ -403,7 +436,7 @@ any HTTP client of your favorite programming language.

## Security

All the feature flags listed in the [Local Installation](#local-installation) section
All the feature flags listed in the [Installation](#installation) section
must be enabled on Grafana server for secure operation of your Grafana instance.
These feature flags enables the plugin to verify
the if the user who is making the request to generate the report has
Expand Down

0 comments on commit b190aae

Please sign in to comment.