1

还有几个其他问题讨论了从 LaunchAgents 访问钥匙串。

其中一个关键是这里joensson提到你需要在你的应用程序列表中设置一个<SessionCreate>

我已经这样做了,现在我的应用程序 plist 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.ionic.python.ionic-fs-watcher.startup</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Applications/IonicFSWatcher.app/Contents/MacOS/ionic-fs-watcher</string>
        <string>--debug</string>
        <string>/Users/timothy/ionicprotected</string>
        <string>--scan</string>
    </array>
    <key>UserName</key>
    <string>timothy</string>
    <key>SessionCreate</key>
    <true />
  </dict>
</plist>

该应用程序是一个 python 应用程序,它使用 pyinstaller 创建,使用 pkgbuild打包,并通过命令行安装。

从命令行运行时,应用程序运行良好。如果应用程序第一次运行,用户会收到允许访问钥匙串的提示,应用程序会从那里继续。

但是,当它作为LaunchAgent. 这是我用来测试的确切命令序列:

# Window 1
sudo launchctl unload -w ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist
sudo launchctl load -w ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist
sudo launchctl debug system/com.ionic.python.ionic-fs-watcher.startup --stdout --stderr

# Window 2
sudo launchctl kickstart -k -p system/com.ionic.python.ionic-fs-watcher.startup

在 python 脚本中,我一直在通过运行一些子命令并捕获输出进行调试。

# OK
status, output = commands.getstatusoutput("security list-keychains")
logger.error("Keychain list :%s (retcode = %s)" % (
    output, status
))
# OK
status, output = commands.getstatusoutput("security find-generic-password -a 'Ionic Security' ")
logger.error("Keychain list item :%s (retcode = %s)" % (
    output, status
))
# Fails, with error code: 9216
# Prompts immediately when running from command line
status, output = commands.getstatusoutput("security find-generic-password -a 'Ionic Security' -g")
logger.error("Keychain access :%s (retcode = %s)" % (
    output, status
))

该部分代码的输出如下所示:

# Correctly shows both keychains
ERROR:root:Keychain list :    "/Users/timothy/Library/Keychains/login.keychain"
    "/Library/Keychains/System.keychain" (retcode = 0)
# Correctly lists information about keychain item
ERROR:root:Keychain list item :keychain: "/Users/timothy/Library/Keychains/login.keychain"
<redacted>
# Fail
ERROR:root:Keychain access : (retcode = 9216)

这些相同的命令在命令行中可以正常工作,最后一个 ( -g) 会提示允许访问钥匙串。

我也尝试com.ionicsecurity.client.sdkKeyChain Access应用程序中打开条目,并设置“允许所有应用程序访问此项目”单选按钮。之后,从 cli 中获取值不再导致提示,但应用程序返回相同的错误代码。

我搜索了有关错误代码 9216 的信息,但没有结果。通过该实用程序运行代码security errors只会给出

$ security error 9216
Error: 0x00002400 9216 unknown error 9216=2400

在作为 LaunchAgent 运行时如何让应用程序访问钥匙串的任何帮助将不胜感激!

4

1 回答 1

3

问题是我用来启动 LaunchAgent 的域。我正在启动到根系统域,而不是启动到我正在为其设置 LaunchAgent 的用户的 gui 域。因为这

  • LaunchAgent 无权访问用户钥匙串(除非我请求以用户身份运行命令并创建会话)
  • LaunchAgent 没有任何方式向用户显示一个弹出窗口以请求访问钥匙串(我假设这就是为什么我在这里得到奇怪的错误代码)

这是从我之前使用的命令(不工作)到我现在使用的命令(工作)的映射。这些命令假定我们为其安装 LaunchAgent 的用户也是当前用户。

激活代理

# Before
sudo launchctl load -w ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist

# Now
launchctl enable user/`id -u`/com.ionic.python.ionic-fs-watcher.startup
launchctl bootstrap gui/`id -u` ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist

停用代理

# Before
sudo launchctl unload -w ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist

# Now
launchctl bootout gui/`id -u`/com.ionic.python.ionic-fs-watcher.startup
launchctl disable user/`id -u`/com.ionic.python.ionic-fs-watcher.startup

设置 LaunchAgent 定义的权限(plist 文件)

# Before
chown root:wheel ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist
chmod 644 ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist

# Now
chown "`id -un`":"`id -gn`" ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist
chmod 644 ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist

LaunchAgent plist 文件内容

UserNameSessionCreate块不是必需的。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.ionic.python.ionic-fs-watcher.startup</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Applications/IonicFSWatcher.app/Contents/MacOS/ionic-fs-watcher</string>
        <string>/Users/timothy/ionicprotected</string>
        <string>--scan</string>
    </array>
  </dict>
于 2018-03-16T14:41:11.863 回答