Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2ef5a7b
Refactor finding notifications
Nov 15, 2022
9c7affb
Refactor test utils
Nov 16, 2022
73395a1
Refactor notify pkg
Nov 16, 2022
cd30f42
Add Kafka notifier
Nov 17, 2022
dd94622
Update config example
Nov 18, 2022
78ffed0
Avoid sending void payloads to Kafka
Nov 18, 2022
70ec059
Include required Kafka client build tag into Dockerfile build
Nov 18, 2022
27e048f
Add comment on Docker build arguments required for Kafka client on Al…
Nov 18, 2022
451509f
Add notify pkg tests for Kafka and Multi implementations
Nov 21, 2022
a5ebeae
Add Kafka to processor's integration tests
Nov 21, 2022
a02273c
Refactor CI tests set up
Nov 22, 2022
dc5be49
Update configs and readme
Nov 22, 2022
fae322c
Add debug log for notifier set up during start up
Nov 23, 2022
244828f
Add gcc to Dockerfile builder stage (kafka lib req)
Nov 23, 2022
bc33410
Fix missing err handling
Nov 24, 2022
80fc99f
Test propagation of errors on notifications delivery
Nov 24, 2022
a2452fd
Improve debug log
Nov 24, 2022
9f4a836
Set concurrent workers to 1
Nov 25, 2022
a4022d0
Set SQS visibility TO to 5min
Nov 25, 2022
dacea38
Fix checks processing idempotency
Nov 25, 2022
40399e4
Add integrations test for checks processing idempotency
Nov 25, 2022
b9e6451
Ignore FIXED finding notifications for legacy SNS integrations
Nov 28, 2022
bd4e862
Add comment on time serialization fmt
Nov 28, 2022
6917dfc
Add AsyncAPI doc and generation script
Nov 29, 2022
2301c58
Revert SQS number of processors
Nov 30, 2022
63a847f
Change initial SQS processing log mssg level
Dec 1, 2022
f1d4532
Add Kafka to local deployment
Dec 1, 2022
e1ffe44
fix issues with kafka lib in arm and upgrade
jesusfcr Apr 12, 2024
8183f04
Fix kafka Events deprecation
jesusfcr Apr 16, 2024
88fdc5e
Allow configure kafka security_protocol and sasl_mechanism
jesusfcr Apr 16, 2024
7c91159
Fix some lint issues
jesusfcr Apr 16, 2024
82f2fa0
Add missing copyright
jesusfcr Apr 16, 2024
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
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
- docker
env:
global:
- CGO_ENABLED=0
- CGO_ENABLED=1
- FLYWAY_VERSION=10.10.0
- INPUT_BUILDARGS=FLYWAY_VERSION=$FLYWAY_VERSION
- INPUT_PLATFORM=linux/amd64 # ,linux/arm64
Expand All @@ -15,13 +15,11 @@ before_install:
# Requirement for 'test-local-deployment'
- pip install --user awscli
- export PATH=$PATH:$HOME/.local/bin
before_script:
- _script/start-pg
gobuild_args: -a -tags netgo -ldflags '-w'
go_import_path: github.com/adevinta/vulnerability-db
script:
- go install ./...
- go test -v -tags integration $(go list ./... | grep -v /vendor/) ./test
- _script/test
after_success:
- bash -c 'source <(curl -s https://raw.githubusercontent.com/adevinta/vulcan-cicd/master/buildx.sh)'
- cd local_deployment
Expand Down
18 changes: 15 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Copyright 2020 Adevinta

FROM --platform=$BUILDPLATFORM golang:1.22-alpine3.18 as builder
FROM --platform=linux/$TARGETARCH golang:1.22-alpine3.18 as builder

# Required because the dependency
# https://github.com/confluentinc/confluent-kafka-go requires the gcc compiler.
RUN apk add --no-cache gcc musl-dev cyrus-sasl-dev mold

WORKDIR /app

Expand All @@ -13,13 +17,21 @@ COPY . .

ARG TARGETOS TARGETARCH

RUN cd cmd/vulnerability-db-consumer/ && GOOS=$TARGETOS GOARCH=$TARGETARCH go build . && cd -
WORKDIR /app/cmd/vulnerability-db-consumer

# -tags musl argument is required for dependency github.com/confluentinc/confluent-kafka-go.
# see documentation: https://github.com/confluentinc/confluent-kafka-go#using-go-modules

RUN CGO_ENABLED=1 GOOS=linux GOARCH=$TARGETARCH \
# explicitly link to libsasl2 installed as part of cyrus-sasl-dev
CGO_LDFLAGS="-fuse-ld=mold -lsasl2" \
go build -tags musl -ldflags "-w -s" .

FROM alpine:3.19

WORKDIR /flyway

RUN apk add --no-cache --update openjdk17-jre bash gettext
RUN apk add --no-cache --update openjdk17-jre bash gettext cyrus-sasl libgcc

ARG FLYWAY_VERSION=10.10.0

Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ cd db && source flyway-migrate.sh && cd -
vulnerability-db-consumer -c _resources/config/local.toml
```

## How to generate AsyncAPI documentation
Generated [AsyncAPI documentation](https://www.asyncapi.com/) can be found in `./docs` directory.
```
cd pkg/asyncapi/_gen && ./gen.sh && cd -
```

## How to run the Vulnerability DB in development mode

You can test the Vulnerability DB Consumer locally in your machine.
The commands bellow will launch the necessary components required by the
The commands bellow will launch the necessary components required by the
application.

```bash
Expand Down Expand Up @@ -83,8 +89,18 @@ Those are the variables you have to use:
|SNS_TOPIC_ARN|ARN of topic to publish new vulnerabilities|arn:aws:sns:xxx:123456789012:yyy|
|RESULTS_URL|External vulcan-results URL|https://results.vulcan.com|
|RESULTS_INTERNAL_URL|Internal vulcan-results URL|http://vulcan-results|
|SQS_QUEUE_ARN|Checks queueu ARN|arn:aws:sqs:xxx:123456789012:yyy|
|AWS_SQS_ENDPOINT|Endpoint for SQS creation queue (optional)|http://custom-aws-endpoint|
|SNS_ENABLED|Enables/Disables notifications sent to SNS|false|
|SNS_TOPIC_ARN|ARN of topic to publish new vulnerabilities|arn:aws:sns:xxx:123456789012:yyy|
|AWS_SNS_ENDPOINT|Endpoint for SNS topic (optional)|http://custom-aws-endpoint|
|KAFKA_ENABLED|Enables/Disables notifications sent to Kafka|false|
|KAFKA_USER|Kafka user||
|KAFKA_PASSWORD|Kafka password||
|KAFKA_BROKER_URL|Kafka Broker URL|localhost:9092|
|KAFKA_TOPIC|Kafka topic|findings|
|KAFKA_SECURITY_PROTOCOL|Security protocol|SASL_SSL|
|KAFKA_SASL_MECHANISM|SALSL_mechanism|SCRAM-SHA-256|

```bash
docker build . -t vdb
Expand Down
9 changes: 8 additions & 1 deletion _resources/config/local.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ timeout = 30
queue_arn = "arn:aws:sqs:xxx:123456789012:yyy"

[sns]
enabled = false
topic_arn = "arn:aws:sns:xxx:123456789012:yyy"
enabled = true

[kafka]
enabled = false
user = "user"
password = "password"
broker_url = "localhost:9092"
topic = "findings"

[report]
url_replace = "https://results.vulcan.example.com|http://localhost:8081"
Expand Down
11 changes: 0 additions & 11 deletions _script/start-pg

This file was deleted.

File renamed without changes.
22 changes: 18 additions & 4 deletions cmd/vulnerability-db-consumer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type config struct {
DB dbConfig
SQS sqsConfig
SNS snsConfig
Kafka kafkaConfig
Report reportConfig
Maintenance maintenanceConfig
}
Expand All @@ -38,19 +39,29 @@ type dbConfig struct {
}

type sqsConfig struct {
NProcessors uint8 `toml:"number_of_processors"`
WaitTime int `toml:"wait_time"`
Timeout int
NProcessors uint `toml:"number_of_processors"`
WaitTime uint `toml:"wait_time"`
Timeout uint
QueueARN string `toml:"queue_arn"`
Endpoint string `toml:"endpoint"`
}

type snsConfig struct {
TopicARN string `toml:"topic_arn"`
Enabled bool
TopicARN string `toml:"topic_arn"`
Endpoint string `toml:"endpoint"`
}

type kafkaConfig struct {
Enabled bool `toml:"enabled"`
User string `toml:"user"`
Pass string `toml:"password"`
BrokerURL string `toml:"broker_url"`
Topic string `toml:"topic"`
SecurityProtocol string `toml:"security_protocol"`
SASLMechanism string `toml:"sasl_mechanism"`
}

type reportConfig struct {
URLReplace string `toml:"url_replace"`
}
Expand All @@ -74,6 +85,9 @@ func parseConfig(cfgFilePath string) (*config, error) {
defer cfgFile.Close()

cfgData, err := io.ReadAll(cfgFile)
if err != nil {
return nil, err
}

var conf config
if _, err := toml.Decode(string(cfgData[:]), &conf); err != nil {
Expand Down
58 changes: 50 additions & 8 deletions cmd/vulnerability-db-consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"sync"

"github.com/adevinta/vulnerability-db/pkg/asyncapi/kafka"
"github.com/adevinta/vulnerability-db/pkg/maintenance"
"github.com/adevinta/vulnerability-db/pkg/notify"
"github.com/adevinta/vulnerability-db/pkg/processor"
Expand Down Expand Up @@ -43,14 +44,9 @@ func main() {
}

// Build notifier.
snsConf := notify.SNSConfig{
TopicArn: conf.SNS.TopicARN,
Enabled: conf.SNS.Enabled,
Endpoint: conf.SNS.Endpoint,
}
snsNotifier, err := notify.NewSNSNotifier(snsConf, logger)
notifier, err := buildNotifier(conf, logger)
if err != nil {
log.Fatalf("Error creating notifier: %v", err)
log.Fatalf("Error building notifier: %v", err)
}

// Build processor.
Expand All @@ -59,7 +55,7 @@ func main() {
log.Fatalf("Error creating results client: %v", err)
}

processor, err := processor.NewCheckProcessor(snsNotifier, db, resultsClient, conf.Report.URLReplace, conf.MaxEventAge, logger)
processor, err := processor.NewCheckProcessor(notifier, db, resultsClient, conf.Report.URLReplace, conf.MaxEventAge, logger)
if err != nil {
log.Fatalf("Error creating queue processor: %v", err)
}
Expand Down Expand Up @@ -97,6 +93,52 @@ func main() {
wg.Wait()
}

// buildNotifier builds the appropiate notifier given the defined configuration.
// TODO: Once the integrations dependent on the old notification format have been
// deprecated or updated to comply with the new format through Kafka topic channel
// we can get rid of SNS and multi implementations of notifier and just use Kafka.
func buildNotifier(conf *config, logger *log.Logger) (notify.Notifier, error) {
if !conf.SNS.Enabled && !conf.Kafka.Enabled {
logger.Info("using noop notifier")
return notify.NewNoopNotifier(), nil
}
if conf.SNS.Enabled && !conf.Kafka.Enabled {
logger.Info("using SNS notifier")
return buildSNSNotifier(conf, logger)
}
if !conf.SNS.Enabled && conf.Kafka.Enabled {
logger.Info("using Kafka notifier")
return buildKafkaNotifier(conf, logger)
}
// Multi Notifier
logger.Info("using multi notifier")
k, err := buildKafkaNotifier(conf, logger)
if err != nil {
return nil, err
}
s, err := buildSNSNotifier(conf, logger)
if err != nil {
return nil, err
}
return notify.NewMultiNotifier(k, s), nil
}

func buildSNSNotifier(conf *config, logger *log.Logger) (*notify.SNSNotifier, error) {
return notify.NewSNSNotifier(notify.SNSConfig{
TopicArn: conf.SNS.TopicARN,
Endpoint: conf.SNS.Endpoint,
}, logger)
}

func buildKafkaNotifier(conf *config, logger *log.Logger) (*notify.KafkaNotifier, error) {
kafkaCli, err := kafka.NewClient(conf.Kafka.User, conf.Kafka.Pass,
conf.Kafka.BrokerURL, conf.Kafka.Topic, conf.Kafka.SecurityProtocol, conf.Kafka.SASLMechanism)
if err != nil {
return nil, err
}
return notify.NewKafkaNotifier(kafkaCli, logger), nil
}

func setupLogger(cfg config) *log.Logger {
var logger = log.New()

Expand Down
3 changes: 3 additions & 0 deletions cmd/vulnerability-db-rstats/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func parseConfig(cfgFilePath string) (*config, error) {
defer cfgFile.Close()

cfgData, err := io.ReadAll(cfgFile)
if err != nil {
return nil, err
}

var conf config
if _, err := toml.Decode(string(cfgData[:]), &conf); err != nil {
Expand Down
11 changes: 10 additions & 1 deletion config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,19 @@ queue_arn = "$SQS_QUEUE_ARN"
endpoint = "$AWS_SQS_ENDPOINT"

[sns]
enabled = $SNS_ENABLED
topic_arn = "$SNS_TOPIC_ARN"
enabled = true
endpoint = "$AWS_SNS_ENDPOINT"

[kafka]
enabled = $KAFKA_ENABLED
user = "$KAFKA_USER"
password = "$KAFKA_PASSWORD"
broker_url = "$KAFKA_BROKER_URL"
topic = "$KAFKA_TOPIC"
security_protocol = "$KAFKA_SECURITY_PROTOCOL"
sasl_mechanism = "$KAFKA_SASL_MECHANISM"

[report]
url_replace = "$RESULTS_URL|$RESULTS_INTERNAL_URL"

Expand Down
Loading