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

xqhu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new 9ab508755f4 Fix gcs audit custom entries (#34942)
9ab508755f4 is described below

commit 9ab508755f4f6fc7ecac30b36ed8d6b082464371
Author: atognolag <[email protected]>
AuthorDate: Wed May 14 14:23:38 2025 -0300

    Fix gcs audit custom entries (#34942)
    
    * Fix gcs audit custom entries
    
    * Linting
    
    * Adding test for repeated GCS custom audit header issue
---
 sdks/python/apache_beam/options/pipeline_options.py      | 10 ++++++++--
 sdks/python/apache_beam/options/pipeline_options_test.py | 14 ++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/sdks/python/apache_beam/options/pipeline_options.py 
b/sdks/python/apache_beam/options/pipeline_options.py
index b51b85963c6..1b9b560099a 100644
--- a/sdks/python/apache_beam/options/pipeline_options.py
+++ b/sdks/python/apache_beam/options/pipeline_options.py
@@ -163,9 +163,11 @@ class _GcsCustomAuditEntriesAction(argparse.Action):
   MAX_KEY_LENGTH = 64
   MAX_VALUE_LENGTH = 1200
   MAX_ENTRIES = 4
+  GCS_AUDIT_PREFIX = 'x-goog-custom-audit-'
 
   def _exceed_entry_limit(self):
-    if 'x-goog-custom-audit-job' in self._custom_audit_entries:
+    job_audit_entry = _GcsCustomAuditEntriesAction.GCS_AUDIT_PREFIX + 'job'
+    if job_audit_entry in self._custom_audit_entries:
       return len(
           self._custom_audit_entries) > 
_GcsCustomAuditEntriesAction.MAX_ENTRIES
     else:
@@ -185,7 +187,11 @@ class _GcsCustomAuditEntriesAction(argparse.Action):
           "The value '%s' in GCS custom audit entries exceeds the %d-character 
limit."  # pylint: disable=line-too-long
           % (value, _GcsCustomAuditEntriesAction.MAX_VALUE_LENGTH))
 
-    self._custom_audit_entries[f"x-goog-custom-audit-{key}"] = value
+    if _GcsCustomAuditEntriesAction.GCS_AUDIT_PREFIX in key:
+      self._custom_audit_entries[key] = value
+    else:
+      self._custom_audit_entries[_GcsCustomAuditEntriesAction.GCS_AUDIT_PREFIX 
+
+                                 key] = value
 
   def __call__(self, parser, namespace, values, option_string=None):
     if not hasattr(namespace, self.dest) or getattr(namespace,
diff --git a/sdks/python/apache_beam/options/pipeline_options_test.py 
b/sdks/python/apache_beam/options/pipeline_options_test.py
index 896b32039eb..099c9e80e21 100644
--- a/sdks/python/apache_beam/options/pipeline_options_test.py
+++ b/sdks/python/apache_beam/options/pipeline_options_test.py
@@ -750,6 +750,20 @@ class PipelineOptionsTest(unittest.TestCase):
             'x-goog-custom-audit-id': '1234'
         })
 
+  def test_gcs_custom_audit_entries_wo_duplicated_prefix(self):
+    options = PipelineOptions([
+        '--gcs_custom_audit_entry=x-goog-custom-audit-user=test-user',
+        '--gcs_custom_audit_entries={"job":"test-job", "id":"1234"}'
+    ])
+    entries = options.view_as(GoogleCloudOptions).gcs_custom_audit_entries
+    self.assertDictEqual(
+        entries,
+        {
+            'x-goog-custom-audit-user': 'test-user',
+            'x-goog-custom-audit-job': 'test-job',
+            'x-goog-custom-audit-id': '1234'
+        })
+
   @mock.patch('apache_beam.options.pipeline_options._BeamArgumentParser.error')
   def test_gcs_custom_audit_entries_with_errors(self, mock_error):
     long_key = 'a' * 65

Reply via email to