Kaynağa Gözat

fix: Patch auth and since DSN

Louis PERDEREAU 2 yıl önce
ebeveyn
işleme
945a381b2e

+ 3 - 0
go.mod

@@ -49,6 +49,7 @@ require (
 	github.com/google/uuid v1.3.0
 	github.com/google/winops v0.0.0-20230712152054-af9b550d0601
 	github.com/goombaio/namegenerator v0.0.0-20181006234301-989e774b106e
+	github.com/gorilla/websocket v1.5.0
 	github.com/hashicorp/go-hclog v1.5.0
 	github.com/hashicorp/go-plugin v1.4.10
 	github.com/hashicorp/go-version v1.2.1
@@ -88,6 +89,8 @@ require (
 	gopkg.in/yaml.v2 v2.4.0
 	gopkg.in/yaml.v3 v3.0.1
 	k8s.io/apiserver v0.27.3
+	gotest.tools/v3 v3.0.3
+	k8s.io/apiserver v0.27.3
 )
 
 require (

+ 21 - 13
pkg/acquisition/modules/loki/loki.go

@@ -19,8 +19,8 @@ import (
 	yaml "gopkg.in/yaml.v2"
 
 	"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
-	"github.com/crowdsecurity/crowdsec/pkg/types"
 	lokiclient "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/loki/internal/lokiclient"
+	"github.com/crowdsecurity/crowdsec/pkg/types"
 )
 
 const (
@@ -37,17 +37,21 @@ var linesRead = prometheus.NewCounterVec(
 	},
 	[]string{"source"})
 
+type LokiAuthConfiguration struct {
+	Username string `yaml:"username"`
+	Password string `yaml:"password"`
+}
+
 type LokiConfiguration struct {
-	URL                               string            `yaml:"url"`    // Loki url
-	Prefix                            string            `yaml:"prefix"` // Loki prefix
-	Query                             string            `yaml:"query"`  // LogQL query
-	Limit                             int               `yaml:"limit"`  // Limit of logs to read
-	DelayFor                          time.Duration     `yaml:"delay_for"`
-	Since                             time.Duration     `yaml:"since"`
-	Headers                           map[string]string `yaml:"headers"`        // HTTP headers for talking to Loki
-	WaitForReady                      time.Duration     `yaml:"wait_for_ready"` // Retry interval, default is 10 seconds
-	Username                          string            `yaml:"username"`
-	Password                          string            `yaml:"password"`
+	URL                               string                `yaml:"url"`    // Loki url
+	Prefix                            string                `yaml:"prefix"` // Loki prefix
+	Query                             string                `yaml:"query"`  // LogQL query
+	Limit                             int                   `yaml:"limit"`  // Limit of logs to read
+	DelayFor                          time.Duration         `yaml:"delay_for"`
+	Since                             time.Duration         `yaml:"since"`
+	Headers                           map[string]string     `yaml:"headers"`        // HTTP headers for talking to Loki
+	WaitForReady                      time.Duration         `yaml:"wait_for_ready"` // Retry interval, default is 10 seconds
+	Auth                              LokiAuthConfiguration `yaml:"auth"`
 	configuration.DataSourceCommonCfg `yaml:",inline"`
 }
 
@@ -146,6 +150,10 @@ func (l *LokiSource) ConfigureByDSN(dsn string, labels map[string]string, logger
 	scheme := "http"
 
 	l.Config.URL = fmt.Sprintf("%s://%s", scheme, u.Host)
+	if u.User != nil {
+		l.Config.Auth.Username = u.User.Username()
+		l.Config.Auth.Password, _ = u.User.Password()
+	}
 	params := u.Query()
 	if q := params.Get("query"); q != "" {
 		l.Config.Query = q
@@ -197,8 +205,8 @@ func (l *LokiSource) ConfigureByDSN(dsn string, labels map[string]string, logger
 		Limit:    l.Config.Limit,
 		Query:    l.Config.Query,
 		Since:    l.Config.Since,
-		Username: l.Config.Username,
-		Password: l.Config.Password,
+		Username: l.Config.Auth.Username,
+		Password: l.Config.Auth.Password,
 	}
 
 	l.Client = lokiclient.NewLokiClient(clientConfig)

+ 12 - 16
pkg/acquisition/modules/loki/loki_test.go

@@ -153,11 +153,11 @@ func TestConfigureDSN(t *testing.T) {
 			dsn:   `loki://127.0.0.1:3100/?since=3h&query={server="demo"}`,
 			since: time.Now().Add(-3 * time.Hour),
 		},
-		/*{
+		{
 			name:     "Basic Auth",
-			dsn:      `loki://login:password@localhost:3100/?query={server="demo"}`,
+			dsn:      `loki://login:password@localhost:3102/?query={server="demo"}`,
 			password: "password",
-		},*/
+		},
 		{
 			name:         "Correct DSN",
 			dsn:          `loki://localhost:3100/?query={server="demo"}&wait_for_ready=5s`,
@@ -171,26 +171,22 @@ func TestConfigureDSN(t *testing.T) {
 			"type": "loki",
 			"name": test.name,
 		})
+		t.Logf("Test : %s", test.name)
 		lokiSource := &loki.LokiSource{}
 		err := lokiSource.ConfigureByDSN(test.dsn, map[string]string{"type": "testtype"}, subLogger, "")
 		cstest.AssertErrorContains(t, err, test.expectedErr)
-		/*if time.Time(lokiSource.Config.Since).Round(time.Second) != test.since.Round(time.Second) {
+
+		noDuration, _ := time.ParseDuration("0s")
+		if lokiSource.Config.Since != noDuration && lokiSource.Config.Since.Round(time.Second) != time.Since(test.since).Round(time.Second) {
 			t.Fatalf("Invalid since %v", lokiSource.Config.Since)
 		}
-		if test.password == "" {
-			if lokiSource.auth != nil {
-				t.Fatalf("Password should be empty : %v", lokiSource.auth)
-			}
-		} else {
-			p, _ := lokiSource.auth.Password()
+
+		if test.password != "" {
+			p := lokiSource.Config.Auth.Password
 			if test.password != p {
-				t.Fatalf("Wrong password : %s != %s", test.password, p)
-			}
-			a := lokiSource.header.Get("authorization")
-			if !strings.HasPrefix(a, "Basic ") {
-				t.Fatalf("Bad auth header : %s", a)
+				t.Fatalf("Password mismatch : %s != %s", test.password, p)
 			}
-		}*/
+		}
 		if test.waitForReady != 0 {
 			if lokiSource.Config.WaitForReady != test.waitForReady {
 				t.Fatalf("Wrong WaitForReady %v != %v", lokiSource.Config.WaitForReady, test.waitForReady)