Skip to content

Commit ac71ca4

Browse files
authored
Set proxy options from context (#955)
* Set proxy options from context * add tests
1 parent 93ac8d4 commit ac71ca4

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

backend/common.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,15 @@ func propagateTenantIDIfPresent(ctx context.Context) context.Context {
237237
return ctx
238238
}
239239

240+
func (s *DataSourceInstanceSettings) ProxyOptionsFromContext(ctx context.Context) (*proxy.Options, error) {
241+
cfg := GrafanaConfigFromContext(ctx)
242+
p, err := cfg.proxy()
243+
if err != nil {
244+
return nil, err
245+
}
246+
return s.ProxyOptions(p.clientCfg)
247+
}
248+
240249
func (s *DataSourceInstanceSettings) ProxyOptions(clientCfg *proxy.ClientCfg) (*proxy.Options, error) {
241250
opts := &proxy.Options{}
242251

backend/common_test.go

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,171 @@ func TestProxyOptions(t *testing.T) {
398398
}
399399
})
400400
}
401+
402+
func TestProxyOptionsFromContext(t *testing.T) {
403+
tcs := []struct {
404+
name string
405+
instanceSettings *DataSourceInstanceSettings
406+
grafanaCfg *GrafanaCfg
407+
expectedClientOptions *proxy.Options
408+
err error
409+
}{
410+
{
411+
name: "Proxy options are configured when enableSecureSocksProxy is true",
412+
instanceSettings: &DataSourceInstanceSettings{
413+
Name: "ds-name",
414+
Type: "example-datasource",
415+
JSONData: []byte("{ \"enableSecureSocksProxy\": true, \"timeout\": 10, \"keepAlive\": 15, \"secureSocksProxyUsername\": \"user\" }"),
416+
DecryptedSecureJSONData: map[string]string{"secureSocksProxyPassword": "pass"},
417+
},
418+
grafanaCfg: NewGrafanaCfg(
419+
map[string]string{
420+
proxy.PluginSecureSocksProxyEnabled: "true",
421+
proxy.PluginSecureSocksProxyClientCert: "/path/to/client-cert",
422+
proxy.PluginSecureSocksProxyClientCertContents: "client-cert-contents",
423+
proxy.PluginSecureSocksProxyClientKey: "/path/to/client-key",
424+
proxy.PluginSecureSocksProxyClientKeyContents: "client-key-contents",
425+
proxy.PluginSecureSocksProxyRootCAs: "/path/to/root-ca",
426+
proxy.PluginSecureSocksProxyRootCAsContents: "root-ca-contents",
427+
proxy.PluginSecureSocksProxyProxyAddress: "localhost:1234",
428+
proxy.PluginSecureSocksProxyServerName: "proxy-server",
429+
proxy.PluginSecureSocksProxyAllowInsecure: "true",
430+
},
431+
),
432+
expectedClientOptions: &proxy.Options{
433+
Enabled: true,
434+
DatasourceName: "ds-name",
435+
DatasourceType: "example-datasource",
436+
Auth: &proxy.AuthOptions{
437+
Username: "user",
438+
Password: "pass",
439+
},
440+
Timeouts: &proxy.TimeoutOptions{
441+
Timeout: time.Second * 10,
442+
KeepAlive: time.Second * 15,
443+
},
444+
ClientCfg: &proxy.ClientCfg{
445+
ClientCert: "/path/to/client-cert",
446+
ClientKey: "/path/to/client-key",
447+
RootCAs: []string{"/path/to/root-ca"},
448+
ClientCertVal: "client-cert-contents",
449+
ClientKeyVal: "client-key-contents",
450+
RootCAsVals: []string{"root-ca-contents"},
451+
ProxyAddress: "localhost:1234",
452+
ServerName: "proxy-server",
453+
AllowInsecure: true,
454+
},
455+
},
456+
},
457+
{
458+
name: "Datasource UID becomes user name when secureSocksProxyUsername is not set",
459+
instanceSettings: &DataSourceInstanceSettings{
460+
Name: "ds-name",
461+
UID: "ds-uid",
462+
Type: "example-datasource",
463+
JSONData: []byte("{ \"enableSecureSocksProxy\": true, \"timeout\": 10, \"keepAlive\": 15 }"),
464+
DecryptedSecureJSONData: map[string]string{"secureSocksProxyPassword": "pass"},
465+
},
466+
grafanaCfg: NewGrafanaCfg(
467+
map[string]string{
468+
proxy.PluginSecureSocksProxyEnabled: "true",
469+
proxy.PluginSecureSocksProxyClientCert: "/path/to/client-cert",
470+
proxy.PluginSecureSocksProxyClientCertContents: "client-cert-contents",
471+
proxy.PluginSecureSocksProxyClientKey: "/path/to/client-key",
472+
proxy.PluginSecureSocksProxyClientKeyContents: "client-key-contents",
473+
proxy.PluginSecureSocksProxyRootCAs: "/path/to/root-ca",
474+
proxy.PluginSecureSocksProxyRootCAsContents: "root-ca-contents",
475+
proxy.PluginSecureSocksProxyProxyAddress: "localhost:1234",
476+
proxy.PluginSecureSocksProxyServerName: "proxy-server",
477+
proxy.PluginSecureSocksProxyAllowInsecure: "true",
478+
},
479+
),
480+
expectedClientOptions: &proxy.Options{
481+
Enabled: true,
482+
DatasourceName: "ds-name",
483+
DatasourceType: "example-datasource",
484+
Auth: &proxy.AuthOptions{
485+
Username: "ds-uid",
486+
Password: "pass",
487+
},
488+
Timeouts: &proxy.TimeoutOptions{
489+
Timeout: time.Second * 10,
490+
KeepAlive: time.Second * 15,
491+
},
492+
ClientCfg: &proxy.ClientCfg{
493+
ClientCert: "/path/to/client-cert",
494+
ClientKey: "/path/to/client-key",
495+
RootCAs: []string{"/path/to/root-ca"},
496+
ClientCertVal: "client-cert-contents",
497+
ClientKeyVal: "client-key-contents",
498+
RootCAsVals: []string{"root-ca-contents"},
499+
ProxyAddress: "localhost:1234",
500+
ServerName: "proxy-server",
501+
AllowInsecure: true,
502+
},
503+
},
504+
},
505+
{
506+
name: "Datasource UID becomes user name when secureSocksProxyUsername is not set",
507+
instanceSettings: &DataSourceInstanceSettings{
508+
Name: "ds-name",
509+
UID: "ds-uid",
510+
Type: "example-datasource",
511+
JSONData: []byte("{ \"enableSecureSocksProxy\": false }"),
512+
},
513+
grafanaCfg: NewGrafanaCfg(
514+
map[string]string{
515+
proxy.PluginSecureSocksProxyEnabled: "true",
516+
proxy.PluginSecureSocksProxyClientCert: "/path/to/client-cert",
517+
proxy.PluginSecureSocksProxyClientCertContents: "client-cert-contents",
518+
proxy.PluginSecureSocksProxyClientKey: "/path/to/client-key",
519+
proxy.PluginSecureSocksProxyClientKeyContents: "client-key-contents",
520+
proxy.PluginSecureSocksProxyRootCAs: "/path/to/root-ca",
521+
proxy.PluginSecureSocksProxyRootCAsContents: "root-ca-contents",
522+
proxy.PluginSecureSocksProxyProxyAddress: "localhost:1234",
523+
proxy.PluginSecureSocksProxyServerName: "proxy-server",
524+
proxy.PluginSecureSocksProxyAllowInsecure: "true",
525+
},
526+
),
527+
expectedClientOptions: nil,
528+
},
529+
{
530+
name: "Proxy options client configuration is not set when proxy.PluginSecureSocksProxyEnabled is false",
531+
instanceSettings: &DataSourceInstanceSettings{
532+
Name: "ds-name",
533+
UID: "ds-uid",
534+
Type: "example-datasource",
535+
JSONData: []byte("{ \"enableSecureSocksProxy\": true }"),
536+
},
537+
grafanaCfg: NewGrafanaCfg(
538+
map[string]string{
539+
proxy.PluginSecureSocksProxyEnabled: "false",
540+
},
541+
),
542+
expectedClientOptions: &proxy.Options{
543+
Enabled: true,
544+
DatasourceName: "ds-name",
545+
DatasourceType: "example-datasource",
546+
Auth: &proxy.AuthOptions{
547+
Username: "ds-uid",
548+
},
549+
Timeouts: &proxy.TimeoutOptions{
550+
Timeout: time.Second * 30,
551+
KeepAlive: time.Second * 30,
552+
},
553+
ClientCfg: nil,
554+
},
555+
},
556+
}
557+
558+
for _, tc := range tcs {
559+
ctx := WithGrafanaConfig(context.Background(), tc.grafanaCfg)
560+
opts, err := tc.instanceSettings.ProxyOptionsFromContext(ctx)
561+
if tc.err != nil {
562+
require.ErrorIs(t, err, tc.err)
563+
continue
564+
}
565+
require.NoError(t, err)
566+
require.Equal(t, tc.expectedClientOptions, opts)
567+
}
568+
}

0 commit comments

Comments
 (0)