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

Fix macOS BigSur #57859

Merged
merged 7 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions salt/utils/mac_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Import Python Libraries
import logging
import os
import platform
import plistlib
import subprocess
import time
Expand Down Expand Up @@ -513,3 +514,34 @@ def console_user(username=False):
return pwd.getpwuid(uid)[0]

return uid


def os_version(only_major_minor=True, as_tuple=False):
"""
Get the macOS version of the machine.

:param bool only_major_minor: If true returns on the major and minor
version of the OS (10.15). Otherwise full version 10.15.2. Defaults
to ``True``

:param bool as_tuple: If true will make return type a tuple otherwise
a string.

:return: The current os version.

:rtype: Tuple of OS version or a string of osversion.

CLI Example:

.. code-block:: bash

import salt.utils.mac_utils
salt.utils.mac_utils.os_version(as_tuple=True)
"""
# lovingly borrowed from the munki project
os_version_tuple = platform.mac_ver()[0].split(".")
if only_major_minor:
os_version_tuple = os_version_tuple[0:2]
if as_tuple:
return tuple(map(int, os_version_tuple))
return ".".join(os_version_tuple)
31 changes: 19 additions & 12 deletions salt/utils/rsax931.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ctypes import c_char_p, c_int, c_void_p, cdll, create_string_buffer, pointer

# Import Salt libs
import salt.utils.mac_utils
import salt.utils.platform
import salt.utils.stringutils

Expand All @@ -30,6 +31,24 @@ def _find_libcrypto():
"""
if sys.platform.startswith("win"):
lib = str("libeay32")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that @s0undt3ch mentions it, here as well.

Copy link
Contributor

@kaorihinata kaorihinata Jul 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, found it. There was a comment here previously noting that Windows required the str call. Is this still the case, or was it an artifact of ancient (early Python 2, perhaps) history?

Edit: I know it wasn't your change (it was lost when I had to split the function for testing previously), but if you were copying this style below on line 51, then perhaps we should comment it again to avoid future confusion.

Copy link
Contributor Author

@weswhet weswhet Jul 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the comment back in for windows and removed the str cast for macOS as it's not required.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@twangboy do we still need this str() cast? I believe this was only necessary for Py2, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I validated that on windows salt with py3 the cast is not needed. @s0undt3ch how do you want to proceed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nuke it too :explosion: :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, Please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@s0undt3ch changed and ready to go :)

elif salt.utils.platform.is_darwin():
# will look for several different location on the system,
# Search in the following order. salts pkg, homebrew, macports, finnally
# system.
# look in salts pkg install location.
lib = glob.glob("/opt/salt/lib/libcrypto.dylib")
# Find library symlinks in Homebrew locations.
lib = lib or glob.glob("/usr/local/opt/openssl/lib/libcrypto.dylib")
lib = lib or glob.glob("/usr/local/opt/openssl@*/lib/libcrypto.dylib")
# look in macports.
lib = lib or glob.glob("/opt/local/lib/libcrypto.dylib")
# check if 10.15, regular libcrypto.dylib is just a false pointer.
if salt.utils.mac_utils.os_version(as_tuple=True) == (10, 15):
lib = lib or glob.glob("/usr/lib/libcrypto.*.dylib")
lib = list(reversed(sorted(lib)))
# last but not least all the other macOS versions should work here.
# including Big Sur
lib = lib[0] if lib else str("/usr/lib/libcrypto.dylib")
elif getattr(sys, "frozen", False) and salt.utils.platform.is_smartos():
lib = glob.glob(os.path.join(os.path.dirname(sys.executable), "libcrypto.so*"))
lib = lib[0] if lib else None
Expand All @@ -53,18 +72,6 @@ def _find_libcrypto():
else:
lib = glob.glob("/opt/freeware/lib/libcrypto.so*")
lib = lib[0] if lib else None
elif salt.utils.platform.is_darwin():
# Find versioned libraries in system locations, being careful
# to avoid the unversioned stub which is no longer permitted.
lib = glob.glob("/usr/lib/libcrypto.*.dylib")
if lib:
# Sort so as to prefer the newest version.
lib = list(reversed(sorted(lib)))
else:
# Find library symlinks in Homebrew locations.
lib = glob.glob("/usr/local/opt/openssl/lib/libcrypto.dylib")
lib = lib or glob.glob("/usr/local/opt/openssl@*/lib/libcrypto.dylib")
lib = lib[0] if lib else None
if not lib:
raise OSError("Cannot locate OpenSSL libcrypto")
return lib
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/utils/test_mac_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import absolute_import, unicode_literals

import os
import platform
import plistlib
import xml.parsers.expat

Expand Down Expand Up @@ -168,6 +169,18 @@ def test_validate_enabled_false(self):
"""
self.assertEqual(mac_utils.validate_enabled(False), "off")

@patch.object(platform, "mac_ver", lambda: ("10.15.5", ("", "", ""), "x86_64"))
def test_os_version(self):
"""
test multiple outputs of os_version
"""
self.assertEqual(mac_utils.os_version(), "10.15")
self.assertEqual(mac_utils.os_version(as_tuple=True), (10, 15))
self.assertEqual(mac_utils.os_version(only_major_minor=False), "10.15.5")
self.assertEqual(
mac_utils.os_version(only_major_minor=False, as_tuple=True), (10, 15, 5)
)

def test_launchctl(self):
"""
test launchctl function
Expand Down
34 changes: 32 additions & 2 deletions tests/unit/utils/test_rsax931.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,25 +174,55 @@ def test_find_libcrypto_aix(self):
)

@skipIf(not salt.utils.platform.is_darwin(), "Host OS is not Darwin-like or macOS.")
def test_find_libcrypto_darwin(self):
@patch.object(salt.utils.mac_utils, "os_version", lambda as_tuple: (10.14))
@patch.object(glob, "glob", lambda _: [])
def test_find_libcrypto_with_system_and_not_catalina(self):
"""
Test _find_libcrypto on a Darwin-like or macOS host.
Test _find_libcrypto on a Catalina-like macOS host, simulate
not finding any other libcryptos and just defaulting to system.
"""
lib_path = _find_libcrypto()
passed = False
for i in (
"/opt/salt/lib/libcrypto.dylib",
"/usr/local/opt/openssl/lib/libcrypto.dylib",
"/usr/local/opt/openssl@*/lib/libcrypto.dylib",
"/opt/local/lib/libcrypto.dylib",
"/usr/lib/libcrypto.*.dylib",
):
if fnmatch.fnmatch(lib_path, i):
passed = True
break
self.assertFalse(passed)
self.assertEqual(lib_path, "/usr/lib/libcrypto.dylib")

@skipIf(not salt.utils.platform.is_darwin(), "Host OS is not Darwin-like or macOS.")
@patch.object(salt.utils.mac_utils, "os_version", lambda as_tuple: (10.15))
def test_find_libcrypto_darwin_catalina(self):
"""
Test _find_libcrypto on a Darwin-like macOS host where there isn't a
lacation returned by ctypes.util.find_library()
"""
lib_path = _find_libcrypto()
passed = False
for i in (
"/opt/salt/lib/libcrypto.dylib",
"/usr/local/opt/openssl/lib/libcrypto.dylib",
"/usr/local/opt/openssl@*/lib/libcrypto.dylib",
"/opt/local/lib/libcrypto.dylib",
"/usr/lib/libcrypto.*.dylib",
):
if fnmatch.fnmatch(lib_path, i):
passed = True
break
self.assertTrue(passed)
# we should never return the stub on catalina
self.assertNotEqual(lib_path, "/usr/lib/libcrypto.dylib")

@patch.object(ctypes.util, "find_library", lambda a: None)
@patch.object(glob, "glob", lambda a: [])
@patch.object(sys, "platform", "unknown")
@patch.object(salt.utils.platform, "is_darwin", lambda: False)
def test_find_libcrypto_unsupported(self):
"""
Ensure that _find_libcrypto works correctly on an unsupported host OS.
Expand Down