Skip to content

Commit

Permalink
Add POLLING_FREQUENCY and REDIS_DATABASE config options (#39)
Browse files Browse the repository at this point in the history
* Add polling frequency config

* Add redis database config

* Use v1.1.0 of github.com/warrant-dev/warrant

* Allow configuring polling frequency option via env var
  • Loading branch information
kkajla12 authored Dec 21, 2023
1 parent 970c3d4 commit 82d1a5c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 32 deletions.
16 changes: 8 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"log"
"net/http"
"strings"
"time"

"github.com/pkg/errors"
Expand All @@ -45,7 +46,7 @@ const (

var (
ErrInvalidUpdateStrategy = errors.New("invalid update strategy")
ErrInvalidPollingFrequency = errors.New("invalid polling frequency (cannot be < 10s)")
ErrInvalidPollingFrequency = errors.New("invalid polling frequency (must be >= 10)")
ErrMissingApiKey = errors.New("missing API key")
)

Expand Down Expand Up @@ -90,13 +91,12 @@ func NewClient(conf ClientConfig) (*Client, error) {
config.UpdateStrategy = conf.UpdateStrategy
}

if conf.PollingFrequency != 0 {
config.PollingFrequency = conf.PollingFrequency
} else if config.PollingFrequency < 10 {
if conf.PollingFrequency < 10 {
return nil, ErrInvalidPollingFrequency
}
config.PollingFrequency = conf.PollingFrequency

if config.UpdateStrategy == UpdateStrategyStreaming {
if strings.EqualFold(config.UpdateStrategy, UpdateStrategyStreaming) {
streamingClient := sse.NewClient(fmt.Sprintf("%s/events", config.StreamingEndpoint))
streamingClient.Headers["Authorization"] = fmt.Sprintf("ApiKey %s", config.ApiKey)
streamingClient.ReconnectStrategy = backoff.WithMaxTries(backoff.NewExponentialBackOff(), 10)
Expand All @@ -106,7 +106,7 @@ func NewClient(conf ClientConfig) (*Client, error) {
config: config,
streamingClient: streamingClient,
}, nil
} else if config.UpdateStrategy == UpdateStrategyPolling || config.UpdateStrategy == "" {
} else if strings.EqualFold(config.UpdateStrategy, UpdateStrategyPolling) {
return &Client{
config: config,
}, nil
Expand All @@ -121,12 +121,12 @@ func (client *Client) Run() error {
return errors.Wrap(err, "error trying to initialize edge agent")
}

if client.config.UpdateStrategy == UpdateStrategyStreaming {
if strings.EqualFold(client.config.UpdateStrategy, UpdateStrategyStreaming) {
err = client.connect()
if err != nil {
return errors.Wrap(err, "error streaming warrant updates")
}
} else if client.config.UpdateStrategy == UpdateStrategyPolling {
} else if strings.EqualFold(client.config.UpdateStrategy, UpdateStrategyPolling) {
err = client.poll()
if err != nil {
return errors.Wrap(err, "error polling warrant updates")
Expand Down
6 changes: 6 additions & 0 deletions cmd/edge-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ const (
PropertyRedisHostname = "REDIS_HOSTNAME"
PropertyRedisPassword = "REDIS_PASSWORD"
PropertyRedisPort = "REDIS_PORT"
PropertyRedisDatabase = "REDIS_DATABASE"
PropertyStreamingEndpoint = "STREAMING_ENDPOINT"
PropertyUpdateStrategy = "UPDATE_STRATEGY"
PropertyPollingFrequency = "POLLING_FREQUENCY"
PropertyReadOnly = "READ_ONLY"
)

Expand All @@ -45,11 +47,13 @@ func main() {
viper.SetDefault(PropertyApiKey, os.Getenv(PropertyApiKey))
viper.SetDefault(PropertyApiEndpoint, os.Getenv(PropertyApiEndpoint))
viper.SetDefault(PropertyUpdateStrategy, os.Getenv(PropertyUpdateStrategy))
viper.SetDefault(PropertyPollingFrequency, os.Getenv(PropertyPollingFrequency))
viper.SetDefault(PropertyStreamingEndpoint, os.Getenv(PropertyStreamingEndpoint))
viper.SetDefault(PropertyDatastore, os.Getenv(PropertyDatastore))
viper.SetDefault(PropertyRedisHostname, os.Getenv(PropertyRedisHostname))
viper.SetDefault(PropertyRedisPort, os.Getenv(PropertyRedisPort))
viper.SetDefault(PropertyRedisPassword, os.Getenv(PropertyRedisPassword))
viper.SetDefault(PropertyRedisDatabase, os.Getenv(PropertyRedisDatabase))
viper.SetDefault(PropertyReadOnly, os.Getenv(PropertyReadOnly))

if err := viper.ReadInConfig(); err != nil {
Expand All @@ -70,6 +74,7 @@ func main() {
Hostname: viper.GetString(PropertyRedisHostname),
Password: viper.GetString(PropertyRedisPassword),
Port: viper.GetString(PropertyRedisPort),
Database: viper.GetInt(PropertyRedisDatabase),
})
if err != nil {
log.Fatal(err)
Expand All @@ -86,6 +91,7 @@ func main() {
ApiEndpoint: viper.GetString(PropertyApiEndpoint),
StreamingEndpoint: viper.GetString(PropertyStreamingEndpoint),
UpdateStrategy: viper.GetString(PropertyUpdateStrategy),
PollingFrequency: viper.GetInt(PropertyPollingFrequency),
Repository: repo,
})
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ require (
github.com/pkg/errors v0.9.1
github.com/r3labs/sse v0.0.0-20210224172625-26fe804710bc
github.com/spf13/viper v1.17.0
github.com/warrant-dev/warrant v0.59.0
github.com/warrant-dev/warrant v1.1.0
gopkg.in/cenkalti/backoff.v1 v1.1.0
)

require (
github.com/antonmedv/expr v1.15.3 // indirect
github.com/antonmedv/expr v1.15.5 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.5 // indirect
github.com/go-playground/validator/v10 v10.16.0 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/golang-jwt/jwt/v5 v5.0.0 // indirect
github.com/golang-jwt/jwt/v5 v5.2.0 // indirect
github.com/golang-migrate/migrate/v4 v4.16.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-github/v39 v39.2.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -35,7 +35,7 @@ require (
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/mattn/go-sqlite3 v1.14.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.18.1 // indirect
Expand Down
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/antonmedv/expr v1.15.3 h1:q3hOJZNvLvhqE8OHBs1cFRdbXFNKuA+bHmRaI+AmRmI=
github.com/antonmedv/expr v1.15.3/go.mod h1:0E/6TxnOlRNp81GMzX9QfDPAmHo2Phg00y4JUv1ihsE=
github.com/antonmedv/expr v1.15.5 h1:y0Iz3cEwmpRz5/r3w4qQR0MfIqJGdGM1zbhD/v0G5Vg=
github.com/antonmedv/expr v1.15.5/go.mod h1:0E/6TxnOlRNp81GMzX9QfDPAmHo2Phg00y4JUv1ihsE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
Expand Down Expand Up @@ -90,8 +90,8 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.15.5 h1:LEBecTWb/1j5TNY1YYG2RcOUN3R7NLylN+x8TTueE24=
github.com/go-playground/validator/v10 v10.15.5/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE=
github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
Expand All @@ -101,8 +101,8 @@ github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg78
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE=
github.com/golang-jwt/jwt/v5 v5.0.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw=
github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang-migrate/migrate/v4 v4.16.2 h1:8coYbMKUyInrFk1lfGfRovTLAW7PhWp8qQDT2iKfuoA=
github.com/golang-migrate/migrate/v4 v4.16.2/go.mod h1:pfcJX4nPHaVdc5nmdCikFBWtm+UBpiZjRNNsyBbp0/o=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down Expand Up @@ -169,13 +169,13 @@ github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -214,8 +214,8 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI=
github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
Expand Down Expand Up @@ -288,8 +288,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/warrant-dev/warrant v0.59.0 h1:9j3muW0RKqwzxVtADbJhfIgvI8f7FdB6n3Yh8laYicw=
github.com/warrant-dev/warrant v0.59.0/go.mod h1:W1Gr2XTlRc0q0Lz/SVJmbGo2qYAsSHXGaABYME70Xas=
github.com/warrant-dev/warrant v1.1.0 h1:LfLsvQSx7w23zWaq9qLBqU9n8loNfkPWeHtmbTRP2+w=
github.com/warrant-dev/warrant v1.1.0/go.mod h1:89Pz1L1C/TeBMfNEl11/8iiCt0tfgNt6hLu458rWva4=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
5 changes: 3 additions & 2 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type RedisRepositoryConfig struct {
Hostname string
Password string
Port string
Database int
}

type RedisRepository struct {
Expand All @@ -49,9 +50,9 @@ func NewRedisRepository(config RedisRepositoryConfig) (*RedisRepository, error)

var connectionString string
if config.Password != "" {
connectionString = fmt.Sprintf("rediss://default:%s@%s:%s/1", config.Password, hostname, port)
connectionString = fmt.Sprintf("rediss://default:%s@%s:%s/%d", config.Password, hostname, port, config.Database)
} else {
connectionString = fmt.Sprintf("redis://default:%s@%s:%s/1", config.Password, hostname, port)
connectionString = fmt.Sprintf("redis://default:%s@%s:%s/%d", config.Password, hostname, port, config.Database)
}

opt, err := redis.ParseURL(connectionString)
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (server *Server) check(w http.ResponseWriter, r *http.Request) {
}

var checkManySpec check.CheckManySpec
err := service.ParseJSONBody(r.Body, &checkManySpec)
err := service.ParseJSONBody(r.Context(), r.Body, &checkManySpec)
if err != nil {
service.SendErrorResponse(w, err)
}
Expand Down

0 comments on commit 82d1a5c

Please sign in to comment.