codeant-ai-for-open-source[bot] commented on code in PR #45870:
URL: https://github.com/apache/superset/pull/45870#discussion_r4444701986


##########
superset/databases/utils.py:
##########
@@ -160,3 +163,113 @@ def make_url_safe(raw_url: str | URL) -> URL:
 
     else:
         return raw_url
+
+
+def _is_present(value: Any) -> bool:
+    """Is a credential field actually filled in? Whitespace does not count."""
+    if isinstance(value, str):
+        return bool(value.strip())
+    return bool(value)
+
+
+def validated_embedded_credentials(credentials: Any) -> dict[str, str] | None:
+    """
+    Return a complete embedded fallback credential, or ``None``.
+
+    The canonical shape is ``{username, password}`` or ``{username, 
auth_method:
+    "keypair", privatekey_body, privatekey_pass}``; a half-filled credential is
+    ignored rather than half-applied. Engines may override this default through
+    ``BaseEngineSpec.validate_embedded_credentials`` for both query-time and
+    authoring-time validation.
+    """
+    if not isinstance(credentials, dict):
+        return None
+
+    username = credentials.get("username")
+    if not isinstance(username, str) or not _is_present(username):
+        return None
+
+    auth_method = credentials.get("auth_method", "password")
+    if auth_method == "keypair":
+        privatekey_body = credentials.get("privatekey_body")
+        privatekey_pass = credentials.get("privatekey_pass", "")
+        if (
+            not isinstance(privatekey_body, str)
+            or not privatekey_body

Review Comment:
   ✅ **CodeAnt verified this suggestion was addressed in subsequent commits and 
marked this thread resolved** as of `37a2ad4`.
   
   Added `_is_present`, which strips strings before checking whether they are 
non-empty, and use it to validate `privatekey_body`.
   
   <sub>If that's not right, unresolve this thread and CodeAnt will leave it 
open.</sub>
   
   <!-- codeant-auto-resolve-reply -->



##########
superset/commands/database/importers/v1/utils.py:
##########
@@ -190,6 +235,32 @@ def import_database(  # noqa: C901
         else:
             config["encrypted_extra"] = masked_encrypted_extra
 
+    if encrypted_extra := config.get("encrypted_extra"):
+        engine_spec = (
+            existing.db_engine_spec
+            if existing
+            else 
Database.get_db_engine_spec(make_url_safe(config["sqlalchemy_uri"]))
+        )

Review Comment:
   ✅ **CodeAnt verified this suggestion was addressed in subsequent commits and 
marked this thread resolved** as of `37a2ad4`.
   
   The importer now selects the engine spec from the incoming SQLAlchemy URI 
before validating encrypted-extra fields and embedded credentials.
   
   <sub>If that's not right, unresolve this thread and CodeAnt will leave it 
open.</sub>
   
   <!-- codeant-auto-resolve-reply -->



##########
superset/databases/schemas.py:
##########
@@ -1146,25 +1236,37 @@ def validate_masked_encrypted_extra(
                 "File contains both `encrypted_extra` and 
`masked_encrypted_extra`"
             )
 
-        if db.session.query(Database).filter_by(uuid=data["uuid"]).first():
-            # Existing DB: sensitive values will be revealed from existing
-            # encrypted_extra in import_database()
-            return
-
         masked_encrypted_extra = json.loads(data["masked_encrypted_extra"])
 
-        # Determine engine spec from sqlalchemy_uri to get sensitive fields
-        sqlalchemy_uri = data["sqlalchemy_uri"]
-        url = make_url_safe(sqlalchemy_uri)
-        backend = url.get_backend_name()
-        driver = url.get_driver_name()
-        db_engine_spec = get_engine_spec(backend, driver=driver)
-
-        # Check if any sensitive field is still masked
-        masked_fields = get_masked_fields(
-            masked_encrypted_extra,
-            db_engine_spec.encrypted_extra_sensitive_field_paths(),
-        )
+        if existing := 
db.session.query(Database).filter_by(uuid=data["uuid"]).first():
+            if not self.overwrites_existing:
+                return
+            # Existing DB: masked values are revealed from its stored
+            # encrypted_extra in import_database(). Any the stored payload has 
no
+            # value for -- e.g. a connection promoted without its OAuth2 
client or
+            # embedded credential -- would otherwise be stored as the mask 
itself,
+            # so ask for those like a new DB.
+            db_engine_spec = existing.db_engine_spec

Review Comment:
   ✅ **CodeAnt verified this suggestion was addressed in subsequent commits and 
marked this thread resolved** as of `37a2ad4`.
   
   The schema now selects secret-field paths using the incoming SQLAlchemy 
URI's engine spec, so overwrite mask checks account for engine changes.
   
   <sub>If that's not right, unresolve this thread and CodeAnt will leave it 
open.</sub>
   
   <!-- codeant-auto-resolve-reply -->



-- 
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