Skip to content

Commit

Permalink
Add support for Apple file list types for validation (Boeing#68)
Browse files Browse the repository at this point in the history
* Add support for Apple file list types for validation

* Fix merge conflicts

* Move large strings from test cases to an embedded file

* Move large text in test case to a variable
  • Loading branch information
sandyydk authored and shiina4119 committed Aug 23, 2024
1 parent 6d0f5b0 commit e976393
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Validator recusively scans a directory to search for configuration files and
validates them using the go package for each configuration type.
Currently json, yaml, toml, xml, ini, properties, hcl and csv configuration file
Currently json, yaml, toml, xml, ini, properties, hcl, csv and plist configuration file
types are supported.
Usage:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ require (
github.com/zclconf/go-cty v1.13.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.11.0 // indirect
howett.net/plist v1.0.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/hcl/v2 v2.18.1 h1:6nxnOJFku1EuSawSD81fuviYUV8DxFr3fp2dUi3ZYSo=
github.com/hashicorp/hcl/v2 v2.18.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
Expand Down Expand Up @@ -54,6 +55,9 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg=
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=
howett.net/plist v1.0.0 h1:7CrbWYbPPO/PyNy38b2EB/+gYbjCe2DXBxgtOOZbSQM=
howett.net/plist v1.0.0/go.mod h1:lqaXoTrLY4hg8tnEzNru53gicrbv7rrk+2xJA/7hw9g=
3 changes: 2 additions & 1 deletion index.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ How many deployments have you done that needed to be rolled back due to a missin
* INI
* Properties
* HCL
* PLIST
* CSV

## Installing
Expand Down Expand Up @@ -101,7 +102,7 @@ validator --exclude-dirs=/path/to/search/tests /path/to/search
![Exclude Dirs Run](./img/exclude_dirs.png)

#### Exclude file types
Exclude file types in the search path. Available file types are `ini`, `json`, `yaml`, `yml`, `toml`, `xml`, `properties`, `hcl` and `csv`
Exclude file types in the search path. Available file types are `ini`, `json`, `yaml`, `yml`, `toml`, `xml`, `properties`, `hcl`, `csv` and `plist`

```
validator --exclude-file-types=json /path/to/search
Expand Down
9 changes: 9 additions & 0 deletions pkg/filetype/file_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ var HclFileType = FileType{
validator.HclValidator{},
}

// Instance of the FileType object to
// represent a Plist file
var PlistFileType = FileType{
"plist",
[]string{"plist"},
validator.PlistValidator{},
}

// Instance of the FileType object to
// represent a CSV file
var CsvFileType = FileType{
Expand All @@ -88,5 +96,6 @@ var FileTypes = []FileType{
IniFileType,
PropFileType,
HclFileType,
PlistFileType,
CsvFileType,
}
22 changes: 22 additions & 0 deletions pkg/validator/plist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package validator

import (
"bytes"

"howett.net/plist"
)

// PlistValidator is used to validate a byte slice that is intended to represent a
// Apple Property List file (plist).
type PlistValidator struct{}

// Validate checks if the provided byte slice represents a valid .plist file.
func (csvv PlistValidator) Validate(b []byte) (bool, error) {
var output interface{}
plistDecoder := plist.NewDecoder(bytes.NewReader(b))
err := plistDecoder.Decode(&output)
if err != nil {
return false, err
}
return true, nil
}
35 changes: 35 additions & 0 deletions pkg/validator/validator_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
package validator

import (
_ "embed"
"testing"
)

var (
validPlistBytes = []byte(`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>`)

invalidPlistBytes = []byte(`<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSAppTransporT-Security</key> <!-- The hyphen in the key name here is invalid -->
<dict>
<key>NSAllowsArbitraryLoads</key>
</dict> <!-- Missing value for the key 'NSAllowsArbitraryLoads' -->
</dict>
</plist>`)
)

var testData = []struct {
name string
testInput []byte
Expand All @@ -27,6 +60,8 @@ var testData = []struct {
{"multipleInvalidHcl", []byte(`"key1" = "value1"\n"key2"="value2"`), false, HclValidator{}},
{"validCSV", []byte(`first_name,last_name,username\nRob,Pike,rob\n`), true, CsvValidator{}},
{"invalidCSV", []byte(`This string has a \" in it`), false, CsvValidator{}},
{"validPlist", validPlistBytes, true, PlistValidator{}},
{"invalidPlist", invalidPlistBytes, false, PlistValidator{}},
}

func Test_ValidationInput(t *testing.T) {
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/good.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>
13 changes: 13 additions & 0 deletions test/fixtures/subdir2/bad.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSAppTransporT-Security</key> <!-- The hyphen in the key name here is invalid -->
<dict>
<key>NSAllowsArbitraryLoads</key>
</dict> <!-- Missing value for the key 'NSAllowsArbitraryLoads' -->
</dict>
</plist>

0 comments on commit e976393

Please sign in to comment.