Skip to content

Commit acd5be1

Browse files
committed
build: make db drivers optional
1 parent 352c172 commit acd5be1

File tree

10 files changed

+86
-39
lines changed

10 files changed

+86
-39
lines changed

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
version: "2"
22

3+
# To lint the windows sources on a linux host, you need to cross-build and
4+
# disable the C-based sqlite driver:
5+
#
6+
# GOOS=windows golangci-lint run --build-tags=windows,sqlite_modernc
7+
#
8+
# In the CI, the linters are run on both linux and windows hosts.
9+
310
run:
411
build-tags:
512
- expr_debug

Makefile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ BUILD_RE2_WASM ?= 0
1818
#expr_debug tag is required to enable the debug mode in expr
1919
GO_TAGS := netgo,osusergo,expr_debug
2020

21-
# By default, build with CGO and sqlite3.
21+
# By default, build with sqlite3.
2222
BUILD_SQLITE ?= mattn
2323
SQLITE_MSG = Using mattn/go-sqlite3
2424

@@ -159,15 +159,21 @@ COMPONENTS := \
159159
datasource_s3 \
160160
datasource_syslog \
161161
datasource_wineventlog \
162-
cscli_setup
162+
cscli_setup \
163+
db_mysql \
164+
db_postgres \
165+
db_sqlite
163166

164167
comma := ,
165168
space := $(empty) $(empty)
166169

167170
# Predefined profiles
168171

169-
# keep only datasource-file
170-
EXCLUDE_MINIMAL := $(subst $(space),$(comma),$(filter-out datasource_file,,$(COMPONENTS)))
172+
# What we want to KEEP in the minimal build,
173+
# which should always include at least one data source and a sql driver.
174+
INCLUDE_MINIMAL := db_sqlite datasource_file
175+
176+
EXCLUDE_MINIMAL := $(filter-out $(INCLUDE_MINIMAL),$(strip $(COMPONENTS)))
171177

172178
# example
173179
# EXCLUDE_MEDIUM := datasource_kafka,datasource_kinesis,datasource_s3

pkg/csconfig/database.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,9 @@ func (d *DatabaseCfg) ConnectionString() (string, error) {
130130

131131
switch d.Type {
132132
case "sqlite":
133-
var sqliteConnectionStringParameters string
133+
sqliteConnectionStringParameters := "_busy_timeout=100000&_fk=1&_pragma=foreign_keys(1)"
134134
if d.UseWal != nil && *d.UseWal {
135-
sqliteConnectionStringParameters = "_busy_timeout=100000&_fk=1&_journal_mode=WAL"
136-
} else {
137-
sqliteConnectionStringParameters = "_busy_timeout=100000&_fk=1"
135+
sqliteConnectionStringParameters += "&_journal_mode=WAL"
138136
}
139137

140138
connString = fmt.Sprintf("file:%s?%s", d.DbPath, sqliteConnectionStringParameters)

pkg/cwversion/component/component.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ var Built = map[string]bool{
2121
"datasource_syslog": false,
2222
"datasource_wineventlog": false,
2323
"datasource_victorialogs": false,
24-
"datasource_http": false,
24+
"datasource_http": false,
2525
"cscli_setup": false,
26+
"db_mysql": false,
27+
"db_postgres": false,
28+
"db_sqlite": false,
2629
}
2730

2831
func Register(name string) {

pkg/database/database.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import (
88
"os"
99

1010
entsql "entgo.io/ent/dialect/sql"
11-
// load database backends
12-
_ "github.com/go-sql-driver/mysql"
13-
_ "github.com/jackc/pgx/v4/stdlib"
1411
log "github.com/sirupsen/logrus"
1512

1613
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
@@ -85,6 +82,7 @@ func NewClient(ctx context.Context, config *csconfig.DatabaseCfg) (*Client, erro
8582
if err != nil {
8683
return nil, fmt.Errorf("failed to generate DB connection string: %w", err)
8784
}
85+
8886
drv, err := getEntDriver(typ, dia, dbConnectionString, config)
8987
if err != nil {
9088
return nil, fmt.Errorf("failed opening connection to %s: %w", config.Type, err)

pkg/database/drv_mysql.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//go:build !no_db_mysql
2+
3+
package database
4+
5+
import (
6+
// register database driver as side effect
7+
_ "github.com/go-sql-driver/mysql"
8+
9+
"github.com/crowdsecurity/crowdsec/pkg/cwversion/component"
10+
)
11+
12+
//nolint:gochecknoinits
13+
func init() {
14+
component.Register("db_mysql")
15+
}

pkg/database/drv_postgres.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//go:build !no_db_postgres
2+
3+
package database
4+
5+
import (
6+
// register database driver as side effect
7+
_ "github.com/jackc/pgx/v4/stdlib"
8+
9+
"github.com/crowdsecurity/crowdsec/pkg/cwversion/component"
10+
)
11+
12+
//nolint:gochecknoinits
13+
func init() {
14+
component.Register("db_postgres")
15+
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
//go:build !sqlite_modernc
1+
//go:build !db_no_sqlite && !sqlite_modernc
22

33
package database
44

55
import (
66
"errors"
77

88
"github.com/mattn/go-sqlite3"
9+
10+
"github.com/crowdsecurity/crowdsec/pkg/cwversion/component"
911
)
1012

13+
//nolint:gochecknoinits
14+
func init() {
15+
component.Register("db_sqlite")
16+
}
17+
1118
func IsSqliteBusyError(err error) bool {
1219
var sqliteErr sqlite3.Error
13-
// sqlite3.Error{
14-
// Code: 5,
15-
// ExtendedCode: 5,
16-
// SystemErrno: 0,
17-
// err: "database is locked",
18-
// }
1920
return errors.As(err, &sqliteErr) && sqliteErr.Code == sqlite3.ErrBusy
2021
}

pkg/database/drv_sqlite_modernc.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build !db_no_sqlite && sqlite_modernc
2+
3+
package database
4+
5+
import (
6+
"database/sql"
7+
"errors"
8+
9+
"modernc.org/sqlite"
10+
sqlite3 "modernc.org/sqlite/lib"
11+
12+
"github.com/crowdsecurity/crowdsec/pkg/cwversion/component"
13+
)
14+
15+
//nolint:gochecknoinits
16+
func init() {
17+
sql.Register("sqlite3", &sqlite.Driver{})
18+
component.Register("db_sqlite")
19+
}
20+
21+
func IsSqliteBusyError(err error) bool {
22+
var se *sqlite.Error
23+
return errors.As(err, &se) && se.Code() == sqlite3.SQLITE_BUSY
24+
}

pkg/database/sqlite_modernc.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)