michael-s-molina commented on code in PR #39171:
URL: https://github.com/apache/superset/pull/39171#discussion_r3624400134


##########
superset/extensions/storage/persistent_model.py:
##########
@@ -0,0 +1,136 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import uuid as uuid_module
+
+from flask_appbuilder import Model
+from sqlalchemy import (
+    Boolean,
+    Column,
+    ForeignKey,
+    Index,
+    Integer,
+    LargeBinary,
+    String,
+    UniqueConstraint,
+)
+from sqlalchemy_utils import UUIDType
+from superset_core.extensions.storage.models import (
+    ExtensionStorageEntry as CoreExtensionStorageEntry,
+)
+
+from superset.models.helpers import AuditMixinNullable
+
+
+class ExtensionStorage(CoreExtensionStorageEntry, AuditMixinNullable, Model):
+    """Generic persistent key-value storage for extensions (Tier 3).
+
+    Each row is identified by (extension_id, user_fk, resource_type,
+    resource_uuid, key):
+
+    * Global scope      — user_fk IS NULL, resource_type IS NULL
+    * User scope        — user_fk set, resource_type IS NULL
+    * Resource scope    — resource_type + resource_uuid set (user_fk optional)
+
+    The payload is stored as raw bytes (value) alongside the identifier of
+    the codec used to encode it (codec), so the same bytes can be decoded
+    back into a value on read. When is_encrypted is True the value has been
+    encrypted at the DAO layer using Fernet and must be decrypted before
+    decoding.
+    """
+
+    __tablename__ = "extension_storage"
+
+    id = Column(Integer, primary_key=True, autoincrement=True)
+    uuid = Column(
+        UUIDType(binary=True),
+        default=uuid_module.uuid4,
+        unique=True,
+        nullable=False,
+    )
+
+    # Extension identity
+    extension_id = Column(String(255), nullable=False)
+
+    # Scope discriminators — all nullable; NULLs define the scope (see 
docstring).
+    # No relationship() is declared for user_fk: extension storage rows are
+    # not deleted when their owning user is (ondelete="SET NULL" demotes them
+    # to global scope instead), and nothing reads through such a relationship,
+    # so there's no ORM object here for a future change to attach a cascade to.
+    user_fk = Column(
+        Integer,
+        ForeignKey(
+            "ab_user.id",
+            ondelete="SET NULL",
+            name="fk_extension_storage_user_fk_ab_user",
+        ),
+        nullable=True,
+    )

Review Comment:
   Fixed — changed `user_fk`'s FK to `ondelete="CASCADE"` (in the model and the 
not-yet-released migration that creates this table), so a deleted user's 
private persistent-storage rows are deleted instead of being demoted to 
shared/global scope. `created_by_fk`/`changed_by_fk` are left as `SET NULL` 
since they're pure audit metadata with no bearing on scope, consistent with 
every other audited model in the codebase.



##########
superset/extensions/storage/api.py:
##########
@@ -0,0 +1,740 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""
+REST API for extension storage.
+
+Provides HTTP endpoints for frontend extensions to access server-side
+ephemeral storage without direct backend code.
+
+All operations are user-scoped by default. Use `?shared=true` query param
+to access shared state visible to all users.
+"""
+
+from __future__ import annotations
+
+import base64
+from typing import Any
+
+from flask import g, request
+from flask.wrappers import Response
+from flask_appbuilder.api import BaseApi, expose, protect, safe
+
+from superset.extensions.storage.codecs import DEFAULT_CODEC, get_codec, 
SAFE_CODECS
+from superset.extensions.storage.ephemeral_dao import (
+    ExtensionEphemeralDAO,
+    ExtensionEphemeralTTLInvalid,
+    ExtensionEphemeralValueTooLarge,
+)
+from superset.extensions.storage.persistent_dao import (
+    ExtensionStorageDAO,
+    ExtensionStorageKeyTooLong,
+    ExtensionStorageListPayloadTooLarge,
+    ExtensionStorageQuotaExceeded,
+    ExtensionStorageValueTooLarge,
+)
+from superset.extensions.storage.utils import get_extension_or_404, parse_ttl
+from superset.key_value.exceptions import KeyValueCodecEncodeException
+from superset.utils.decorators import transaction
+
+
+def _decoded_result_for_wire(decoded: Any) -> tuple[Any, bool]:
+    """Convert a codec-decoded value into its JSON wire representation.
+
+    JSON has no byte type, so a raw `bytes` value is base64-encoded to a
+    string for the response and flagged as such; every other value is
+    already JSON-representable as-is. Checked on the decoded value's
+    actual type, not the codec's name, so this keeps working for a codec
+    this module didn't define.
+
+    :returns: (wire_value, is_binary)
+    """
+    if isinstance(decoded, bytes):
+        return base64.b64encode(decoded).decode("ascii"), True
+    return decoded, False
+
+
+def _wire_value_for_request(value: Any, is_binary: bool) -> Any:
+    """Convert a request body's JSON `value` into a codec's input type.
+
+    `is_binary` is an explicit flag from the caller: JSON has no byte
+    type, so there is no way to tell, from the JSON `value` alone,
+    whether it is a base64 string that must be decoded to bytes before
+    being handed to the codec's `encode`, or a literal value to pass
+    through as-is. Only the caller knows which it sent.
+
+    :raises ValueError: if `is_binary` is set and `value` is not a valid
+        base64 string.
+    """
+    if is_binary:
+        return base64.b64decode(value, validate=True)
+    return value

Review Comment:
   Not a bug — `binascii.Error` is a subclass of `ValueError` in Python 3, so 
the existing `except (ValueError, TypeError)` in the request-parsing path 
already catches invalid base64 payloads and returns a 400. No change needed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to