firebase / firebase-functions-python

Apache License 2.0
129 stars 22 forks source link

Missing "authtype" and "authid" for "on_document_written_with_auth_context" trigger functions in emulators #210

Open andreas-thomik opened 6 days ago

andreas-thomik commented 6 days ago

Pretty much what the title says. The authid and authtype fields are present for all the other on_document_<...>_with_auth_context functions but are missing for on_document_written_with_auth_context

This might be an issue with the emulators themselves, if so please let me know and I'll post it to the relevant issue tracker.

exaby73 commented 6 days ago

Hello @andreas-thomik. Is this only reproducible with emulators?

andreas-thomik commented 6 days ago

Honestly not tested since #207 makes the _with_auth_context functions useless at the moment but I'll try a minimal setup later tonight and will report back.

andreas-thomik commented 5 days ago

@exaby73 I can confirm that this happens for both emulators and live functions.

Here's a minimal example to reproduce:

import logging

import firebase_functions.firestore_fn
from firebase_functions.firestore_fn import (
    AuthEvent,
    Change,
    DocumentSnapshot,
    _firestore_endpoint_handler,
    on_document_created_with_auth_context,
    on_document_written_with_auth_context,
)

logger = logging.getLogger()

def _inspect_endpoint_handler(*args, **kwargs):
    logger.warning("Args: %s", args)
    logger.warning("Kwargs: %s", kwargs)

    return _firestore_endpoint_handler(*args, **kwargs)

firebase_functions.firestore_fn._firestore_endpoint_handler = (
    _inspect_endpoint_handler
)

@on_document_written_with_auth_context(document="tests/{docId}")
def test_doc_written(event: AuthEvent[Change[DocumentSnapshot | None]]):
    return event

@on_document_created_with_auth_context(document="tests/{docId}")
def test_doc_created(event: AuthEvent[DocumentSnapshot]):
    return event

The actual parameters sent to _firestore_endpoint_handler can be seen in the logs, here's a (slightly redacted) copy of mine:

Logs for on_document_written_with_auth_context

{
  "attributes": {
    "specversion": "1.0", 
    "id": "...", 
    "source": "//firestore.googleapis.com/projects/MY-PROJECT/databases/(default)", 
    "type": "google.cloud.firestore.document.v1.written", 
    "datacontenttype": "application/protobuf", 
    "dataschema": "https://github.com/googleapis/google-cloudevents/blob/main/proto/google/events/cloud/firestore/v1/data.proto", 
    "subject": "documents/tests/LmbRywPwqsWgVJmG4JNe", 
    "time": "2024-07-01T19:24:45.860351Z", 
    "namespace": "(default)", 
    "document": "tests/LmbRywPwqsWgVJmG4JNe", 
    "location": "europe-west6", 
    "database": "(default)", 
    "project": "MY-PROJECT"
  }, 
  "data": "..."
}

Compare this with the logs for on_document_created_with_auth_context:

{
  "attributes": {
    "specversion": "1.0",
    "id": "...",
    "source": "//firestore.googleapis.com/projects/MY-PROJECT/databases/(default)",
    "type": "google.cloud.firestore.document.v1.created.withAuthContext",
    "datacontenttype": "application/protobuf",
    "dataschema": "https://github.com/googleapis/google-cloudevents/blob/main/proto/google/events/cloud/firestore/v1/data.proto", 
    "subject": "documents/tests/LmbRywPwqsWgVJmG4JNe", 
    "time": "2024-07-01T19:24:45.860351Z", 
    "location": "europe-west6", 
    "namespace": "(default)", 
    "document": "tests/LmbRywPwqsWgVJmG4JNe", 
    "authtype": "unknown", 
    "project": "MY-PROJECT", 
    "database": "(default)", 
    "authid": "MY-EMAIL"
  }, 
  "data": "..."
}
exaby73 commented 4 days ago

This seems to be an upstream issue. I've raised this forward. I'll get back to you soon on an update on this issue

exaby73 commented 4 days ago

For context, seems to be a similar issue on the Node SDK as well (https://github.com/firebase/firebase-functions/issues/1582)