Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic auth to loki client #394

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ type Loki struct {
Certificate string `mapstructure:"certificate"`
Path string `mapstructure:"path"`
Channels []*Loki `mapstructure:"channels"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
}

// Elasticsearch configuration
Expand Down
10 changes: 10 additions & 0 deletions pkg/config/target_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ func (f *TargetFactory) createLokiClient(config, parent *Loki) target.Client {
setFallback(&config.Certificate, parent.Certificate)
setFallback(&config.Path, parent.Path)
setBool(&config.SkipTLS, parent.SkipTLS)
setFallback(&config.Username, parent.Username)
setFallback(&config.Password, parent.Password)

config.MapBaseParent(parent.TargetBaseOptions)

Expand All @@ -391,6 +393,8 @@ func (f *TargetFactory) createLokiClient(config, parent *Loki) target.Client {
Host: config.Host + config.Path,
CustomLabels: config.CustomFields,
HTTPClient: http.NewClient(config.Certificate, config.SkipTLS),
Username: config.Username,
Password: config.Password,
})
}

Expand Down Expand Up @@ -799,6 +803,12 @@ func (f *TargetFactory) mapSecretValues(config any, ref, mountedSecret string) {
if values.Host != "" {
c.Host = values.Host
}
if values.Username != "" {
c.Username = values.Username
}
if values.Password != "" {
c.Password = values.Password
}

case *Slack:
if values.Webhook != "" {
Expand Down
30 changes: 26 additions & 4 deletions pkg/config/target_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,21 @@ func Test_GetValuesFromSecret(t *testing.T) {
t.Fatal("Expected one client created")
}

fv := reflect.ValueOf(clients[0]).Elem().FieldByName("host")
if v := fv.String(); v != "http://localhost:9200/api/prom/push" {
client := reflect.ValueOf(clients[0]).Elem()

if v := client.FieldByName("host").String(); v != "http://localhost:9200/api/prom/push" {
t.Errorf("Expected host from secret, got %s", v)
}

username := client.FieldByName("username").String()
if username != "username" {
t.Errorf("Expected username from secret, got %s", username)
}

password := client.FieldByName("password").String()
if password != "password" {
t.Errorf("Expected password from secret, got %s", password)
}
})

t.Run("Get Elasticsearch values from Secret", func(t *testing.T) {
Expand Down Expand Up @@ -625,10 +636,21 @@ func Test_GetValuesFromMountedSecret(t *testing.T) {
t.Error("Expected one client created")
}

fv := reflect.ValueOf(clients[0]).Elem().FieldByName("host")
if v := fv.String(); v != "http://localhost:9200/api/prom/push" {
client := reflect.ValueOf(clients[0]).Elem()
if v := client.FieldByName("host").String(); v != "http://localhost:9200/api/prom/push" {
t.Errorf("Expected host from mounted secret, got %s", v)
}

username := client.FieldByName("username").String()
if username != "username" {
t.Errorf("Expected username from mounted secret, got %s", username)
}

password := client.FieldByName("password").String()
if password != "password" {
t.Errorf("Expected password from mounted secret, got %s", password)
}

})

t.Run("Get Elasticsearch values from MountedSecret", func(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/target/loki/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Options struct {
Host string
CustomLabels map[string]string
HTTPClient http.Client
Username string
Password string
}

type payload struct {
Expand Down Expand Up @@ -92,6 +94,8 @@ type client struct {
host string
client http.Client
customLabels map[string]string
username string
password string
}

func (l *client) Send(result v1alpha2.PolicyReportResult) {
Expand All @@ -101,6 +105,9 @@ func (l *client) Send(result v1alpha2.PolicyReportResult) {
}

req.Header.Set("Content-Type", "application/json")
if l.username != "" {
req.SetBasicAuth(l.username, l.password)
}

resp, err := l.client.Do(req)
http.ProcessHTTPResponse(l.Name(), resp, err)
Expand All @@ -113,5 +120,7 @@ func NewClient(options Options) target.Client {
options.Host,
options.HTTPClient,
options.CustomLabels,
options.Username,
options.Password,
}
}
6 changes: 6 additions & 0 deletions pkg/target/loki/loki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func Test_LokiTarget(t *testing.T) {
t.Errorf("Unexpected Host: %s", url)
}

if req.Header.Get("Authorization") == "" {
t.Error("Expected Authentication header for BasicAuth is set")
}

expectedLine := fmt.Sprintf("[%s] %s", strings.ToUpper(fixtures.CompleteTargetSendResult.Priority.String()), fixtures.CompleteTargetSendResult.Message)
labels, line := convertAndValidateBody(req, t)
if line != expectedLine {
Expand Down Expand Up @@ -95,6 +99,8 @@ func Test_LokiTarget(t *testing.T) {
Host: "http://localhost:3100/api/prom/push",
CustomLabels: map[string]string{"custom": "label"},
HTTPClient: testClient{callback, 200},
Username: "username",
Password: "password",
})
client.Send(fixtures.CompleteTargetSendResult)
})
Expand Down
Loading