Skip to content

Commit d565319

Browse files
chore: update dependency from github.com/oschwald/geoip2-golang v1.9.0 to v2.0.0 (#118)
Upgrade geoip2-golang from v1 to v2 - Updated dependency from github.com/oschwald/geoip2-golang v1.9.0 to v2.0.0 - Updated maxminddb-golang to v2.0.0 (transitive dependency) - Changed GetASN and GetCity methods to accept netip.Addr instead of *net.IP - Updated field names: IsoCode -> ISOCode in GetIsoCodeFromRecord - Updated pkg/spoa/root.go to pass netip.Addr directly to GetCity - Updated geo_test.go to use netip.MustParseAddr and Names.English field - All tests pass and code builds successfully
1 parent 671b2fc commit d565319

File tree

5 files changed

+57
-56
lines changed

5 files changed

+57
-56
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/crowdsecurity/go-cs-lib v0.0.23
99
github.com/google/uuid v1.6.0
1010
github.com/negasus/haproxy-spoe-go v1.0.7
11-
github.com/oschwald/geoip2-golang v1.9.0
11+
github.com/oschwald/geoip2-golang/v2 v2.0.0
1212
github.com/prometheus/client_golang v1.23.2
1313
github.com/prometheus/client_model v0.6.2
1414
github.com/sirupsen/logrus v1.9.3
@@ -48,7 +48,7 @@ require (
4848
github.com/mitchellh/mapstructure v1.5.0 // indirect
4949
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
5050
github.com/oklog/ulid v1.3.1 // indirect
51-
github.com/oschwald/maxminddb-golang v1.12.0 // indirect
51+
github.com/oschwald/maxminddb-golang/v2 v2.0.0 // indirect
5252
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
5353
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
5454
github.com/prometheus/common v0.66.1 // indirect
@@ -60,7 +60,7 @@ require (
6060
go.mongodb.org/mongo-driver v1.17.4 // indirect
6161
go.yaml.in/yaml/v2 v2.4.2 // indirect
6262
golang.org/x/net v0.44.0 // indirect
63-
golang.org/x/sys v0.36.0 // indirect
63+
golang.org/x/sys v0.37.0 // indirect
6464
google.golang.org/protobuf v1.36.8 // indirect
6565
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637 // indirect
6666
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ github.com/negasus/haproxy-spoe-go v1.0.7 h1:OhRY0zapeHudrRqoblI9DjIolJjWI0s/TO6
8181
github.com/negasus/haproxy-spoe-go v1.0.7/go.mod h1:ZrBizxtx2EeLN37Jkg9w9g32a1AFCJizA8vg46PaAp4=
8282
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
8383
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
84-
github.com/oschwald/geoip2-golang v1.9.0 h1:uvD3O6fXAXs+usU+UGExshpdP13GAqp4GBrzN7IgKZc=
85-
github.com/oschwald/geoip2-golang v1.9.0/go.mod h1:BHK6TvDyATVQhKNbQBdrj9eAvuwOMi2zSFXizL3K81Y=
86-
github.com/oschwald/maxminddb-golang v1.12.0 h1:9FnTOD0YOhP7DGxGsq4glzpGy5+w7pq50AS6wALUMYs=
87-
github.com/oschwald/maxminddb-golang v1.12.0/go.mod h1:q0Nob5lTCqyQ8WT6FYgS1L7PXKVVbgiymefNwIjPzgY=
84+
github.com/oschwald/geoip2-golang/v2 v2.0.0 h1:1GZ7MsQsbIKeOXMDV2MqBVfV8NuCIqWatomkS67LwQo=
85+
github.com/oschwald/geoip2-golang/v2 v2.0.0/go.mod h1:MR3+NmQTkjxyJ/u6iZJSfpfy5al/QqsaJL1Gg6uI7sU=
86+
github.com/oschwald/maxminddb-golang/v2 v2.0.0 h1:Gyljxck1kHbBxDgLM++NfDWBqvu1pWWfT8XbosSo0bo=
87+
github.com/oschwald/maxminddb-golang/v2 v2.0.0/go.mod h1:gG4V88LsawPEqtbL1Veh1WRh+nVSYwXzJ1P5Fcn77g0=
8888
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
8989
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
9090
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -131,8 +131,8 @@ golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
131131
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
132132
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
133133
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
134-
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
135-
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
134+
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
135+
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
136136
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
137137
google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc=
138138
google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=

internal/geo/geo_test.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package geo
22

33
import (
44
"context"
5-
"net"
5+
"net/netip"
66
"path/filepath"
77
"testing"
88

@@ -20,28 +20,34 @@ func TestGetCityAndASN(t *testing.T) {
2020

2121
g.Init(ctx)
2222

23-
ip := net.ParseIP("2.125.160.216")
24-
city, err := g.GetCity(&ip)
23+
ip := netip.MustParseAddr("2.125.160.216")
24+
city, err := g.GetCity(ip)
2525
if err != nil {
2626
t.Fatalf("GetCity returned error: %v", err)
2727
}
2828

29-
assert.Equal(t, "Boxford", city.City.Names["en"], "Expected city name 'Boxford', got '%s'", city.City.Names["en"])
30-
assert.Equal(t, "Europe", city.Continent.Names["en"], "Expected continent name 'Europe', got '%s'", city.City.Names["en"])
29+
// v2: Names is a struct with fields (English, German, etc.) instead of a map
30+
cityName := city.City.Names.English
31+
assert.Equal(t, "Boxford", cityName, "Expected city name 'Boxford', got '%s'", cityName)
32+
continentName := city.Continent.Names.English
33+
assert.Equal(t, "Europe", continentName, "Expected continent name 'Europe', got '%s'", continentName)
3134

32-
ip = net.ParseIP("1.0.0.1")
33-
asn, err := g.GetASN(&ip)
35+
ip = netip.MustParseAddr("1.0.0.1")
36+
asn, err := g.GetASN(ip)
3437
if err != nil {
35-
t.Fatalf("GetCity returned error: %v", err)
38+
t.Fatalf("GetASN returned error: %v", err)
3639
}
3740
assert.Equal(t, uint(15169), asn.AutonomousSystemNumber, "Expected ASN 15169, got %d", asn.AutonomousSystemNumber)
38-
t.Logf("City: %+v", asn)
41+
t.Logf("ASN: %+v", asn)
3942

40-
ip = net.ParseIP("1.1.1.1")
41-
city, err = g.GetCity(&ip)
43+
ip = netip.MustParseAddr("1.1.1.1")
44+
city, err = g.GetCity(ip)
4245
if err != nil {
4346
t.Fatalf("GetCity returned error: %v", err)
4447
}
45-
assert.Empty(t, city.City.Names, "Expected empty city names map, got %v", city.City.Names)
46-
assert.Empty(t, city.Continent.Names, "Expected empty continent names map, got %v", city.Continent.Names)
48+
// v2: Names is a struct with fields, check English field
49+
cityName = city.City.Names.English
50+
assert.Empty(t, cityName, "Expected empty city name, got '%s'", cityName)
51+
continentName = city.Continent.Names.English
52+
assert.Empty(t, continentName, "Expected empty continent name, got '%s'", continentName)
4753
}

internal/geo/root.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package geo
33
import (
44
"context"
55
"fmt"
6-
"net"
6+
"net/netip"
77
"os"
88
"sync"
99
"time"
1010

11-
"github.com/oschwald/geoip2-golang"
11+
"github.com/oschwald/geoip2-golang/v2"
1212
log "github.com/sirupsen/logrus"
1313
)
1414

@@ -93,42 +93,39 @@ func (g *GeoDatabase) IsValid() bool {
9393
return !g.loadFailed
9494
}
9595

96-
func (g *GeoDatabase) GetASN(ip *net.IP) (*geoip2.ASN, error) {
97-
96+
func (g *GeoDatabase) GetASN(ip netip.Addr) (*geoip2.ASN, error) {
9897
if !g.IsValid() {
9998
return nil, ErrNotValidConfig
10099
}
101100

102101
g.RLock()
103102
defer g.RUnlock()
104-
record := &geoip2.ASN{}
105103

106104
if g.asnReader == nil {
107-
return record, nil
105+
return &geoip2.ASN{}, nil
108106
}
109107

110-
record, err := g.asnReader.ASN(*ip)
108+
record, err := g.asnReader.ASN(ip)
111109
if err != nil {
112110
return record, err
113111
}
114112

115113
return record, nil
116114
}
117115

118-
func (g *GeoDatabase) GetCity(ip *net.IP) (*geoip2.City, error) {
116+
func (g *GeoDatabase) GetCity(ip netip.Addr) (*geoip2.City, error) {
119117
if !g.IsValid() {
120118
return nil, ErrNotValidConfig
121119
}
122120

123121
g.RLock()
124122
defer g.RUnlock()
125-
record := &geoip2.City{}
126123

127124
if g.cityReader == nil {
128-
return record, nil
125+
return &geoip2.City{}, nil
129126
}
130127

131-
record, err := g.cityReader.City(*ip)
128+
record, err := g.cityReader.City(ip)
132129
if err != nil {
133130
return record, err
134131
}
@@ -137,19 +134,20 @@ func (g *GeoDatabase) GetCity(ip *net.IP) (*geoip2.City, error) {
137134
}
138135

139136
// Just a simple helper function to get the ISO code from the record in the same order as CrowdSec
137+
// Updated for v2: field names changed from IsoCode to ISOCode
140138
func GetIsoCodeFromRecord(record *geoip2.City) string {
141139
if record == nil {
142140
return ""
143141
}
144142

145-
if record.Country.IsoCode != "" {
146-
return record.Country.IsoCode
143+
if record.Country.ISOCode != "" {
144+
return record.Country.ISOCode
147145
}
148-
if record.RegisteredCountry.IsoCode != "" {
149-
return record.RegisteredCountry.IsoCode
146+
if record.RegisteredCountry.ISOCode != "" {
147+
return record.RegisteredCountry.ISOCode
150148
}
151-
if record.RepresentedCountry.IsoCode != "" {
152-
return record.RepresentedCountry.IsoCode
149+
if record.RepresentedCountry.ISOCode != "" {
150+
return record.RepresentedCountry.ISOCode
153151
}
154152
return ""
155153
}

pkg/spoa/root.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -631,24 +631,21 @@ func (s *Spoa) getIPRemediation(req *request.Request, ip netip.Addr) (remediatio
631631

632632
// If no IP-specific remediation, check country-based
633633
if r < remediation.Unknown && s.geoDatabase.IsValid() {
634-
ipStd := net.IP(ip.AsSlice())
635-
if ipStd != nil {
636-
record, err := s.geoDatabase.GetCity(&ipStd)
637-
if err != nil && !errors.Is(err, geo.ErrNotValidConfig) {
638-
s.logger.WithFields(log.Fields{
639-
"ip": ip.String(),
640-
"error": err,
641-
}).Warn("Failed to get geo location")
642-
} else if record != nil {
643-
iso := geo.GetIsoCodeFromRecord(record)
644-
if iso != "" {
645-
cnR, cnOrigin := s.dataset.CheckCN(iso)
646-
if cnR > remediation.Unknown {
647-
r = cnR
648-
origin = cnOrigin
649-
}
650-
req.Actions.SetVar(action.ScopeTransaction, "isocode", iso)
634+
record, err := s.geoDatabase.GetCity(ip)
635+
if err != nil && !errors.Is(err, geo.ErrNotValidConfig) {
636+
s.logger.WithFields(log.Fields{
637+
"ip": ip.String(),
638+
"error": err,
639+
}).Warn("Failed to get geo location")
640+
} else if record != nil {
641+
iso := geo.GetIsoCodeFromRecord(record)
642+
if iso != "" {
643+
cnR, cnOrigin := s.dataset.CheckCN(iso)
644+
if cnR > remediation.Unknown {
645+
r = cnR
646+
origin = cnOrigin
651647
}
648+
req.Actions.SetVar(action.ScopeTransaction, "isocode", iso)
652649
}
653650
}
654651
}

0 commit comments

Comments
 (0)