Skip to content

WIP: add support for checking nginx configuration files #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func Test_flags(t *testing.T) {
{"flags set, wrong reporter", []string{"--exclude-dirs=subdir", "--reporter=wrong", "."}, 1},
{"flags set, json reporter", []string{"--exclude-dirs=subdir", "--reporter=json", "."}, 0},
{"flags set, junit reported", []string{"--exclude-dirs=subdir", "--reporter=junit", "."}, 0},
{"junit reported, wrong groupby", []string{"-groupby=directory", "--reporter=junit", "."}, 1},
{"bad path", []string{"/path/does/not/exit"}, 1},
{"exclude file types set", []string{"--exclude-file-types=json", "."}, 0},
{"multiple paths", []string{"../../test/fixtures/subdir/good.json", "../../test/fixtures/good.json"}, 0},
Expand All @@ -32,6 +33,7 @@ func Test_flags(t *testing.T) {
{"wrong output set", []string{"--output", "/path/not/exist", "--reporter", "json", "."}, 1},
{"incorrect group", []string{"-groupby=badgroup", "."}, 1},
{"correct group", []string{"-groupby=directory", "."}, 0},
{"duplicate groupby", []string{"-groupby=directory -groupby=directory", "."}, 1},
}
for _, tc := range cases {
// this call is required because otherwise flags panics,
Expand Down
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/magiconair/properties v1.8.7
github.com/pelletier/go-toml/v2 v2.0.6
github.com/stretchr/testify v1.8.1
github.com/tufanbarisyildirim/gonginx v0.0.0-20231222202608-ba16e88a9436
gopkg.in/ini.v1 v1.67.0
gopkg.in/yaml.v3 v3.0.1
howett.net/plist v1.0.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tufanbarisyildirim/gonginx v0.0.0-20231222202608-ba16e88a9436 h1:i9TLbw23bUawnhimf5SghqkLrDRdpa65vw0hUqYhCB0=
github.com/tufanbarisyildirim/gonginx v0.0.0-20231222202608-ba16e88a9436/go.mod h1:4fTjBxMoWGOIVnGFSTS9GAZ0yMyiGzTdATQS0krQv18=
github.com/zclconf/go-cty v1.13.0 h1:It5dfKTTZHe9aeppbNOda3mN7Ag7sg6QkBNm6TkyFa0=
github.com/zclconf/go-cty v1.13.0/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -61,5 +63,7 @@ gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o=
gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g=
howett.net/plist v1.0.0 h1:7CrbWYbPPO/PyNy38b2EB/+gYbjCe2DXBxgtOOZbSQM=
howett.net/plist v1.0.0/go.mod h1:lqaXoTrLY4hg8tnEzNru53gicrbv7rrk+2xJA/7hw9g=
19 changes: 19 additions & 0 deletions pkg/filetype/file_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type FileType struct {
Name string
Extensions []string
FileMasks []string
Validator validator.Validator
}

Expand All @@ -19,6 +20,7 @@ type FileType struct {
var JsonFileType = FileType{
"json",
[]string{"json"},
[]string{},
validator.JsonValidator{},
}

Expand All @@ -27,6 +29,7 @@ var JsonFileType = FileType{
var YamlFileType = FileType{
"yaml",
[]string{"yml", "yaml"},
[]string{},
validator.YamlValidator{},
}

Expand All @@ -35,6 +38,7 @@ var YamlFileType = FileType{
var XmlFileType = FileType{
"xml",
[]string{"xml"},
[]string{},
validator.XmlValidator{},
}

Expand All @@ -43,6 +47,7 @@ var XmlFileType = FileType{
var TomlFileType = FileType{
"toml",
[]string{"toml"},
[]string{},
validator.TomlValidator{},
}

Expand All @@ -51,6 +56,7 @@ var TomlFileType = FileType{
var IniFileType = FileType{
"ini",
[]string{"ini"},
[]string{},
validator.IniValidator{},
}

Expand All @@ -59,6 +65,7 @@ var IniFileType = FileType{
var PropFileType = FileType{
"properties",
[]string{"properties"},
[]string{},
validator.PropValidator{},
}

Expand All @@ -67,6 +74,7 @@ var PropFileType = FileType{
var HclFileType = FileType{
"hcl",
[]string{"hcl"},
[]string{},
validator.HclValidator{},
}

Expand All @@ -75,6 +83,7 @@ var HclFileType = FileType{
var PlistFileType = FileType{
"plist",
[]string{"plist"},
[]string{},
validator.PlistValidator{},
}

Expand All @@ -83,14 +92,23 @@ var PlistFileType = FileType{
var CsvFileType = FileType{
"csv",
[]string{"csv"},
[]string{},
validator.CsvValidator{},
}

var NginxFileType = FileType{
"nginx",
[]string{},
[]string{"nginx*.conf"},
validator.NginxValidator{},
}

// Instance of the FileType object to
// represent a HOCON file
var HoconFileType = FileType{
"hocon",
[]string{"hocon"},
[]string{},
validator.HoconValidator{},
}

Expand All @@ -106,5 +124,6 @@ var FileTypes = []FileType{
HclFileType,
PlistFileType,
CsvFileType,
NginxFileType,
HoconFileType,
}
13 changes: 13 additions & 0 deletions pkg/finder/fsfinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ func (fsf FileSystemFinder) findOne(pathRoot string) ([]FileMetadata, error) {
}
}
}
} else {
for _, fileType := range fsf.FileTypes {
for _, fileMask := range fileType.FileMasks {
pattern := filepath.Join(filepath.Dir(path), filepath.Base(path), fileMask)
foundFiles, _ := filepath.Glob(pattern)
for _, matchedFilePath := range foundFiles {
ss := strings.Split(matchedFilePath, string(os.PathSeparator))
matchedFileName := ss[len(ss)-1]
fileMetadata := FileMetadata{matchedFileName, matchedFilePath, fileType}
matchingFiles = append(matchingFiles, fileMetadata)
}
}
}
}

return nil
Expand Down
17 changes: 17 additions & 0 deletions pkg/validator/nginx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package validator

import (
"github.com/tufanbarisyildirim/gonginx/parser"
)

type NginxValidator struct{}

func (nv NginxValidator) Validate(b []byte) (result bool, errMsg error) {
p := parser.NewStringParser(string(b))
_, err := p.Parse()
if err != nil {
return false, err
}

return true, nil
}
2 changes: 2 additions & 0 deletions pkg/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ var testData = []struct {
{"invalidCSV", []byte(`This string has a \" in it`), false, CsvValidator{}},
{"validPlist", validPlistBytes, true, PlistValidator{}},
{"invalidPlist", invalidPlistBytes, false, PlistValidator{}},
{"validNginx", []byte(`http { server { listen 80; server_name localhost; }}`), true, NginxValidator{}},
{"invalidNginx", []byte(`invalid_key {}`), false, NginxValidator{}},
{"validHocon", []byte(`test = [1, 2, 3]`), true, HoconValidator{}},
{"invalidHocon", []byte(`test = [1, 2,, 3]`), false, HoconValidator{}},
}
Expand Down