Skip to content

Commit

Permalink
Merge pull request #15 from woblerr/fix_hide_values
Browse files Browse the repository at this point in the history
Fix hide labels.
  • Loading branch information
woblerr authored Oct 28, 2023
2 parents 4b6e159 + 9a6d0b4 commit 7a8ac72
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ all: run-test docker-run-test
.PHONY: test
test:
@echo "Run tests for $(APP_NAME)"
go test -mod=vendor -timeout=60s -count 1 ./...
TZ="Etc/UTC" go test -mod=vendor -timeout=60s -count 1 ./...

.PHONY: build
build:
Expand Down
13 changes: 8 additions & 5 deletions promexporter/geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@ func checkGeoDBFlags(logger log.Logger) {
}
}

func getIPDetailsFromLocalDB(returnValues *geoInfo, ipAddres string, logger log.Logger) {
func getIPDetailsFromLocalDB(returnValues *geoInfo, ipAddress string, logger log.Logger) {
geodb, err := geoip2.Open(geodbPath)
if err != nil {
level.Error(logger).Log("msg", "Error opening GeoIp database file", "err", err)
return
}
defer geodb.Close()
ip := net.ParseIP(ipAddres)
level.Debug(logger).Log("msg", "Parse IP", "ip", ipAddress)
ip := net.ParseIP(ipAddress)
if ip == nil {
level.Error(logger).Log("msg", "Error parsing ip address", "ip", ipAddres)
level.Error(logger).Log("msg", "Error parsing IP address", "ip", ipAddress)
return
}
record, err := geodb.City(ip)
Expand All @@ -79,18 +80,20 @@ func getIPDetailsFromLocalDB(returnValues *geoInfo, ipAddres string, logger log.
returnValues.cityName = record.City.Names[geoLang]
}

func getIPDetailsFromURL(returnValues *geoInfo, ipAddres string, logger log.Logger) {
func getIPDetailsFromURL(returnValues *geoInfo, ipAddress string, logger log.Logger) {
// Timeout for get and read response body.
client := http.Client{
Timeout: time.Duration(geoTimeout) * time.Second,
}
response, err := client.Get(geoURL + ipAddres)
level.Debug(logger).Log("msg", "Get IP details from url", "url", geoURL+ipAddress)
response, err := client.Get(geoURL + ipAddress)
if err != nil {
level.Error(logger).Log("msg", "Error getting GeoIp URL", "err", err)
return
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
level.Debug(logger).Log("msg", "Response body", "body", string(body))
if err != nil {
level.Error(logger).Log("msg", "Error getting body from GeoIp URL", "err", err)
return
Expand Down
18 changes: 9 additions & 9 deletions promexporter/geo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestGetIPDetailsFromURL(t *testing.T) {
defer srv.Close()
type args struct {
returnValues *geoInfo
ipAddres string
ipAddress string
}

tests := []struct {
Expand All @@ -123,7 +123,7 @@ func TestGetIPDetailsFromURL(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
geoURL = tt.testGeoURL
getIPDetailsFromURL(tt.args.returnValues, tt.args.ipAddres, logger)
getIPDetailsFromURL(tt.args.returnValues, tt.args.ipAddress, logger)
if !reflect.DeepEqual(tt.args.returnValues, tt.want) {
t.Errorf("\ngetIPDetailsFromURL() =\n%v,\nwant=\n%v", tt.args.returnValues, tt.want)
}
Expand All @@ -136,7 +136,7 @@ func TestGetIPDetailsFromURLErrors(t *testing.T) {
defer srv.Close()
type args struct {
returnValues *geoInfo
ipAddres string
ipAddress string
}
reqArgs := args{
&geoInfo{"", "", ""},
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestGetIPDetailsFromURLErrors(t *testing.T) {
geoURL = tt.testGeoURL
out := &bytes.Buffer{}
lc := log.NewLogfmtLogger(out)
getIPDetailsFromURL(tt.args.returnValues, tt.args.ipAddres, lc)
getIPDetailsFromURL(tt.args.returnValues, tt.args.ipAddress, lc)
if !strings.Contains(out.String(), tt.testText) {
t.Errorf("\nVariable do not match:\n%s\nwant:\n%s", tt.testText, out.String())
}
Expand All @@ -185,7 +185,7 @@ func TestGetIPDetailsFromURLErrors(t *testing.T) {
func TestGetIPDetailsFromLocalDB(t *testing.T) {
type args struct {
returnValues *geoInfo
ipAddres string
ipAddress string
}
geoLang = "en"
tests := []struct {
Expand All @@ -206,7 +206,7 @@ func TestGetIPDetailsFromLocalDB(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
geodbPath = getFullPath(tt.testGeoFile)
getIPDetailsFromLocalDB(tt.args.returnValues, tt.args.ipAddres, logger)
getIPDetailsFromLocalDB(tt.args.returnValues, tt.args.ipAddress, logger)
if !reflect.DeepEqual(tt.args.returnValues, tt.want) {
t.Errorf("\ngetIPDetailsFromURL() =\n%v,\nwant=\n%v", tt.args.returnValues, tt.want)
}
Expand All @@ -217,7 +217,7 @@ func TestGetIPDetailsFromLocalDB(t *testing.T) {
func TestGetIPDetailsFromLocalDBErrors(t *testing.T) {
type args struct {
returnValues *geoInfo
ipAddres string
ipAddress string
}
tests := []struct {
name string
Expand All @@ -239,15 +239,15 @@ func TestGetIPDetailsFromLocalDBErrors(t *testing.T) {
"12.123.12.",
},
"../test_data/geolite2_test.mmdb",
"Error parsing ip address",
"Error parsing IP address",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
geodbPath = getFullPath(tt.testGeoFile)
out := &bytes.Buffer{}
lc := log.NewLogfmtLogger(out)
getIPDetailsFromLocalDB(tt.args.returnValues, tt.args.ipAddres, lc)
getIPDetailsFromLocalDB(tt.args.returnValues, tt.args.ipAddress, lc)
if !strings.Contains(out.String(), tt.testText) {
t.Errorf("\nVariable do not match:\n%s\nwant:\n%s", tt.testText, out.String())
}
Expand Down
27 changes: 15 additions & 12 deletions promexporter/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type authLogLine struct {
}

func parseLine(line *tail.Line, logger log.Logger) {
parsedLog := &authLogLine{}
parsedLog := authLogLine{}
matches := make(map[string]string)
// Find the type of log and parse it.
for t, re := range authLineRegexps {
Expand All @@ -46,26 +46,22 @@ func parseLine(line *tail.Line, logger log.Logger) {
if len(matches) == 0 {
return
}
geoIPData := &geoInfo{}
if !metricHideUser {
parsedLog.Username = matches["user"]
}
if !metricHideIP {
parsedLog.IPAddress = matches["ipAddress"]
}
parsedLog.Username = matches["user"]
parsedLog.IPAddress = matches["ipAddress"]
geoIPData := geoInfo{}
// Get geo information.
if geodbIs {
if geodbType == "db" {
getIPDetailsFromLocalDB(geoIPData, parsedLog.IPAddress, logger)
getIPDetailsFromLocalDB(&geoIPData, parsedLog.IPAddress, logger)
} else {
getIPDetailsFromURL(geoIPData, parsedLog.IPAddress, logger)
getIPDetailsFromURL(&geoIPData, parsedLog.IPAddress, logger)
}
}
// Add metric.
authVentsMetric.WithLabelValues(
parsedLog.Type,
parsedLog.Username,
parsedLog.IPAddress,
hideValue(metricHideUser, parsedLog.Username),
hideValue(metricHideIP, parsedLog.IPAddress),
geoIPData.countyISOCode,
geoIPData.countryName,
geoIPData.cityName,
Expand All @@ -83,3 +79,10 @@ func getMatches(line string, re *regexp.Regexp) map[string]string {
}
return results
}

func hideValue(boolValue bool, value string) string {
if boolValue {
return ""
}
return value
}
112 changes: 112 additions & 0 deletions promexporter/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package promexporter

import (
"bytes"
"fmt"
"reflect"
"regexp"
"testing"
"time"

"github.com/nxadm/tail"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
)

func TestGetMatches(t *testing.T) {
Expand Down Expand Up @@ -121,3 +128,108 @@ func TestGetMatches(t *testing.T) {
})
}
}

func TestHideValue(t *testing.T) {
type args struct {
value string
boolValue bool
}
tests := []struct {
name string
args args
want string
}{
{
"hideTrue",
args{
value: "test",
boolValue: true,
},
"",
},
{
"hideFalse",
args{
value: "test",
boolValue: false,
},
"test",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := hideValue(tt.args.boolValue, tt.args.value); got != tt.want {
t.Errorf("hideValue() = %v, want %v", got, tt.want)
}
})
}
}

func TestParseLine(t *testing.T) {
type args struct {
lineTest *tail.Line
geodbIsTest bool
}
tests := []struct {
name string
args args
testText string
}{
{
"ParseLineExist",
args{
valToPtr(tail.Line{
Text: "Aug 30 12:02:00 hostname sshd[17917]: User root from 12.123.12.123 not allowed because not listed in AllowUsers",
Num: 3,
SeekInfo: tail.SeekInfo{Offset: 305, Whence: 0},
Time: time.Unix(1693560000, 0),
Err: nil,
}),
false,
},
`# HELP authlog_events_total The total number of auth events.
# TYPE authlog_events_total counter
authlog_events_total{cityName="",countryName="",countyISOCode="",eventType="notAllowedUser",ipAddress="12.123.12.123",user="root"} 1
`,
},
{
"ParseLineNotExist",
args{
valToPtr(tail.Line{
Text: "Some text",
Num: 3,
SeekInfo: tail.SeekInfo{Offset: 305, Whence: 0},
Time: time.Unix(1693560000, 0),
Err: nil,
}),
false,
},
"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
authVentsMetric.Reset()
geodbIs = tt.args.geodbIsTest
parseLine(tt.args.lineTest, logger)
reg := prometheus.NewRegistry()
reg.MustRegister(
authVentsMetric,
)
metricFamily, err := reg.Gather()
if err != nil {
fmt.Println(err)
}
out := &bytes.Buffer{}
for _, mf := range metricFamily {
if _, err := expfmt.MetricFamilyToText(out, mf); err != nil {
panic(err)
}
}
if tt.testText != out.String() {
t.Errorf("\nVariables do not match, metrics:\n%s\nwant:\n%s", tt.testText, out.String())
}
})
}
}

0 comments on commit 7a8ac72

Please sign in to comment.