This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
17-ensure-code-quality.go
177 lines (146 loc) · 4.08 KB
/
17-ensure-code-quality.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package defaultRules
import (
"encoding/json"
githubConnector "github.com/allero-io/allero/pkg/connectors/github"
gitlabConnector "github.com/allero-io/allero/pkg/connectors/gitlab"
)
func EnsureCodeQualityScanner(githubData map[string]*githubConnector.GithubOwner, gitlabData map[string]*gitlabConnector.GitlabGroup) ([]*SchemaError, error) {
schemaErrors := make([]*SchemaError, 0)
var err error
if githubData != nil {
schemaErrors, err = githubErrorsRule17(githubData)
if err != nil {
return nil, err
}
}
if gitlabData != nil {
schemaErrors, err = gitlabErrorsRule17(gitlabData)
if err != nil {
return nil, err
}
}
return schemaErrors, nil
}
func githubErrorsRule17(githubData map[string]*githubConnector.GithubOwner) ([]*SchemaError, error) {
schemaErrors := make([]*SchemaError, 0)
usesRegexExpressions := []string{
".*paambaati/codeclimate-action@.*",
".*kitabisa/sonarqube-action@.*",
".*sonarsource/sonarcloud-github-action@.*",
}
runRegexExpressions := []string{
".*docker .* run .*codeclimate/codeclimate analyze.*",
}
for _, owner := range githubData {
for _, repo := range owner.Repositories {
foundCodeQualityScanner := false
for _, workflow := range repo.GithubActionsWorkflows {
content := workflow.Content
contentByteArr, err := json.Marshal(content)
if err != nil {
return nil, err
}
var workflowObj GithubWorkflow
err = json.Unmarshal(contentByteArr, &workflowObj)
if err != nil {
return nil, err
}
for _, job := range workflowObj.Jobs {
for _, step := range job.Steps {
for _, regexExpression := range usesRegexExpressions {
if matchRegex(regexExpression, step.Uses) {
foundCodeQualityScanner = true
break
}
}
for _, regexExpression := range runRegexExpressions {
if matchRegex(regexExpression, step.Run) {
foundCodeQualityScanner = true
break
}
}
if foundCodeQualityScanner {
break
}
}
if foundCodeQualityScanner {
break
}
}
if foundCodeQualityScanner {
break
}
}
if !foundCodeQualityScanner {
schemaErrors = append(schemaErrors, &SchemaError{
ErrorLevel: 1,
RepositryName: repo.Name,
CiCdPlatform: "",
OwnerName: owner.Name,
ScmPlatform: "github",
})
}
}
}
return schemaErrors, nil
}
func gitlabErrorsRule17(gitlabData map[string]*gitlabConnector.GitlabGroup) ([]*SchemaError, error) {
schemaErrors := make([]*SchemaError, 0)
for _, group := range gitlabData {
for _, project := range group.Projects {
foundCodeQualityScanner, err := findCodeQualityScannerRule17(project)
if err != nil {
return nil, err
}
if !foundCodeQualityScanner {
schemaErrors = append(schemaErrors, &SchemaError{
ErrorLevel: 2,
RepositryName: project.Name,
CiCdPlatform: "",
OwnerName: group.Name,
ScmPlatform: "gitlab",
})
}
}
}
return schemaErrors, nil
}
func findCodeQualityScannerRule17(project *gitlabConnector.GitlabProject) (bool, error) {
scriptRegexExpressions := []string{
".*docker .* run .*codeclimate/codeclimate analyze.*",
}
for _, pipeline := range project.GitlabCi {
for _, stage := range pipeline.Content {
stageBytes, err := json.Marshal(stage)
if err != nil {
return false, err
}
var stageWithSingleScript GitlabStageScript
var stageWithScripts GitlabStageScripts
err = json.Unmarshal(stageBytes, &stageWithSingleScript)
if err != nil {
err = json.Unmarshal(stageBytes, &stageWithScripts)
if err != nil {
continue
}
}
if stageWithSingleScript.Script != "" {
for _, regexExpression := range scriptRegexExpressions {
if matchRegex(regexExpression, stageWithSingleScript.Script) {
return true, nil
}
}
}
if stageWithScripts.Scripts != nil {
for _, script := range stageWithScripts.Scripts {
for _, regexExpression := range scriptRegexExpressions {
if matchRegex(regexExpression, script) {
return true, nil
}
}
}
}
}
}
return false, nil
}