<p>Friendly Automation <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/testsuite/+/19450">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span></span><br></pre><div style="white-space:pre-wrap">Approvals:
  George Joseph: Looks good to me, approved
  Friendly Automation: Approved for Submit

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">python libs: Add capability to set file permissions on a tests files<br><br>When testing encryption related scenarios, key files may need to<br>have specific permissions set in order for them to be used. See<br>https://issues.asterisk.org/jira/browse/ASTERISK-30213 for more<br>context.  Since git doesn't natively support saving file<br>permissions other than the executable bit, we needed a way to set<br>them ourselves.<br><br>* The asterisk instance setup functions have been updated to<br>  look for a file named ".permissions" in a test's config and<br>  files directories which contains a list of files and octal<br>  permissions to set before running the test.<br><br>  For files in the config directory that are copied to<br>  /etc/asterisk, the .permissions file must be in the<br>  testdir/configs/astX directory.<br><br>  Example:<br>  testdir/<br>    test_config.yaml<br>    configs/<br>      ast1/<br>        .permissions<br>        mykey.pem<br><br>  The .permissions file would have the following:<br>  # Permissions file for private key<br>  mykey.pem:0600<br><br>  For files copied to other places, the .permissions file<br>  must be under the last symbolic part of the path.<br>  Example:<br><br>  testdir/<br>    test_config.yaml<br>    files/<br>      ast1/<br>        astvarlibdir/<br>          .permissions<br>          keys/<br>            test.key<br><br>  The .permissions file would have the following:<br>  # Permissions file for private key<br>  keys/test.key:0600<br><br>  When this particular test runs, the .permissions file<br>  will be processed and permissions set before the test<br>  actually executes.<br><br>* .permissions files  have been created for the two immediate tests<br>  with issues:<br>  tests/channels/iax2/encrypted-calls/rsa<br>  tests/channels/iax2/encrypted-calls/rsa-dynamic<br><br>Change-Id: I4169b64c4e61b5856468a048c9fa138d81209831<br>---<br>M lib/python/asterisk/asterisk.py<br>M lib/python/asterisk/test_suite_utils.py<br>A tests/channels/iax2/encrypted-calls/rsa-dynamic/files/ast1/astvarlibdir/.permissions<br>A tests/channels/iax2/encrypted-calls/rsa/files/ast1/astvarlibdir/.permissions<br>4 files changed, 121 insertions(+), 0 deletions(-)<br><br></pre>
<pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/lib/python/asterisk/asterisk.py b/lib/python/asterisk/asterisk.py</span><br><span>index d88cfb5..b32e760 100644</span><br><span>--- a/lib/python/asterisk/asterisk.py</span><br><span>+++ b/lib/python/asterisk/asterisk.py</span><br><span>@@ -723,6 +723,15 @@</span><br><span>             if os.path.isfile(target):</span><br><span>                 self.install_config(target)</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+        permissions_file = os.path.join(cfg_path, ".permissions")</span><br><span style="color: hsl(120, 100%, 40%);">+        if os.path.exists(permissions_file):</span><br><span style="color: hsl(120, 100%, 40%);">+            try:</span><br><span style="color: hsl(120, 100%, 40%);">+                test_suite_utils.set_file_permissions(</span><br><span style="color: hsl(120, 100%, 40%);">+                    self.astetcdir, permissions_file)</span><br><span style="color: hsl(120, 100%, 40%);">+            except BaseException as err:</span><br><span style="color: hsl(120, 100%, 40%);">+                raise Exception("All permissions were not applied to '%s': %s" % (target_path, str(err)))</span><br><span style="color: hsl(120, 100%, 40%);">+                </span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>     def install_config(self, cfg_path, target_filename=None):</span><br><span>         """Install a custom configuration file for this instance of Asterisk.</span><br><span> </span><br><span>@@ -841,6 +850,13 @@</span><br><span>                 dirpath = direntry[1:] if direntry[0] == '/' else direntry</span><br><span>                 dest = os.path.join(self.base, dirpath)</span><br><span>                 polyfill.copytree(source, dest, dirs_exist_ok=True)</span><br><span style="color: hsl(120, 100%, 40%);">+                permissions_file = os.path.join(source, ".permissions")</span><br><span style="color: hsl(120, 100%, 40%);">+                if os.path.exists(permissions_file):</span><br><span style="color: hsl(120, 100%, 40%);">+                    try:</span><br><span style="color: hsl(120, 100%, 40%);">+                        test_suite_utils.set_file_permissions(</span><br><span style="color: hsl(120, 100%, 40%);">+                            dest, permissions_file)</span><br><span style="color: hsl(120, 100%, 40%);">+                    except BaseException as err:</span><br><span style="color: hsl(120, 100%, 40%);">+                        raise Exception("All permissions were not applied to '%s': %s" % (dest, str(err)))</span><br><span> </span><br><span>     def _overwrite_file(self, filename, values):</span><br><span>         """Overwrite a particular config file</span><br><span>diff --git a/lib/python/asterisk/test_suite_utils.py b/lib/python/asterisk/test_suite_utils.py</span><br><span>index 185adaa..dcf4bbc 100644</span><br><span>--- a/lib/python/asterisk/test_suite_utils.py</span><br><span>+++ b/lib/python/asterisk/test_suite_utils.py</span><br><span>@@ -14,6 +14,8 @@</span><br><span> import logging</span><br><span> import re</span><br><span> import sys</span><br><span style="color: hsl(120, 100%, 40%);">+import stat</span><br><span style="color: hsl(120, 100%, 40%);">+import numpy as np</span><br><span> </span><br><span> from os import close</span><br><span> from os import remove</span><br><span>@@ -210,3 +212,41 @@</span><br><span>                 _ast_conf.directories[var] = val</span><br><span> </span><br><span>     return _ast_conf</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+def set_file_permissions(dir_path, permissions_file):</span><br><span style="color: hsl(120, 100%, 40%);">+    """Set permissions on files from a file</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    The permissions_file must be a list in the format</span><br><span style="color: hsl(120, 100%, 40%);">+    <relative_path>:<octal_permissions></span><br><span style="color: hsl(120, 100%, 40%);">+    <relative_path>:<octal_permissions></span><br><span style="color: hsl(120, 100%, 40%);">+    ...</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    relative_path: A path relative to dir_path.  If a directory</span><br><span style="color: hsl(120, 100%, 40%);">+                   is specified, only the directory permissions</span><br><span style="color: hsl(120, 100%, 40%);">+                   are changed not the contents.</span><br><span style="color: hsl(120, 100%, 40%);">+    permissions: 3 or 4 digit octal permissions</span><br><span style="color: hsl(120, 100%, 40%);">+    </span><br><span style="color: hsl(120, 100%, 40%);">+    Whitespace and blank lines are ignored.</span><br><span style="color: hsl(120, 100%, 40%);">+    The '#' is the comment character.</span><br><span style="color: hsl(120, 100%, 40%);">+    </span><br><span style="color: hsl(120, 100%, 40%);">+    Example:</span><br><span style="color: hsl(120, 100%, 40%);">+    <testdir>/files/ast1/astvarlibdir/.permissions:</span><br><span style="color: hsl(120, 100%, 40%);">+    # My permissions file</span><br><span style="color: hsl(120, 100%, 40%);">+    keys/key.pem: 0600</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    Note: The paths in the .permissions file must be below the</span><br><span style="color: hsl(120, 100%, 40%);">+    last replaceable element in the dir_path.  In this example,</span><br><span style="color: hsl(120, 100%, 40%);">+    it has to be below astvarlibdir.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    :param dir_path: Path containing the directories/files to change</span><br><span style="color: hsl(120, 100%, 40%);">+    :param permissions_file: File with the subdirectories/files and new permissions.</span><br><span style="color: hsl(120, 100%, 40%);">+    """</span><br><span style="color: hsl(120, 100%, 40%);">+    converter = lambda x: int(x, 8)</span><br><span style="color: hsl(120, 100%, 40%);">+    permlist=np.genfromtxt(permissions_file,</span><br><span style="color: hsl(120, 100%, 40%);">+        dtype="S255,I", names="filename,permissions",</span><br><span style="color: hsl(120, 100%, 40%);">+        delimiter=':', autostrip=True,</span><br><span style="color: hsl(120, 100%, 40%);">+        converters={"permissions": converter})</span><br><span style="color: hsl(120, 100%, 40%);">+    for filename, permissions in permlist.flat:</span><br><span style="color: hsl(120, 100%, 40%);">+        path=os.path.join(dir_path, filename.decode('utf-8'))</span><br><span style="color: hsl(120, 100%, 40%);">+        os.chmod(path,permissions)</span><br><span style="color: hsl(120, 100%, 40%);">+    return None</span><br><span>diff --git a/tests/channels/iax2/encrypted-calls/rsa-dynamic/files/ast1/astvarlibdir/.permissions b/tests/channels/iax2/encrypted-calls/rsa-dynamic/files/ast1/astvarlibdir/.permissions</span><br><span>new file mode 100644</span><br><span>index 0000000..9bd35f8</span><br><span>--- /dev/null</span><br><span>+++ b/tests/channels/iax2/encrypted-calls/rsa-dynamic/files/ast1/astvarlibdir/.permissions</span><br><span>@@ -0,0 +1 @@</span><br><span style="color: hsl(120, 100%, 40%);">+keys/test.key:0600</span><br><span>diff --git a/tests/channels/iax2/encrypted-calls/rsa/files/ast1/astvarlibdir/.permissions b/tests/channels/iax2/encrypted-calls/rsa/files/ast1/astvarlibdir/.permissions</span><br><span>new file mode 100644</span><br><span>index 0000000..9bd35f8</span><br><span>--- /dev/null</span><br><span>+++ b/tests/channels/iax2/encrypted-calls/rsa/files/ast1/astvarlibdir/.permissions</span><br><span>@@ -0,0 +1 @@</span><br><span style="color: hsl(120, 100%, 40%);">+keys/test.key:0600</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/testsuite/+/19450">change 19450</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/testsuite/+/19450"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: testsuite </div>
<div style="display:none"> Gerrit-Branch: 19 </div>
<div style="display:none"> Gerrit-Change-Id: I4169b64c4e61b5856468a048c9fa138d81209831 </div>
<div style="display:none"> Gerrit-Change-Number: 19450 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>