Skip to content

Commit e6253c6

Browse files
Exposed loki CA certificate path configuration (#181)
1 parent 7151dd7 commit e6253c6

File tree

6 files changed

+20
-7
lines changed

6 files changed

+20
-7
lines changed

cmd/plugin-backend.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var (
3030
lokiLabels = flag.String("loki-labels", "SrcK8S_Namespace,SrcK8S_OwnerName,DstK8S_Namespace,DstK8S_OwnerName,FlowDirection", "Loki labels, comma separated")
3131
lokiTimeout = flag.Duration("loki-timeout", 10*time.Second, "Timeout of the Loki query to retrieve logs")
3232
lokiTenantID = flag.String("loki-tenant-id", "", "Tenant organization ID for multi-tenant-loki (submitted as the X-Scope-OrgID HTTP header)")
33+
lokiCAPath = flag.String("loki-ca-path", "", "Path to loki CA certificate")
3334
lokiSkipTLS = flag.Bool("loki-skip-tls", false, "Skip TLS checks for loki HTTPS connection")
3435
lokiMock = flag.Bool("loki-mock", false, "Fake loki results using saved mocks")
3536
logLevel = flag.String("loglevel", "info", "log level (default: info)")
@@ -74,7 +75,7 @@ func main() {
7475
CORSAllowMethods: *corsMethods,
7576
CORSAllowHeaders: *corsHeaders,
7677
CORSMaxAge: *corsMaxAge,
77-
Loki: loki.NewConfig(lURL, *lokiTimeout, *lokiTenantID, *lokiSkipTLS, *lokiMock, strings.Split(lLabels, ",")),
78+
Loki: loki.NewConfig(lURL, *lokiTimeout, *lokiTenantID, *lokiSkipTLS, *lokiCAPath, *lokiMock, strings.Split(lLabels, ",")),
7879
FrontendConfig: *frontendConfig,
7980
})
8081
}

pkg/handler/loki.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func newLokiClient(cfg *loki.Config) httpclient.Caller {
4444
}
4545

4646
// TODO: loki with auth
47-
return httpclient.NewHTTPClient(cfg.Timeout, headers, cfg.SkipTLS)
47+
return httpclient.NewHTTPClient(cfg.Timeout, headers, cfg.SkipTLS, cfg.CAPath)
4848
}
4949

5050
/* loki query will fail if spaces or quotes are not encoded

pkg/httpclient/http_client.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package httpclient
22

33
import (
44
"crypto/tls"
5+
"crypto/x509"
56
"io/ioutil"
67
"net"
78
"net/http"
@@ -22,16 +23,24 @@ type httpClient struct {
2223

2324
var slog = logrus.WithField("module", "server")
2425

25-
func NewHTTPClient(timeout time.Duration, overrideHeaders map[string][]string, skipTLS bool) Caller {
26+
func NewHTTPClient(timeout time.Duration, overrideHeaders map[string][]string, skipTLS bool, capath string) Caller {
2627
transport := &http.Transport{
2728
DialContext: (&net.Dialer{Timeout: timeout}).DialContext,
2829
IdleConnTimeout: timeout,
2930
}
3031

31-
//TODO: add loki tls config https://issues.redhat.com/browse/NETOBSERV-309
3232
if skipTLS {
3333
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
3434
slog.Warn("skipping TLS checks. SSL certificate verification is now disabled !")
35+
} else if capath != "" {
36+
caCert, err := ioutil.ReadFile(capath)
37+
if err != nil {
38+
slog.Errorf("Cannot load loki ca certificate: %v", err)
39+
} else {
40+
pool := x509.NewCertPool()
41+
pool.AppendCertsFromPEM(caCert)
42+
transport.TLSClientConfig = &tls.Config{RootCAs: pool}
43+
}
3544
}
3645

3746
return &httpClient{

pkg/loki/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ type Config struct {
1212
Timeout time.Duration
1313
TenantID string
1414
SkipTLS bool
15+
CAPath string
1516
UseMocks bool
1617
Labels map[string]struct{}
1718
}
1819

19-
func NewConfig(url *url.URL, timeout time.Duration, tenantID string, skipTLS bool, useMocks bool, labels []string) Config {
20+
func NewConfig(url *url.URL, timeout time.Duration, tenantID string, skipTLS bool, capath string, useMocks bool, labels []string) Config {
2021
return Config{
2122
URL: url,
2223
Timeout: timeout,
2324
TenantID: tenantID,
2425
SkipTLS: skipTLS,
26+
CAPath: capath,
2527
UseMocks: useMocks,
2628
Labels: utils.GetMapInterface(labels),
2729
}

pkg/loki/query_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func TestFlowQuery_AddLabelFilters(t *testing.T) {
1313
lokiURL, err := url.Parse("/")
1414
require.NoError(t, err)
15-
cfg := NewConfig(lokiURL, time.Second, "", false, false, []string{"foo", "flis"})
15+
cfg := NewConfig(lokiURL, time.Second, "", false, "", false, []string{"foo", "flis"})
1616
query := NewFlowQueryBuilderWithDefaults(&cfg)
1717
err = query.AddFilter("foo", `"bar"`)
1818
require.NoError(t, err)
@@ -25,7 +25,7 @@ func TestFlowQuery_AddLabelFilters(t *testing.T) {
2525
func TestQuery_BackQuote_Error(t *testing.T) {
2626
lokiURL, err := url.Parse("/")
2727
require.NoError(t, err)
28-
cfg := NewConfig(lokiURL, time.Second, "", false, false, []string{"lab1", "lab2"})
28+
cfg := NewConfig(lokiURL, time.Second, "", false, "", false, []string{"lab1", "lab2"})
2929
query := NewFlowQueryBuilderWithDefaults(&cfg)
3030
assert.Error(t, query.AddFilter("key", "backquoted`val"))
3131
}

pkg/server/server_flows_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ func TestLokiFiltering(t *testing.T) {
231231
time.Second,
232232
"",
233233
false,
234+
"",
234235
false,
235236
[]string{"SrcK8S_Namespace", "SrcK8S_OwnerName", "DstK8S_Namespace", "DstK8S_OwnerName", "FlowDirection"},
236237
),

0 commit comments

Comments
 (0)