Skip to content

Commit d7c22ef

Browse files
committed
ci: secure fork PR workflows with environment protection
- Use pull_request_target with secure-build environment for container builds - Add PR validation workflow for immediate feedback without secrets - Require manual approval for forked PRs accessing organization secrets Signed-off-by: Tomas Turek <[email protected]>
1 parent b5dd67a commit d7c22ef

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

.github/workflows/main.yml

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ on:
33
push:
44
branches: [ "main", "release*" ]
55
tags: [ "*" ]
6-
pull_request:
6+
pull_request_target:
77
branches: [ "main", "release*" ]
8+
workflow_dispatch:
9+
inputs:
10+
pr_number:
11+
description: 'PR number to test (optional)'
12+
required: false
13+
type: string
814

915
env:
1016
GO_VERSION: 1.24
@@ -20,12 +26,19 @@ jobs:
2026
build-operator:
2127
name: Build-operator
2228
runs-on: ubuntu-24.04
29+
# Use protected environment for security - requires manual approval for forked PRs
30+
environment:
31+
name: "secure-build"
2332
permissions:
2433
contents: read
2534
packages: write
2635
steps:
2736
- name: Checkout source
2837
uses: actions/checkout@v4
38+
with:
39+
# For pull_request_target, checkout the PR head
40+
# For workflow_dispatch, checkout specified PR or current ref
41+
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || (github.event_name == 'workflow_dispatch' && github.event.inputs.pr_number != '' && format('refs/pull/{0}/head', github.event.inputs.pr_number)) || github.ref }}
2942

3043
- name: Install Go
3144
uses: actions/setup-go@v5
@@ -49,12 +62,16 @@ jobs:
4962
build-bundle:
5063
name: Build-bundle-image
5164
runs-on: ubuntu-24.04
65+
environment:
66+
name: "secure-build"
5267
permissions:
5368
contents: read
5469
packages: write
5570
steps:
5671
- name: Checkout source
5772
uses: actions/checkout@v4
73+
with:
74+
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || (github.event_name == 'workflow_dispatch' && github.event.inputs.pr_number != '' && format('refs/pull/{0}/head', github.event.inputs.pr_number)) || github.ref }}
5875

5976
- name: Log in to GitHub Container Registry
6077
uses: redhat-actions/podman-login@v1
@@ -81,13 +98,17 @@ jobs:
8198
build-fbc:
8299
name: Build-fbc
83100
runs-on: ubuntu-24.04
101+
environment:
102+
name: "secure-build"
84103
permissions:
85104
contents: read
86105
packages: write
87106
needs: build-bundle
88107
steps:
89108
- name: Checkout source
90109
uses: actions/checkout@v4
110+
with:
111+
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || (github.event_name == 'workflow_dispatch' && github.event.inputs.pr_number != '' && format('refs/pull/{0}/head', github.event.inputs.pr_number)) || github.ref }}
91112

92113
- name: Log in to GitHub Container Registry
93114
uses: redhat-actions/podman-login@v1
@@ -179,6 +200,8 @@ jobs:
179200
test-kind:
180201
name: Test kind deployment
181202
runs-on: ubuntu-24.04
203+
environment:
204+
name: "secure-build"
182205
permissions:
183206
contents: read
184207
packages: read
@@ -188,6 +211,8 @@ jobs:
188211
steps:
189212
- name: Checkout source
190213
uses: actions/checkout@v4
214+
with:
215+
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || (github.event_name == 'workflow_dispatch' && github.event.inputs.pr_number != '' && format('refs/pull/{0}/head', github.event.inputs.pr_number)) || github.ref }}
191216

192217
- name: Install Go
193218
uses: actions/setup-go@v5
@@ -269,6 +294,8 @@ jobs:
269294
test-upgrade:
270295
name: Test upgrade operator
271296
runs-on: ubuntu-24.04
297+
environment:
298+
name: "secure-build"
272299
permissions:
273300
contents: read
274301
packages: read
@@ -279,6 +306,8 @@ jobs:
279306
steps:
280307
- name: Checkout source
281308
uses: actions/checkout@v4
309+
with:
310+
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || (github.event_name == 'workflow_dispatch' && github.event.inputs.pr_number != '' && format('refs/pull/{0}/head', github.event.inputs.pr_number)) || github.ref }}
282311

283312
- name: Install Go
284313
uses: actions/setup-go@v5
@@ -352,6 +381,8 @@ jobs:
352381
test-ha-install:
353382
name: Test with High Availability
354383
runs-on: ubuntu-24.04
384+
environment:
385+
name: "secure-build"
355386
permissions:
356387
contents: read
357388
packages: read
@@ -360,6 +391,8 @@ jobs:
360391
steps:
361392
- name: Checkout source
362393
uses: actions/checkout@v4
394+
with:
395+
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || (github.event_name == 'workflow_dispatch' && github.event.inputs.pr_number != '' && format('refs/pull/{0}/head', github.event.inputs.pr_number)) || github.ref }}
363396

364397
- name: Install Go
365398
uses: actions/setup-go@v5
@@ -443,6 +476,8 @@ jobs:
443476
test-custom-install:
444477
name: Test with custom operator installation
445478
runs-on: ubuntu-24.04
479+
environment:
480+
name: "secure-build"
446481
permissions:
447482
contents: read
448483
packages: read
@@ -451,6 +486,8 @@ jobs:
451486
steps:
452487
- name: Checkout source
453488
uses: actions/checkout@v4
489+
with:
490+
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || (github.event_name == 'workflow_dispatch' && github.event.inputs.pr_number != '' && format('refs/pull/{0}/head', github.event.inputs.pr_number)) || github.ref }}
454491

455492
- name: Install Go
456493
uses: actions/setup-go@v5
@@ -466,7 +503,7 @@ jobs:
466503
auth_file_path: /tmp/config.json
467504

468505
- name: Log in to registry.redhat.io
469-
uses: redhat-actions/podman-login@9184318aae1ee5034fbfbacc0388acf12669171f # v1
506+
uses: redhat-actions/podman-login@v1
470507
with:
471508
username: ${{ secrets.REGISTRY_USER }}
472509
password: ${{ secrets.REGISTRY_PASSWORD }}
@@ -519,6 +556,8 @@ jobs:
519556
test-e2e:
520557
name: Execute securesign/sigstore-e2e
521558
runs-on: ubuntu-24.04
559+
environment:
560+
name: "secure-build"
522561
permissions:
523562
contents: read
524563
packages: read
@@ -529,6 +568,8 @@ jobs:
529568
steps:
530569
- name: Checkout source
531570
uses: actions/checkout@v4
571+
with:
572+
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || (github.event_name == 'workflow_dispatch' && github.event.inputs.pr_number != '' && format('refs/pull/{0}/head', github.event.inputs.pr_number)) || github.ref }}
532573
- name: Checkout test source repository
533574
uses: actions/checkout@v4
534575
with:
@@ -549,7 +590,7 @@ jobs:
549590
auth_file_path: /tmp/config.json
550591

551592
- name: Log in to registry.redhat.io
552-
uses: redhat-actions/podman-login@9184318aae1ee5034fbfbacc0388acf12669171f # v1
593+
uses: redhat-actions/podman-login@v1
553594
with:
554595
username: ${{ secrets.REGISTRY_USER }}
555596
password: ${{ secrets.REGISTRY_PASSWORD }}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: PR Validation
2+
3+
on:
4+
pull_request:
5+
branches: [ "main", "release*" ]
6+
7+
env:
8+
GO_VERSION: 1.24
9+
10+
jobs:
11+
validate-pr:
12+
name: Basic PR Validation
13+
runs-on: ubuntu-24.04
14+
permissions:
15+
contents: read
16+
steps:
17+
- name: Checkout source
18+
uses: actions/checkout@v4
19+
20+
- name: Install Go
21+
uses: actions/setup-go@v5
22+
with:
23+
go-version: ${{ env.GO_VERSION }}
24+
25+
- name: Verify dependencies
26+
run: go mod verify
27+
28+
- name: Build project
29+
run: go build ./...
30+
31+
- name: Run unit tests
32+
run: go test -v ./...
33+
34+
- name: Run vet
35+
run: go vet ./...
36+
37+
- name: Generate and validate manifests
38+
run: |
39+
make manifests generate fmt vet
40+
41+
- name: Check if generated files are up to date
42+
run: |
43+
if ! git diff --exit-code; then
44+
echo "❌ Generated files are not up to date"
45+
echo "Please run 'make manifests generate fmt' and commit the changes"
46+
git diff
47+
exit 1
48+
else
49+
echo "✅ Generated files are up to date"
50+
fi
51+

0 commit comments

Comments
 (0)