Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do_file_sub error with "global flags not at the start of the expression at position 1" for juju plugin on ubuntu #3301

Closed
nkshirsagar opened this issue Jul 7, 2023 · 8 comments · Fixed by #3303

Comments

@nkshirsagar
Copy link
Contributor

I ran into this while testing 4.5.4 proposed lunar packages for Ubuntu (with the fix for #3261 built additionally into 4.5.4) and I wonder if RH plugins might hit this too in openshift.py like it was the case in #3261 , since this seems related to the way regexes work in python 3.11

sos.logs shows when sos report --all-logs was run in an openstack environment running lunar lobster,

2023-07-07 07:54:20,426 ERROR: caught exception in plugin method "juju.postproc()"
2023-07-07 07:54:20,426 ERROR: writing traceback to sos_logs/juju-plugin-errors.txt

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/sos/report/__init__.py", line 1459, in postproc
    plug.postproc()
  File "/usr/lib/python3/dist-packages/sos/report/plugins/juju.py", line 68, in postproc
    self.do_path_regex_sub(agents_path, keys_regex, sub_regex)
  File "/usr/lib/python3/dist-packages/sos/report/plugins/__init__.py", line 1329, in do_path_regex_sub
    self.do_file_sub(file['srcpath'], regexp, subst)
  File "/usr/lib/python3/dist-packages/sos/report/plugins/__init__.py", line 1292, in do_file_sub
    result, replacements = re.subn(pattern, subst, content,
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/__init__.py", line 196, in subn
    return _compile(pattern, flags).subn(repl, string, count)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/__init__.py", line 294, in _compile
    p = _compiler.compile(pattern, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_compiler.py", line 743, in compile
    p = _parser.parse(p, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_parser.py", line 980, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_parser.py", line 455, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_parser.py", line 863, in _parse
    p = _parse_sub(source, state, sub_verbose, nested + 1)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_parser.py", line 455, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_parser.py", line 841, in _parse
    raise source.error('global flags not at the start '
re.error: global flags not at the start of the expression at position 1

https://opendev.org/openstack/python-cinderclient/commit/20506ef3a8b5fb1b7a9b0a2c31dbe5f60eea2130 might be relevant which mentions "In Python3.11, global flags must be placed right at the start of a regular expression."

@TurboTurtle
Copy link
Member

I believe this should be resolved by #3263 which is in 4.5.5.

@nkshirsagar
Copy link
Contributor Author

I believe this should be resolved by #3263 which is in 4.5.5.

Negative, the packages I am testing does take that patch too on top of 4.5.4

@nkshirsagar
Copy link
Contributor Author

This reproduces on current sos main too,

s# git branch
* main
# ls
AUTHORS  LICENSE  MANIFEST.in  README.md  bin  docs  man  plugins_overview.py  po  requirements.txt  setup.py  snap  sos  sos.conf  sos.spec  tests  tmpfiles
# bin/sos report

sosreport (version 4.5.5)

This command will collect system configuration and diagnostic
information from this Ubuntu system.

For more information on Canonical visit:

        Community Website  : https://www.ubuntu.com/
        Commercial Support : https://www.canonical.com

The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.

No changes will be made to system configuration.


Press ENTER to continue, or CTRL-C to quit.

Optionally, please enter the case id that you are generating this report for []: 

 Setting up archive ...
 Setting up plugins ...
[plugin:lxd] skipped command 'lxd.buginfo': required kmods missing: ebtables, ip6table_raw, iptable_raw, iptable_mangle, bpfilter, ip6table_filter, iptable_filter, ebtable_filter, ip6table_nat, ip6_tables, iptable_nat, ip6table_mangle.  
[plugin:networking] skipped command 'ip -s macsec show': required kmods missing: macsec.   Use '--allow-system-changes' to enable collection.
[plugin:networking] skipped command 'ss -peaonmi': required kmods missing: udp_diag, af_packet_diag, inet_diag, netlink_diag, unix_diag, tcp_diag, xsk_diag.   Use '--allow-system-changes' to enable collection.
[plugin:ufw] skipped command 'ufw status numbered': required kmods missing: iptable_filter, bpfilter.  
[plugin:ufw] skipped command 'ufw app list': required kmods missing: iptable_filter, bpfilter.  
 Running plugins. Please wait ...

  Finishing plugins              [Running: ubuntu]                                        
  Finished running plugins                                                               
caught exception in plugin method "juju.postproc()"
writing traceback to sos_logs/juju-plugin-errors.txt
Creating compressed archive...

Your sosreport has been generated and saved in:
	/tmp/sosreport-juju-6127db-lunar-testing1-3-2023-07-07-nhzcknh.tar.xz

 Size	3.08MiB
 Owner	root
 sha256	58e9fe6629d8af8fd3069579eacbb2f0dbb9e7a47e88c41a8b06db185c25ab1a

Please send this file to your support representative.


/tmp# tar -xf /tmp/sosreport-juju-6127db-lunar-testing1-3-2023-07-07-nhzcknh.tar.xz
/tmp/sosreport-juju-6127db-lunar-testing1-3-2023-07-07-nhzcknh/sos_logs# ls
juju-plugin-errors.txt  sos.log  ui.log

:/tmp/sosreport-juju-6127db-lunar-testing1-3-2023-07-07-nhzcknh/sos_logs# cat juju-plugin-errors.txt 
Traceback (most recent call last):
  File "/home/ubuntu/sos/sos/sos/report/__init__.py", line 1459, in postproc
    plug.postproc()
  File "/home/ubuntu/sos/sos/sos/report/plugins/juju.py", line 68, in postproc
    self.do_path_regex_sub(agents_path, keys_regex, sub_regex)
  File "/home/ubuntu/sos/sos/sos/report/plugins/__init__.py", line 1329, in do_path_regex_sub
    self.do_file_sub(file['srcpath'], regexp, subst)
  File "/home/ubuntu/sos/sos/sos/report/plugins/__init__.py", line 1292, in do_file_sub
    result, replacements = re.subn(pattern, subst, content,
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/__init__.py", line 196, in subn
    return _compile(pattern, flags).subn(repl, string, count)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/__init__.py", line 294, in _compile
    p = _compiler.compile(pattern, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_compiler.py", line 743, in compile
    p = _parser.parse(p, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_parser.py", line 980, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_parser.py", line 455, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_parser.py", line 863, in _parse
    p = _parse_sub(source, state, sub_verbose, nested + 1)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_parser.py", line 455, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_parser.py", line 841, in _parse
    raise source.error('global flags not at the start '
re.error: global flags not at the start of the expression at position 1

@nkshirsagar
Copy link
Contributor Author

this seems purely a juju plugin issue, and should be fixed by this patch,

diff --git a/sos/report/plugins/juju.py b/sos/report/plugins/juju.py
index 3a947b54..75c3f575 100644
--- a/sos/report/plugins/juju.py
+++ b/sos/report/plugins/juju.py
@@ -63,7 +63,7 @@ class Juju(Plugin, UbuntuPlugin):
         ]
 
         # Redact simple yaml style "key: value".
-        keys_regex = r"((?m)^\s*(%s)\s*:\s*)(.*)" % "|".join(protect_keys)
+        keys_regex = r"(?m)(^\s*(%s)\s*:\s*)(.*)" % "|".join(protect_keys)
         sub_regex = r"\1*********"
         self.do_path_regex_sub(agents_path, keys_regex, sub_regex)
         # Redact certificates

I tested it, and no longer see the issue

root@juju-6127db-lunar-testing1-3:/home/ubuntu/sos/sos# git diff
diff --git a/sos/report/plugins/juju.py b/sos/report/plugins/juju.py
index 3a947b54..75c3f575 100644
--- a/sos/report/plugins/juju.py
+++ b/sos/report/plugins/juju.py
@@ -63,7 +63,7 @@ class Juju(Plugin, UbuntuPlugin):
         ]
 
         # Redact simple yaml style "key: value".
-        keys_regex = r"((?m)^\s*(%s)\s*:\s*)(.*)" % "|".join(protect_keys)
+        keys_regex = r"(?m)(^\s*(%s)\s*:\s*)(.*)" % "|".join(protect_keys)
         sub_regex = r"\1*********"
         self.do_path_regex_sub(agents_path, keys_regex, sub_regex)
         # Redact certificates
root@juju-6127db-lunar-testing1-3:/home/ubuntu/sos/sos# bin/sos report -o juju

sosreport (version 4.5.5)

This command will collect system configuration and diagnostic
information from this Ubuntu system.

For more information on Canonical visit:

        Community Website  : https://www.ubuntu.com/
        Commercial Support : https://www.canonical.com

The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.

No changes will be made to system configuration.


Press ENTER to continue, or CTRL-C to quit.

Optionally, please enter the case id that you are generating this report for []: 

 Setting up archive ...
 Setting up plugins ...
 Running plugins. Please wait ...

  Starting 1/1   juju            [Running: juju]                                          
  Finished running plugins                                                               
Creating compressed archive...

Your sosreport has been generated and saved in:
	/tmp/sosreport-juju-6127db-lunar-testing1-3-2023-07-07-ukxeuis.tar.xz

 Size	49.64KiB
 Owner	root
 sha256	8cf2cd322090adb5cc6ce6fc884bf994d5446048505cd3839efcf988a8f2ead8

Please send this file to your support representative.

root@juju-6127db-lunar-testing1-3:/home/ubuntu/sos/sos# 

Thoughts @arif-ali @dnegreira ?

@arif-ali
Copy link
Member

arif-ali commented Jul 7, 2023

This is a python3.11 issue

From the python 3.11 docs

(?aiLmsux)
(One or more letters from the set 'a', 'i', 'L', 'm', 's', 'u', 'x'.) The group matches the empty string; the letters set the corresponding flags: re.A (ASCII-only matching), re.I (ignore case), re.L (locale dependent), re.M (multi-line), re.S (dot matches all), re.U (Unicode matching), and re.X (verbose), for the entire regular expression. (The flags are described in Module Contents.) This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to the re.compile() function. Flags should be used first in the expression string.

Changed in version 3.11: This construction can only be used at the start of the expression.

So, in essence a lot of the regex in the sos code would need to change to conform to this new way, maybe something similar to the diff below, but all the plugins that have this regex format

diff --git a/sos/report/plugins/openstack_cinder.py b/sos/report/plugins/openstack_cinder.py
index 23483a8d..38eec6dc 100644
--- a/sos/report/plugins/openstack_cinder.py
+++ b/sos/report/plugins/openstack_cinder.py
@@ -94,13 +94,13 @@ class OpenStackCinder(Plugin):
         connection_keys = ["connection"]
 
         self.apply_regex_sub(
-            r"((?m)^\s*(%s)\s*=\s*)(.*)" % "|".join(protect_keys),
-            r"\1*********"
+            r"(?m)(^\s*(%s)\s*=\s*)(.*)" % "|".join(protect_keys),
+            r"\1\2*********"
         )
         self.apply_regex_sub(
-            r"((?m)^\s*(%s)\s*=\s*(.*)://(\w*):)(.*)(@(.*))" %
+            r"(?m)(^\s*(%s)\s*=\s*(.*)://(\w*):)(.*)(@(.*))" %
             "|".join(connection_keys),
-            r"\1*********\6"
+            r"\1\2*********\6"
         )

At first glance and greping for ((, there’s around 70 places this would need changing. Most openstack plugins, openshift, juju, grafana and such

@TurboTurtle
Copy link
Member

Ok, so looking at where/how this is implemented in our plugins, they're all using the same (?m) to make the following regex multi-line.

While this touches a decent number of plugins, the change is actually fairly simple. But, in changing this should we still keep this form of passing flags, or should we instead allow plugins to define their own flags in these calls (or perhaps even make re.M a default alongside re.I?)

arif-ali added a commit to arif-ali/sos that referenced this issue Jul 14, 2023
Add a common regex for re.MULTILINE so that doesn't need to be defined
and remove the definitions where `(?m)` is being defined

Closes: sosreport#3301
Signed-off-by: Arif Ali <arif.ali@canonical.com>
@arif-ali
Copy link
Member

We liked the idea of having a default of re.MULTILINE alongside re.I as per our discussions within Canonical , I've added a PR above

@nkshirsagar
Copy link
Contributor Author

nkshirsagar commented Jul 14, 2023

I tested the PR #3303 and it works well - testing shows the issue reproducing on the 4.5.4 lunar packages as well as upstream main but fixed in Arif's patch


ubuntu@nkshirsagar-bastion:~$ juju ssh cinder/0 sudo -s

root@juju-6127db-lunar-testing1-0:/home/ubuntu# sos report -o juju

sosreport (version 4.5.4)

This command will collect system configuration and diagnostic
information from this Ubuntu system.

For more information on Canonical visit:

        Community Website  : https://www.ubuntu.com/
        Commercial Support : https://www.canonical.com

The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.

No changes will be made to system configuration.


Press ENTER to continue, or CTRL-C to quit.

Optionally, please enter the case id that you are generating this report for []: 

 Setting up archive ...
 Setting up plugins ...
 Running plugins. Please wait ...

  Starting 1/1   juju            [Running: juju]                                          
  Finished running plugins                                                               
caught exception in plugin method "juju.postproc()"
writing traceback to sos_logs/juju-plugin-errors.txt
Creating compressed archive...

Your sosreport has been generated and saved in:
	/tmp/sosreport-juju-6127db-lunar-testing1-0-2023-07-14-qaquwcn.tar.xz

 Size	156.68KiB
 Owner	root
 sha256	7574fc460c2224f2b3997c4dea3162da19292cd24cb00bf5938c5d0b0094032a

Please send this file to your support representative.

root@juju-6127db-lunar-testing1-0:/home/ubuntu/sos_upstream# cd sos/
root@juju-6127db-lunar-testing1-0:/home/ubuntu/sos_upstream/sos# ls
AUTHORS  LICENSE  MANIFEST.in  README.md  bin  docs  man  plugins_overview.py  po  requirements.txt  setup.py  snap  sos  sos.conf  sos.spec  tests  tmpfiles
root@juju-6127db-lunar-testing1-0:/home/ubuntu/sos_upstream/sos# bin/sos report -o juju

sosreport (version 4.5.5)

This command will collect system configuration and diagnostic
information from this Ubuntu system.

For more information on Canonical visit:

        Community Website  : https://www.ubuntu.com/
        Commercial Support : https://www.canonical.com

The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.

No changes will be made to system configuration.


Press ENTER to continue, or CTRL-C to quit.

Optionally, please enter the case id that you are generating this report for []: 

 Setting up archive ...
 Setting up plugins ...
 Running plugins. Please wait ...

  Starting 1/1   juju            [Running: juju]                                          
  Finished running plugins                                                               
caught exception in plugin method "juju.postproc()"
writing traceback to sos_logs/juju-plugin-errors.txt
Creating compressed archive...

Your sosreport has been generated and saved in:
	/tmp/sosreport-juju-6127db-lunar-testing1-0-2023-07-14-mzbrnhq.tar.xz

 Size	156.66KiB
 Owner	root
 sha256	2f130b88bc092d4134af953f21151d8bc894e827050b24eff8de743cfc4b23ce

Please send this file to your support representative.

root@juju-6127db-lunar-testing1-0:/home/ubuntu/sos_arif/sos# git branch  -a | grep py311
  remotes/origin/py311_regex_fix
root@juju-6127db-lunar-testing1-0:/home/ubuntu/sos_arif/sos# git checkout py311_regex_fix
branch 'py311_regex_fix' set up to track 'origin/py311_regex_fix'.
Switched to a new branch 'py311_regex_fix'
root@juju-6127db-lunar-testing1-0:/home/ubuntu/sos_arif/sos# git log
commit f116210d57f0472b9d3369a309a9c71a4b2932d0 (HEAD -> py311_regex_fix, origin/py311_regex_fix)
Author: Arif Ali <arif.ali@canonical.com>
Date:   Fri Jul 14 12:50:58 2023 +0100

    [plugins] py311 fix for re.M
    
    Add a common regex for re.MULTILINE so that doesn't need to be defined
    and remove the definitions where `(?m)` is being defined
    
    Closes: #3301
    Signed-off-by: Arif Ali <arif.ali@canonical.com>

commit 59c2660584734af92eca2eae31af3fbf5557f853
Author: Jan Jansky <jjansky@redhat.com>
Date:   Mon Jul 10 13:10:22 2023 +0200

    [clean] Properly obfuscate MAC addresses
    
    Some of mac addresses was not properly obfuscated because
    some collected data contains mac addresses in format
    01:<mac> and parser was not ready for that.
    
    Also added mapper which will obfuscate mac address in case
    it is in format with _ instead of : as for example
    
    00_50_56_87_5d_01
    
    instead of
    
    00:50:56:87:5d:01
    
    Format with _ is used for example by vmware plugin.
    
    Resolves: #3302
    
    Signed-off-by: Jan Jansky <jjansky@redhat.com>

commit aade89e49303d69ccb9b636c5e53575e4c0614f4
Author: Pablo Acevedo Montserrat <pacevedo@redhat.com>
Date:   Tue Jul 4 17:15:15 2023 +0200

    [microshift] Add initial support for backup info
    
    Signed-off-by: Pablo Acevedo Montserrat <pacevedo@redhat.com>

root@juju-6127db-lunar-testing1-0:/home/ubuntu/sos_arif/sos# ls
AUTHORS  LICENSE  MANIFEST.in  README.md  bin  docs  man  plugins_overview.py  po  requirements.txt  setup.py  snap  sos  sos.conf  sos.spec  tests  tmpfiles
root@juju-6127db-lunar-testing1-0:/home/ubuntu/sos_arif/sos# bin/sos report -o juju 

sosreport (version 4.5.5)

This command will collect system configuration and diagnostic
information from this Ubuntu system.

For more information on Canonical visit:

        Community Website  : https://www.ubuntu.com/
        Commercial Support : https://www.canonical.com

The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.

No changes will be made to system configuration.


Press ENTER to continue, or CTRL-C to quit.

Optionally, please enter the case id that you are generating this report for []: 

 Setting up archive ...
 Setting up plugins ...
 Running plugins. Please wait ...

  Starting 1/1   juju            [Running: juju]                                          
  Finished running plugins                                                               
Creating compressed archive...

Your sosreport has been generated and saved in:
	/tmp/sosreport-juju-6127db-lunar-testing1-0-2023-07-14-smlqxub.tar.xz

 Size	154.75KiB
 Owner	root
 sha256	433cb52ec85739f605079ae3c2a1efcad6e61d498872bce5e38019499ffe0448

Please send this file to your support representative.

root@juju-6127db-lunar-testing1-0:/home/ubuntu/sos_arif/sos# bin/sos report -all-logs
usage: sos report [options]
sos report: error: argument -l/--list-plugins: ignored explicit argument '-logs'
root@juju-6127db-lunar-testing1-0:/home/ubuntu/sos_arif/sos# bin/sos report --all-logs

sosreport (version 4.5.5)

This command will collect system configuration and diagnostic
information from this Ubuntu system.

For more information on Canonical visit:

        Community Website  : https://www.ubuntu.com/
        Commercial Support : https://www.canonical.com

The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.

No changes will be made to system configuration.


Press ENTER to continue, or CTRL-C to quit.

Optionally, please enter the case id that you are generating this report for []: 

 Setting up archive ...
 Setting up plugins ...
[plugin:lxd] skipped command 'lxd.buginfo': required kmods missing: ip6_tables, iptable_mangle, ip6table_nat, iptable_filter, iptable_nat, bpfilter, iptable_raw, ip6table_filter, ebtables, ebtable_filter, ip6table_raw, ip6table_mangle.  
[plugin:networking] skipped command 'ip -s macsec show': required kmods missing: macsec.   Use '--allow-system-changes' to enable collection.
[plugin:networking] skipped command 'ss -peaonmi': required kmods missing: netlink_diag, xsk_diag, udp_diag, unix_diag, inet_diag, af_packet_diag, tcp_diag.   Use '--allow-system-changes' to enable collection.
[plugin:ufw] skipped command 'ufw status numbered': required kmods missing: iptable_filter, bpfilter.  
[plugin:ufw] skipped command 'ufw app list': required kmods missing: iptable_filter, bpfilter.  
 Running plugins. Please wait ...

  Finishing plugins              [Running: systemd]                                       
  Finished running plugins                                                               
Creating compressed archive...



Your sosreport has been generated and saved in:
	/tmp/sosreport-juju-6127db-lunar-testing1-0-2023-07-14-dnaoyod.tar.xz

 Size	74.89MiB
 Owner	root
 sha256	246172dbd28558a82d2d89c118f417a86cdc94b86f1bfcedb364b2af0fcc4f99

Please send this file to your support representative.

root@juju-6127db-lunar-testing1-0:/home/ubuntu/sos_arif/sos# 

TurboTurtle pushed a commit that referenced this issue Jul 19, 2023
Add a common regex for re.MULTILINE so that doesn't need to be defined
and remove the definitions where `(?m)` is being defined

Closes: #3301
Signed-off-by: Arif Ali <arif.ali@canonical.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants