Skip to content

Commit 556153c

Browse files
committed
Add csp-security-policies
1 parent 611f797 commit 556153c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+556
-804
lines changed

.buildkite/scripts/generate_notice.py

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
notice_overrides = [
2727
{"name": "github.com/elastic/beats/v7", "licenceType": "Elastic"},
2828
{"name": "github.com/build-security/beats/v7", "licenceType": "Elastic"},
29-
{"name": "github.com/elastic/csp-security-policies", "licenceType": "Elastic"},
3029
{"name": "github.com/golang/glog", "licenceType": "Apache-2.0"},
3130
{"name": "github.com/spdx/tools-golang", "licenceFile": "LICENSE.code", "licenceType": "Apache-2.0"},
3231
]

.github/pull_request_template.md

+6
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ If this PR adds a new feature, please add an example screenshot or data (finding
1919
### Checklist
2020
- [ ] I have added tests that prove my fix is effective or that my feature works
2121
- [ ] I have added the necessary README/documentation (if appropriate)
22+
23+
#### Introducing a new rule?
24+
25+
- [ ] Generate rule metadata using [this script](https://github.com/elastic/cloudbeat/tree/main/security-policies/dev#generate-rules-metadata)
26+
- [ ] Add relevant unit tests
27+
- [ ] Generate relevant rule templates using [this script](https://github.com/elastic/cloudbeat/tree/main/security-policies/dev#generate-rule-templates), and open a PR in [elastic/packages/cloud_security_posture](https://github.com/elastic/integrations/tree/main/packages/cloud_security_posture)

.github/workflows/cloudbeat-ci.yml

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ jobs:
3434
- name: Init Hermit
3535
run: ./bin/hermit env -r >> $GITHUB_ENV
3636

37+
- name: Install poetry
38+
run: |
39+
pip3 install poetry
40+
cd security-policies
41+
poetry install
42+
3743
- name: Pre-commit Hooks
3844
env:
3945
# Skipping golangci-lint as it's tested by golangci/golangci-lint-action

security-policies/.github/workflows/test-coverage.yml .github/workflows/test-opa-coverage.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Test Coverage
1+
name: Test OPA Coverage
22

33
on:
44
push:
@@ -8,15 +8,15 @@ on:
88
jobs:
99
test-coverage:
1010
runs-on: ubuntu-latest
11+
defaults:
12+
run:
13+
working-directory: ./security-policies
1114
steps:
1215
- name: Checkout
13-
uses: actions/checkout@v3
16+
uses: actions/checkout@v4
1417

15-
- name: Download OPA Binary
16-
run: |
17-
curl -L -o opa https://openpolicyagent.org/downloads/latest/opa_linux_amd64
18-
chmod +x opa
19-
./opa -h
18+
- name: Init Hermit
19+
run: ../bin/hermit env -r >> $GITHUB_ENV
2020

2121
- name: OPA test coverage
2222
run: |
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Test OPA Policies
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [ main ]
7+
8+
jobs:
9+
test-rego-policies:
10+
name: Test Rego Policies
11+
runs-on: ubuntu-latest
12+
defaults:
13+
run:
14+
working-directory: ./security-policies
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
19+
- name: Init Hermit
20+
run: ../bin/hermit env -r >> $GITHUB_ENV
21+
22+
- name: OPA format
23+
run: opa fmt ./bundle --fail=true --diff
24+
25+
- name: OPA format list failed files
26+
run: opa fmt ./bundle --list
27+
28+
- name: OPA build
29+
run: opa build -b ./bundle -e ./bundle/compliance
30+
31+
- name: OPA test
32+
run: opa test -b ./bundle -v
33+
34+
- name: OPA check -strict
35+
run: opa check --strict --bundle ./bundle

.mergify.yml

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ queue_rules:
44
- check-success=Cloudbeat-CI / Integration Tests (pull_request)
55
- check-success=Cloudbeat-CI / Build (pull_request)
66
- check-success=UnitTests / Unit Tests (pull_request)
7+
- check-success=Test Rego Policies (pull_request)
8+
- check-success=Update rules status (pull_request)
9+
- check-success=Update rules metadata (pull_request)
710
pull_request_rules:
811
- name: self-assign PRs
912
conditions:

.pre-commit-config.yaml

+62-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repos:
1212
- id: check-json
1313
- id: check-executables-have-shebangs
1414
- id: trailing-whitespace
15-
exclude: cloudbeat.reference.yml
15+
exclude: (cloudbeat.reference.yml|security-policies/bundle/.*)
1616

1717
## Security
1818
- id: detect-private-key
@@ -44,6 +44,8 @@ repos:
4444
"-rn", # Only display messages
4545
"--rcfile=tests/pylintrc", # Link to your config file
4646
]
47+
# TODO: Apply pylint in security-policies
48+
exclude: security-policies.*
4749

4850
- repo: https://github.com/psf/black
4951
rev: 23.9.1
@@ -91,3 +93,62 @@ repos:
9193
language: system
9294
files: .*.go
9395
pass_filenames: false
96+
97+
- repo: local
98+
hooks:
99+
- id: opa-fmt
100+
name: OPA fmt
101+
description: Formats Rego policy using opa fmt
102+
entry: opa fmt
103+
args: [ -w ]
104+
language: system
105+
files: (\.rego)$
106+
107+
- id: opa-check
108+
name: OPA check
109+
description: Check syntax of staged Rego files
110+
entry: opa check
111+
args: [ -S, './security-policies/bundle/compliance' ]
112+
pass_filenames: false
113+
language: system
114+
files: (\.rego)$
115+
116+
- id: opa-test
117+
name: OPA test
118+
description: Runs OPA unit tests on rego source files
119+
entry: opa test
120+
args: [ -b, './security-policies/bundle' ]
121+
pass_filenames: false
122+
language: system
123+
files: (\.rego)$
124+
125+
- id: update-rules-status
126+
name: Update rule status
127+
description: Update rule status in rules_table.md
128+
require_serial: true
129+
entry: poetry run -C security-policies python security-policies/dev/update_rule_status.py
130+
pass_filenames: false
131+
language: system
132+
files: (\.rego)$
133+
134+
- id: update-rule-metadata
135+
name: Update rule metadata
136+
description: Update rule status in rules_table.md
137+
require_serial: true
138+
entry: poetry run -C security-policies python security-policies/dev/generate_rule_metadata.py
139+
pass_filenames: false
140+
language: system
141+
files: (\.rego)$
142+
143+
- repo: https://github.com/igorshubovych/markdownlint-cli
144+
rev: v0.32.2
145+
hooks:
146+
- id: markdownlint
147+
args: [ '--disable',
148+
MD013, # Line length can be ignored for now
149+
MD033, # Allow inline HTML
150+
MD046, # Allow code blocks to be fenced with backticks
151+
MD041, # Allow multiple top level headers
152+
'--' ]
153+
# TODO: Apply makrdownlint in other files
154+
files: security-policies/.*\.(md|markdown)$

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ It is designed to be used as part of the [Cloud Security](https://www.elastic.co
1010

1111
### CSP Security Policies
1212

13-
Cloudbeat uses security policies from the [CSP Security Policies](https://github.com/elastic/csp-security-policies) repository to evaluate cloud resources.
13+
Cloudbeat uses security policies from the [Security Policies](./security-policies) directory to evaluate cloud resources.
1414

1515
## Getting Started
1616

justfile

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ kustomizeVanillaOverlay := "deploy/kustomize/overlays/cloudbeat-vanilla"
44
kustomizeVanillaNoCertOverlay := "deploy/kustomize/overlays/cloudbeat-vanilla-nocert"
55
kustomizeEksOverlay := "deploy/kustomize/overlays/cloudbeat-eks"
66
kustomizeAwsOverlay := "deploy/kustomize/overlays/cloudbeat-aws"
7-
cspPoliciesPkg := "github.com/elastic/csp-security-policies"
87
hermitActivationScript := "bin/activate-hermit"
98

109
# use env var if available

magefile.go

+1-47
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"github.com/elastic/beats/v7/dev-tools/mage/gotool"
3636
"github.com/elastic/e2e-testing/pkg/downloads"
3737
"github.com/go-git/go-git/v5"
38-
"github.com/go-git/go-git/v5/config"
3938
"github.com/go-git/go-git/v5/plumbing"
4039
"github.com/magefile/mage/mg"
4140
"github.com/magefile/mage/sh"
@@ -50,7 +49,6 @@ import (
5049
_ "github.com/elastic/beats/v7/dev-tools/mage/target/unittest"
5150

5251
cloudbeat "github.com/elastic/cloudbeat/scripts/mage"
53-
"github.com/elastic/cloudbeat/version"
5452
)
5553

5654
const (
@@ -350,57 +348,13 @@ func checkoutBranch(wt *git.Worktree, branch string) error {
350348
}
351349

352350
func BuildOpaBundle() (err error) {
353-
owner := "elastic"
354-
repoName := "csp-security-policies"
355-
356351
// Override default SIGINT behaviour which does not allow deferred functions to be called
357352
c := make(chan os.Signal, 1)
358353
signal.Notify(c, os.Interrupt)
359354

360-
cspPoliciesPkgDir, err := os.MkdirTemp("", repoName)
361-
if err != nil {
362-
return err
363-
}
364-
365-
defer func() {
366-
rmErr := os.RemoveAll(cspPoliciesPkgDir)
367-
if rmErr != nil && err == nil {
368-
err = rmErr
369-
}
370-
signal.Stop(c)
371-
}()
372-
373-
repo, err := git.PlainClone(cspPoliciesPkgDir, false, &git.CloneOptions{
374-
URL: fmt.Sprintf("https://github.com/%s/%s.git", owner, repoName),
375-
})
376-
if err != nil {
377-
return err
378-
}
379-
err = repo.Fetch(&git.FetchOptions{
380-
RefSpecs: []config.RefSpec{"refs/*:refs/*", "HEAD:refs/heads/HEAD"},
381-
})
382-
if err != nil {
383-
return err
384-
}
385-
// Check out the provided release tag commit
386-
wt, err := repo.Worktree()
387-
if err != nil {
388-
return err
389-
}
390-
391-
branch := getMajorMinorVersion(version.CloudbeatVersion().Version)
392-
if err := checkoutBranch(wt, branch); err != nil {
393-
fmt.Printf("Fallback from %s to main branch\n", branch)
394-
branch = "main"
395-
if err = checkoutBranch(wt, branch); err != nil {
396-
return err
397-
}
398-
}
399-
400-
if err = sh.Run("bin/opa", "build", "-b", cspPoliciesPkgDir+"/bundle", "-e", cspPoliciesPkgDir+"/bundle/compliance"); err != nil {
355+
if err = sh.Run("bin/opa", "build", "-b", "security-policies/bundle", "-e", "security-policies/bundle/compliance"); err != nil {
401356
return err
402357
}
403358

404-
fmt.Printf("Generated OPA bundle from %s branch at %s\n", branch, cspPoliciesPkgDir)
405359
return nil
406360
}

security-policies/.github/CODEOWNERS

-1
This file was deleted.

security-policies/.github/ISSUE_TEMPLATE/bug_report.md

-40
This file was deleted.

security-policies/.github/ISSUE_TEMPLATE/feature_request.md

-23
This file was deleted.

security-policies/.github/ISSUE_TEMPLATE/issue_template.md

-26
This file was deleted.

security-policies/.github/pull_request_template.md

-26
This file was deleted.

0 commit comments

Comments
 (0)