Skip to content

Commit

Permalink
Merge pull request #613 from addshore/pkg-client_custom-headers
Browse files Browse the repository at this point in the history
Custom Headers in Alerts in golang part
  • Loading branch information
Slach authored Oct 28, 2024
2 parents 454ead0 + 32e139d commit f5c3607
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docker/grafana/alerting/unified_alerts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ groups:
relativeTimeRange:
from: 600
to: 0
# clickhouse
datasourceUid: P7E099F39B84EA795
# clickhouse-x-auth-http
datasourceUid: P3B5EB012C49EA995
model:
database: default
dateColDataType: ""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: 1

datasources:
- name: clickhouse-x-auth
- name: clickhouse-x-auth-http
type: vertamedia-clickhouse-datasource
access: proxy
url: http://clickhouse:8123
Expand All @@ -10,12 +10,12 @@ datasources:
jsonData:
addCorsHeader: true
serverName: "clickhouse"
tlsAuth: true
tlsAuthWithCACert: true
usePOST: true
useYandexCloudAuthorization: true
xHeaderUser: "demo"
# @todo remove workaround after merge https://github.com/grafana/grafana/pull/80858
dataSourceUrl: "https://clickhouse:8443"
dataSourceUrl: "http://clickhouse:8123"
httpHeaderName1: "test-header"
secureJsonData:
xHeaderKey: "demo"
httpHeaderValue1: "test-value"
6 changes: 6 additions & 0 deletions pkg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"compress/flate"
"compress/gzip"

"github.com/andybalholm/brotli"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/klauspost/compress/zstd"
Expand Down Expand Up @@ -76,6 +77,11 @@ func (client *ClickHouseClient) Query(ctx context.Context, query string) (*Respo
req.Header.Set("X-ClickHouse-Key", password)
}
}
if client.settings.CustomHeaders != nil {
for k, v := range client.settings.CustomHeaders {
req.Header.Set(k, v)
}
}

tlsCACert, tlsCACertExists := client.settings.Instance.DecryptedSecureJSONData["tlsCACert"]
tlsClientCert, tlsClientCertExists := client.settings.Instance.DecryptedSecureJSONData["tlsClientCert"]
Expand Down
25 changes: 24 additions & 1 deletion pkg/datasource_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
)

// DatasourceSettings TODO Add Support custom headers
type DatasourceSettings struct {
Instance backend.DataSourceInstanceSettings

Expand All @@ -22,16 +22,39 @@ type DatasourceSettings struct {
UseCompression bool `json:"useCompression,omitempty"`
CompressionType string `json:"compressionType,omitempty"`
TLSSkipVerify bool `json:"tlsSkipVerify"`

CustomHeaders map[string]string `json:"-,omitempty"`
}

func NewDatasourceSettings(ctx context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {

var dsSettings = DatasourceSettings{}

err := json.Unmarshal(settings.JSONData, &dsSettings)
if err != nil {
return nil, fmt.Errorf("unable to parse settings json %s. Error: %w", settings.JSONData, err)
}

dsSettings.CustomHeaders = make(map[string]string)

var tmpMap = make(map[string]interface{})
err = json.Unmarshal(settings.JSONData, &tmpMap)
if err != nil {
return nil, fmt.Errorf("unable to parse settings json %s. Error: %w", settings.JSONData, err)
}

for headerKey, value := range tmpMap {
if len(headerKey) >= 14 && headerKey[:14] == "httpHeaderName" {
headerName := value.(string)
valueKey := strings.Replace(headerKey, "httpHeaderName", "httpHeaderValue", 1)
if decryptedHeaderValue, exists := settings.DecryptedSecureJSONData[valueKey]; !exists {
return nil, fmt.Errorf("%s not present in settings.DecryptedSecureJSONData", valueKey)
} else {
dsSettings.CustomHeaders[headerName] = decryptedHeaderValue
}
}
}

dsSettings.Instance = settings

return &dsSettings, nil
Expand Down
57 changes: 57 additions & 0 deletions pkg/datasource_settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"context"
"testing"

"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/stretchr/testify/require"
)

func TestNewDatasourceSettings(t *testing.T) {
ctx := context.Background()

// Create a mock backend.DataSourceInstanceSettings
settings := backend.DataSourceInstanceSettings{
JSONData: []byte(`{
"addCorsHeader": true,
"defaultDatabase": "myDatabase",
"usePOST": false,
"useYandexCloudAuthorization": true,
"xHeaderKey": "myKey",
"xHeaderUser": "myUser",
"useCompression": true,
"compressionType": "gzip",
"tlsSkipVerify": false,
"httpHeaderName1": "header1",
"httpHeaderValue1": "value1",
"httpHeaderName2": "header2",
"httpHeaderValue2": "value2"
}`),
DecryptedSecureJSONData: map[string]string{
"httpHeaderValue1": "value1",
"httpHeaderValue2": "value2",
},
}

// Call the NewDatasourceSettings function
instance, err := NewDatasourceSettings(ctx, settings)
require.NoError(t, err)

// Assert the instance type
dsSettings, ok := instance.(*DatasourceSettings)
require.True(t, ok)

// Assert the instance fields
require.Equal(t, true, dsSettings.AddCorsHeader)
require.Equal(t, "myDatabase", dsSettings.DefaultDatabase)
require.Equal(t, false, dsSettings.UsePost)
require.Equal(t, true, dsSettings.UseYandexCloudAuthorization)
require.Equal(t, "myKey", dsSettings.XHeaderKey)
require.Equal(t, "myUser", dsSettings.XHeaderUser)
require.Equal(t, true, dsSettings.UseCompression)
require.Equal(t, "gzip", dsSettings.CompressionType)
require.Equal(t, false, dsSettings.TLSSkipVerify)
require.Equal(t, "value1", dsSettings.CustomHeaders["header1"])
require.Equal(t, "value2", dsSettings.CustomHeaders["header2"])
}

0 comments on commit f5c3607

Please sign in to comment.