Skip to content

Commit

Permalink
add support for tls client auth in prometheus datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
izolight committed Jul 29, 2024
1 parent bb1093e commit 2622664
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions datasource/prometheus/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ func WithCertificate(certificate string) Option {
}
}

// WithTLSClientAuth sets the client tls keypair.
func WithTLSClientAuth(certificate, key string) Option {
return func(datasource *Prometheus) error {
datasource.builder.JSONData.(map[string]interface{})["tlsAuth"] = true
datasource.builder.SecureJSONData.(map[string]interface{})["tlsClientCert"] = certificate
datasource.builder.SecureJSONData.(map[string]interface{})["tlsClientKey"] = key

return nil
}
}

// WithCredentials joins credentials such as cookies or auth headers to cross-site requests.
func WithCredentials() Option {
return func(datasource *Prometheus) error {
Expand Down
10 changes: 10 additions & 0 deletions datasource/prometheus/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/K-Phoen/grabana/datasource"

Check failure on line 8 in datasource/prometheus/options_test.go

View workflow job for this annotation

GitHub Actions / Tests (1.20)

"github.com/K-Phoen/grabana/datasource" imported and not used

Check failure on line 8 in datasource/prometheus/options_test.go

View workflow job for this annotation

GitHub Actions / Tests (1.20)

"github.com/K-Phoen/grabana/datasource" imported and not used (typecheck)

Check failure on line 8 in datasource/prometheus/options_test.go

View workflow job for this annotation

GitHub Actions / Tests (1.20)

"github.com/K-Phoen/grabana/datasource" imported and not used (typecheck)

Check failure on line 8 in datasource/prometheus/options_test.go

View workflow job for this annotation

GitHub Actions / Tests (1.21)

"github.com/K-Phoen/grabana/datasource" imported and not used (typecheck)

Check failure on line 8 in datasource/prometheus/options_test.go

View workflow job for this annotation

GitHub Actions / Tests (1.21)

"github.com/K-Phoen/grabana/datasource" imported and not used (typecheck)

Check failure on line 8 in datasource/prometheus/options_test.go

View workflow job for this annotation

GitHub Actions / Tests (1.21)

"github.com/K-Phoen/grabana/datasource" imported and not used
"github.com/K-Phoen/grabana/errors"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -114,6 +115,15 @@ func TestWithCertificate(t *testing.T) {
req.Equal("certificate-content", datasource.builder.SecureJSONData.(map[string]interface{})["tlsCACert"])
}

func TestWithTLSClientAuuth(t *testing.T) {
req := require.New(t)
datasource, err := New("", "", WithTLSClientAuth("cert-content", "key-content"))
req.NoError(err)
req.Equal(true, datasource.builder.JSONData.(map[string]interface{})["tlsAuth"])
req.Equal("cert-content", datasource.builder.SecureJSONData.(map[string]interface{})["tlsClientCert"])
req.Equal("key-content", datasource.builder.SecureJSONData.(map[string]interface{})["tlsClientKey"])
}

func TestWithCredentials(t *testing.T) {
req := require.New(t)

Expand Down

0 comments on commit 2622664

Please sign in to comment.