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 adding certificates with empty password to keychain on macOS 15.1 #436

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ Version 0.54.2
-------------

**Bugfixes**
- Fix action `keychain add-certificates` for macOS 15.1 when adding certificates with empty password. [PR #436](https://github.com/codemagic-ci-cd/cli-tools/pull/436)
- Introduce a new retrying condition for `altool` commands as part of `app-store-connect` action when unexpected return codes occurs. [PR #435](https://github.com/codemagic-ci-cd/cli-tools/pull/435)


Version 0.54.1
-------------

Expand Down
30 changes: 19 additions & 11 deletions src/codemagic/tools/keychain.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class KeychainError(cli.CliAppException):
pass


class _CertificateDataDecodeError(IOError):
class _SecurityKeychainPkcs12FormatImportError(IOError):
pass


Expand Down Expand Up @@ -392,7 +392,7 @@ def _add_certificate(
allowed_applications=allowed_applications,
import_format="pkcs12",
)
except _CertificateDataDecodeError:
except _SecurityKeychainPkcs12FormatImportError:
# Attempt import again, but now using different format specifier.
self._run_add_certificate_process(
certificate_path=certificate_path,
Expand Down Expand Up @@ -434,17 +434,25 @@ def _run_add_certificate_process(
process = self.execute(import_cmd, obfuscate_patterns=obfuscate_patterns)

if process.returncode == 0:
return
elif "The specified item already exists in the keychain" in process.stderr:
# It is fine that the certificate is already in keychain
pass
elif import_format == "pkcs12" and "Unable to decode the provided data" in process.stderr:
return # All good, certificate was successfully imported

if "The specified item already exists in the keychain" in process.stderr:
return # It is fine that the certificate is already in keychain

if import_format == "pkcs12":
# MacOS has not been very compliant with unencrypted PEM-formatted PKCS#12
# containers generated by OpenSSL. But starting from macOS 15.0 security
# just rejects them with error message "Unable to decode the provided data".
raise _CertificateDataDecodeError()
else:
raise KeychainError(f"Unable to add certificate {certificate_path} to keychain {self.path}", process)
# just rejects them with the following message in STDERR stream:
# `security: SecKeychainItemImport: Unable to decode the provided data.`
if "Unable to decode the provided data" in process.stderr:
raise _SecurityKeychainPkcs12FormatImportError()
# On macOS 15.1 importing PKCS#12 containers that are exported from Keychain Access with
# empty password fails when using pkcs12 format specifier with this message in STDERR:
# "security: SecKeychainItemImport: The user name or passphrase you entered is not correct."
if "The user name or passphrase you entered is not correct" in process.stderr:
raise _SecurityKeychainPkcs12FormatImportError()

raise KeychainError(f"Unable to add certificate {certificate_path} to keychain {self.path}", process)

def _find_certificates(self):
process = self.execute(("security", "find-certificate", "-a", "-p", self.path), show_output=False)
Expand Down
Loading