【问题标题】:Running security import command from Python has different behaviour than command line从 Python 运行安全导入命令的行为与命令行不同
【发布时间】:2016-10-13 11:59:02
【问题描述】:

我正在尝试使用以下命令将 pkcs#12 证书导入 OS X 钥匙串:

security import filename -k ~/Library/Keychains/login.keychain -P password -f pkcs12

在 python 中,我像这样使用subprocess

  if os.path.isfile(_file) and platform.system() == 'Darwin':
    keychain = os.path.expanduser('~/Library/Keychains/login.keychain')
    command_line = 'security import {} -k {} -P {} -f pkcs12'.format(_file, keychain, password)
    logger.info('Importing {} into OS X KeyChain.'.format(_file))
    return subprocess.call(shlex.split(command_line))

但是我收到此错误消息:

security: SecKeychainItemImport: One or more parameters passed to a function were not valid.

我什至尝试过使用shell=True,但后来我得到了security 的用法,就好像我传递了一些错误的论点一样。

Usage: security [-h] [-i] [-l] [-p prompt] [-q] [-v] [command] [opt ...]
...
...

但是,当从命令行运行它时,该命令按预期工作:

security import <filename> -k <home>/Library/Keychains/login.keychain -P DTWLDHPYNBWBJB3 -f pkcs12
1 identity imported.
1 certificate imported.

有什么想法吗?从非交互式控制台运行security 时是否有限制?

任何python库都可以实现相同的功能吗?

问候

【问题讨论】:

    标签: python macos keychain pkcs#12 pyopenssl


    【解决方案1】:

    这实际上是由于另一个问题。

    我使用的 tmpfile 没有被刷新或关闭。

    当脚本运行时,该函数在该文件中找不到任何内容。

    脚本结束后,文件(“delete=False”)被刷新,因此命令行运行没有问题。

    解决方案是设置bufsize=0 :(

    【讨论】:

      猜你喜欢
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 2015-11-03
      • 2013-12-27
      • 2018-01-30
      • 2017-07-04
      相关资源
      最近更新 更多