-
-
Notifications
You must be signed in to change notification settings - Fork 5
Track container image metadata in sqlite records #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nezhar
wants to merge
3
commits into
main
Choose a base branch
from
issue77
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """Best-effort collection of container image metadata for sqlite records.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from dataclasses import dataclass | ||
| from typing import Final | ||
|
|
||
| from vibepod.core.docker import _parse_image_name | ||
|
|
||
| AGENT_VERSION_LABELS: Final = ( | ||
| "vibepod.agent.version", | ||
| "org.opencontainers.image.version", | ||
| ) | ||
|
|
||
| # A bare engine image ID (possibly truncated) used directly as the image | ||
| # reference — its colon is not a repository/tag separator. | ||
| _BARE_IMAGE_ID: Final = re.compile(r"sha256:[0-9a-fA-F]{6,64}") | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ImageMetadata: | ||
| """Image provenance persisted alongside session and task rows.""" | ||
|
|
||
| image_tag: str | None | ||
| image_hash: str | None | ||
| agent_version: str | None | ||
|
|
||
|
|
||
| def collect_image_metadata(container: object, image: str) -> ImageMetadata: | ||
| """Extract tag, hash, and agent version for the image behind *container*. | ||
|
|
||
| Everything is best-effort: agent images without version labels, stub | ||
| containers in tests, or a daemon that dropped away mid-inspect must never | ||
| break the launch — missing pieces simply become ``None``. | ||
| """ | ||
| if _BARE_IMAGE_ID.fullmatch(image): | ||
| image_tag = None | ||
| else: | ||
| _, image_tag = _parse_image_name(image) | ||
|
|
||
| image_hash: str | None = None | ||
| agent_version: str | None = None | ||
| # docker-py's Container.image and Image.labels are properties backed by API | ||
| # calls and raw inspect payloads — each access can raise (KeyError included, | ||
| # which getattr defaults do not swallow), so every read stays guarded. | ||
| try: | ||
| image_obj = getattr(container, "image", None) | ||
| except Exception: | ||
| image_obj = None | ||
| if image_obj is not None: | ||
| try: | ||
| image_id = getattr(image_obj, "id", None) | ||
| except Exception: | ||
| image_id = None | ||
| if isinstance(image_id, str) and image_id: | ||
| image_hash = image_id | ||
| try: | ||
| labels = getattr(image_obj, "labels", None) | ||
| except Exception: | ||
| labels = None | ||
| if isinstance(labels, dict): | ||
| for label in AGENT_VERSION_LABELS: | ||
| value = labels.get(label) | ||
| if isinstance(value, str) and value: | ||
| agent_version = value | ||
| break | ||
|
|
||
| if image_hash is None: | ||
| attrs = getattr(container, "attrs", None) | ||
| if isinstance(attrs, dict): | ||
| attr_image = attrs.get("Image") | ||
| if isinstance(attr_image, str) and attr_image: | ||
| image_hash = attr_image | ||
|
|
||
| return ImageMetadata(image_tag=image_tag, image_hash=image_hash, agent_version=agent_version) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """Shared ALTER-based sqlite column migrations.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import sqlite3 | ||
|
|
||
|
|
||
| def add_missing_columns( | ||
| conn: sqlite3.Connection, | ||
| table: str, | ||
| columns: dict[str, str], | ||
| ) -> bool: | ||
| """Add each column in *columns* missing from *table*. | ||
|
|
||
| Returns ``True`` when the table changed. A concurrently launching process | ||
| can add a column between the PRAGMA read and the ALTER — that | ||
| duplicate-column failure is tolerated (the column exists either way, and | ||
| callers' backfills are idempotent); any other ``OperationalError`` | ||
| propagates. | ||
| """ | ||
| existing = {row[1] for row in conn.execute(f"PRAGMA table_info({table})").fetchall()} | ||
| changed = False | ||
| for column, definition in columns.items(): | ||
| if column in existing: | ||
| continue | ||
| try: | ||
| conn.execute(f"ALTER TABLE {table} ADD COLUMN {column} {definition}") | ||
| except sqlite3.OperationalError as exc: | ||
| if "duplicate column" not in str(exc).lower(): | ||
| raise | ||
| changed = True | ||
| return changed | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
Validate all interpolated schema inputs.
table,column, anddefinitionare inserted directly into SQL. Current callers pass constants, but this public helper accepts arbitrary strings; a future caller could introduce SQL injection or break migrations with malformed identifiers. Parameter placeholders cannot bind identifiers, so validate/allow-list names and type definitions, then quote identifiers before constructing these statements.🧰 Tools
🪛 OpenGrep (1.26.0)
[ERROR] 19-19: SQL query built via f-string passed to execute()/executemany(). Use parameterized queries with placeholders instead.
(coderabbit.sql-injection.python-fstring-execute)
[ERROR] 25-25: SQL query built via f-string passed to execute()/executemany(). Use parameterized queries with placeholders instead.
(coderabbit.sql-injection.python-fstring-execute)
🤖 Prompt for AI Agents
Source: Linters/SAST tools