Add a keychain to search list?

て烟熏妆下的殇ゞ 提交于 2019-12-05 00:30:10

It's 2017 and on macos 10.12.4 security create-keychain still does not add a new keychain to the search list. Here's my script to add and destroy temporary keychains step by step:

#!/bin/bash -e

uuid="$(uuidgen)"

echo "New Keychain name: $uuid"

keychains=$(security list-keychains -d user)

keychainNames=();

for keychain in $keychains
do
  basename=$(basename "$keychain")
  keychainName=${basename::${#basename}-4}
  keychainNames+=("$keychainName")
done

echo "User keychains on this machine: ${keychainNames[@]}";




read -p "Enter to create keychain"
security -v create-keychain -p test123 $uuid

read -p "Enter to add keychain to searchlist"
security -v list-keychains -s "${keychainNames[@]}" $uuid

read -p "Enter to unlock keychain"
security -v unlock-keychain -p test123 $uuid

read -p "Enter to import certificate"
security -v import build-assets/certficate.p12 -k $uuid -P certificate_password

read -p "Enter to delete keychain"
security -v delete-keychain $uuid

Which automated tools are you using? I had a similar problem with building for iPhone using Jenkins under tomcat. I tried adding keychains in the shell script but it proved very flakey at best.

In the end, I worked around the problem by switching our build process to be running via LaunchAgents instead of LaunchDemons. This way the build tools run in the user context and things have become lot more reliable.

Is this a possibility for you? If so, I can provide more detail.

A one line version of @mles solution above:

security list-keychains -d user -s $(security list-keychains -d user | sed -e s/\"//g) <new keychain>

The issue with directly piping in the output of security list-keychains -d user is it surrounds the results with quotes. Solution uses sed to strip them out.

There is NOT a better way that I'm aware of - however it appears that maybe create-keychain will do what you want:

security create-keychain -h

returns:

Usage: create-keychain [-P] [-p password] [keychains...]
    -p  Use "password" as the password for the keychains being created
    -P  Prompt the user for a password using the SecurityAgent
Use of the -p option is insecure
        Create keychains and add them to the search list.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!