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

Loki integration #2 #2306

Merged
merged 74 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
343724b
loki draft.
athoune Jun 5, 2022
bc6f327
Ready and Read once.
athoune Jun 6, 2022
437c2af
Prometheus counter. Better URL handling. Streamin acquisition.
athoune Jun 6, 2022
01646c7
Test and fix config.
athoune Jun 6, 2022
7037463
Stream test draft.
athoune Jun 6, 2022
b17711e
Push log to Loki.
athoune Jun 7, 2022
bd28c2c
Fix: specific entry unmarshal
athoune Jun 7, 2022
5937d6c
Since.
athoune Jun 7, 2022
6632bca
Handling empty JSON. Does it really happen?
athoune Jun 7, 2022
92e3ea5
Lazy query. More error.
athoune Jun 7, 2022
6c21382
Read loop.
athoune Jun 7, 2022
4570046
Simpler JSON.
athoune Jun 7, 2022
c46fb2b
OK, it's this test logs, not an old run.
athoune Jun 7, 2022
c2b3d75
DSN.
athoune Jun 7, 2022
d99feef
Tenant ID.
athoune Jun 7, 2022
94d0f55
Bring your own headers.
athoune Jun 7, 2022
856da41
Authorization
athoune Jun 7, 2022
17224b8
Fix: unsanitized user input
athoune Jun 13, 2022
c11cbcc
More interface implementation
athoune Jun 14, 2022
5edcdb6
Date or Duration diff
athoune Jun 14, 2022
48431f2
Specific time.Time type for YAML parsing.
athoune Jun 14, 2022
bf138b9
Loky query is mandatory
athoune Jun 15, 2022
d8d63a2
Fix: parse JSON loki response.
athoune Jun 15, 2022
ea860b1
Play this test.
athoune Jun 15, 2022
6bf80ee
OneShot has its test
athoune Jun 15, 2022
a81d52a
FeedLoki routine.
athoune Jun 16, 2022
d7a1c6a
Paginate Loki query.
athoune Jun 16, 2022
4f1aae7
Explicit loki error.
athoune Jun 16, 2022
195adba
Wait for ready is a parameter.
athoune Jun 16, 2022
0dad75b
Better wait. Defer Close.
athoune Jun 16, 2022
24f67c0
Default config mode is tail.
athoune Jun 16, 2022
e57150a
Fix: New request error handling.
athoune Jun 16, 2022
1d7bce7
Fix: path can be / or empty
athoune Jun 17, 2022
ae718e0
Fix: unreachable return
athoune Jun 17, 2022
066d2a1
Forever loop over loki websocket can be stopped.
athoune Jun 17, 2022
3471ce0
Close the websocket.
athoune Jun 17, 2022
1c88584
Don't buffer before reading JSON. JSON draining looks far better.
athoune Jun 20, 2022
caa7db0
Fix: it's ok to stop the connection.
athoune Jun 20, 2022
473b64b
Better error report, and more logical variable place.
athoune Jun 20, 2022
4bfb138
Websocket can be closed.
athoune Jun 20, 2022
4bbe1c7
Stop the loki tailing.
athoune Jun 20, 2022
4edd25f
Loki is now registered as an acquisition module.
athoune Jun 20, 2022
e76d58c
Fix: it's a StreamingAcquisition error, not an ugly copy/paste error …
athoune Jun 20, 2022
be95db0
Fix: don't break the loop, retry.
athoune Jun 20, 2022
47d85e3
use a dedicated package for loki queries
blotus Jul 28, 2022
3f56a07
fix potential null deref
blotus Nov 28, 2022
2195509
properly build URL
blotus Nov 28, 2022
8f3a20b
linting
blotus Nov 28, 2022
3dadf68
fix: Update
lperdereau Jun 24, 2023
945a381
fix: Patch auth and since DSN
lperdereau Jun 24, 2023
784703a
fix: Add uniq id
lperdereau Jun 24, 2023
810f525
fix: Update auth yaml config
lperdereau Jun 24, 2023
2017e6b
fix: Add https to dsn
lperdereau Jun 24, 2023
9520670
feat: Add to detect test
lperdereau Jun 24, 2023
5acf6a8
fix: Add loki readiness probe to CI
lperdereau Jul 3, 2023
8d05565
fix: Path health command for loki
lperdereau Jul 3, 2023
fc33f8d
fix: Patch loki acquisition
lperdereau Jul 4, 2023
a3e4199
fix: Update go-cs-lib/cstest
lperdereau Oct 2, 2023
efc6213
fix: Update go.mod and go.sum
lperdereau Oct 2, 2023
e750412
feat: Add delay for on tail request
lperdereau Oct 6, 2023
a3eff54
feat: Refactor Tail and add metrics
lperdereau Oct 9, 2023
1a8c127
Merge branch 'master' into loki
buixor Oct 16, 2023
755ca02
improve bubble up errors and fix a few logging messages
buixor Oct 17, 2023
9341d3f
improve bubble up errors and fix a few logging messages
buixor Oct 17, 2023
f39c61e
Merge branch 'master' into loki
buixor Oct 17, 2023
a54090b
Merge branch 'master' into loki
buixor Oct 18, 2023
5029450
use query_range instead of tail
blotus Oct 17, 2023
4146fb1
fix tests
blotus Oct 17, 2023
a6bfb05
don't defer in a select
blotus Oct 17, 2023
d4e088a
add support for modular ticker, and ensure we use the same tomb so ac…
buixor Oct 17, 2023
593de90
just a bit of logging
buixor Oct 19, 2023
e85d98e
Merge branch 'master' into loki
blotus Nov 22, 2023
0dab9eb
skip tests on windows
buixor Nov 22, 2023
695a104
fix test
buixor Nov 22, 2023
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
12 changes: 12 additions & 0 deletions .github/workflows/go-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ jobs:
--health-timeout 10s
--health-retries 5

loki:
image: grafana/loki:2.8.0
ports:
- "3100:3100"
options: >-
--name=loki1
--health-cmd "wget -q -O - http://localhost:3100/ready | grep 'ready'"
--health-interval 30s
--health-timeout 10s
--health-retries 5
--health-start-period 30s

steps:

- name: Check out CrowdSec repository
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -87,6 +88,7 @@ require (
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
gotest.tools/v3 v3.5.0
k8s.io/apiserver v0.27.3
)

Expand Down Expand Up @@ -126,7 +128,6 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/huandu/xstrings v1.3.2 // indirect
Expand Down Expand Up @@ -201,7 +202,6 @@ require (
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gotest.tools/v3 v3.5.0 // indirect
k8s.io/api v0.27.3 // indirect
k8s.io/apimachinery v0.27.3 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
Expand Down
5 changes: 3 additions & 2 deletions pkg/acquisition/acquisition.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
kafkaacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/kafka"
kinesisacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/kinesis"
k8sauditacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/kubernetesaudit"
lokiacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/loki"
s3acquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/s3"
syslogacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/syslog"
wineventlogacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/wineventlog"
Expand All @@ -36,7 +37,7 @@

type DataSourceUnavailableError struct {
Name string
Err error
Err error
}

func (e *DataSourceUnavailableError) Error() string {
Expand All @@ -47,7 +48,6 @@
return e.Err
}


// The interface each datasource must implement
type DataSource interface {
GetMetrics() []prometheus.Collector // Returns pointers to metrics that are managed by the module
Expand All @@ -74,6 +74,7 @@
"wineventlog": func() DataSource { return &wineventlogacquisition.WinEventLogSource{} },
"kafka": func() DataSource { return &kafkaacquisition.KafkaSource{} },
"k8s-audit": func() DataSource { return &k8sauditacquisition.KubernetesAuditSource{} },
"loki": func() DataSource { return &lokiacquisition.LokiSource{} },
"s3": func() DataSource { return &s3acquisition.S3Source{} },
}

Expand Down Expand Up @@ -322,7 +323,7 @@

for i := 0; i < len(sources); i++ {
subsrc := sources[i] //ensure its a copy
log.Debugf("starting one source %d/%d ->> %T", i, len(sources), subsrc)

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

Sensitive data returned by an access to APIKeys
flows to a logging call.
Sensitive data returned by an access to Password
flows to a logging call.
Sensitive data returned by an access to Password
flows to a logging call.

AcquisTomb.Go(func() error {
defer trace.CatchPanic("crowdsec/acquis")
Expand Down
60 changes: 60 additions & 0 deletions pkg/acquisition/modules/loki/entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package loki

import (
"encoding/json"
"strconv"
"time"
)

type Entry struct {
Timestamp time.Time
Line string
}

func (e *Entry) UnmarshalJSON(b []byte) error {
var values []string
err := json.Unmarshal(b, &values)
if err != nil {
return err
}
t, err := strconv.Atoi(values[0])
if err != nil {
return err
}
e.Timestamp = time.Unix(int64(t), 0)
e.Line = values[1]
return nil

Check warning on line 26 in pkg/acquisition/modules/loki/entry.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/loki/entry.go#L14-L26

Added lines #L14 - L26 were not covered by tests
}

type Stream struct {
Stream map[string]string `json:"stream"`
Entries []Entry `json:"values"`
}

type DroppedEntry struct {
Labels map[string]string `json:"labels"`
Timestamp time.Time `json:"timestamp"`
}

type Tail struct {
Streams []Stream `json:"streams"`
DroppedEntries []DroppedEntry `json:"dropped_entries"`
}

// LokiQuery GET response.
// See https://grafana.com/docs/loki/latest/api/#get-lokiapiv1query
type LokiQuery struct {
Status string `json:"status"`
Data Data `json:"data"`
}

type Data struct {
ResultType string `json:"resultType"`
Result []StreamResult `json:"result"` // Warning, just stream value is handled
Stats interface{} `json:"stats"` // Stats is boring, just ignore it
}

type StreamResult struct {
Stream map[string]string `json:"stream"`
Values []Entry `json:"values"`
}
Loading
Loading