diff --git a/.github/workflows/oas-validation.yml b/.github/workflows/oas-validation.yml index 77512210..54b48b7e 100644 --- a/.github/workflows/oas-validation.yml +++ b/.github/workflows/oas-validation.yml @@ -24,3 +24,8 @@ jobs: - name: Validate OpenAPI using spec-validator run: openapi-spec-validator spec.yaml + - name: Check notification schemas + run: | + "$(pipx environment --value PIPX_HOME)/venvs/openapi-spec-validator/bin/python" \ + tests/test_notification_schema.py + diff --git a/spec.yaml b/spec.yaml index 46e326a3..2e10f757 100644 --- a/spec.yaml +++ b/spec.yaml @@ -384,17 +384,116 @@ paths: post: summary: General Notification endpoint description: > - This optional endpoint is used to inform the other party about a change - that concerns a previously known entity, such as a Share or a trusted User, - or to request an action to be performed (such as sharing or re-sharing) - on a previously known resource. + Notifications and requests concerning Resources, Collections, Recipients + or Federation membership. See [Notifications](https://datatracker.ietf.org/doc/html/draft-ietf-ocm-open-cloud-mesh#notifications) - for more details. + for details. requestBody: content: application/json: schema: $ref: "#/components/schemas/NewNotification" + examples: + requestShare: + summary: A recipient requests shares from a collection + value: + notificationType: REQUEST_SHARE + resourceType: collection + shareType: user + senderDomain: alice.example.org + collectionId: research-data-123 + notification: + collection: + owner: bob@bob.example.org + shareWith: alice@alice.example.org + permissions: + - read + contactWasRemoved: + summary: A user removed a single contact + value: + notificationType: CONTACT_REMOVED + shareType: user + senderDomain: alice.example.org + notification: + message: User is not a trusted contact any longer + user: + userId: alice@alice.example.org + targetUserId: bob@bob.example.org + shareWasAccepted: + summary: A recipient accepted a share + value: + notificationType: SHARE_ACCEPTED + resourceType: file + shareType: user + senderDomain: sender.example.org + notification: + message: Recipient accepted the share + file: + providerId: 7c084226-d9a1-11e6-bf26-cec0c932ce01 + shareWasUnshared: + summary: A provider removed a share + value: + notificationType: SHARE_UNSHARED + resourceType: file + senderDomain: sender.example.org + notification: + file: + providerId: 7c084226-d9a1-11e6-bf26-cec0c932ce01 + userLeft: + summary: A user left the sending server + value: + notificationType: USER_REMOVED + shareType: user + senderDomain: alice.example.org + notification: + message: User left the system + user: + userId: alice@alice.example.org + mlsWelcome: + summary: MLS Welcome envelope (illustrative content) + value: + notificationType: MLS_WELCOME + shareType: community + senderDomain: cloud.example.org + notification: + community: + mlsGroupId: 51dc30ddc473d43a6011e9ebba6ca770 + userId: bob@cloud.example.org + content: base64 MLS welcome + customType: + summary: An implementer-defined notification type + value: + notificationType: EXAMPLE_CUSTOM_EVENT + resourceType: file + senderDomain: sender.example.org + notification: + message: Something happened + file: + customKey: customValue + ocmServerWasAdded: + summary: An OCM server was added to a federation + value: + notificationType: OCM_SERVER_ADDED + resourceType: federation + senderDomain: ocm-server.example.org + notification: + message: Your OCM Server is now part of our federation + federation: + federationId: sciencemesh + name: The ScienceMesh Directory + url: https://cloud.example.org + ocmServerLeft: + summary: An OCM server left a federation + value: + notificationType: OCM_SERVER_REMOVED + resourceType: federation + senderDomain: ocm-server.example.org + notification: + message: We are leaving this federation + federation: + federationId: sciencemesh + name: The ScienceMesh Directory + url: https://ocm-server.example.org description: The JSON object with a new notification required: true responses: @@ -1252,14 +1351,11 @@ components: `create`, `update`, or `delete` is present. - `share` allows re-share rights on the resource. items: - type: string - enum: - - read - - create - - update - - delete - - write - - share + allOf: + - $ref: "#/components/schemas/SharePermission" + - not: + enum: + - view requirements: type: array items: @@ -1341,15 +1437,7 @@ components: - `share` allows re-share rights on the resource. This only applies to web apps that provide a mechanism for re-sharing. items: - type: string - enum: - - view - - read - - create - - update - - delete - - write - - share + $ref: "#/components/schemas/SharePermission" requirements: type: array minItems: 1 @@ -1548,9 +1636,9 @@ components: minLength: 1 description: > Share type, according to the OCM IANA Registry for share types. - E.g. user, group, community, etc. Implementers MAY define - additional custom types, provided that they are registered in the - respective OCM IANA Registry. + E.g. user, group, community, etc. Implementers MAY define additional + custom types, provided that they are registered in the respective + OCM IANA Registry. providerId: type: string description: > @@ -1587,74 +1675,339 @@ components: description: > Additional payload objects keyed by resource type or share type. Required fields are defined by the notification type. - example: - shareWasAccepted: - notificationType: SHARE_ACCEPTED - resourceType: file - shareType: user - senderDomain: sender.example.org - notification: - message: Recipient accepted the share + allOf: + - anyOf: + - required: + - resourceType + - required: + - shareType + - anyOf: + - $ref: "#/components/schemas/ShareStateNotification" + - $ref: "#/components/schemas/CollectionDeclinedNotification" + - $ref: "#/components/schemas/ReshareRequestNotification" + - $ref: "#/components/schemas/ShareRequestNotification" + - $ref: "#/components/schemas/UserRemovedNotification" + - $ref: "#/components/schemas/GroupRemovedNotification" + - $ref: "#/components/schemas/ContactRemovedNotification" + - $ref: "#/components/schemas/FederationMembershipNotification" + - $ref: "#/components/schemas/NotificationOfOtherType" + ShareStateNotification: + type: object + description: > + Payload constraints for share lifecycle events; see Share Identity + in the OCM Internet-Draft. + required: + - notificationType + - resourceType + - notification + properties: + notificationType: + type: string + enum: + - SHARE_ACCEPTED + - SHARE_DECLINED + - SHARE_UNSHARED + - SHARE_CHANGE_PERMISSION + resourceType: + not: + enum: + - collection + - federation + notification: + type: object + required: + - file + properties: + file: + type: object + required: + - providerId + properties: + providerId: + type: string + minLength: 1 + description: > + Identifier assigned to this Share by its resource provider. + permissions: + type: array + items: + $ref: "#/components/schemas/SharePermission" + CollectionDeclinedNotification: + type: object + required: + - notificationType + - resourceType + - collectionId + - notification + properties: + notificationType: + type: string + enum: + - SHARE_DECLINED + resourceType: + type: string + enum: + - collection + notification: + type: object + required: + - collection + properties: + collection: + type: object + required: + - shareWith + properties: + shareWith: + $ref: "#/components/schemas/OCMAddress" + ReshareRequestNotification: + type: object + required: + - notificationType + - resourceType + - notification + properties: + notificationType: + type: string + enum: + - REQUEST_RESHARE + resourceType: + not: + enum: + - collection + - federation + notification: + type: object + required: + - file + properties: file: - providerId: 7c084226-d9a1-11e6-bf26-cec0c932ce01 - contactWasRemoved: - notificationType: CONTACT_REMOVED - shareType: user - senderDomain: alice.example.org - notification: - message: User is not a trusted contact any longer + type: object + required: + - providerId + - shareWith + properties: + providerId: + type: string + minLength: 1 + shareWith: + $ref: "#/components/schemas/OCMAddress" + permissions: + type: array + items: + $ref: "#/components/schemas/SharePermission" + ShareRequestNotification: + type: object + required: + - notificationType + - resourceType + - collectionId + - shareType + - notification + properties: + notificationType: + type: string + enum: + - REQUEST_SHARE + resourceType: + type: string + enum: + - collection + notification: + type: object + required: + - collection + properties: + collection: + type: object + required: + - owner + - shareWith + - permissions + properties: + owner: + $ref: "#/components/schemas/OCMAddress" + shareWith: + $ref: "#/components/schemas/OCMAddress" + permissions: + type: array + items: + $ref: "#/components/schemas/SharePermission" + not: + required: + - collectionSize + UserRemovedNotification: + type: object + required: + - notificationType + - shareType + - notification + properties: + notificationType: + type: string + enum: + - USER_REMOVED + shareType: + type: string + enum: + - user + notification: + type: object + required: + - user + properties: user: - userId: alice@alice.example.org - targetUserId: bob@bob.example.org - userLeft: - notificationType: USER_REMOVED - shareType: user - senderDomain: alice.example.org - notification: - message: User left the system + type: object + required: + - userId + properties: + userId: + $ref: "#/components/schemas/OCMAddress" + not: + required: + - targetUserId + not: + required: + - resourceType + GroupRemovedNotification: + type: object + required: + - notificationType + - shareType + - notification + properties: + notificationType: + type: string + enum: + - GROUP_REMOVED + shareType: + type: string + enum: + - group + notification: + type: object + required: + - group + properties: + group: + type: object + required: + - groupId + properties: + groupId: + type: string + minLength: 1 + not: + required: + - resourceType + ContactRemovedNotification: + type: object + required: + - notificationType + - shareType + - notification + properties: + notificationType: + type: string + enum: + - CONTACT_REMOVED + shareType: + type: string + enum: + - user + notification: + type: object + required: + - user + properties: user: - userId: alice@alice.example.org - ocmServerWasAdded: - notificationType: OCM_SERVER_ADDED - resourceType: federation - senderDomain: ocm-server.example.org - notification: - message: Your OCM Server is now part of our federation - federation: - federationId: sciencemesh - name: The ScienceMesh Directory - url: https://cloud.example.org - ocmServerLeft: - notificationType: OCM_SERVER_REMOVED - resourceType: federation - senderDomain: ocm-server.example.org - notification: - message: We are leaving this federation + type: object + required: + - userId + - targetUserId + properties: + userId: + description: > + OCM Address of the actor. Its OCM Server Address equals + senderDomain. + allOf: + - $ref: "#/components/schemas/OCMAddress" + targetUserId: + description: > + OCM Address of the removed contact at the receiving server. + allOf: + - $ref: "#/components/schemas/OCMAddress" + not: + required: + - resourceType + FederationMembershipNotification: + type: object + required: [notificationType, resourceType, notification] + not: + required: [shareType] + properties: + notificationType: + type: string + enum: [OCM_SERVER_ADDED, OCM_SERVER_REMOVED] + resourceType: + type: string + enum: [federation] + notification: + type: object + required: [federation] + properties: federation: - federationId: sciencemesh - name: The ScienceMesh Directory - url: https://ocm-server.example.org - mlsWelcome: - notificationType: MLS_WELCOME - shareType: community - senderDomain: cloud.example.org - notification: - community: - mlsGroupId: 51dc30ddc473d43a6011e9ebba6ca770 - userId: bob@cloud.example.org - content: base64 MLS welcome - requestShare: - notificationType: REQUEST_SHARE - resourceType: collection - shareType: user - senderDomain: alice.example.org - collectionId: research-data-123 - notification: - collection: - owner: bob@bob.example.org - shareWith: alice@alice.example.org - permissions: - - read + type: object + required: [federationId, url] + properties: + federationId: + type: string + description: Matches federation.federationId in the sender's directory. + name: + type: string + url: + type: string + format: uri + description: > + The recipient's server URL for OCM_SERVER_ADDED, or the + sender's server URL for OCM_SERVER_REMOVED, as defined in + Federation Membership of the OCM Internet-Draft. + mlsGroupId: + type: string + NotificationOfOtherType: + type: object + description: > + Other notification types satisfy the common envelope. Built-in types + cannot use this branch to bypass payload validation. + properties: + notificationType: + type: string + not: + enum: + - SHARE_ACCEPTED + - SHARE_DECLINED + - SHARE_UNSHARED + - SHARE_CHANGE_PERMISSION + - REQUEST_RESHARE + - REQUEST_SHARE + - USER_REMOVED + - CONTACT_REMOVED + - GROUP_REMOVED + - OCM_SERVER_ADDED + - OCM_SERVER_REMOVED + SharePermission: + type: string + description: > + A permission value of the webdav or webapp protocol. `view` + applies to webapp only. + enum: + - read + - create + - update + - delete + - write + - share + - view OCMServerAddress: type: string minLength: 1 diff --git a/tests/test_notification_schema.py b/tests/test_notification_schema.py new file mode 100644 index 00000000..93e99444 --- /dev/null +++ b/tests/test_notification_schema.py @@ -0,0 +1,266 @@ +"""Regression checks for the notification shapes defined in IETF-OCM.md.""" + +from copy import deepcopy +from pathlib import Path +import unittest + +from jsonschema import Draft4Validator +import yaml + + +SHARE_EVENTS = ( + "SHARE_ACCEPTED", "SHARE_DECLINED", "SHARE_UNSHARED", + "SHARE_CHANGE_PERMISSION", +) + + +def envelope(event, **fields): + return {"notificationType": event, "senderDomain": "sender.example.org", **fields} + + +# Fixtures follow Share Acceptance and Updating, Resharing, and Recipient Removal. +PAYLOADS = { + event: envelope(event, resourceType="file", + notification={"file": {"providerId": "share-123"}}) + for event in SHARE_EVENTS +} +PAYLOADS.update({ + "REQUEST_RESHARE": envelope( + "REQUEST_RESHARE", resourceType="file", + notification={"file": {"providerId": "share-123", + "shareWith": "bob@receiver.example.org"}}), + "REQUEST_SHARE": envelope( + "REQUEST_SHARE", resourceType="collection", shareType="user", + senderDomain="alice.example.org", collectionId="research-data-123", + notification={"collection": {"owner": "bob@bob.example.org", + "shareWith": "alice@alice.example.org", + "permissions": ["read"]}}), + "USER_REMOVED": envelope( + "USER_REMOVED", shareType="user", + notification={"user": {"userId": "alice@sender.example.org"}}), + "GROUP_REMOVED": envelope( + "GROUP_REMOVED", shareType="group", + notification={"group": {"groupId": "team-789"}}), + "CONTACT_REMOVED": envelope( + "CONTACT_REMOVED", shareType="user", + notification={"user": {"userId": "alice@sender.example.org", + "targetUserId": "bob@receiver.example.org"}}), +}) + +SERVER_EVENTS = ("OCM_SERVER_ADDED", "OCM_SERVER_REMOVED") +PAYLOADS.update({ + event: envelope(event, resourceType="federation", notification={ + "federation": {"federationId": "sciencemesh", + "url": ("https://sender.example.org" + if event == "OCM_SERVER_REMOVED" + else "https://receiver.example.org")}}) + for event in SERVER_EVENTS +}) + + +COLLECTION_DECLINE = envelope( + "SHARE_DECLINED", resourceType="collection", collectionId="research-data-123", + notification={"collection": {"shareWith": "alice@alice.example.org"}}) + + +class NotificationSchemaTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + spec_path = Path(__file__).resolve().parents[1] / "spec.yaml" + cls.spec = yaml.safe_load(spec_path.read_text(encoding="utf-8")) + # Keep local OpenAPI references available to the Draft4 validator. + cls.validator = Draft4Validator({ + "$ref": "#/components/schemas/NewNotification", + "components": cls.spec["components"], + }) + cls.resource_validator = Draft4Validator({ + "$ref": "#/components/schemas/ShareStateNotification", + "components": cls.spec["components"], + }) + + def test_builtin_payloads(self): + for event, payload in PAYLOADS.items(): + with self.subTest(event=event): + self.validator.validate(payload) + missing = deepcopy(payload) + del missing["notification"] + self.assertFalse(self.validator.is_valid(missing)) + empty = deepcopy(payload) + empty["notification"] = {} + self.assertFalse(self.validator.is_valid(empty)) + contact = deepcopy(PAYLOADS["CONTACT_REMOVED"]) + del contact["notification"]["user"]["targetUserId"] + self.assertFalse(self.validator.is_valid(contact)) + + def test_forbidden_fields_and_share_types(self): + for event in ("USER_REMOVED", "GROUP_REMOVED", "CONTACT_REMOVED"): + wrong_share_type = "user" if event == "GROUP_REMOVED" else "group" + for field, value in (("resourceType", "file"), + ("shareType", wrong_share_type)): + with self.subTest(event=event, field=field): + invalid = deepcopy(PAYLOADS[event]) + invalid[field] = value + self.assertFalse(self.validator.is_valid(invalid)) + removed = deepcopy(PAYLOADS["USER_REMOVED"]) + removed["notification"]["user"]["targetUserId"] = "bob@receiver.example.org" + self.assertFalse(self.validator.is_valid(removed)) + + def test_collection_request_required_fields(self): + for field in ("resourceType", "collectionId", "shareType", "notification"): + with self.subTest(field=field): + request = deepcopy(PAYLOADS["REQUEST_SHARE"]) + del request[field] + self.assertFalse(self.validator.is_valid(request)) + for field in ("owner", "shareWith", "permissions"): + with self.subTest(collection_field=field): + request = deepcopy(PAYLOADS["REQUEST_SHARE"]) + del request["notification"]["collection"][field] + self.assertFalse(self.validator.is_valid(request)) + + def test_collection_request_extensions(self): + request = deepcopy(PAYLOADS["REQUEST_SHARE"]) + request["notification"]["collection"]["permissions"] = [] + self.validator.validate(request) + request["shareType"] = "EXAMPLE_CUSTOM_RECIPIENT" + request["collectionId"] = "opaque @collection/id?#" + request["notification"]["file"] = {"opaque": "extension"} + self.validator.validate(request) + request["collectionId"] = "" + self.validator.validate(request) + request["collectionId"] = None + self.assertFalse(self.validator.is_valid(request)) + + def test_collection_request_forbids_collection_size(self): + for value in (1, None, "", []): + with self.subTest(value=value): + request = deepcopy(PAYLOADS["REQUEST_SHARE"]) + request["collectionSize"] = value + self.assertFalse(self.validator.is_valid(request)) + + def test_retired_request_cannot_use_fallback(self): + request = envelope( + "REQUEST_SHARE", resourceType="file", + notification={"file": {"owner": "alice@sender.example.org", + "shareWith": "bob@receiver.example.org", + "shareId": "resource-456", "permissions": ["read"]}}) + self.assertFalse(self.validator.is_valid(request)) + + def test_collection_decline(self): + self.validator.validate(COLLECTION_DECLINE) + decline = deepcopy(COLLECTION_DECLINE) + decline["shareType"] = "EXAMPLE_CUSTOM_RECIPIENT" + self.validator.validate(decline) + for field in ("collectionId", "notification"): + with self.subTest(field=field): + missing = deepcopy(COLLECTION_DECLINE) + del missing[field] + self.assertFalse(self.validator.is_valid(missing)) + missing = deepcopy(COLLECTION_DECLINE) + del missing["notification"]["collection"]["shareWith"] + self.assertFalse(self.validator.is_valid(missing)) + + def test_collection_decline_cannot_match_resource_state(self): + decline = deepcopy(COLLECTION_DECLINE) + decline["notification"]["file"] = {"providerId": "incidental-grant-id"} + self.validator.validate(decline) + self.assertFalse(self.resource_validator.is_valid(decline)) + del decline["collectionId"] + self.assertFalse(self.validator.is_valid(decline)) + + def test_resource_events_exclude_reserved_types(self): + for event in (*SHARE_EVENTS, "REQUEST_RESHARE"): + with self.subTest(event=event): + payload = deepcopy(PAYLOADS[event]) + for resource in ("collection", "federation"): + payload["resourceType"] = resource + self.assertFalse(self.validator.is_valid(payload), resource) + payload["resourceType"] = "EXAMPLE_CUSTOM_RESOURCE" + self.validator.validate(payload) + folder = deepcopy(PAYLOADS["SHARE_DECLINED"]) + folder["resourceType"] = "folder" + self.validator.validate(folder) + + def test_deprecated_id_cannot_replace_nested_share_id(self): + for event in SHARE_EVENTS: + with self.subTest(event=event): + payload = deepcopy(PAYLOADS[event]) + payload["providerId"] = payload["notification"]["file"].pop("providerId") + self.assertFalse(self.validator.is_valid(payload)) + + def test_server_membership_payloads(self): + for event in SERVER_EVENTS: + payload = PAYLOADS[event] + for field in ("federationId", "url"): + for remove in (True, False): + invalid = deepcopy(payload) + if remove: + del invalid["notification"]["federation"][field] + else: + invalid["notification"]["federation"][field] = 7 + with self.subTest(event=event, field=field, remove=remove): + self.assertFalse(self.validator.is_valid(invalid)) + for change in ({"shareType": "user"}, {"shareType": None}, + {"resourceType": "file"}): + with self.subTest(event=event, change=change): + self.assertFalse(self.validator.is_valid({**payload, **change})) + extended = deepcopy(payload) + extended["notification"]["federation"].update( + name="ScienceMesh", mlsGroupId="opaque", future={"value": 1}) + self.validator.validate(extended) + + def test_notification_permissions(self): + contexts = (("SHARE_CHANGE_PERMISSION", "file"), + ("REQUEST_RESHARE", "file"), ("REQUEST_SHARE", "collection")) + permissions = ("read", "create", "update", "delete", "write", "share", "view") + for event, key in contexts: + for permission in (*permissions, "admin", 7, None): + payload = deepcopy(PAYLOADS[event]) + payload["notification"][key]["permissions"] = [permission] + with self.subTest(event=event, permission=permission): + self.assertEqual(self.validator.is_valid(payload), + permission in permissions) + + def test_share_protocol_view_permission(self): + protocol = self.spec["components"]["schemas"]["NewShare"]["properties"]["protocol"] + validator = Draft4Validator({ + "$ref": "#/components/schemas/NewShare/properties/protocol", + "components": self.spec["components"], + }) + for name, accepted in (("webdav", False), ("webapp", True)): + payload = deepcopy(protocol["example"]["multipleProtocols"]) + payload[name]["permissions"] = ["view"] + with self.subTest(protocol=name): + self.assertEqual(validator.is_valid(payload), accepted) + + def test_unknown_type(self): + self.validator.validate(envelope("EXAMPLE_CUSTOM_EVENT", resourceType="file")) + self.validator.validate(envelope("EXAMPLE_COLLECTION_EVENT", resourceType="collection")) + self.validator.validate(envelope("EXAMPLE_FEDERATION_EVENT", resourceType="federation")) + + def test_fallback_excludes_exactly_the_builtin_types(self): + schemas = self.spec["components"]["schemas"] + branches = schemas["NewNotification"]["allOf"][1]["anyOf"] + builtin_types = set() + for branch in branches: + name = branch["$ref"].rsplit("/", 1)[1] + if name != "NotificationOfOtherType": + builtin_types.update(schemas[name]["properties"]["notificationType"]["enum"]) + fallback_type = schemas["NotificationOfOtherType"]["properties"]["notificationType"] + exclusions = fallback_type["not"]["enum"] + self.assertEqual(len(exclusions), 11) + self.assertEqual(set(exclusions), builtin_types) + self.assertEqual(set(PAYLOADS), builtin_types) + + def test_published_operation_examples(self): + operation = self.spec["paths"]["/notifications"]["post"] + examples = operation["requestBody"]["content"]["application/json"]["examples"] + self.assertTrue(examples) + self.assertIn("requestShare", examples) + self.assertEqual(examples["requestShare"]["value"], PAYLOADS["REQUEST_SHARE"]) + for name, example in examples.items(): + with self.subTest(example=name): + self.validator.validate(example["value"]) + + +if __name__ == "__main__": + unittest.main()