Skip to content

Commit

Permalink
add config validations
Browse files Browse the repository at this point in the history
  • Loading branch information
shahbazn committed Dec 4, 2023
1 parent ff86cac commit f3a158c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 16 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/caarlos0/env/v9 v9.0.0
github.com/cosmos/ibc-go/v7 v7.2.0
github.com/cosmos/relayer/v2 v2.4.1
github.com/go-playground/validator/v10 v10.16.0
github.com/google/go-github/v55 v55.0.0
github.com/prometheus/client_golang v1.15.0
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -73,9 +74,12 @@ require (
github.com/ethereum/go-ethereum v1.10.26 // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
Expand Down Expand Up @@ -116,6 +120,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.16.3 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/linxGnu/grocksdb v1.7.16 // indirect
Expand Down
19 changes: 13 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
Expand All @@ -486,13 +488,17 @@ github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KE
github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E=
github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
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.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
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-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
Expand Down Expand Up @@ -772,8 +778,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/libp2p/go-buffer-pool v0.1.0 h1:oK4mSFcQz7cTQIfqbe4MIj9gLW+mnanjyFtc6cdF0Y8=
Expand Down Expand Up @@ -1026,6 +1032,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
Expand Down
59 changes: 51 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"errors"
"fmt"
"os"
"strconv"
"strings"

"cosmossdk.io/math"
"github.com/caarlos0/env/v9"
"github.com/go-playground/validator/v10"
"github.com/google/go-github/v55/github"
"gopkg.in/yaml.v3"

Expand All @@ -30,23 +32,23 @@ type Account struct {
}

type RPC struct {
ChainName string `yaml:"chainName"`
ChainID string `yaml:"chainId"`
URL string `yaml:"url"`
ChainName string `yaml:"chainName" validate:"required"`
ChainID string `yaml:"chainId" validate:"required"`
URL string `yaml:"url" validate:"required,http_url,has_port"`
Timeout string `yaml:"timeout"`
}

type Config struct {
Accounts []Account `yaml:"accounts"`
GlobalRPCTimeout string `env:"GLOBAL_RPC_TIMEOUT" envDefault:"5s"`
RPCs []RPC `yaml:"rpc"`
RPCs []RPC `yaml:"rpc" validate`

Check failure on line 44 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

[GolangCI Lint] pkg/config/config.go#L44

structtag: struct field tag `yaml:"rpc" validate` not compatible with reflect.StructTag.Get: bad syntax for struct tag pair (govet)
Raw output
pkg/config/config.go:44:2: structtag: struct field tag `yaml:"rpc" validate` not compatible with reflect.StructTag.Get: bad syntax for struct tag pair (govet)
	RPCs             []RPC     `yaml:"rpc" validate`
	^
GitHub struct {
Org string `yaml:"org"`
Repo string `yaml:"repo"`
IBCDir string `yaml:"dir"`
Org string `yaml:"org" validate:"required"`
Repo string `yaml:"repo" validate:"required"`
IBCDir string `yaml:"dir" validate:"required"`
TestnetsIBCDir string `yaml:"testnetsDir"`
Token string `env:"GITHUB_TOKEN"`
} `yaml:"github"`
} `yaml:"github" validate:"required"`
}

type IBCChainMeta struct {
Expand Down Expand Up @@ -221,6 +223,42 @@ func (c *Config) getPaths(ctx context.Context, dir string, client *github.Client
return ibcs, nil
}

func (c *Config) Validate() error {
validate := validator.New(validator.WithRequiredStructEnabled())

// register custom validation for http url as expected by go relayer i.e.
// http_url must have port defined.
// https://github.com/cosmos/relayer/blob/259b1278264180a2aefc2085f1b55753849c4815/cregistry/chain_info.go#L115
validate.RegisterValidation("has_port", func(fl validator.FieldLevel) bool {

Check failure on line 232 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

[GolangCI Lint] pkg/config/config.go#L232

Error return value of `validate.RegisterValidation` is not checked (errcheck)
Raw output
pkg/config/config.go:232:29: Error return value of `validate.RegisterValidation` is not checked (errcheck)
	validate.RegisterValidation("has_port", func(fl validator.FieldLevel) bool {
	                           ^
val := fl.Field().String()
val_arr := strings.Split(val, ":")

Check failure on line 234 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

[GolangCI Lint] pkg/config/config.go#L234

var-naming: don't use underscores in Go names; var val_arr should be valArr (revive)
Raw output
pkg/config/config.go:234:3: var-naming: don't use underscores in Go names; var val_arr should be valArr (revive)
		val_arr := strings.Split(val, ":")
		^
port := val_arr[len(val_arr)-1]

// Port must be a iny <= 65535.
if portNum, err := strconv.ParseInt(
port, 10, 32,
); err != nil || portNum > 65535 || portNum < 1 {
return false
}
return true
})

// validate top level fields
if err := validate.Struct(c); err != nil {
return err

Check failure on line 249 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

[GolangCI Lint] pkg/config/config.go#L249

File is not `gofumpt`-ed with `-extra` (gofumpt)
Raw output
pkg/config/config.go:249: File is not `gofumpt`-ed with `-extra` (gofumpt)
}

Check failure on line 250 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

[GolangCI Lint] pkg/config/config.go#L250

block should not end with a whitespace (or comment) (wsl)
Raw output
pkg/config/config.go:250:2: block should not end with a whitespace (or comment) (wsl)
	}
	^

// validate RPCs
for _, rpc := range c.RPCs {
if err := validate.Struct(rpc); err != nil {
return fmt.Errorf("%v for RPC config: %+v", err, rpc)
}
}

return nil
}

func NewConfig(configPath string) (*Config, error) {
config := &Config{}

Expand All @@ -239,5 +277,10 @@ func NewConfig(configPath string) (*Config, error) {
return nil, err
}

err = config.Validate()
if err != nil {
return nil, err
}

return config, nil
}
6 changes: 4 additions & 2 deletions pkg/ibc/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func GetClientsInfo(ctx context.Context, ibc *config.IBCData, rpcs *map[string]c
chainB,
)
if err != nil {
return ClientsInfo{}, fmt.Errorf("Error: %w path %v <-> %v", err, cdA, cdB)
return ClientsInfo{}, fmt.Errorf("%w path %v <-> %v", err, cdA, cdB)
}

clientsInfo.ChainBClientExpiration, clientsInfo.ChainBClientInfo, err = relayer.QueryClientExpiration(
Expand All @@ -85,7 +85,7 @@ func GetClientsInfo(ctx context.Context, ibc *config.IBCData, rpcs *map[string]c
chainA,
)
if err != nil {
return ClientsInfo{}, fmt.Errorf("Error: %w path %v <-> %v", err, cdB, cdA)
return ClientsInfo{}, fmt.Errorf("%w path %v <-> %v", err, cdB, cdA)
}

return clientsInfo, nil
Expand All @@ -105,6 +105,8 @@ func GetChannelsInfo(ctx context.Context, ibc *config.IBCData, rpcs *map[string]
channelInfo.Channels = append(channelInfo.Channels, channel)
}

fmt.Printf("Type: %T Value %v\n", (*rpcs)[ibc.Chain2.ChainName], (*rpcs)[ibc.Chain2.ChainName])
fmt.Printf("Type: %T Value %v\n", (*rpcs)[ibc.Chain2.ChainName], (*rpcs)[ibc.Chain2.ChainName])
if (*rpcs)[ibc.Chain1.ChainName].ChainID == "" || (*rpcs)[ibc.Chain2.ChainName].ChainID == "" {
return channelInfo, fmt.Errorf(
"Error: RPC data is missing, cannot retrieve channel data: %v",
Expand Down

0 comments on commit f3a158c

Please sign in to comment.