Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 0385f973dd53deb01d1718936a0d0c1723d94ea6
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Thu Nov 23 23:21:15 2023 -0400

    Fixes

commit dfb473e
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Tue Nov 21 14:46:55 2023 -0400

    Fixed sonar issues

commit 0ddfc7b
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Tue Nov 21 14:08:51 2023 -0400

    Fixed default value

commit 1eae9e5
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Sat Nov 18 21:20:10 2023 -0400

    Updated configuration

commit 32c8543
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Sat Nov 4 14:45:47 2023 -0300

    Added more validators

commit cfb3f72
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Sat Nov 4 13:46:47 2023 -0300

    Added more validators

commit c6b8906
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Sat Nov 4 12:01:24 2023 -0300

    Reorganize validators

commit b2ef4e4
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Sat Nov 4 11:33:52 2023 -0300

    Added more validators

commit 837a17e
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Sat Nov 4 10:33:20 2023 -0300

    Added more validators

commit 6eeb941
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Fri Nov 3 10:45:26 2023 -0300

    Added more validators

commit f49d179
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Thu Nov 2 22:13:48 2023 -0300

    Added proxy validator

commit 2209fdb
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Mon Oct 30 02:44:49 2023 -0300

    Added object path joining

commit c94139e
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Mon Oct 30 02:11:09 2023 -0300

    Added path validation

commit 1b76ca4
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Mon Oct 30 01:28:45 2023 -0300

    Added schema validation

commit 9ad6465
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Sun Oct 29 19:40:03 2023 -0300

    Added tests for validators

commit 9811a93
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Mon Oct 23 10:20:57 2023 -0300

    Added validators draft

commit fcea2e7
Author: Evgeny Abramovich <evg.abramovitch@gmail.com>
Date:   Sun Oct 22 15:39:22 2023 -0300

    Created validation draft
  • Loading branch information
evg4b committed Nov 24, 2023
1 parent 52c4e6a commit 665e6ea
Show file tree
Hide file tree
Showing 53 changed files with 2,279 additions and 65 deletions.
7 changes: 7 additions & 0 deletions .run/format.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="format" type="CompoundRunConfigurationType">
<toRun name="gofmt" type="ShConfigurationType" />
<toRun name="gofumpt" type="ShConfigurationType" />
<method v="2" />
</configuration>
</component>
12 changes: 12 additions & 0 deletions .run/go test uncors.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="go test uncors" type="GoTestRunConfiguration" factoryName="Go Test" nameIsGenerated="true">
<module name="uncors" />
<working_directory value="$PROJECT_DIR$" />
<kind value="DIRECTORY" />
<package value="github.com/evg4b/uncors" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<framework value="gotest" />
<method v="2" />
</configuration>
</component>
17 changes: 17 additions & 0 deletions .run/gofmt.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="gofmt" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="gofmt -l -s -w ." />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="/bin/zsh" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="true" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
</component>
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/bmatcuk/doublestar/v4 v4.6.1
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a
github.com/go-playground/validator/v10 v10.16.0
github.com/gobuffalo/validate v2.0.4+incompatible
github.com/gojuno/minimock/v3 v3.1.3
github.com/gorilla/mux v1.8.1
github.com/hashicorp/go-version v1.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
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/gobuffalo/validate v2.0.4+incompatible h1:ZTxozrIw8qQ5nfhShmc4izjYPTsPhfdXTdhXOd5OS9o=
github.com/gobuffalo/validate v2.0.4+incompatible/go.mod h1:N+EtDe0J8252BgfzQUChBgfd6L93m9weay53EWFVsMM=
github.com/gojuno/minimock/v3 v3.1.3 h1:9jakBeOqffZvR9BGBTulphLwiUfiju1w7JspU5eX/fY=
github.com/gojuno/minimock/v3 v3.1.3/go.mod h1:WylRuaQInND/eg0HqP0/6etOdtv67AIfOgPW1z8QtKU=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 3 additions & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const (
var flags *pflag.FlagSet

type UncorsConfig struct {
HTTPPort int `mapstructure:"http-port" validate:"required"`
Mappings Mappings `mapstructure:"mappings" validate:"required"`
HTTPPort int `mapstructure:"http-port"`
Mappings Mappings `mapstructure:"mappings"`
Proxy string `mapstructure:"proxy"`
Debug bool `mapstructure:"debug"`
HTTPSPort int `mapstructure:"https-port"`
Expand Down Expand Up @@ -76,10 +76,6 @@ func LoadConfiguration(viperInstance *viper.Viper, args []string) *UncorsConfig
configuration.IsHTTPSEnabled(),
)

if err := Validate(configuration); err != nil {
panic(err)
}

return configuration
}

Expand All @@ -89,7 +85,7 @@ func defineFlags() {
flags.StringSliceP("to", "t", []string{}, "Target host with protocol for to the resource to be proxy")
flags.StringSliceP("from", "f", []string{}, "Local host with protocol for to the resource from which proxying will take place") //nolint: lll
flags.UintP("http-port", "p", defaultHTTPPort, "Local HTTP listening port")
flags.UintP("https-port", "s", defaultHTTPSPort, "Local HTTPS listening port")
flags.UintP("https-port", "s", 0, "Local HTTPS listening port")
flags.String("cert-file", "", "Path to HTTPS certificate file")
flags.String("key-file", "", "Path to matching for certificate private key")
flags.String("proxy", "", "HTTP/HTTPS proxy to provide requests to real server (used system by default)")
Expand Down
8 changes: 3 additions & 5 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ func TestLoadConfiguration(t *testing.T) {
name: "return default config",
args: []string{},
expected: &config.UncorsConfig{
HTTPPort: 80,
HTTPSPort: 443,
Mappings: config.Mappings{},
HTTPPort: 80,
Mappings: config.Mappings{},
CacheConfig: config.CacheConfig{
ExpirationTime: config.DefaultExpirationTime,
ClearTime: config.DefaultClearTime,
Expand All @@ -109,8 +108,7 @@ func TestLoadConfiguration(t *testing.T) {
name: "minimal config is set",
args: []string{params.Config, minimalConfigPath},
expected: &config.UncorsConfig{
HTTPPort: 8080,
HTTPSPort: 443,
HTTPPort: 8080,
Mappings: config.Mappings{
{From: hosts.Localhost.HTTPPort(8080), To: hosts.Github.HTTPS()},
},
Expand Down
7 changes: 6 additions & 1 deletion internal/config/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ func (m *Mock) Clone() Mock {
}

func (m *Mock) String() string {
return helpers.Sprintf("[%s %d] %s", m.Method, m.Response.Code, m.Path)
method := "*"
if m.Method != "" {
method = m.Method
}

return helpers.Sprintf("[%s %d] %s", method, m.Response.Code, m.Path)
}

type Mocks []Mock
Expand Down
11 changes: 0 additions & 11 deletions internal/config/validation.go

This file was deleted.

32 changes: 0 additions & 32 deletions internal/config/validation_test.go

This file was deleted.

44 changes: 44 additions & 0 deletions internal/config/validators/base/directory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package base

import (
"fmt"
"os"

"github.com/evg4b/uncors/internal/helpers"
"github.com/gobuffalo/validate"
"github.com/spf13/afero"
)

type DirectoryValidator struct {
Field string
Value string
Fs afero.Fs
}

func (f *DirectoryValidator) IsValid(errors *validate.Errors) {
if f.Value == "" {
errors.Add(f.Field, fmt.Sprintf("%s must not be empty", f.Field))

return
}

helpers.PassedOrOsFs(&f.Fs)

stat, err := f.Fs.Stat(f.Value)
if err != nil {
switch {
case os.IsNotExist(err):
errors.Add(f.Field, fmt.Sprintf("%s directory does not exist", f.Field))
case os.IsPermission(err):
errors.Add(f.Field, fmt.Sprintf("%s directory is not accessible", f.Field))
default:
errors.Add(f.Field, fmt.Sprintf("%s is not a directory", f.Field))
}

return
}

if !stat.IsDir() {
errors.Add(f.Field, fmt.Sprintf("%s is not a directory", f.Field))
}
}
67 changes: 67 additions & 0 deletions internal/config/validators/base/directory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package base_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/evg4b/uncors/internal/config/validators/base"
"github.com/evg4b/uncors/testing/testutils"
"github.com/gobuffalo/validate"
"github.com/stretchr/testify/assert"
)

func TestDirectoryValidator(t *testing.T) {
const field = "test"
const path = "/demo"

fs := testutils.FsFromMap(t, map[string]string{
"file.go": "package validators",
})
testutils.CheckNoError(t, fs.Mkdir(path, 0o755))

t.Run("should not register error if file error", func(t *testing.T) {
errors := validate.Validate(&base.DirectoryValidator{
Field: field,
Value: path,
Fs: fs,
})

assert.False(t, errors.HasAny())
})

t.Run("should register error for", func(t *testing.T) {
tests := []struct {
name string
path string
error string
}{
{
name: "empty path",
path: "",
error: "test must not be empty",
},
{
name: "directory does not exist",
path: "directory_does_not_exist",
error: "test directory does not exist",
},
{
name: "file instead of directory",
path: "file.go",
error: "test is not a directory",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
errors := validate.Validate(&base.DirectoryValidator{
Field: field,
Value: test.path,
Fs: fs,
})

require.EqualError(t, errors, test.error)
})
}
})
}
34 changes: 34 additions & 0 deletions internal/config/validators/base/duration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package base

import (
"fmt"
"time"

"github.com/gobuffalo/validate"
)

type DurationValidator struct {
Field string
Value time.Duration
AllowZero bool
}

func (d *DurationValidator) IsValid(errors *validate.Errors) {
if d.AllowZero {
d.validateWithZero(errors)
} else {
d.validateWithoutZero(errors)
}
}

func (d *DurationValidator) validateWithoutZero(errors *validate.Errors) {
if d.Value <= 0 {
errors.Add(d.Field, fmt.Sprintf("%s must be greater than 0", d.Field))
}
}

func (d *DurationValidator) validateWithZero(errors *validate.Errors) {
if d.Value < 0 {
errors.Add(d.Field, fmt.Sprintf("%s must be greater than or equal to 0", d.Field))
}
}
Loading

0 comments on commit 665e6ea

Please sign in to comment.