Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Dockerfile
.git
_*
# Only the helper scripts are copied from this build context.
**

!initdb-postgis.sh
!update-postgis.sh
23 changes: 23 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: 2

updates:
- package-ecosystem: docker
directory: /examples/image-with-postgis-clis
schedule:
interval: weekly
cooldown:
default-days: 10
groups:
docker:
patterns:
- "*"
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
cooldown:
default-days: 10
groups:
github-actions:
patterns:
- "*"
25 changes: 25 additions & 0 deletions .github/scripts/check-current-candidate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python3
"""Stop a delayed candidate from replacing an image published since its run began."""
import importlib.util
import os
from pathlib import Path
from urllib.error import HTTPError
from urllib.parse import quote

spec = importlib.util.spec_from_file_location('update_policy', Path(__file__).with_name('update-policy.py'))
policy = importlib.util.module_from_spec(spec)
spec.loader.exec_module(policy)
tag = os.environ['VERSION'] + ('-alpine' if os.environ['VARIANT'] == 'alpine' else '')
run = policy.request_json(f"https://api.github.com/repos/{os.environ['GITHUB_REPOSITORY']}/actions/runs/{os.environ['RUN_ID']}",
token=os.environ.get('GH_TOKEN'))
try:
image = policy.request_json('https://hub.docker.com/v2/repositories/postgis/postgis/tags/' + quote(tag, safe='') + '/')
except HTTPError as error:
if error.code != 404:
raise
else:
updated = image.get('tag_last_pushed') or image.get('last_updated')
if not updated:
raise SystemExit(f'No publication timestamp for {tag}')
if policy.published_after_run(updated, run['created_at']):
raise SystemExit(f'{tag} was published after this candidate was selected')
27 changes: 27 additions & 0 deletions .github/scripts/check-release-policy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python3
"""Fail closed if a release environment lacks its required protection."""
import importlib.util
import json
import os
from pathlib import Path

spec = importlib.util.spec_from_file_location('update_policy', Path(__file__).with_name('update-policy.py'))
policy = importlib.util.module_from_spec(spec)
spec.loader.exec_module(policy)
images = json.loads(os.environ['IMAGES'])['include']
manual = os.environ['EVENT_NAME'] == 'workflow_dispatch'
if any(image.get('security') is not manual for image in images):
raise SystemExit('Release approval metadata does not match the workflow event')
needed = set()
if any(image.get('security') for image in images):
needed.add('postgis-security')
if os.environ['EVENT_NAME'] == 'schedule' and any(not image.get('security') for image in images):
needed.add('postgis-routine')
for name in sorted(needed):
environment = policy.request_json(f"https://api.github.com/repos/{os.environ['GITHUB_REPOSITORY']}/environments/{name}",
token=os.environ.get('GH_TOKEN'))
protected = (policy.security_environment_is_protected(environment) if name == 'postgis-security'
else policy.routine_environment_is_protected(environment))
if not protected:
raise SystemExit(f'{name} lacks the required release protection')
print(f'{name} has the required release protection')
79 changes: 79 additions & 0 deletions .github/scripts/oci-evidence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3
"""Check the tested OCI image and record its immutable descriptors."""
import hashlib
import json
import sys
import tarfile

archive, output, tag, architecture, loaded_id = sys.argv[1:]

with tarfile.open(archive, 'r:*') as layout:
def read_blob(descriptor, capture=False):
digest = descriptor['digest']
if not digest.startswith('sha256:'):
raise ValueError(f'unsupported digest: {digest}')
checksum = hashlib.sha256()
data = bytearray() if capture else None
size = 0
with layout.extractfile('blobs/' + digest.replace(':', '/')) as source:
while chunk := source.read(1024 * 1024):
checksum.update(chunk)
size += len(chunk)
if capture:
data.extend(chunk)
if size != descriptor['size'] or checksum.hexdigest() != digest[7:]:
raise ValueError(f'invalid blob: {digest}')
return data

def read_json(descriptor):
return json.loads(read_blob(descriptor, capture=True))

index = json.load(layout.extractfile('index.json'))
refs = [item for item in index['manifests']
if item.get('annotations', {}).get('org.opencontainers.image.ref.name') == tag.split(':', 1)[1]]
if len(refs) != 1:
raise ValueError(f'expected one OCI reference for {tag}')
root = refs[0]
manifests = read_json(root)['manifests']
images = [item for item in manifests if item.get('platform', {}).get('os') == 'linux'
and item.get('platform', {}).get('architecture') == architecture]
if len(images) != 1:
raise ValueError(f'expected one linux/{architecture} image')
image = images[0]
manifest = read_json(image)
config = manifest['config']
read_blob(config)
if loaded_id not in (root['digest'], config['digest']):
raise ValueError('loaded image does not match the OCI archive')
if not manifest['layers']:
raise ValueError('image has no layers')
for layer in manifest['layers']:
read_blob(layer)

predicates = set()
attestations = []
for descriptor in manifests:
annotations = descriptor.get('annotations', {})
if annotations.get('vnd.docker.reference.type') != 'attestation-manifest':
continue
if annotations.get('vnd.docker.reference.digest') != image['digest']:
raise ValueError('attestation refers to a different image')
attestation = read_json(descriptor)
if attestation.get('subject', {}).get('digest', image['digest']) != image['digest']:
raise ValueError('attestation subject does not match image')
for layer in attestation['layers']:
statement = read_json(layer)
if not any(subject.get('digest', {}).get('sha256') == image['digest'][7:]
for subject in statement.get('subject', [])):
raise ValueError('attestation statement refers to a different image')
predicates.add(statement['predicateType'])
attestations.append(descriptor['digest'])
if not any(value.startswith('https://slsa.dev/provenance/') for value in predicates):
raise ValueError('missing provenance attestation')
if 'https://spdx.dev/Document' not in predicates:
raise ValueError('missing SBOM attestation')

with open(output, 'w') as stream:
json.dump({'tag': tag, 'platform': ['linux', architecture], 'index': root,
'manifest': image, 'config': config, 'layers': manifest['layers'],
'attestations': attestations}, stream, separators=(',', ':'))
63 changes: 63 additions & 0 deletions .github/scripts/update-policy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3
"""Select changed candidates and check release gates."""
import hashlib
import json
from pathlib import Path
from datetime import datetime
import urllib.request


def request_json(url, token=None):
headers = {'Accept': 'application/vnd.github+json', 'User-Agent': 'docker-postgis-update-check'}
if token:
headers['Authorization'] = 'Bearer ' + token
with urllib.request.urlopen(urllib.request.Request(url, headers=headers), timeout=30) as response:
return json.load(response)


def candidate_fingerprint(version, variant, base_times, root=Path('.')):
digest = hashlib.sha256()
dockerfile = root / version / ('alpine/Dockerfile' if variant == 'alpine' else 'Dockerfile')
paths = [dockerfile, root / 'initdb-postgis.sh', root / 'update-postgis.sh', root / '.dockerignore', root / 'docker-bake.hcl']
if variant == 'alpine':
paths.append(root / version / 'alpine/pgrouting-version.txt')
for path in paths:
if path.is_file():
digest.update(str(path.relative_to(root)).encode() + b'\0' + path.read_bytes() + b'\0')
digest.update(json.dumps(base_times, sort_keys=True).encode())
return digest.hexdigest()


def select_candidate(state, name, fingerprint, run_id, previous_status):
"""Suppress a candidate while its prior run is pending or succeeded."""
previous = state.get(name, {})
if previous.get('run_id') and str(previous['run_id']) != str(run_id):
status, conclusion = previous_status(previous['run_id'])
if previous.get('fingerprint') == fingerprint and (status != 'completed' or conclusion == 'success'):
return False
state[name] = {'fingerprint': fingerprint, 'run_id': run_id}
return True


def published_status(run, jobs, image_name):
if run['status'] != 'completed':
return run['status'], None
required = {f'Publish {image_name}', f'Verify published {image_name}'}
passed = {job['name'] for job in jobs if job['status'] == 'completed'
and job['conclusion'] == 'success'
and all(step.get('conclusion') != 'failure' for step in job.get('steps', []))}
return 'completed', 'success' if required <= passed else 'failure'


def published_after_run(updated, created):
return datetime.fromisoformat(updated.replace('Z', '+00:00')) > datetime.fromisoformat(created.replace('Z', '+00:00'))


def security_environment_is_protected(environment):
return environment.get('can_admins_bypass') is False and any(rule.get('type') == 'required_reviewers' and rule.get('reviewers')
for rule in environment.get('protection_rules', []))


def routine_environment_is_protected(environment):
return environment.get('can_admins_bypass') is False and any(rule.get('type') == 'wait_timer' and rule.get('wait_timer', 0) >= 4320
for rule in environment.get('protection_rules', []))
Loading