diff --git a/.gitignore b/.gitignore index 685de04c..462d3ff3 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,4 @@ build /tmp -config-bundle-private/ \ No newline at end of file +config-bundle-private* \ No newline at end of file diff --git a/go.mod b/go.mod index 38c23b77..4a24e9bf 100644 --- a/go.mod +++ b/go.mod @@ -32,5 +32,5 @@ require ( golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be gopkg.in/square/go-jose.v2 v2.5.1 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) diff --git a/go.sum b/go.sum index 175d58bd..4eb79976 100644 --- a/go.sum +++ b/go.sum @@ -520,6 +520,8 @@ gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71 h1:Xe2gvTZUJpsvOWUnvmL/tmhVB gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= mellium.im/sasl v0.2.1 h1:nspKSRg7/SyO0cRGY71OkfHab8tf9kCts6a6oTDut0w= diff --git a/pkg/lib/fieldgroups/database/database_validator.go b/pkg/lib/fieldgroups/database/database_validator.go index 04cc4636..e41aabd5 100644 --- a/pkg/lib/fieldgroups/database/database_validator.go +++ b/pkg/lib/fieldgroups/database/database_validator.go @@ -1,6 +1,7 @@ package database import ( + "context" "crypto/tls" "crypto/x509" "database/sql" @@ -9,6 +10,7 @@ import ( "net" "net/url" "strings" + "time" "github.com/go-pg/pg/v10" mysql "github.com/go-sql-driver/mysql" //mysql driver @@ -123,7 +125,9 @@ func ValidateDatabaseConnection(opts shared.Options, uri *url.URL, caCert string defer db.Close() // Try to ping database - err = db.Ping() + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + err = db.PingContext(ctx) if err != nil { return err } diff --git a/pkg/lib/shared/jsonTypes.go b/pkg/lib/shared/jsonTypes.go new file mode 100644 index 00000000..06ed8f67 --- /dev/null +++ b/pkg/lib/shared/jsonTypes.go @@ -0,0 +1,18 @@ +package shared + +import ( + "bytes" + "encoding/json" +) + +// IntOrString is an int that may be unmarshaled from either a JSON number +// literal, or a JSON string. +type IntOrString int + +// UnmarshalJSON will unmarshal an array of bytes into this type +func (i *IntOrString) UnmarshalJSON(d []byte) error { + var v int + err := json.Unmarshal(bytes.Trim(d, `"`), &v) + *i = IntOrString(v) + return err +} diff --git a/pkg/lib/shared/validators.go b/pkg/lib/shared/validators.go index e4eca507..2aec6c65 100644 --- a/pkg/lib/shared/validators.go +++ b/pkg/lib/shared/validators.go @@ -140,7 +140,8 @@ func ValidateRedisConnection(options *redis.Options, field, fgName string) (bool rdb := redis.NewClient(options) // Ping client - var ctx = context.Background() + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() _, err := rdb.Ping(ctx).Result() if err != nil { newError := ValidationError{