Skip to content

Commit c1cf7a6

Browse files
committed
Merge branch 'main' of github.com:advanced-security/policy-as-code into ghastoolkit-updates
2 parents 93fada3 + a1525a1 commit c1cf7a6

File tree

8 files changed

+73
-40
lines changed

8 files changed

+73
-40
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21-
python-version: ["3.9", "3.10", "3.11"]
21+
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
2222

2323
steps:
2424
- uses: actions/checkout@v4

.release.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
name: "policy-as-code"
2-
version: "2.8.0"
2+
repository: "advanced-security/policy-as-code"
3+
version: "2.10.1"
4+
5+
ecosystems:
6+
- Python
37

48
locations:
59
- name: "Update Docs"
610
paths:
711
- "*.md"
12+
- "docs/*.md"
813
patterns:
9-
- 'advanced-security/policy-as-code@v([0-9]\.[0-9]\.[0-9])'
10-
- '--branch "v([0-9]\.[0-9]\.[0-9])"'
11-
14+
- "{repository}@v{version}"
15+
- '--branch "v{version}"'

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Here is how you can quickly setup policy-as-code.
4545
```yaml
4646
# Policy as Code
4747
- name: Advance Security Policy as Code
48-
uses: advanced-security/policy-as-code@v2.8.0
48+
uses: advanced-security/policy-as-code@v2.10.1
4949
```
5050
5151
> [!WARNING]
@@ -61,15 +61,15 @@ The Policy as Code project is a self-contained Python based CLI tool.
6161
**Bash / Zsh:**
6262

6363
```bash
64-
git clone --branch "v2.8.0" https://github.com/advanced-security/policy-as-code.git && cd ./policy-as-code
64+
git clone --branch "v2.10.1" https://github.com/advanced-security/policy-as-code.git && cd ./policy-as-code
6565
6666
./policy-as-code --help
6767
```
6868

6969
**Powershell:**
7070

7171
```Powershell
72-
git clone --branch "v2.8.0" https://github.com/advanced-security/policy-as-code.git
72+
git clone --branch "v2.10.1" https://github.com/advanced-security/policy-as-code.git
7373
cd policy-as-code
7474
7575
.\policy-as-code.ps1 --help
@@ -84,23 +84,35 @@ For Policy as Code to work correctly, you need to have the following permissions
8484

8585
- [required] Repository Permissions
8686
- [`security_events: read`][permissions]
87-
- [Dependabot Alerts][permissions-dependabot]
8887
- [Code Scanning][permissions-codescanning]
89-
- [Secret Scanning][permissions-secretscanning]
9088
- [`content: read`][permissions]
9189
- [Dependency Graph][permissions-dependencygraph] / [Dependency Licenses][permissions-dependencygraph]
9290
- [`pull-requests: write`][permissions]
9391
- Policy as Code Pull Request Summary
92+
- ["Secret scanning alerts" repository permissions (read)][permissions-secretscanning]
93+
- ⚠️ GitHub App or PAT only, not Actions Token
94+
- ["Dependabot alerts" repository permissions (read)][permissions-dependabot]
95+
- ⚠️ GitHub App or PAT only, not Actions Token
9496
- [optional] Policy Repository
9597
- `content: read` to be able to clone external sources of the policies
9698

99+
> [!WARNING]
100+
> Secret Scanning and Dependabot Alerts results cannot be accessed using the Actions Token, use a GitHub App
101+
102+
**GitHub App:**
103+
104+
- Contents
105+
- [Code scanning alerts][permissions-codescanning]
106+
- [Dependabot alerts][permissions-dependabot]
107+
- [Secret scanning alerts][permissions-secretscanning]
108+
97109
**[Action Permissions Example][permissions]:**
98110

99111
```yaml
100112
# workflow or job level
101113
permissions:
102-
content: read
103-
security_events: read
114+
contents: read
115+
security-events: read
104116
# pull request summaries
105117
pull-requests: write
106118
```
@@ -128,7 +140,7 @@ Here is an example of using a simple yet cross-organization using Policy as Code
128140
```yaml
129141
# Compliance
130142
- name: Advance Security Policy as Code
131-
uses: advanced-security/policy-as-code@v2.8.0
143+
uses: advanced-security/policy-as-code@2.10.0
132144
with:
133145
# The owner/repo of where the policy is stored
134146
policy: GeekMasher/security-queries

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ inputs:
3535
description: Policy as Code branch
3636
default: main
3737

38+
retries:
39+
description: Number of times to retry the action
40+
default: 240 # 1 hours worth of retries
41+
3842
argvs:
3943
description: "Additional Arguments"
4044

@@ -55,4 +59,5 @@ runs:
5559
--github-policy "${{ inputs.policy }}" \
5660
--github-policy-path "${{ inputs.policy-path }}" \
5761
--github-policy-branch "${{ inputs.policy-branch }}" \
62+
--retry-count "${{ inputs.retries }}" \
5863
${{ inputs.argvs }}

examples/workflows/appAuth.yml

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
1-
name: Licensing Compliance
1+
name: Policy as Code
22

33
on:
44
push:
5-
branches: [master, main]
5+
branches: [ master, main ]
66
pull_request:
7-
branches: [master, main]
7+
branches: [ master, main ]
88
workflow_dispatch:
99

1010
jobs:
11-
licensing:
11+
policy-as-code:
1212
runs-on: ubuntu-latest
1313

1414
steps:
15-
- uses: tibdex/github-app-token@v1
16-
id: get-token
15+
- uses: actions/create-github-app-token@v1
16+
id: app-token
1717
with:
18-
private_key: ${{ secrets.GIT_APP_PRIVATE_KEY }}
19-
app_id: ${{ secrets.GIT_APP_ID }}
18+
app-id: ${{ secrets.GIT_APP_ID }}
19+
private-key: ${{ secrets.GIT_APP_PRIVATE_KEY }}
2020

2121
- name: Security Compliance Action
2222
uses: advanced-security/policy-as-code@main
2323
with:
24-
policy: KPMG-UK/security-compliance
24+
# Which repository the policy is stored
25+
policy: advanced-security/security
2526
# The local (within the workspace) or repository
26-
policy-path: policies/kpmg-uk.yml
27-
# The branch you want to target
27+
policy-path: policies/production.yml
28+
# [optional] The branch you want to target
2829
policy-branch: main
29-
30-
# The branch you want to target using `policy` argument
31-
# policy-branch: main
32-
3330
# GitHub Personal Access Token to access the GitHub API.
3431
# Secret Scanning and Dependabot do not allow their resources to be
3532
# exposed to Actions so this might need to be set using a token that has
3633
# the ability to access the resources
37-
token: ${{ steps.get-token.outputs.token }}
34+
token: ${{ steps.app-token.outputs.token }}
35+
# [optional]: What action to take if the policy requirements are broken
3836
action: continue
37+
# This is needed to specify that the token being used is a GitHub App token
3938
argvs: "--is-github-app-token"

ghascompliance/__main__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
parser = argparse.ArgumentParser(tool_name)
2525

2626
parser.add_argument(
27-
"--debug", action="store_true", default=bool(os.environ.get("DEBUG"))
27+
"--debug",
28+
action="store_true",
29+
default=bool(os.environ.get("RUNNER_DEBUG", os.environ.get("DEBUG", 0))),
2830
)
2931
parser.add_argument("--disable-caching", action="store_false")
3032
parser.add_argument("--disable-code-scanning", action="store_true")
@@ -50,6 +52,8 @@
5052
"--github-policy-path",
5153
default=os.path.join(HERE, "defaults", "policy.yml"),
5254
)
55+
github_arguments.add_argument("--retry-count", type=int, default=240)
56+
github_arguments.add_argument("--retry-sleep", type=int, default=15)
5357

5458
thresholds = parser.add_argument_group("Thresholds")
5559
thresholds.add_argument(
@@ -73,6 +77,8 @@
7377
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
7478
)
7579
Octokit.setLevel(logging.DEBUG if arguments.debug else logging.INFO)
80+
ghastoolkit_logger = logging.getLogger("ghastoolkit")
81+
ghastoolkit_logger.setLevel(logging.DEBUG if arguments.debug else logging.INFO)
7682

7783
if arguments.debug:
7884
Octokit.debug("Debugging enabled")
@@ -186,6 +192,8 @@
186192
display=arguments.display,
187193
results_path=results,
188194
caching=arguments.disable_caching,
195+
retry_count=arguments.retry_count,
196+
retry_sleep=arguments.retry_sleep,
189197
)
190198

191199
errors = 0
@@ -213,7 +221,7 @@
213221
Summary.addLine(Summary.formatItalics(str(err)))
214222

215223
except Exception as err:
216-
Octokit.error("Unknown Exception was hit, please repo this to " + __url__)
224+
Octokit.error("Unknown Exception was hit, please report this to " + __url__)
217225
Octokit.error(str(err))
218226

219227
errors += 1 # add to error count

ghascompliance/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
__version__ = "2.8.0"
2+
__version__ = "2.10.1"
33

44
__title__ = "GitHub Advanced Security Policy as Code"
55
__name__ = "ghascompliance"

ghascompliance/checks.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def __init__(
3131
debugging: bool = False,
3232
results_path: str = ".compliance",
3333
caching: bool = True,
34+
retry_count: int = 240,
35+
retry_sleep: int = 15,
3436
):
3537
self.policy = policy
3638

@@ -39,6 +41,9 @@ def __init__(
3941
self.results = results_path
4042

4143
self.caching = caching
44+
# Retry count and sleep for Code Scanning
45+
self.retry_count = retry_count
46+
self.retry_sleep = retry_sleep
4247

4348
os.makedirs(self.results, exist_ok=True)
4449

@@ -77,14 +82,20 @@ def checkCodeScanning(self):
7782
]
7883
code_scanning_violations = []
7984

80-
codescanning = CodeScanning()
85+
# Code Scanning + Retries for large repos (e.g. 240 * 15 = 60 minutes)
86+
codescanning = CodeScanning(
87+
retry_count=self.retry_count, retry_sleep=self.retry_sleep
88+
)
8189

8290
if not self.policy.checkTechnologyActive("codescanning"):
8391
Octokit.info("Code Scanning is not active in the policy")
8492
return 0
8593

8694
if GitHub.repository.isInPullRequest():
8795
Octokit.info("Code Scanning Alerts from Pull Request (alert diff)")
96+
Octokit.info(
97+
f"Code Scanning retries enabled :: x{self.retry_count}/{self.retry_sleep}s"
98+
)
8899
pr_base = (
89100
GitHub.repository.getPullRequestInfo().get("base", {}).get("ref", "")
90101
)
@@ -201,13 +212,7 @@ def checkDependabot(self):
201212

202213
else:
203214
# Alerts
204-
try:
205-
alerts = dependabot.getAlerts("open")
206-
except Exception as err:
207-
Octokit.warning(f"Unable to get Dependabot alerts :: {err}")
208-
Octokit.warning("Trying GraphQL API")
209-
alerts = dependabot.getAlertsGraphQL()
210-
215+
alerts = dependabot.getAlerts("open")
211216
# Dependencies
212217
dependencies = depgraph.getDependencies()
213218

0 commit comments

Comments
 (0)