You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2026/04/23 18:43:14 UTC

(allura) branch master updated (ba211a889 -> 31840bd9d)

This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git


    from ba211a889 [#8600] Upgrade lxml 6.0.4 -> 6.1.0
     new 72a31893d Fix convert plaintext to encrypted script to support nested fields
     new d38f6371a add limit option to convert_encrypted_field.py
     new 31840bd9d remove pygments optimization no longer needed

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 Allura/allura/lib/patches.py       |   3 -
 scripts/convert_encrypted_field.py | 130 ++++++++++++++++++++++++++++---------
 2 files changed, 101 insertions(+), 32 deletions(-)


(allura) 03/03: remove pygments optimization no longer needed

Posted by br...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 31840bd9d3ae97e89ef6d10d27bd580fc2064319
Author: Dave Brondsema <da...@brondsema.net>
AuthorDate: Thu Apr 23 14:42:23 2026 -0400

    remove pygments optimization no longer needed
---
 Allura/allura/lib/patches.py | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/Allura/allura/lib/patches.py b/Allura/allura/lib/patches.py
index 6ce181075..42d9afae5 100644
--- a/Allura/allura/lib/patches.py
+++ b/Allura/allura/lib/patches.py
@@ -104,9 +104,6 @@ def with_trailing_slash(func, *args, **kwargs):
     def encode(self, o):
         return original_tg_jsonify_JSONEncoder_encode(self, o).replace('<', r'\u003C')
 
-    # can remove if https://github.com/pygments/pygments/pull/2979 is merged and released
-    pygments.plugin.entry_points = functools.cache(pygments.plugin.entry_points)
-
 
 old_controller_call = tg.controllers.DecoratedController._call
 


(allura) 01/03: Fix convert plaintext to encrypted script to support nested fields

Posted by br...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 72a31893d6a3176b47afe9b421bbddf599070c22
Author: Carlos Cruz <ca...@slashdotmedia.com>
AuthorDate: Wed Apr 15 21:00:59 2026 +0000

    Fix convert plaintext to encrypted script to support nested fields
---
 scripts/convert_encrypted_field.py | 105 +++++++++++++++++++++++++++++--------
 1 file changed, 82 insertions(+), 23 deletions(-)

diff --git a/scripts/convert_encrypted_field.py b/scripts/convert_encrypted_field.py
index 04a5b3bfe..46e2f61ba 100644
--- a/scripts/convert_encrypted_field.py
+++ b/scripts/convert_encrypted_field.py
@@ -21,23 +21,62 @@
 
 import defopt
 
-from allura.lib.utils import chunked_find
 from ming import schema
-from ming.odm import session
+from ming.odm import mapper, session
 from ming.odm.declarative import MappedClass
 from ming.odm.property import FieldProperty, DecryptedProperty
 
 
-def _encrypted_field_name(plain_field_name: str) -> str:
-    path = plain_field_name.split('.')
-    path[-1] = f'{path[-1]}_encrypted'
-    return '.'.join(path)
+CHUNK_SIZE = 1000
 
 
-def _get_field_value(record, field_name: str):
-    value = record
-    for part in field_name.split('.'):
-        value = value[part]
+class MissingFieldPathError(Exception):
+    pass
+
+
+def _default_encrypted_field_name(plain_field_name: str) -> str:
+    parent, sep, leaf = plain_field_name.rpartition('.')
+    return f'{parent}{sep}{leaf}_encrypted'
+
+
+def _split_field_path(field_name: str) -> list[str]:
+    field_path = field_name.split('.')
+    assert field_path and all(field_path), f'Invalid dotted field path: {field_name!r}'
+    return field_path
+
+
+def _schema_for_field_path(Model: type[MappedClass], field_name: str):
+    field_path = _split_field_path(field_name)
+    top_level_name = field_path[0]
+    try:
+        top_level_prop = getattr(Model, top_level_name)
+    except AttributeError as e:
+        raise MissingFieldPathError(
+            f'Missing field path {field_name!r}; no top-level field {top_level_name!r}') from e
+    assert isinstance(top_level_prop, FieldProperty)
+
+    current_schema = top_level_prop.field.schema
+    for i, path_part in enumerate(field_path[1:], start=1):
+        traversed = '.'.join(field_path[:i])
+        if isinstance(current_schema, schema.Array):
+            raise AssertionError(
+                f'Nested array paths are not supported: {field_name!r} (array at {traversed!r})')
+        if not isinstance(current_schema, schema.Object):
+            raise AssertionError(
+                f'Invalid nested field path {field_name!r}; {traversed!r} is not an object field')
+        if path_part not in current_schema.fields:
+            raise MissingFieldPathError(
+                f'Invalid nested field path {field_name!r}; missing key {traversed + "." + path_part!r}')
+
+        current_schema = current_schema.fields[path_part]
+
+    return current_schema
+
+
+def _get_nested_value(rec: dict, field_name: str):
+    value = rec
+    for path_part in _split_field_path(field_name):
+        value = value[path_part]
     return value
 
 
@@ -45,23 +84,31 @@ def main(class_name: str, plain_field_name: str,
          *, remove_unencrypted: bool = False, redo_all: bool = False):
     """
     :param class_name: full class name, e.g. allura.model.user.User
-    :param plain_field_name: name of the unencrypted field, e.g. display_name or author.username
+    :param plain_field_name: name of the unencrypted field, e.g. display_name
     :param remove_unencrypted: WARNING only run this after your codebase is already on the latest code
     :param redo_all: re-encrypt records that already have encrypted values (in case they changed since last run)
     """
-    encrypted_field_name = _encrypted_field_name(plain_field_name)
-    is_nested_field = '.' in plain_field_name
+    encrypted_field_name = _default_encrypted_field_name(plain_field_name)
 
     module_name, class_basename = class_name.rsplit('.', 1)
     module = import_module(module_name)
     Model: type[MappedClass] = getattr(module, class_basename)
 
     # sanity checks that the fields are correct and ready
-    if not is_nested_field:
-        encr_prop = getattr(Model, encrypted_field_name)
-        assert isinstance(encr_prop, FieldProperty)
-        assert encr_prop.field.type == schema.Binary
-        if remove_unencrypted:
+    try:
+        encr_schema = _schema_for_field_path(Model, encrypted_field_name)
+    except MissingFieldPathError:
+        # pre-migration support: encrypted field might not yet be present in
+        # model schema, but we can still create it with raw MongoDB updates
+        encr_schema = None
+    if encr_schema is not None:
+        assert isinstance(encr_schema, schema.Binary)
+    elif remove_unencrypted:
+        raise AssertionError(
+            f'Cannot use --remove-unencrypted because {encrypted_field_name!r} '
+            'is not present in model schema yet')
+    if remove_unencrypted:
+        if '.' not in plain_field_name:
             plain_prop = Model.__dict__[plain_field_name]  # getattr() better but needs Ming fix released
             assert isinstance(plain_prop, DecryptedProperty)
             assert plain_prop.encrypted_field == encrypted_field_name
@@ -90,16 +137,28 @@ def main(class_name: str, plain_field_name: str,
     }
     if not redo_all:
         q[encrypted_field_name] = None
+
+    m = mapper(Model)
+    raw_collection = sess.impl.db[m.collection.m.collection_name]
+
     count = 0
-    for chunk in chunked_find(Model, q):
-        for rec in chunk:
-            val = _get_field_value(rec, plain_field_name)
+    projection = {'_id': 1, plain_field_name: 1}
+    last_id = None
+    while True:
+        chunk_q = {'$and': [q, {'_id': {'$gt': last_id}}]} if last_id is not None else q
+        docs = list(raw_collection.find(chunk_q, projection).sort('_id', 1).limit(CHUNK_SIZE))
+        if not docs:
+            break
+
+        for rec_doc in docs:
+            val = _get_nested_value(rec_doc, plain_field_name)
             encr_val = Model.encr(val)
-            Model.query.update({'_id': rec['_id']}, {
+            raw_collection.update_one({'_id': rec_doc['_id']}, {
                 '$set': {encrypted_field_name: encr_val},
             })
             count += 1
-            sess.expunge(rec)
+
+        last_id = docs[-1]['_id']
         print(f'Converted {count} so far...')
     if not count:
         print(f'Did not find any {class_name} records with {plain_field_name} values to encrypt')


(allura) 02/03: add limit option to convert_encrypted_field.py

Posted by br...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git

commit d38f6371ab2be1c944c3b50b702d67af405afde7
Author: Dave Brondsema <da...@brondsema.net>
AuthorDate: Thu Apr 23 14:41:43 2026 -0400

    add limit option to convert_encrypted_field.py
---
 scripts/convert_encrypted_field.py | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/scripts/convert_encrypted_field.py b/scripts/convert_encrypted_field.py
index 46e2f61ba..78ce8155c 100644
--- a/scripts/convert_encrypted_field.py
+++ b/scripts/convert_encrypted_field.py
@@ -81,12 +81,13 @@ def _get_nested_value(rec: dict, field_name: str):
 
 
 def main(class_name: str, plain_field_name: str,
-         *, remove_unencrypted: bool = False, redo_all: bool = False):
+         *, remove_unencrypted: bool = False, redo_all: bool = False, limit: int | None = None):
     """
     :param class_name: full class name, e.g. allura.model.user.User
     :param plain_field_name: name of the unencrypted field, e.g. display_name
     :param remove_unencrypted: WARNING only run this after your codebase is already on the latest code
     :param redo_all: re-encrypt records that already have encrypted values (in case they changed since last run)
+    :param limit: convert this many records per update type (bulk, individual, removal) default All
     """
     encrypted_field_name = _default_encrypted_field_name(plain_field_name)
 
@@ -120,16 +121,22 @@ def main(class_name: str, plain_field_name: str,
     # encrypt all records that need it.  Even for --remove-unencrypted, to make sure everything's converted first
     bulk_update_values = ["", None]
     for bulk_val in bulk_update_values:
+        query = {
+            plain_field_name: {'$exists': True, '$eq': bulk_val},  # $exists to avoid converting missing fields
+        }
+        if not redo_all:
+            query[encrypted_field_name] = {'$exists': False}
+        if limit:
+            ids_to_remove = [rec._id for rec in Model.query.find(query).limit(limit)]
+            query |= {'_id': {'$in': ids_to_remove}}
         bulk_update_result = Model.query.update(
-            {
-                plain_field_name: {'$exists': True, '$eq': bulk_val},  # $exists to avoid converting missing fields
-            },
+            query,
             {
                 '$set': {encrypted_field_name: Model.encr(bulk_val)},
             },
             multi=True,
         )
-        print(f'Converted {bulk_update_result.modified_count} {class_name} records with {plain_field_name}={bulk_val!r}')
+        print(f'Converted {bulk_update_result.modified_count} (of {bulk_update_result.matched_count}) {class_name} records with {plain_field_name}={bulk_val!r}')
         # bulk_update_result.matched_count has # matches, already handled if run multiple times
 
     q = {
@@ -144,7 +151,7 @@ def main(class_name: str, plain_field_name: str,
     count = 0
     projection = {'_id': 1, plain_field_name: 1}
     last_id = None
-    while True:
+    while count < limit if limit else True:
         chunk_q = {'$and': [q, {'_id': {'$gt': last_id}}]} if last_id is not None else q
         docs = list(raw_collection.find(chunk_q, projection).sort('_id', 1).limit(CHUNK_SIZE))
         if not docs:
@@ -157,6 +164,8 @@ def main(class_name: str, plain_field_name: str,
                 '$set': {encrypted_field_name: encr_val},
             })
             count += 1
+            if limit and count >= limit:
+                break
 
         last_id = docs[-1]['_id']
         print(f'Converted {count} so far...')
@@ -166,8 +175,12 @@ def main(class_name: str, plain_field_name: str,
         print(f'Encrypted {count} {class_name} records with {plain_field_name} values')
 
     if remove_unencrypted:
+        query = {}
+        if limit:
+            ids_to_remove = [rec._id for rec in Model.query.find(query).limit(limit)]
+            query |= {'_id': {'$in': ids_to_remove}}
         remove_result = Model.query.update(
-            {},
+            query,
             {
                 "$unset": {plain_field_name: True}
             },