Skip to content

Commit c813a60

Browse files
feat(config): support YAML files (fanal#155)
* feat: add config * feat(analyzer/config): add yaml analyzer * chore(mod): update * chore(ci): bump up Go to 1.15 * test(analyzer/config): add anchors yaml test * test(analyzer/config): add circular referneces yaml test * refactor(analyzer/config) change yaml interface * test(analyzer/config) add multiple yaml test * chore(analyzer) change comment Co-authored-by: masahiro331 <[email protected]>
1 parent 907e6be commit c813a60

File tree

19 files changed

+1504
-44
lines changed

19 files changed

+1504
-44
lines changed

.github/workflows/bench.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ jobs:
66
runs-on: ubuntu-latest
77
steps:
88

9-
- name: Set up Go 1.13
9+
- name: Set up Go 1.15
1010
uses: actions/setup-go@v1
1111
with:
12-
go-version: 1.13
12+
go-version: 1.15
1313
id: go
1414

1515
- name: Check out code into the Go module directory

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ jobs:
99
name: Unit Test
1010
runs-on: ubuntu-latest
1111
steps:
12-
- name: Set up Go 1.13
12+
- name: Set up Go 1.15
1313
uses: actions/setup-go@v1
1414
with:
15-
go-version: 1.13
15+
go-version: 1.15
1616
id: go
1717

1818
- name: Check out code into the Go module directory
@@ -28,10 +28,10 @@ jobs:
2828
name: Integration Test
2929
runs-on: ubuntu-latest
3030
steps:
31-
- name: Set up Go 1.13
31+
- name: Set up Go 1.15
3232
uses: actions/setup-go@v1
3333
with:
34-
go-version: 1.13
34+
go-version: 1.15
3535
id: go
3636

3737
- name: Check out code into the Go module directory

analyzer/analyzer.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ type AnalysisResult struct {
5656
OS *types.OS
5757
PackageInfos []types.PackageInfo
5858
Applications []types.Application
59+
Configs []types.Config
5960
}
6061

6162
func (r *AnalysisResult) isEmpty() bool {
62-
return r.OS == nil && len(r.PackageInfos) == 0 && len(r.Applications) == 0
63+
return r.OS == nil && len(r.PackageInfos) == 0 && len(r.Applications) == 0 && len(r.Configs) == 0
6364
}
6465

6566
func (r *AnalysisResult) Sort() {
@@ -70,6 +71,10 @@ func (r *AnalysisResult) Sort() {
7071
sort.Slice(r.Applications, func(i, j int) bool {
7172
return r.Applications[i].FilePath < r.Applications[j].FilePath
7273
})
74+
75+
sort.Slice(r.Configs, func(i, j int) bool {
76+
return r.Configs[i].FilePath < r.Configs[j].FilePath
77+
})
7378
}
7479

7580
func (r *AnalysisResult) Merge(new *AnalysisResult) {
@@ -97,6 +102,10 @@ func (r *AnalysisResult) Merge(new *AnalysisResult) {
97102
if len(new.Applications) > 0 {
98103
r.Applications = append(r.Applications, new.Applications...)
99104
}
105+
106+
if len(new.Configs) > 0 {
107+
r.Configs = append(r.Configs, new.Configs...)
108+
}
100109
}
101110

102111
func AnalyzeFile(wg *sync.WaitGroup, result *AnalysisResult, filePath string, info os.FileInfo, opener Opener,

analyzer/config/const.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package config
2+
3+
const (
4+
YAML = "yaml"
5+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
default: &default
2+
line: single line
3+
4+
john: &J
5+
john_name: john
6+
fred: &F
7+
fred_name: fred
8+
9+
main:
10+
<<: *default
11+
name:
12+
<<: [*J, *F]
13+
comment: |
14+
multi
15+
line
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
apiVersion": foo: bar
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
circular: &circular
2+
name:
3+
<<: *circular
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: hello-kubernetes
5+
spec:
6+
replicas: 3
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: hello-kubernetes
5+
spec:
6+
replicas: 3
7+
8+
---
9+
10+
apiVersion: v1
11+
kind: Service
12+
metadata:
13+
name: hello-kubernetes
14+
spec:
15+
ports:
16+
- protocol: TCP
17+
port: 80
18+
targetPort: 8080

analyzer/config/yaml/yaml.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package yaml
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/open-policy-agent/conftest/parser/yaml"
8+
"golang.org/x/xerrors"
9+
10+
"github.com/aquasecurity/fanal/analyzer"
11+
"github.com/aquasecurity/fanal/analyzer/config"
12+
"github.com/aquasecurity/fanal/types"
13+
)
14+
15+
func init() {
16+
analyzer.RegisterAnalyzer(&yamlConfigAnalyzer{
17+
parser: &yaml.Parser{},
18+
})
19+
}
20+
21+
var (
22+
requiredExts = []string{".yaml", ".yml"}
23+
)
24+
25+
type yamlConfigAnalyzer struct {
26+
parser *yaml.Parser
27+
}
28+
29+
func (a yamlConfigAnalyzer) Analyze(target analyzer.AnalysisTarget) (*analyzer.AnalysisResult, error) {
30+
var parsed interface{}
31+
if err := a.parser.Unmarshal(target.Content, &parsed); err != nil {
32+
return nil, xerrors.Errorf("unable to parse YAML (%s): %w", target.FilePath, err)
33+
}
34+
return &analyzer.AnalysisResult{
35+
Configs: []types.Config{{
36+
Type: config.YAML,
37+
FilePath: target.FilePath,
38+
Content: parsed,
39+
}},
40+
}, nil
41+
}
42+
43+
func (a yamlConfigAnalyzer) Required(filePath string, _ os.FileInfo) bool {
44+
ext := filepath.Ext(filePath)
45+
for _, required := range requiredExts {
46+
if ext == required {
47+
return true
48+
}
49+
}
50+
return false
51+
}
52+
53+
func (a yamlConfigAnalyzer) Type() analyzer.Type {
54+
return analyzer.TypeYaml
55+
}

0 commit comments

Comments
 (0)