Skip to content

Commit

Permalink
Add PluginAppClientSecret to request config (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
andresmgot authored Apr 1, 2024
1 parent 1d1bcd4 commit 5ec91ca
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 296 deletions.
21 changes: 20 additions & 1 deletion backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"os"
"strconv"
"strings"

Expand All @@ -21,6 +22,7 @@ const (
SQLMaxIdleConnsDefault = "GF_SQL_MAX_IDLE_CONNS_DEFAULT"
SQLMaxConnLifetimeSecondsDefault = "GF_SQL_MAX_CONN_LIFETIME_SECONDS_DEFAULT"
ResponseLimit = "GF_RESPONSE_LIMIT"
AppClientSecret = "GF_PLUGIN_APP_CLIENT_SECRET" // nolint:gosec
)

type configKey struct{}
Expand Down Expand Up @@ -149,7 +151,11 @@ func (c *GrafanaCfg) proxy() (Proxy, error) {
func (c *GrafanaCfg) AppURL() (string, error) {
url, ok := c.config[AppURL]
if !ok {
return "", fmt.Errorf("app URL not set in config. A more recent version of Grafana may be required")
// Fallback to environment variable for backwards compatibility
url = os.Getenv(AppURL)
if url == "" {
return "", errors.New("app URL not set in config. A more recent version of Grafana may be required")
}
}
return url, nil
}
Expand Down Expand Up @@ -246,6 +252,19 @@ func (c *GrafanaCfg) ResponseLimit() int64 {
return i
}

func (c *GrafanaCfg) PluginAppClientSecret() (string, error) {
value, ok := c.config[AppClientSecret]
if !ok {
// Fallback to environment variable for backwards compatibility
value = os.Getenv(AppClientSecret)
if value == "" {
return "", errors.New("PluginAppClientSecret not set in config")
}
}

return value, nil
}

type userAgentKey struct{}

// UserAgentFromContext returns user agent from context.
Expand Down
30 changes: 30 additions & 0 deletions backend/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backend

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -194,6 +195,15 @@ func TestAppURL(t *testing.T) {
_, err := cfg.AppURL()
require.Error(t, err)
})

t.Run("it should return the configured app URL from env", func(t *testing.T) {
os.Setenv(AppURL, "http://localhost-env:3000")
defer os.Unsetenv(AppURL)
cfg := NewGrafanaCfg(map[string]string{})
v, err := cfg.AppURL()
require.NoError(t, err)
require.Equal(t, "http://localhost-env:3000", v)
})
}

func TestUserAgentFromContext(t *testing.T) {
Expand Down Expand Up @@ -328,3 +338,23 @@ func TestSql(t *testing.T) {
require.ErrorContains(t, err, "not a valid integer")
})
}

func TestPluginAppClientSecret(t *testing.T) {
t.Run("it should return the configured PluginAppClientSecret", func(t *testing.T) {
cfg := NewGrafanaCfg(map[string]string{
AppClientSecret: "client-secret",
})
v, err := cfg.PluginAppClientSecret()
require.NoError(t, err)
require.Equal(t, "client-secret", v)
})

t.Run("it should return the configured PluginAppClientSecret from env", func(t *testing.T) {
os.Setenv(AppClientSecret, "client-secret")
defer os.Unsetenv(AppClientSecret)
cfg := NewGrafanaCfg(map[string]string{})
v, err := cfg.PluginAppClientSecret()
require.NoError(t, err)
require.Equal(t, "client-secret", v)
})
}
57 changes: 0 additions & 57 deletions experimental/oauthtokenretriever/sign.go

This file was deleted.

56 changes: 0 additions & 56 deletions experimental/oauthtokenretriever/sign_test.go

This file was deleted.

116 changes: 0 additions & 116 deletions experimental/oauthtokenretriever/token.go

This file was deleted.

64 changes: 0 additions & 64 deletions experimental/oauthtokenretriever/token_test.go

This file was deleted.

Loading

0 comments on commit 5ec91ca

Please sign in to comment.