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
449453func (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 {
0 commit comments