Skip to content

Commit cdd6153

Browse files
committed
fix: improve security and error handling in OS libs automation
Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
1 parent 9048488 commit cdd6153

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed

.github/workflows/update_os_libraries.yml

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ jobs:
2323
with:
2424
persist-credentials: false
2525

26-
- name: Install Dagger
27-
env:
26+
- name: Fetch extensions
27+
id: get-extensions-dagger
28+
uses: dagger/dagger-for-github@v8.2.0
29+
with:
2830
# renovate: datasource=github-tags depName=dagger/dagger versioning=semver
29-
DAGGER_VERSION: 0.19.7
30-
run: |
31-
curl -L https://dl.dagger.io/dagger/install.sh | BIN_DIR=$HOME/.local/bin sh
31+
version: 0.19.7
32+
verb: call
33+
module: ./dagger/maintenance/
34+
args: get-oslibs-targets
3235

33-
- name: Fetch extensions
36+
- name: Set extensions output
3437
id: get-extensions
3538
run: |
36-
EXTENSIONS_JSON=$(dagger call -m ./dagger/maintenance/ get-oslibs-targets)
37-
echo "extensions=$EXTENSIONS_JSON" >> $GITHUB_OUTPUT
39+
echo "extensions=${{ steps.get-extensions-dagger.outputs.output }}" >> $GITHUB_OUTPUT
3840
3941
update-extension-os-libs:
4042
name: Update OS libs for ${{ matrix.extension }}
@@ -55,26 +57,34 @@ jobs:
5557
username: ${{ github.actor }}
5658
password: ${{ secrets.GITHUB_TOKEN }}
5759

58-
- name: Install Dagger
59-
env:
60-
# renovate: datasource=github-tags depName=dagger/dagger versioning=semver
61-
DAGGER_VERSION: 0.19.7
62-
run: |
63-
curl -L https://dl.dagger.io/dagger/install.sh | BIN_DIR=$HOME/.local/bin sh
64-
6560
- name: Update OS libs for ${{ matrix.extension }}
66-
run: |
67-
dagger call -m ./dagger/maintenance/ update-oslibs --target ${{ matrix.extension }} \
68-
export --path .
61+
uses: dagger/dagger-for-github@v8.2.0
62+
with:
63+
# renovate: datasource=github-tags depName=dagger/dagger versioning=semver
64+
version: 0.19.7
65+
verb: call
66+
module: ./dagger/maintenance/
67+
args: update-oslibs --target ${{ matrix.extension }} export --path=.
6968

7069
- name: Diff
7170
run: |
7271
git status
7372
git diff
7473
74+
- name: Check for changes
75+
id: check-changes
76+
run: |
77+
if git diff --quiet; then
78+
echo "No changes detected for ${{ matrix.extension }}"
79+
echo "changed=false" >> $GITHUB_OUTPUT
80+
else
81+
echo "Changes detected for ${{ matrix.extension }}"
82+
echo "changed=true" >> $GITHUB_OUTPUT
83+
fi
84+
7585
- name: Create a PR if versions have been updated on main
7686
uses: peter-evans/create-pull-request@271a8d0340265f705b14b6d32b9829c1cb33d45e # v7
77-
if: github.ref == 'refs/heads/main'
87+
if: github.ref == 'refs/heads/main' && steps.check-changes.outputs.changed == 'true'
7888
with:
7989
token: ${{ secrets.REPO_GHA_PAT }}
8090
title: "chore: update ${{ matrix.extension }} OS libraries"

dagger/maintenance/updatelibs.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import (
99
"dagger/maintenance/internal/dagger"
1010
)
1111

12+
// libsRegex matches library dependencies from apt-get output
13+
// Format: library-name MD5Sum:checksum
14+
var libsRegex = regexp.MustCompile(`(?m)^.*\s(lib\S*).*(MD5Sum:.*)$`)
15+
1216
func updateOSLibsOnTarget(
1317
ctx context.Context,
1418
target string,
@@ -17,6 +21,7 @@ func updateOSLibsOnTarget(
1721
) (*dagger.File, error) {
1822
postgresBaseImage := fmt.Sprintf("ghcr.io/cloudnative-pg/postgresql:%s-minimal-%s", majorVersion, distribution)
1923
packageName := fmt.Sprintf("postgresql-%s-%s", majorVersion, target)
24+
2025
out, err := dag.Container().
2126
From(postgresBaseImage).
2227
WithUser("root").
@@ -26,17 +31,30 @@ func updateOSLibsOnTarget(
2631
"apt-get install -qq --print-uris --no-install-recommends " + packageName,
2732
}).Stdout(ctx)
2833
if err != nil {
29-
return nil, err
34+
return nil, fmt.Errorf("failed to fetch OS libs for extension %s (PostgreSQL %s on %s): %w",
35+
target, majorVersion, distribution, err)
36+
}
37+
38+
matches := libsRegex.FindAllStringSubmatch(out, -1)
39+
if len(matches) == 0 {
40+
return nil, fmt.Errorf("no library dependencies found for extension %s (PostgreSQL %s on %s): apt-get may have failed or package has no lib dependencies",
41+
target, majorVersion, distribution)
3042
}
31-
var re = regexp.MustCompile(`(?m)^.*\s(lib\S*).*(MD5Sum:.*)$`)
32-
matches := re.FindAllStringSubmatch(out, -1)
43+
3344
var result string
3445
for _, m := range matches {
3546
if len(m) >= 3 {
3647
result += m[1] + " " + m[2] + "\n"
3748
}
3849
}
39-
file := dag.File(fmt.Sprintf("%s-%s-os-libs.txt", majorVersion, distribution), result)
50+
51+
if result == "" {
52+
return nil, fmt.Errorf("parsed empty content for extension %s (PostgreSQL %s on %s): regex matched but extracted no data",
53+
target, majorVersion, distribution)
54+
}
55+
56+
fileName := fmt.Sprintf("%s-%s-os-libs.txt", majorVersion, distribution)
57+
file := dag.File(fileName, result)
4058

4159
return file, nil
4260
}

0 commit comments

Comments
 (0)