/
branch.sp
559 lines (545 loc) · 19.4 KB
/
branch.sp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
query "default_branch_all_build_steps_as_code" {
sql = <<-EOQ
with repositories as (
select
name_with_owner,
url
from
github_my_repository
order by
name_with_owner
),
pipelines as (
select
name,
repository_full_name,
pipeline
from
github_workflow
where
repository_full_name in (select name_with_owner from repositories)
),
build_jobs as (
select distinct
p.repository_full_name
from
pipelines as p,
jsonb_array_elements(pipeline -> 'jobs') as j
where
(j -> 'metadata' -> 'build')::bool
)
select distinct
-- Required Columns
r.url as resource,
case
when j.repository_full_name is null then 'alarm'
else 'ok'
end as status,
case
when j.repository_full_name is null then 'No build steps are defined as code.'
else 'All build steps are defined as code.'
end as reason,
-- Additional Dimensions
r.name_with_owner
from
repositories as r
left join build_jobs as j on r.name_with_owner = j.repository_full_name;
EOQ
}
query "default_branch_blocks_force_push" {
sql = <<-EOQ
select
-- Required Columns
url as resource,
case
when (default_branch_ref -> 'branch_protection_rule') is null then 'info'
when (default_branch_ref -> 'branch_protection_rule' ->> 'allows_force_pushes') = 'false' then 'ok'
else 'alarm'
end as status,
name_with_owner || ' default branch ' || (default_branch_ref ->> 'name') ||
case
when (default_branch_ref -> 'branch_protection_rule' ->> 'allows_force_pushes') = 'false' then ' prevents force push.'
when (default_branch_ref -> 'branch_protection_rule' ->> 'allows_force_pushes') = 'true' then ' allows force push.'
-- If not false or true, then null, which means no branch protection rule exists
else ' branch protection rule unknown.'
end as reason,
-- Additional Dimensions
name_with_owner
from
github_my_repository;
EOQ
}
query "default_branch_code_change_review_dismissal_restrictions" {
sql = <<-EOQ
select
-- Required Columns
url as resource,
case
when (default_branch_ref -> 'branch_protection_rule') is null then 'info'
when (default_branch_ref -> 'branch_protection_rule' ->> 'restricts_review_dismissals')::bool = true then 'ok'
else 'alarm'
end as status,
name_with_owner || ' default branch ' || (default_branch_ref ->> 'name') ||
case
when (default_branch_ref -> 'branch_protection_rule') is null then ' branch protection rule unknown.'
when (default_branch_ref -> 'branch_protection_rule' ->> 'restricts_review_dismissals')::bool = true then ' has code change review dismissal restrictions.'
else ' does not have code change review dismissal restrictions.'
end as reason,
-- Additional Dimensions
name_with_owner
from
github_my_repository;
EOQ
}
query "default_branch_must_dismiss_stale_approvals" {
sql = <<-EOQ
select
-- Required Columns
url as resource,
case
when (default_branch_ref -> 'branch_protection_rule') is null then 'info'
when (default_branch_ref -> 'branch_protection_rule' ->> 'dismisses_stale_reviews') = 'true' then 'ok'
else 'alarm'
end as status,
name_with_owner || ' default branch ' || (default_branch_ref ->> 'name') ||
case
when (default_branch_ref -> 'branch_protection_rule') is null then ' branch protection rule unknown.'
when (default_branch_ref -> 'branch_protection_rule' ->> 'dismisses_stale_reviews') = 'true' then ' stale reviews are dismissed.'
else ' stale reviews are not dismissed.'
end as reason,
-- Additional Dimensions
name_with_owner
from
github_my_repository;
EOQ
}
query "default_branch_pipeline_locks_external_dependencies_for_build_process" {
sql = <<-EOQ
with repositories as (
select
name_with_owner,
url
from
github_my_repository
order by
name_with_owner
),
pipelines as (
select
name,
repository_full_name,
pipeline
from
github_workflow
where
repository_full_name in (select name_with_owner from repositories)
),
unpinned_task_count as (
select
count(*) filter (where step ->> 'type' = 'task' and (step -> 'task' ->> 'version_type')::text != 'commit' ) as unpinned_task_count,
count(*) filter (where step ->> 'type' = 'task' and (step -> 'task' ->> 'version_type')::text = 'commit' ) as pinned_task_count,
p.repository_full_name
from
pipelines as p,
jsonb_array_elements(pipeline -> 'jobs') as job,
jsonb_array_elements(job -> 'steps') as step
group by
repository_full_name
)
select
-- Required Columns
r.url as resource,
case
when u.unpinned_task_count > 0 then 'alarm'
else 'ok'
end as status,
case
when u.unpinned_task_count > 0 then unpinned_task_count::text || ' task(s) are not pinned.'
when u.repository_full_name is null then 'No build task(s) in the repository.'
else 'All task(s) are pinned.'
end as reason,
-- Additional Dimensions
r.name_with_owner
from
repositories as r
left join unpinned_task_count as u on r.name_with_owner = u.repository_full_name;
EOQ
}
query "default_branch_pipeline_must_have_jobs_with_sbom_generation" {
sql = <<-EOQ
with repositories as (
select
name_with_owner,
url
from
github_my_repository
order by
name_with_owner
),
pipelines as (
select
name,
repository_full_name,
pipeline
from
github_workflow
where
repository_full_name in (select name_with_owner from repositories)
),
build_jobs_sbom_details as (
select
p.repository_full_name,
count(*) filter (
where
(
(step ->> 'type' = 'task'
and (step -> 'task' ->> 'name')::text in (
'argonsecurity/actions/generate-manifest',
'anchore/sbom-action',
'CycloneDX/gh-\w+-generate-sbom'
)
)
or (step ->> 'type' = 'shell'
and ((step -> 'shell' ->> 'script')::text like glob('billy generate') or
(step -> 'shell' ->> 'script')::text like glob('trivy sbom') or
(step -> 'shell' ->> 'script')::text like glob('trivy .* --format cyclonedx') or
(step -> 'shell' ->> 'script')::text like glob('syft .*') or
(step -> 'shell' ->> 'script')::text like glob('spdx-sbom-generator') or
(step -> 'shell' ->> 'script')::text like glob('cyclonedx-\w+') or
(step -> 'shell' ->> 'script')::text like glob('jake sbom')
)
)
)
) as jobs_with_sbom,
count(*) filter (
where
(job -> 'metadata' -> 'build')::bool
and (
not (step ->> 'type' = 'task'
and (step -> 'task' ->> 'name')::text in (
'argonsecurity/actions/generate-manifest',
'anchore/sbom-action',
'CycloneDX/gh-\w+-generate-sbom'
)
)
or not (step ->> 'type' = 'shell'
and ((step -> 'shell' ->> 'script')::text like glob('billy generate') or
(step -> 'shell' ->> 'script')::text like glob('trivy sbom') or
(step -> 'shell' ->> 'script')::text like glob('trivy .* --format cyclonedx') or
(step -> 'shell' ->> 'script')::text like glob('syft .*') or
(step -> 'shell' ->> 'script')::text like glob('spdx-sbom-generator') or
(step -> 'shell' ->> 'script')::text like glob('cyclonedx-\w+') or
(step -> 'shell' ->> 'script')::text like glob('jake sbom')
)
)
)
) as jobs_without_sbom
from
pipelines as p,
jsonb_array_elements(pipeline -> 'jobs') as job,
jsonb_array_elements(job -> 'steps') as step
group by
p.repository_full_name,
pipeline
),
pipeline_with_sbom_job_details as (
select
repository_full_name,
count(*) filter (where jobs_with_sbom > 0) as pipeline_with_sbom_jobs,
count(*) filter (where jobs_without_sbom > 0) as pipeline_without_sbom_jobs
from
build_jobs_sbom_details
group by
repository_full_name
)
select
-- Required Columns
r.url as resource,
case
when ps.pipeline_without_sbom_jobs > 0 then 'alarm'
when ps.repository_full_name is null then 'info'
else 'ok'
end as status,
case
when ps.pipeline_without_sbom_jobs > 0 then ps.pipeline_without_sbom_jobs::text || ' pipeline(s) contain a build job without SBOM generation.'
when ps.repository_full_name is null then 'No pipelines available in the repository.'
else 'All pipeline(s) contain a build job with SBOM generation.'
end as reason,
-- Additional Dimensions
r.name_with_owner
from
repositories as r
left join pipeline_with_sbom_job_details as ps on r.name_with_owner = ps.repository_full_name;
EOQ
}
query "default_branch_pipelines_scan_for_vulnerabilities" {
sql = <<-EOQ
with repositories as (
select
name_with_owner,
url
from
github_my_repository
order by
name_with_owner
),
pipelines as (
select
name,
repository_full_name,
pipeline
from
github_workflow
where
repository_full_name in (select name_with_owner from repositories)
),
vulnerability_task_repos as (
select distinct
p.repository_full_name
from
pipelines as p,
jsonb_array_elements(pipeline -> 'jobs') as job,
jsonb_array_elements(job -> 'steps') as step
where
step ->> 'type' = 'task'
and (step -> 'task' ->> 'name')::text in (
'argonsecurity/scanner-action',
'aquasecurity/trivy-action'
)
)
select
-- Required Columns
r.url as resource,
case
when v.repository_full_name is null then 'alarm'
else 'ok'
end as status,
case
when v.repository_full_name is null then 'Automated vulnerabilities scanning is not set for pipelines.'
else 'Automated vulnerabilities scanning is set for pipelines.'
end as reason,
-- Additional Dimensions
r.name_with_owner
from
repositories as r
left join vulnerability_task_repos as v on r.name_with_owner = v.repository_full_name;
EOQ
}
query "default_branch_pipelines_scanners_set_to_prevent_sensitive_data" {
sql = <<-EOQ
with repositories as (
select
name_with_owner,
url
from
github_my_repository
order by
name_with_owner
),
pipelines as (
select
name,
repository_full_name,
pipeline
from
github_workflow
where
repository_full_name in (select name_with_owner from repositories)
),
vulnerability_scanner_repos as (
select distinct
p.repository_full_name
from
pipelines as p,
jsonb_array_elements(pipeline -> 'jobs') as job,
jsonb_array_elements(job -> 'steps') as step
where
(step ->> 'type' = 'task'
and (step -> 'task' ->> 'name')::text in (
'argonsecurity/scanner-action',
'aquasecurity/trivy-action',
'zricethezav/gitleaks-action',
'ShiftLeftSecurity/scan-action'
)) or
(step ->> 'type' = 'shell'
and ((step -> 'shell' ->> 'script')::text like glob('spectral.* scan') or
(step -> 'shell' ->> 'script')::text like glob('git secrets --scan') or
(step -> 'shell' ->> 'script')::text like glob('whispers') or
(step -> 'shell' ->> 'script')::text like glob('docker run.* abhartiya/tools_gitallsecrets') or
(step -> 'shell' ->> 'script')::text like glob('detect-secrets.* scan')
))
)
select
-- Required Columns
r.url as resource,
case
when v.repository_full_name is null then 'alarm'
else 'ok'
end as status,
case
when v.repository_full_name is null then 'Scanners are not set to identify and prevent sensitive data in pipeline files.'
else 'Scanners are set to identify and prevent sensitive data in pipeline files.'
end as reason,
-- Additional Dimensions
r.name_with_owner
from
repositories as r
left join vulnerability_scanner_repos as v on r.name_with_owner = v.repository_full_name;
EOQ
}
query "default_branch_protections_apply_to_admins" {
sql = <<-EOQ
select
-- Required Columns
url as resource,
case
when (default_branch_ref -> 'branch_protection_rule') is null then 'info'
when (default_branch_ref -> 'branch_protection_rule' ->> 'is_admin_enforced') = 'true' then 'ok'
else 'alarm'
end as status,
name_with_owner || ' default branch ' || (default_branch_ref ->> 'name') ||
case
when (default_branch_ref -> 'branch_protection_rule' ->> 'is_admin_enforced') = 'true' then ' protections apply to admins.'
when (default_branch_ref -> 'branch_protection_rule' ->> 'is_admin_enforced') = 'false' then ' protections do not apply to admins.'
-- If not false or true, then null, which means no branch protection rule exists
else ' branch protection rule unknown.'
end as reason,
-- Additional Dimensions
name_with_owner
from
github_my_repository;
EOQ
}
query "default_branch_requires_2_pull_request_reviews" {
sql = <<-EOQ
select
-- Required Columns
url as resource,
case
when (default_branch_ref -> 'branch_protection_rule') is null then 'info'
when (default_branch_ref -> 'branch_protection_rule') is not null and (default_branch_ref -> 'branch_protection_rule' ->> 'required_approving_review_count')::integer >= 2 then 'ok'
else 'alarm'
end as status,
name_with_owner || ' default branch ' || (default_branch_ref ->> 'name') ||
case
when (default_branch_ref -> 'branch_protection_rule') is null then ' branch protection rule unknown.'
when (default_branch_ref -> 'branch_protection_rule' ->> 'required_approving_review_count')::integer >= 2 then ' has at least two pull request reviews.'
else ' does not have at least two pull request reviews.'
end as reason,
-- Additional Dimensions
name_with_owner
from
github_my_repository;
EOQ
}
query "default_branch_requires_code_owners_review" {
sql = <<-EOQ
select
-- Required Columns
url as resource,
case
when (default_branch_ref -> 'branch_protection_rule') is null then 'info'
when (default_branch_ref -> 'branch_protection_rule' ->> 'requires_code_owner_reviews')::bool is true then 'ok'
else 'alarm'
end as status,
name_with_owner || ' default branch ' || (default_branch_ref ->> 'name') ||
case
when (default_branch_ref -> 'branch_protection_rule') is null then ' branch protection rule unknown.'
when (default_branch_ref -> 'branch_protection_rule' ->> 'requires_code_owner_reviews')::bool is true then ' enforces code owner''s review.'
else ' does not enforce code owner''s review.'
end as reason,
-- Additional Dimensions
name_with_owner
from
github_my_repository;
EOQ
}
query "default_branch_requires_signed_commits" {
sql = <<-EOQ
select
-- Required Columns
url as resource,
case
when (default_branch_ref -> 'branch_protection_rule') is null then 'info'
when (default_branch_ref -> 'branch_protection_rule' ->> 'requires_commit_signatures')::bool = true then 'ok'
else 'alarm'
end as status,
name_with_owner || ' default branch ' || (default_branch_ref ->> 'name') ||
case
when (default_branch_ref -> 'branch_protection_rule') is null then ' branch protection rule unknown.'
when (default_branch_ref -> 'branch_protection_rule' ->> 'requires_commit_signatures')::bool = true then ' requires signed commits before merge.'
else ' does not require signed commits before merge.'
end as reason,
-- Additional Dimensions
name_with_owner
from
github_my_repository;
EOQ
}
query "default_branch_requires_status_checks" {
sql = <<-EOQ
select
-- Required Columns
url as resource,
case
when (default_branch_ref -> 'branch_protection_rule') is null then 'info'
when (default_branch_ref -> 'branch_protection_rule' ->> 'requires_status_checks')::bool = true then 'ok'
else 'alarm'
end as status,
name_with_owner || ' default branch ' || (default_branch_ref ->> 'name') ||
case
when (default_branch_ref -> 'branch_protection_rule') is not null then ' branch protection status unknown.'
when (default_branch_ref -> 'branch_protection_rule' ->> 'requires_status_checks')::bool = true then ' requires status checks.'
else ' does not require status checks.'
end as reason,
-- Additional Dimensions
name_with_owner
from
github_my_repository;
EOQ
}
query "default_branch_restrict_push_and_merge" {
sql = <<-EOQ
select
-- Required Columns
url as resource,
case
when (default_branch_ref -> 'branch_protection_rule') is null then 'info'
when (default_branch_ref -> 'branch_protection_rule' ->> 'restricts_pushes')::bool = true then 'ok'
else 'alarm'
end as status,
name_with_owner || ' default branch ' || (default_branch_ref ->> 'name') ||
case
when (default_branch_ref -> 'branch_protection_rule') is not null then ' branch protection rule unknown.'
when (default_branch_ref -> 'branch_protection_rule' ->> 'restricts_pushes')::bool = true then ' has restrictions for code push and merge.'
else ' has no restrictions for code push and merge.'
end as reason,
-- Additional Dimensions
name_with_owner
from
github_my_repository;
EOQ
}
query "default_branch_setting_block_deletion" {
sql = <<-EOQ
select
-- Required Columns
url as resource,
case
when (default_branch_ref -> 'branch_protection_rule') is null then 'info'
when (default_branch_ref -> 'branch_protection_rule' ->> 'allows_deletions') = 'false' then 'ok'
else 'alarm'
end as status,
name_with_owner || ' default branch ' || (default_branch_ref ->> 'name') ||
case
when (default_branch_ref -> 'branch_protection_rule' ->> 'allows_deletions') = 'false' then ' prevents deletions.'
when (default_branch_ref -> 'branch_protection_rule' ->> 'allows_deletions') = 'true' then ' allows deletions.'
-- If not false or true, then null, which means no branch protection rule exists
else ' branch protection rule unknown.'
end as reason,
-- Additional Dimensions
name_with_owner
from
github_my_repository;
EOQ
}