Skip to content

Commit e259b09

Browse files
authored
Adjustments to GitHub Actions Parsing (#192)
* added omitempty to models for more flexible in reusing as library
1 parent fc5f744 commit e259b09

File tree

5 files changed

+52
-43
lines changed

5 files changed

+52
-43
lines changed

analyze/analyze.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func (a *Analyzer) generatePackageInsights(ctx context.Context, tempDir string,
287287

288288
pkg := &models.PackageInsights{
289289
Purl: purl.String(),
290-
LastCommitedAt: commitDate.String(),
290+
LastCommitedAt: commitDate.Format(time.RFC3339),
291291
SourceGitCommitSha: commitSha,
292292
SourceScmType: repo.GetProviderName(),
293293
SourceGitRepo: repo.GetRepoIdentifier(),

models/github_actions.go

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ type StringList []string
6464

6565
type GithubActionsInput struct {
6666
Name string `json:"name"`
67-
Description string `json:"description"`
67+
Description string `json:"description,omitempty"`
6868
Required bool `json:"required"`
6969
Type string `json:"type"`
7070
}
7171

7272
type GithubActionsOutput struct {
7373
Name string `json:"name"`
74-
Description string `json:"description"`
74+
Description string `json:"description,omitempty"`
7575
Value string `json:"value"`
7676
}
7777

@@ -81,19 +81,19 @@ type GithubActionsEnv struct {
8181
}
8282

8383
type GithubActionsStep struct {
84-
ID string `json:"id"`
85-
Name string `json:"name"`
86-
If string `json:"if"`
87-
Env GithubActionsEnvs `json:"env"`
88-
Uses string `json:"uses"`
89-
Shell string `json:"shell"`
90-
Run string `json:"run" yaml:"run"`
91-
WorkingDirectory string `json:"working_directory" yaml:"working-directory"`
92-
With GithubActionsWith `json:"with"`
93-
WithRef string `json:"with_ref" yaml:"-"`
94-
WithScript string `json:"with_script" yaml:"-"`
84+
ID string `json:"id,omitempty"`
85+
Name string `json:"name,omitempty"`
86+
If string `json:"if,omitempty"`
87+
Env GithubActionsEnvs `json:"env,omitempty"`
88+
Uses string `json:"uses,omitempty"`
89+
Shell string `json:"shell,omitempty"`
90+
Run string `json:"run,omitempty" yaml:"run"`
91+
WorkingDirectory string `json:"working_directory,omitempty" yaml:"working-directory"`
92+
With GithubActionsWith `json:"with,omitempty"`
93+
WithRef string `json:"with_ref,omitempty" yaml:"-"`
94+
WithScript string `json:"with_script,omitempty" yaml:"-"`
9595
Line int `json:"line" yaml:"-"`
96-
Action string `json:"action" yaml:"-"`
96+
Action string `json:"action,omitempty" yaml:"-"`
9797

9898
Lines map[string]int `json:"lines" yaml:"-"`
9999
}
@@ -128,18 +128,18 @@ type GithubActionsPermission struct {
128128

129129
type GithubActionsEvent struct {
130130
Name string `json:"name"`
131-
Types StringList `json:"types"`
132-
Branches StringList `json:"branches"`
133-
BranchesIgnore StringList `json:"branches_ignore"`
134-
Paths StringList `json:"paths"`
135-
PathsIgnore StringList `json:"paths_ignore"`
136-
Tags StringList `json:"tags"`
137-
TagsIgnore StringList `json:"tags_ignore"`
138-
Cron StringList `json:"cron"`
139-
Inputs GithubActionsInputs `json:"inputs"`
140-
Outputs GithubActionsOutputs `json:"outputs"`
141-
Secrets GithubActionsSecrets `json:"secrets"`
142-
Workflows StringList `json:"workflows"`
131+
Types StringList `json:"types,omitempty"`
132+
Branches StringList `json:"branches,omitempty"`
133+
BranchesIgnore StringList `json:"branches_ignore,omitempty"`
134+
Paths StringList `json:"paths,omitempty"`
135+
PathsIgnore StringList `json:"paths_ignore,omitempty"`
136+
Tags StringList `json:"tags,omitempty"`
137+
TagsIgnore StringList `json:"tags_ignore,omitempty"`
138+
Cron StringList `json:"cron,omitempty"`
139+
Inputs GithubActionsInputs `json:"inputs,omitempty"`
140+
Outputs GithubActionsOutputs `json:"outputs,omitempty"`
141+
Secrets GithubActionsSecrets `json:"secrets,omitempty"`
142+
Workflows StringList `json:"workflows,omitempty"`
143143
}
144144

145145
type GithubActionsJobContainer struct {
@@ -148,7 +148,7 @@ type GithubActionsJobContainer struct {
148148

149149
type GithubActionsJobEnvironment struct {
150150
Name string `json:"name"`
151-
Url string `json:"url"`
151+
Url string `json:"url,omitempty"`
152152
}
153153

154154
type GithubActionsJobSecret struct {
@@ -158,18 +158,18 @@ type GithubActionsJobSecret struct {
158158

159159
type GithubActionsJob struct {
160160
ID string `json:"id"`
161-
Name string `json:"name"`
162-
Uses string `json:"uses"`
163-
Secrets GithubActionsJobSecrets `json:"secrets"`
164-
With GithubActionsWith `json:"with"`
165-
Permissions GithubActionsPermissions `json:"permissions"`
166-
Needs StringList `json:"needs"`
167-
If string `json:"if"`
161+
Name string `json:"name,omitempty"`
162+
Uses string `json:"uses,omitempty"`
163+
Secrets GithubActionsJobSecrets `json:"secrets,omitempty"`
164+
With GithubActionsWith `json:"with,omitempty"`
165+
Permissions GithubActionsPermissions `json:"permissions,omitempty"`
166+
Needs StringList `json:"needs,omitempty"`
167+
If string `json:"if,omitempty"`
168168
RunsOn GithubActionsJobRunsOn `json:"runs_on" yaml:"runs-on"`
169169
Container GithubActionsJobContainer `json:"container"`
170-
Environment GithubActionsJobEnvironments `json:"environment"`
171-
Outputs GithubActionsEnvs `json:"outputs"`
172-
Env GithubActionsEnvs `json:"env"`
170+
Environment GithubActionsJobEnvironments `json:"environment,omitempty"`
171+
Outputs GithubActionsEnvs `json:"outputs,omitempty"`
172+
Env GithubActionsEnvs `json:"env,omitempty"`
173173
Steps GithubActionsSteps `json:"steps"`
174174
ReferencesSecrets []string `json:"references_secrets" yaml:"-"`
175175
Line int `json:"line" yaml:"-"`
@@ -181,8 +181,8 @@ type GithubActionsWorkflow struct {
181181
Path string `json:"path" yaml:"-"`
182182
Name string `json:"name"`
183183
Events GithubActionsEvents `json:"events" yaml:"on"`
184-
Permissions GithubActionsPermissions `json:"permissions"`
185-
Env GithubActionsEnvs `json:"env"`
184+
Permissions GithubActionsPermissions `json:"permissions,omitempty"`
185+
Env GithubActionsEnvs `json:"env,omitempty"`
186186
Jobs GithubActionsJobs `json:"jobs"`
187187
}
188188

models/package_insights.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ type PackageInsights struct {
1111

1212
Purl string `json:"purl"`
1313

14-
AnalysisResult string `json:"analysis_result"`
15-
AnalysisDetails string `json:"analysis_details"`
1614
PackageEcosystem string `json:"package_ecosystem"`
1715
PackageName string `json:"package_name"`
1816
PackageNamespace string `json:"package_namespace"`

opa/rego/rules/default_permissions_on_risky_events.rego

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,14 @@ results contains poutine.finding(rule, pkg.purl, {"path": workflow.path}) if {
3030
utils.empty(workflow.permissions)
3131
utils.empty(job.permissions)
3232
}
33+
34+
results contains poutine.finding(rule, pkg.purl, {"path": workflow.path}) if {
35+
pkg := input.packages[_]
36+
workflow = pkg.github_actions_workflows[_]
37+
job := workflow.jobs[_]
38+
39+
utils.filter_workflow_events(workflow, github.events)
40+
41+
not workflow.permissions
42+
not job.permissions
43+
}

providers/gitops/gitops.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (g *GitClient) Clone(ctx context.Context, clonePath string, url string, tok
7878

7979
for _, c := range commands {
8080
if _, err := g.Command.Run(ctx, c.cmd, c.args, clonePath); err != nil {
81-
if strings.Contains(err.Error(), token) {
81+
if token != "" && strings.Contains(err.Error(), token) {
8282
return errors.New(strings.ReplaceAll(err.Error(), token, "REDACTED"))
8383
}
8484

0 commit comments

Comments
 (0)