Skip to content

Commit

Permalink
Merge pull request #47 from roma-glushko/35-integrate-with-maxmind-le…
Browse files Browse the repository at this point in the history
…gal-changes

#35 integrate with maxmind legal changes
  • Loading branch information
roma-glushko authored Feb 23, 2020
2 parents 31d85a2 + d31d750 commit 54d5e24
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 127 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ require (
github.com/gobuffalo/logger v1.0.3 // indirect
github.com/gobuffalo/packd v1.0.0 // indirect
github.com/gobuffalo/packr/v2 v2.7.1
github.com/maxmind/geoipupdate/v4 v4.2.2
github.com/oschwald/geoip2-golang v1.3.0
github.com/oschwald/maxminddb-golang v1.3.1 // indirect
github.com/rogpeppe/go-internal v1.5.2 // indirect
github.com/stretchr/testify v1.4.0
github.com/stretchr/testify v1.5.1
github.com/urfave/cli v1.21.0
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d // indirect
golang.org/x/sys v0.0.0-20200219091948-cb0a6d8edb6c // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)

go 1.13
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ github.com/gobuffalo/packd v1.0.0 h1:6ERZvJHfe24rfFmA9OaoKBdC7+c9sydrytMg8SdFGBM
github.com/gobuffalo/packd v1.0.0/go.mod h1:6VTc4htmJRFB7u1m/4LeMTWjFoYrUiBkU9Fdec9hrhI=
github.com/gobuffalo/packr/v2 v2.7.1 h1:n3CIW5T17T8v4GGK5sWXLVWJhCz7b5aNLSxW6gYim4o=
github.com/gobuffalo/packr/v2 v2.7.1/go.mod h1:qYEvAazPaVxy7Y7KR0W8qYEE+RymX74kETFqjFoFlOc=
github.com/gofrs/flock v0.7.1 h1:DP+LD/t0njgoPBvT5MJLeliUIVQR03hiKR6vezdwHlc=
github.com/gofrs/flock v0.7.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand All @@ -39,6 +41,8 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
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/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
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=
Expand All @@ -49,13 +53,17 @@ github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/maxmind/geoipupdate/v4 v4.2.2 h1:UjG6BleKBST4FCfNA9wp4AA4CscpmZ4H2+dTk4eNfG8=
github.com/maxmind/geoipupdate/v4 v4.2.2/go.mod h1:7cJjBXiQToWjkOuQPoWJ/4vqdU0L2RzoyeBjyWS/jTo=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/oschwald/geoip2-golang v1.3.0 h1:D+Hsdos1NARPbzZ2aInUHZL+dApIzo8E0ErJVsWcku8=
github.com/oschwald/geoip2-golang v1.3.0/go.mod h1:0LTTzix/Ao1uMvOhAV4iLU0Lz7eCrP94qZWBTDKf0iE=
github.com/oschwald/maxminddb-golang v1.3.1 h1:kPc5+ieL5CC/Zn0IaXJPxDFlUxKTQEU8QBTtmfQDAIo=
github.com/oschwald/maxminddb-golang v1.3.1/go.mod h1:3jhIUymTJ5VREKyIhWm66LJiQt04F0UCDdodShpjWsY=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand All @@ -75,6 +83,8 @@ github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tL
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -84,6 +94,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
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/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/urfave/cli v1.21.0 h1:wYSSj06510qPIzGSua9ZqsncMmWE3Zr55KBERygyrxE=
github.com/urfave/cli v1.21.0/go.mod h1:lxDj6qX9Q6lWQxIrbrT0nwecwUtRnhVZAJjJZrVUZZQ=
Expand Down Expand Up @@ -120,3 +132,5 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7 changes: 3 additions & 4 deletions internal/cli/command/geo-report-command.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ func GeoReportCommand(cliContext *cli.Context) error {

// ensure that geo library is in place
if os.IsNotExist(err) {
installMaxmindLibUsecase := di.InitInstallMaxmindLibUsecase()
fmt.Println("🚨 Cannot perform geo reports without MaxMind geo database installed")
fmt.Println("🚨 Please run 'tango geo-lib -h' to get more info about installation")

fmt.Println("💃 installing geo lib...")
installMaxmindLibUsecase.Install()
fmt.Println("🎉 geo lib has been installed")
return nil
}

geoReportUsecase := di.InitGeoReportUsecase(geoLibPath)
Expand Down
46 changes: 41 additions & 5 deletions internal/cli/command/install-geo-lib-command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,63 @@ import (
// InstallGeoLibCommand installs the geo lib
func InstallGeoLibCommand(cliContext *cli.Context) error {
needUpdate := cliContext.Bool("update")

homeDirResolver := di.InitHomeDirResolver()
geoLibResolver := di.InitMaxmindGeoLibResolver()
geoConfResolver := di.InitMaxmindConfResolver()
generateConfUsecase := di.InitGenerateMaxmindConfUsecase()

fmt.Println("💃 Tango is on the scene!")

geoLibPath, err := geoLibResolver.GetPath()
geoConfPath, geoConfExistErr := geoConfResolver.GetPath()

if os.IsNotExist(geoConfExistErr) {
// generate maxmind conf file
fmt.Println("🛠 Generating MaxMind Config file..")

accountID := cliContext.String("account-id")
licenseKey := cliContext.String("license-key")

if accountID == "" || licenseKey == "" {
fmt.Println("")
fmt.Println("🚨 MaxMind Config file should be generated to work with Geo data")
fmt.Println("🚨 You need to provide 'account-id' and 'license-key' arguments for this command")
fmt.Println("🚨 Credentials can be found under https://www.maxmind.com/en/accounts/current/license-key page")

return nil
}

err := generateConfUsecase.Generate(geoConfPath, accountID, licenseKey)

if err != nil {
fmt.Printf("🚨 Failed to generate MaxMind Config file: ", err.Error())

return nil
}

if !os.IsNotExist(err) && !needUpdate {
fmt.Println("🛠 MaxMind Config file has been generated: ", geoConfPath)
fmt.Println("🛠 You can customize the config file if needed (https://github.com/maxmind/geoipupdate/blob/master/doc/GeoIP.conf.md)")
}

fmt.Println("🛠 geo conf file: ", geoConfPath)

geoLibPath, geoLibExistErr := geoLibResolver.GetPath()

if !os.IsNotExist(geoLibExistErr) && !needUpdate {
fmt.Println("🎉 geo lib has already been installed")
fmt.Println("🛠 geo lib path: ", geoLibPath)
return nil
}

installMaxmindLibUsecase := di.InitInstallMaxmindLibUsecase()

if needUpdate {
fmt.Println("💃 updating geo lib...")
} else {
fmt.Println("💃 installing geo lib...")
}

installMaxmindLibUsecase.Install()
homeDir := homeDirResolver.GetPath()
installMaxmindLibUsecase := di.InitInstallMaxmindLibUsecase()
installMaxmindLibUsecase.Install(geoConfPath, homeDir, true)

if needUpdate {
fmt.Println("🎉 geo lib has been updated")
Expand Down
12 changes: 11 additions & 1 deletion internal/cli/tango-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,24 @@ func getTangoCommands() []cli.Command {
Name: "geo-lib",
Aliases: []string{"install-geo-lib", "get-geo-lib"},
Category: "Misc",
Usage: "Install Geo Lib (from MaxMind)",
Usage: "Install Geo Lib (from MaxMind).\nCredentials can be generated under https://www.maxmind.com/en/accounts/current/license-key page",
Action: command.InstallGeoLibCommand,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "update, u",
Usage: "Update/reinstall geo library",
Required: false,
},
cli.StringFlag{
Name: "account-id, a",
Usage: "MaxMind AccountID",
Required: false,
},
cli.StringFlag{
Name: "license-key, l",
Usage: "MaxMind LicenseKey",
Required: false,
},
},
},
// report commands
Expand Down
15 changes: 12 additions & 3 deletions internal/di/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,21 @@ func InitMaxmindGeoLibResolver() *geo.MaxMindGeoLibResolver {
return geo.NewMaxMindGeoLibResolver(homeDirResolver)
}

// InitMaxmindConfResolver inits Maxmind Conf file resolver
func InitMaxmindConfResolver() *geo.MaxMindConfResolver {
homeDirResolver := InitHomeDirResolver()

return geo.NewMaxMindConfResolver(homeDirResolver)
}

// InitInstallMaxmindLibUsecase inits a usecase
func InitInstallMaxmindLibUsecase() *geodata.InstallMaxmindLibUsecase {
homeDirResolver := InitHomeDirResolver()
maxmindGeoLibResolver := InitMaxmindGeoLibResolver()
return geodata.NewInstallMaxmindLibUsecase()
}

return geodata.NewInstallMaxmindLibUsecase(homeDirResolver, maxmindGeoLibResolver)
// InitGenerateMaxmindConfUsecase inits a usecase
func InitGenerateMaxmindConfUsecase() *geodata.GenerateMaxmindConfUsecase {
return geodata.NewGenerateMaxmindConfUsecase()
}

// InitReadAccessLogUsecase inits a usecase
Expand Down
31 changes: 31 additions & 0 deletions internal/infrastructure/geo/maxmind-geo-conf-resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package geo

import (
"os"
"path/filepath"
"tango/internal/infrastructure/filesystem"
)

// MaxMindConfResolver retrieves path to the MaxMind Geo Confi file
type MaxMindConfResolver struct {
homeDirResolver *filesystem.HomeDirResolver
}

// NewMaxMindConfResolver creates a new instance of MaxMindConfResolver
func NewMaxMindConfResolver(homeDirResolver *filesystem.HomeDirResolver) *MaxMindConfResolver {
return &MaxMindConfResolver{
homeDirResolver: homeDirResolver,
}
}

// GetPath provides path to MaxMind Geo Library
// there is only one possible path where the lib file can be found: $HOME/.tango/maxmind.conf
// $HOME path should be different on diff OS
func (r *MaxMindConfResolver) GetPath() (string, error) {
homeDirectory := r.homeDirResolver.GetPath()

maxmindConfPath := filepath.Join(homeDirectory, "maxmind.conf")
_, maxmindConfExistError := os.Stat(maxmindConfPath)

return maxmindConfPath, maxmindConfExistError
}
40 changes: 40 additions & 0 deletions internal/usecase/geodata/generate-maxmind-conf-usecase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package geodata

import (
"fmt"
"os"
)

// GenerateMaxmindConfUsecase usecase
type GenerateMaxmindConfUsecase struct {
}

// NewGenerateMaxmindConfUsecase creates a new instance of InstallMaxMindLibraryUsecase
func NewGenerateMaxmindConfUsecase() *GenerateMaxmindConfUsecase {
return &GenerateMaxmindConfUsecase{}
}

// Generate MaxMind Conf File
func (u *GenerateMaxmindConfUsecase) Generate(confPath string, accountID string, licenseKey string) error {
confFile, err := os.Create(confPath)

if err != nil {
return err
}

defer confFile.Close()

confFile.WriteString("# This MaxMind config file was generated by Tango \n")
confFile.WriteString(fmt.Sprintf("AccountID %s\n", accountID))
confFile.WriteString(fmt.Sprintf("LicenseKey %s\n", licenseKey))
confFile.WriteString(fmt.Sprintf("EditionIDs %s\n", u.GetEditionIDs()))

confFile.Sync()

return nil
}

// GetEditionIDs retrieves MaxMind Product IDs which are used by Tango
func (u *GenerateMaxmindConfUsecase) GetEditionIDs() string {
return "GeoLite2-City"
}
Loading

0 comments on commit 54d5e24

Please sign in to comment.