Skip to content

Commit

Permalink
Add option to pseudonymize the common name
Browse files Browse the repository at this point in the history
  • Loading branch information
phihos committed Jun 5, 2022
1 parent 26fdd1a commit b8fe910
Show file tree
Hide file tree
Showing 13 changed files with 613 additions and 28 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '^1.14.1'
go-version: '^1.17.6'
- name: Tests
run: |
make fmt
make vet
make test
make build
- name: golangci-lint
uses: golangci/golangci-lint-action@v2.3.0
uses: golangci/golangci-lint-action@v3.2.0
with:
version: v1.33
version: v1.46.2
- name: Build release type artifacts
run: make release
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '^1.14.1'
go-version: '^1.17.6'
- name: Set VERSION env
run: echo VERSION=$(echo ${GITHUB_REF} | rev | cut -d'/' -f 1 | rev ) >> $GITHUB_ENV
- name: Build artifacts
Expand Down
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ lint:

.PHONY: test
test:
@which goverage > /dev/null; if [ $$? -ne 0 ]; then \
GO111MODULE=off $(GO) get -u github.com/haya14busa/goverage; \
fi
goverage -v -coverprofile coverage.out $(PACKAGES)
go test $(PACKAGES) -v -covermode=atomic -cover -coverprofile coverage.out -coverpkg ./...

.PHONY: build
build: $(BIN)/$(EXECUTABLE)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ GLOBAL OPTIONS:
--web.root value Root path to exporter endpoints (default: "/") [$OPENVPN_EXPORTER_WEB_ROOT]
--status-file value The OpenVPN status file(s) to export (example test:./example/version1.status ) [$OPENVPN_EXPORTER_STATUS_FILE]
--disable-client-metrics Disables per client (bytes_received, bytes_sent, connected_since) metrics (default: false) [$OPENVPN_EXPORTER_DISABLE_CLIENT_METRICS]
--pseudonymize-client-metrics Replaces common name in per client (bytes_received, bytes_sent, connected_since) metrics with a pseudonym - will not persist across restarts (default: false) [$OPENVPN_EXPORTER_PSEUDONYMIZE_CLIENT_METRICS]
--enable-golang-metrics Enables golang and process metrics for the exporter) (default: false) [$OPENVPN_EXPORTER_ENABLE_GOLANG_METRICS]
--log.level value Only log messages with given severity (default: "info") [$OPENVPN_EXPORTER_LOG_LEVEL]
--help, -h Show help (default: false)
Expand Down
22 changes: 21 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
module github.com/patrickjahns/openvpn_exporter

go 1.14
go 1.17

require (
github.com/go-kit/kit v0.9.0
github.com/prometheus/client_golang v1.5.1
github.com/stretchr/testify v1.4.0
github.com/urfave/cli/v2 v2.2.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logfmt/logfmt v0.4.0 // indirect
github.com/golang/protobuf v1.3.2 // indirect
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.9.1 // indirect
github.com/prometheus/procfs v0.0.8 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 // indirect
gopkg.in/yaml.v2 v2.2.5 // indirect
)
7 changes: 6 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0 h1:wDJmvq38kDhkVxi50ni9ykkdUr1PKgqKOoi01fa0Mdk=
Expand All @@ -36,8 +37,10 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj2vyii2bbUNDw3kt9VxK2EY=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand Down Expand Up @@ -75,6 +78,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4=
github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
Expand All @@ -92,12 +96,13 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 h1:ywK/j/KkyTHcdyYSZNXGjMwgmDSfjglYZ3vStQ/gSCU=
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
13 changes: 11 additions & 2 deletions pkg/collector/openvpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type OpenVPNCollector struct {
logger log.Logger
collectClientMetrics bool
parserFunction func(statusfile string) (*openvpn.Status, error)
OpenVPNServer []OpenVPNServer
LastUpdated *prometheus.Desc
ConnectedClients *prometheus.Desc
Expand All @@ -31,10 +32,18 @@ type OpenVPNServer struct {
}

// NewOpenVPNCollector returns a new OpenVPNCollector
func NewOpenVPNCollector(logger log.Logger, openVPNServer []OpenVPNServer, collectClientMetrics bool) *OpenVPNCollector {
func NewOpenVPNCollector(logger log.Logger, openVPNServer []OpenVPNServer,
parserDecorators []openvpn.ParserDecorator, collectClientMetrics bool) *OpenVPNCollector {

parserFunc := openvpn.ParseFile
for _, parserFuncDecorator := range parserDecorators {
parserFunc = parserFuncDecorator.DecorateParseFile(parserFunc)
}

return &OpenVPNCollector{
logger: logger,
OpenVPNServer: openVPNServer,
parserFunction: parserFunc,
collectClientMetrics: collectClientMetrics,

LastUpdated: prometheus.NewDesc(
Expand Down Expand Up @@ -115,7 +124,7 @@ func (c *OpenVPNCollector) collect(ovpn OpenVPNServer, ch chan<- prometheus.Metr
"statusFile", ovpn.StatusFile,
"name", ovpn.Name,
)
status, err := openvpn.ParseFile(ovpn.StatusFile)
status, err := c.parserFunction(ovpn.StatusFile)
if err != nil {
level.Warn(c.logger).Log(
"msg", "error parsing statusfile",
Expand Down
62 changes: 48 additions & 14 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"strings"

"github.com/patrickjahns/openvpn_exporter/pkg/openvpn"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -18,7 +20,21 @@ import (

// Run parses the command line arguments and executes the program.
func Run() error {
app, cfg := initApp()

app.Action = func(c *cli.Context) error {
server, logger := run(cfg)
if err := server.ListenAndServe(); err != nil {
level.Error(logger).Log("msg", "http listenandserve error", "err", err)
return err
}
return nil
}

return app.Run(os.Args)
}

func initApp() (*cli.App, *config.Config) {
app := &cli.App{
Name: "openvpn_exporter",
Version: version.Info(),
Expand Down Expand Up @@ -78,6 +94,18 @@ func Run() error {
Usage: "Disables per client (bytes_received, bytes_sent, connected_since) metrics",
EnvVars: []string{"OPENVPN_EXPORTER_DISABLE_CLIENT_METRICS"},
},
&cli.BoolFlag{
Name: "pseudonymize-client-metrics",
Usage: "Pseudonymized per client (bytes_received, bytes_sent, connected_since) metrics by replacing " +
"usernames with a random string",
EnvVars: []string{"OPENVPN_EXPORTER_PSEUDONYMIZE_CLIENT_METRICS"},
},
&cli.IntFlag{
Name: "pseudonymize-client-metrics-length",
Value: 8,
Usage: "Length of the client pseudonym string",
EnvVars: []string{"OPENVPN_EXPORTER_PSEUDONYMIZE_CLIENT_METRICS_LENGTH"},
},
&cli.BoolFlag{
Name: "enable-golang-metrics",
Value: false,
Expand All @@ -97,17 +125,14 @@ func Run() error {
app.Before = func(c *cli.Context) error {
cfg.StatusCollector.StatusFile = c.StringSlice("status-file")
cfg.StatusCollector.ExportClientMetrics = !c.Bool("disable-client-metrics")
cfg.StatusCollector.PseudonymizeClientMetrics = c.Bool("pseudonymize-client-metrics")
cfg.StatusCollector.PseudonymizeClientMetricsLength = c.Int("pseudonymize-client-metrics-length")
return nil
}

app.Action = func(c *cli.Context) error {
return run(cfg)
}

return app.Run(os.Args)
return app, cfg
}

func run(cfg *config.Config) error {
func run(cfg *config.Config) (*http.Server, log.Logger) {
// setup logging
logger := setupLogging(cfg)
level.Info(logger).Log(
Expand Down Expand Up @@ -141,16 +166,28 @@ func run(cfg *config.Config) error {
)
openVPServers = append(openVPServers, collector.OpenVPNServer{Name: serverName, StatusFile: statusFile, ParseError: 0})
}

var parserDecorators []openvpn.ParserDecorator
if cfg.StatusCollector.PseudonymizeClientMetrics {
parserDecorators = append(
parserDecorators,
openvpn.NewOpenVPNPseudonymizingDecorator(
cfg.StatusCollector.PseudonymizeClientMetricsLength,
),
)
}
r.MustRegister(collector.NewOpenVPNCollector(
logger,
openVPServers,
parserDecorators,
cfg.StatusCollector.ExportClientMetrics,
))

http.Handle(cfg.Server.Path,
mux := http.NewServeMux()
mux.Handle(cfg.Server.Path,
promhttp.HandlerFor(r, promhttp.HandlerOpts{}),
)
http.HandleFunc(cfg.Server.Root, func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(cfg.Server.Root, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>OpenVPN Exporter</title></head>
<body>
Expand All @@ -161,11 +198,8 @@ func run(cfg *config.Config) error {
})

level.Info(logger).Log("msg", "Listening on", "addr", cfg.Server.Addr)
if err := http.ListenAndServe(cfg.Server.Addr, nil); err != nil {
level.Error(logger).Log("msg", "http listenandserve error", "err", err)
return err
}
return nil
server := &http.Server{Addr: cfg.Server.Addr, Handler: mux}
return server, logger
}

func parseStatusFileSlice(statusFile string) (string, string) {
Expand Down
Loading

0 comments on commit b8fe910

Please sign in to comment.