Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

add instance information to webhooks #577

Merged
9 commits merged into from
Feb 19, 2021
13 changes: 13 additions & 0 deletions docs/webhook_events.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Each event will be submitted via HTTP POST to the user provided URL.
},
"event_id": "00000000-0000-0000-0000-000000000000",
"event_type": "ping",
"instance_id": "00000000-0000-0000-0000-000000000000",
"instance_name": "example",
"webhook_id": "00000000-0000-0000-0000-000000000000"
}
```
Expand Down Expand Up @@ -4016,6 +4018,15 @@ Each event will be submitted via HTTP POST to the user provided URL.
"event_type": {
"$ref": "#/definitions/EventType"
},
"instance_id": {
"format": "uuid",
"title": "Instance Id",
"type": "string"
},
"instance_name": {
"title": "Instance Name",
"type": "string"
},
"webhook_id": {
"format": "uuid",
"title": "Webhook Id",
Expand All @@ -4025,6 +4036,8 @@ Each event will be submitted via HTTP POST to the user provided URL.
"required": [
"event_type",
"event",
"instance_id",
"instance_name",
"webhook_id"
],
"title": "WebhookMessage",
Expand Down
8 changes: 7 additions & 1 deletion src/api-service/__app__/onefuzzlib/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from onefuzztypes.events import Event, EventMessage, get_event_type

from .azure.creds import get_instance_id, get_instance_name
from .webhooks import Webhook

EVENTS: Queue = Queue()
Expand All @@ -35,6 +36,11 @@ def get_events() -> Optional[str]:
def send_event(event: Event) -> None:
event_type = get_event_type(event)
logging.info("sending event: %s - %s", event_type, event)
event_message = EventMessage(event_type=event_type, event=event)
event_message = EventMessage(
event_type=event_type,
event=event,
instance_id=get_instance_id(),
instance_name=get_instance_name(),
)
EVENTS.put(event_message)
Webhook.send_event(event_message)
19 changes: 17 additions & 2 deletions src/api-service/__app__/onefuzzlib/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pydantic import BaseModel

from .__version__ import __version__
from .azure.creds import get_instance_id, get_instance_name
from .azure.queue import queue_object
from .azure.storage import StorageType
from .orm import ORMMixin
Expand Down Expand Up @@ -169,13 +170,22 @@ def _add_event(self, event_message: EventMessage) -> None:
event_id=event_message.event_id,
event_type=event_message.event_type,
event=event_message.event,
instance_id=event_message.instance_id,
instance_name=event_message.instance_name,
)
message.save()
message.queue_webhook()

def ping(self) -> EventPing:
ping = EventPing(ping_id=uuid4())
self._add_event(EventMessage(event_type=EventType.ping, event=ping))
self._add_event(
EventMessage(
event_type=EventType.ping,
event=ping,
instance_id=get_instance_id(),
instance_name=get_instance_name(),
)
)
return ping

def send(self, message_log: WebhookMessageLog) -> bool:
Expand Down Expand Up @@ -213,7 +223,12 @@ def build_message(
) -> Tuple[bytes, Optional[str]]:
data = (
WebhookMessage(
webhook_id=webhook_id, event_id=event_id, event_type=event_type, event=event
webhook_id=webhook_id,
event_id=event_id,
event_type=event_type,
event=event,
instance_id=get_instance_id(),
instance_name=get_instance_name(),
)
.json(sort_keys=True, exclude_none=True)
.encode()
Expand Down
25 changes: 19 additions & 6 deletions src/api-service/tests/test_webhook_hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,52 @@
# Licensed under the MIT License.

import unittest
from unittest.mock import MagicMock, patch
from uuid import UUID

from onefuzztypes.events import EventPing, EventType

from __app__.onefuzzlib.webhooks import build_message


class TestWebhookHmac(unittest.TestCase):
def test_webhook_hmac(self) -> None:
@patch("__app__.onefuzzlib.webhooks.get_instance_id")
@patch("__app__.onefuzzlib.webhooks.get_instance_name")
def test_webhook_hmac(self, mock_name: MagicMock, mock_id: MagicMock) -> None:
mock_name.return_value = "example"
mock_id.return_value = UUID(int=3)

# late import to enable the patch to function
bmc-msft marked this conversation as resolved.
Show resolved Hide resolved
from __app__.onefuzzlib.webhooks import build_message

webhook_id = UUID(int=0)
event_id = UUID(int=1)
event_type = EventType.ping
event = EventPing(ping_id=UUID(int=2))

data, digest = build_message(
webhook_id=webhook_id, event_id=event_id, event_type=event_type, event=event
webhook_id=webhook_id,
event_id=event_id,
event_type=event_type,
event=event,
)

expected = (
b"{"
b'"event": {"ping_id": "00000000-0000-0000-0000-000000000002"}, '
b'"event_id": "00000000-0000-0000-0000-000000000001", '
b'"event_type": "ping", '
b'"instance_id": "00000000-0000-0000-0000-000000000003", '
b'"instance_name": "example", '
b'"webhook_id": "00000000-0000-0000-0000-000000000000"'
b"}"
)

expected_digest = (
"3502f83237ce006b7f6cfa40b89c0295009e3ccb0a1e62ce1d689700c2c6e698"
"61c0de81e011495c2ca89fbf99485b841cee257bcfba326a3edc66f39dc1feec"
"2f0610d708d9938dc053200cd2242b4cca4e9bb7227e5e662f28307f57c7fc9b"
"d1ffcab6fff9f409b3fb856db4f358be9078ec0b64874efac2b8f065211e2a14"
)

print(repr(expected))
print(repr(data))
self.assertEqual(data, expected)
self.assertEqual(digest, None)

Expand Down
2 changes: 2 additions & 0 deletions src/pytypes/extra/generate-docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ def main():
event_id=UUID(int=0),
event_type=EventType.ping,
event=EventPing(ping_id=UUID(int=0)),
instance_id=UUID(int=0),
instance_name="example",
)

layer(
Expand Down
2 changes: 2 additions & 0 deletions src/pytypes/onefuzztypes/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,5 @@ class EventMessage(BaseEvent):
event_id: UUID = Field(default_factory=uuid4)
event_type: EventType
event: Event
instance_id: UUID
instance_name: str