Skip to content

Commit ad1133e

Browse files
new: [core] Added new report_error function and preparing a first trusted release on Pypi.
1 parent 4649b22 commit ad1133e

File tree

7 files changed

+104
-31
lines changed

7 files changed

+104
-31
lines changed

.github/workflows/release.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
on:
2+
release:
3+
types:
4+
- published
5+
6+
name: release
7+
8+
jobs:
9+
pypi-publish:
10+
name: Upload release to PyPI
11+
runs-on: ubuntu-latest
12+
environment:
13+
name: pypi
14+
url: https://pypi.org/p/FediVuln
15+
16+
permissions:
17+
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
18+
steps:
19+
- uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
- name: Install Poetry
23+
run: python -m pip install --upgrade pip poetry
24+
- name: Build artifacts
25+
run: poetry build
26+
- name: Publish package distributions to PyPI
27+
uses: pypa/gh-action-pypi-publish@release/v1

CHANGELOG.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Changelog
2+
3+
## Release 1.0.0 (2025-02-13)
4+
5+
This release introduces the capability to report errors, warnings,
6+
and heartbeats to a Valkey datastore, facilitating centralized monitoring.
7+
8+
9+
## Release 0.8.0 (2025-01-23)
10+
11+
Bugfix (stupid bug) and updated dependencies.
12+
13+
14+
## Release 0.7.0 (2025-01-14)
15+
16+
Regular expressions are now defined in the configuration file.
17+
18+
19+
## Release 0.6.2 (2024-12-16)
20+
21+
Directly return the updated status before trying another source of
22+
vulnerabilities. This prevents overriding the content generated in
23+
the previous try block.

fedivuln/publish.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from mastodon import Mastodon
88

99
from fedivuln import config
10-
from fedivuln.utils import heartbeat
10+
from fedivuln.utils import heartbeat, report_error
1111

1212
# Set up your Mastodon instance with access credentials
1313
if config.mastodon_clientcred_push and config.mastodon_usercred_push:
@@ -117,10 +117,13 @@ def listen_to_http_event_stream(url, headers=None, params=None, topic="comment")
117117

118118
except requests.exceptions.RequestException as req_err:
119119
print(f"Request error: {req_err}")
120+
report_error("error", f"Request error with HTTP event stream: {req_err}")
120121
except KeyboardInterrupt:
121122
print("\nStream interrupted by user. Closing connection.")
123+
report_error("error", "Stream interrupted by user. Closing connection.")
122124
except Exception as e:
123125
print(f"Unexpected error: {e}")
126+
report_error("error", f"Unexpected error in listen_to_http_event_stream: {e}")
124127

125128

126129
def listen_to_valkey_stream(topic="comment"):
@@ -141,7 +144,7 @@ def listen_to_valkey_stream(topic="comment"):
141144
# Send entire JSON object as a single `data:` line
142145
json_message = json.dumps(message["data"]) # Ensure single-line JSON
143146
yield f"{json_message}"
144-
heartbeat(process_name=f"process_FediVuln-Publish_{topic}_heartbeat")
147+
heartbeat(process_name=f"process_heartbeat_FediVuln-Publish_{topic}")
145148
except GeneratorExit:
146149
valkey_client.unsubscribe(topic)
147150
except valkey.exceptions.ConnectionError:

fedivuln/stream.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pyvulnerabilitylookup import PyVulnerabilityLookup
88

99
from fedivuln import config
10-
from fedivuln.utils import heartbeat
10+
from fedivuln.utils import heartbeat, report_error
1111

1212

1313
# Custom encoder for datetime
@@ -50,11 +50,11 @@ def on_update(self, status):
5050
if self.push_sighting:
5151
push_sighting_to_vulnerability_lookup(
5252
status["uri"], vulnerability_ids
53-
) # Push the sighting to Vulnerability Lookup
53+
) # Push the sighting to Vulnerability-Lookup
5454
# if self.push_status:
5555
# push_status_to_vulnerability_lookup(
5656
# status, vulnerability_ids
57-
# ) # Push the status to Vulnerability Lookup
57+
# ) # Push the status to Vulnerability-Lookup
5858
else:
5959
print("No ID detected. Ignoring.")
6060

@@ -71,9 +71,10 @@ def on_direct_message(self, message):
7171
# Handle any errors in streaming
7272
def on_abort(self, err):
7373
print("Stream aborted with error:", err)
74+
report_error("error", f"Stream aborted with error: {err}")
7475

7576
def handle_heartbeat(self):
76-
heartbeat(process_name="process_FediVuln-Stream_heartbeat")
77+
heartbeat(process_name="process_heartbeat_FediVuln")
7778

7879

7980
def remove_case_insensitive_duplicates(input_list):
@@ -86,23 +87,27 @@ def remove_case_insensitive_duplicates(input_list):
8687

8788

8889
def push_sighting_to_vulnerability_lookup(status_uri, vulnerability_ids):
89-
"""Create a sighting from an incoming status and push it to the Vulnerability Lookup instance."""
90-
print("Pushing sighting to Vulnerability Lookup…")
90+
"""Create a sighting from an incoming status and push it to the Vulnerability-Lookup instance."""
91+
print("Pushing sighting to Vulnerability-Lookup…")
9192
vuln_lookup = PyVulnerabilityLookup(
9293
config.vulnerability_lookup_base_url, token=config.vulnerability_auth_token
9394
)
9495
for vuln in vulnerability_ids:
9596
# Create the sighting
9697
sighting = {"type": "seen", "source": status_uri, "vulnerability": vuln}
9798

98-
# Post the JSON to Vulnerability Lookup
99+
# Post the JSON to Vulnerability-Lookup
99100
try:
100101
r = vuln_lookup.create_sighting(sighting=sighting)
101102
if "message" in r:
102103
print(r["message"])
103104
except Exception as e:
104105
print(
105-
f"Error when sending POST request to the Vulnerability Lookup server:\n{e}"
106+
f"Error when sending POST request to the Vulnerability-Lookup server:\n{e}"
107+
)
108+
report_error(
109+
"error",
110+
f"Error when sending POST request to the Vulnerability-Lookup server: {e}",
106111
)
107112

108113

@@ -123,12 +128,12 @@ def main():
123128
parser.add_argument(
124129
"--push-sighting",
125130
action="store_true",
126-
help="Push the sightings to Vulnerability Lookup.",
131+
help="Push the sightings to Vulnerability-Lookup.",
127132
)
128133
# parser.add_argument(
129134
# "--push-status",
130135
# action="store_true",
131-
# help="Push the status to Vulnerability Lookup.",
136+
# help="Push the status to Vulnerability-Lookup.",
132137
# )
133138

134139
arguments = parser.parse_args()

fedivuln/utils.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
from fedivuln import config
66

7-
valkey_client = valkey.Valkey(config.valkey_host, config.valkey_port)
7+
if config.heartbeat_enabled:
8+
valkey_client = valkey.Valkey(config.valkey_host, config.valkey_port)
89

910

1011
def heartbeat(process_name) -> None:
@@ -16,3 +17,16 @@ def heartbeat(process_name) -> None:
1617
except Exception as e:
1718
print(f"Heartbeat error: {e}")
1819
raise # Propagate the error to stop the process
20+
21+
22+
def report_error(level="warning", message="", key="process_logs_FediVuln") -> None:
23+
"""Reports an error or warning in the Valkey datastore."""
24+
timestamp = time.time()
25+
log_entry = {"timestamp": timestamp, "level": level, "message": message}
26+
try:
27+
# Add the log entry to a list, so multiple messages are preserved
28+
valkey_client.rpush(key, str(log_entry))
29+
valkey_client.expire(key, 86400) # Expire after 24 hours
30+
except Exception as e:
31+
print(f"Error reporting failure: {e}")
32+
raise

poetry.lock

+16-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ build-backend = "poetry.core.masonry.api"
55

66
[project]
77
name = "FediVuln"
8-
version = "0.8.0"
8+
version = "1.0.0"
99
description = "A client to gather vulnerability-related information from the Fediverse."
1010
authors = [
1111
{name = "Cédric Bonhomme", email = "[email protected]"}
1212
]
1313
license = "GPL-3.0-or-later"
1414
readme = "README.md"
1515
keywords = ["Vulnerability-Lookup", "Vulnerability", "CVE", "Fediverse", "Mastodon"]
16-
# classifieres is dynamic because we want to create Python classifiers automatically
16+
1717
dynamic = ["classifiers"]
1818

1919
requires-python = ">=3.10,<4.0"
@@ -28,6 +28,7 @@ dependencies = [
2828
[project.urls]
2929
Homepage = "https://github.com/vulnerability-lookup/FediVuln"
3030
Repository = "https://github.com/vulnerability-lookup/FediVuln"
31+
Changelog = "https://github.com/vulnerability-lookup/FediVuln/blob/main/CHANGELOG.md"
3132

3233

3334
[project.scripts]

0 commit comments

Comments
 (0)