Skip to content

Commit e886b5a

Browse files
committed
✨ Feat: ad cred passtrough initial commit
1 parent 2e9f67d commit e886b5a

File tree

7 files changed

+83
-17
lines changed

7 files changed

+83
-17
lines changed

docker-compose.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ services:
77
build:
88
context: ./.config
99
args:
10-
grafana_image: ${GRAFANA_IMAGE:-grafana-enterprise}
11-
grafana_version: ${GRAFANA_VERSION:-10.0.3}
10+
grafana_image: ${GRAFANA_IMAGE:-grafana}
11+
grafana_version: ${GRAFANA_VERSION:-11.6.0}
1212
ports:
1313
- 3000:3000/tcp
1414
volumes:

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ toolchain go1.24.2
66

77
require (
88
github.com/databricks/databricks-sql-go v1.7.0
9+
github.com/grafana/grafana-azure-sdk-go v1.13.1
910
github.com/grafana/grafana-plugin-sdk-go v0.277.0
1011
golang.org/x/oauth2 v0.29.0
1112
)
@@ -76,7 +77,7 @@ require (
7677
github.com/olekukonko/tablewriter v0.0.5 // indirect
7778
github.com/perimeterx/marshmallow v1.1.5 // indirect
7879
github.com/pierrec/lz4/v4 v4.1.22 // indirect
79-
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
80+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
8081
github.com/pkg/errors v0.9.1 // indirect
8182
github.com/prometheus/client_golang v1.20.5 // indirect
8283
github.com/prometheus/client_model v0.6.1 // indirect

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1
9494
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
9595
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
9696
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
97+
github.com/grafana/grafana-azure-sdk-go v1.13.1/go.mod h1:SAlwLdEuox4vw8ZaeQwnepYXnhznnQQdstJbcw8LH68=
9798
github.com/grafana/grafana-plugin-sdk-go v0.277.0 h1:VDU2F4Y5NeRS//ejctdZtsAshrGaEdbtW33FsK0EQss=
9899
github.com/grafana/grafana-plugin-sdk-go v0.277.0/go.mod h1:mAUWg68w5+1f5TLDqagIr8sWr1RT9h7ufJl5NMcWJAU=
99100
github.com/grafana/otel-profiling-go v0.5.1 h1:stVPKAFZSa7eGiqbYuG25VcqYksR6iWvF3YH66t4qL8=
@@ -190,6 +191,7 @@ github.com/pierrec/lz4/v4 v4.1.22 h1:cKFw6uJDK+/gfw5BcDL0JL5aBsAFdsIT18eRtLj7VIU
190191
github.com/pierrec/lz4/v4 v4.1.22/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
191192
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
192193
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
194+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
193195
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
194196
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
195197
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -351,6 +353,7 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
351353
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
352354
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
353355
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
356+
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
354357
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
355358
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
356359
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

pkg/integrations/azure_ad.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package integrations
2+
3+
import (
4+
"github.com/databricks/databricks-sql-go/auth"
5+
"github.com/grafana/grafana-azure-sdk-go/azsettings"
6+
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
7+
"net/http"
8+
)
9+
10+
type azureAdCredentials struct {
11+
azuresettings *azsettings.AzureSettings
12+
}
13+
14+
func (c *azureAdCredentials) Authenticate(r *http.Request) error {
15+
16+
log.DefaultLogger.Info("AzureSetting", "azuresettings", c.azuresettings)
17+
ctx := r.Context()
18+
log.DefaultLogger.Info("Auth Token", "token", ctx.Value("token"))
19+
log.DefaultLogger.Info("Auth IDToken", "idtoken", ctx.Value("idToken"))
20+
21+
return nil
22+
23+
}
24+
25+
func NewAzureADCredentials(azuresettings *azsettings.AzureSettings) auth.Authenticator {
26+
return &azureAdCredentials{
27+
azuresettings: azuresettings,
28+
}
29+
}

pkg/plugin/plugin.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
dbsql "github.com/databricks/databricks-sql-go"
1010
"github.com/databricks/databricks-sql-go/auth"
1111
"github.com/databricks/databricks-sql-go/auth/oauth/m2m"
12+
"github.com/grafana/grafana-azure-sdk-go/azsettings"
1213
"github.com/grafana/grafana-plugin-sdk-go/backend"
1314
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
1415
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
@@ -69,10 +70,11 @@ type ConnectionSettings struct {
6970
MaxRetryDuration time.Duration
7071
Timeout time.Duration
7172
MaxRows int
73+
idToken string
7274
}
7375

7476
// NewSampleDatasource creates a new datasource instance.
75-
func NewSampleDatasource(_ context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
77+
func NewSampleDatasource(ctx context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
7678
datasourceSettings := new(DatasourceSettings)
7779
err := json.Unmarshal(settings.JSONData, datasourceSettings)
7880
if err != nil {
@@ -91,7 +93,7 @@ func NewSampleDatasource(_ context.Context, settings backend.DataSourceInstanceS
9193
port = portInt
9294
}
9395

94-
if datasourceSettings.AuthenticationMethod == "m2m" || datasourceSettings.AuthenticationMethod == "oauth2_client_credentials" {
96+
if datasourceSettings.AuthenticationMethod == "m2m" || datasourceSettings.AuthenticationMethod == "oauth2_client_credentials" || datasourceSettings.AuthenticationMethod == "azure_ad_forward" {
9597
var authenticator auth.Authenticator
9698

9799
if datasourceSettings.AuthenticationMethod == "oauth2_client_credentials" {
@@ -112,6 +114,15 @@ func NewSampleDatasource(_ context.Context, settings backend.DataSourceInstanceS
112114
datasourceSettings.Hostname,
113115
[]string{},
114116
)
117+
} else if datasourceSettings.AuthenticationMethod == "azure_ad_forward" {
118+
azureSettings, err := azsettings.ReadSettings(ctx)
119+
if err != nil {
120+
log.DefaultLogger.Info("Failed to get Azure Setting", "err", err)
121+
return nil, err
122+
}
123+
authenticator = integrations.NewAzureADCredentials(
124+
azureSettings,
125+
)
115126
} else {
116127
log.DefaultLogger.Info("Authentication Method Parse Error", "err", nil)
117128
return nil, fmt.Errorf("authentication Method Parse Error")
@@ -131,18 +142,11 @@ func NewSampleDatasource(_ context.Context, settings backend.DataSourceInstanceS
131142
return nil, err
132143
} else {
133144
log.DefaultLogger.Info("Init Databricks SQL DB")
134-
databricksDB := sql.OpenDB(connector)
135-
136-
if err := databricksDB.Ping(); err != nil {
137-
log.DefaultLogger.Info("Ping Error (Could not ping Databricks)", "err", err)
138-
return nil, err
139-
}
140145

141-
SetDatasourceSettings(databricksDB, connectionSettings)
142146
log.DefaultLogger.Info("Store Databricks SQL DB Connection")
143147
return &Datasource{
144148
connector: connector,
145-
databricksDB: databricksDB,
149+
databricksDB: nil,
146150
connectionSettings: connectionSettings,
147151
}, nil
148152
}
@@ -449,6 +453,22 @@ func (d *Datasource) query(ctx context.Context, pCtx backend.PluginContext, quer
449453
func (d *Datasource) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
450454
log.DefaultLogger.Info("CheckHealth called", "request", req)
451455

456+
token := strings.Fields(req.GetHTTPHeader(backend.OAuthIdentityTokenHeaderName))
457+
idToken := req.GetHTTPHeader(backend.OAuthIdentityIDTokenHeaderName)
458+
log.DefaultLogger.Info("Token", "token", token)
459+
log.DefaultLogger.Info("ID Token", "idToken", idToken)
460+
461+
ctx = context.WithValue(ctx, backend.OAuthIdentityTokenHeaderName, token)
462+
ctx = context.WithValue(ctx, backend.OAuthIdentityIDTokenHeaderName, idToken)
463+
464+
if d.databricksDB == nil {
465+
err := d.RefreshDBConnection()
466+
if err != nil {
467+
log.DefaultLogger.Info("RefreshDBConnection Error", "err", err)
468+
return nil, err
469+
}
470+
}
471+
452472
rows, err := d.QueryContext(ctx, "SELECT 1")
453473

454474
if err != nil {

src/components/ConfigEditor/ConfigEditor.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,20 @@ export class ConfigEditor extends PureComponent<Props, State> {
3030

3131
onSelectValueChange = (value: string | undefined, key: string) => {
3232
const {onOptionsChange, options} = this.props;
33+
let jsonData = (options.jsonData || {}) as DatabricksDataSourceOptions;
34+
jsonData = {
35+
...jsonData,
36+
[key]: value
37+
}
38+
if (key == 'authenticationMethod') {
39+
jsonData = {
40+
...jsonData,
41+
oauthPassThru: true,
42+
}
43+
}
3344
onOptionsChange({
3445
...options,
35-
jsonData: {
36-
...options.jsonData,
37-
[key]: value,
38-
},
46+
jsonData: jsonData
3947
});
4048
}
4149

@@ -118,6 +126,10 @@ export class ConfigEditor extends PureComponent<Props, State> {
118126
value: 'oauth2_client_credentials',
119127
label: 'OAuth2 Client Credentials',
120128
},
129+
{
130+
value: 'azure_ad_forward',
131+
label: 'Forward Azure AD Auth',
132+
},
121133
]}
122134
value={jsonData.authenticationMethod || 'dsn'}
123135
backspaceRemovesValue

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface DatabricksDataSourceOptions extends SQLOptions {
1616
maxRetryDuration?: string;
1717
timeout?: string;
1818
maxRows?: string;
19+
oauthPassThru?: boolean;
1920
}
2021

2122
/**

0 commit comments

Comments
 (0)