1
0
mirror of https://github.com/golang/go synced 2024-11-23 16:30:06 -07:00
go/misc/ios/detect.go

135 lines
3.2 KiB
Go
Raw Normal View History

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// +build ignore
// detect attempts to autodetect the correct
// values of the environment variables
// used by go_ios_exec.
// detect shells out to ideviceinfo, a third party program that can
// be obtained by following the instructions at
// https://github.com/libimobiledevice/libimobiledevice.
package main
import (
"bytes"
misc/ios: make detect.go more robust To enable the exec wrapper go_darwin_arm_exec.go to run binaries on iOS devices, the GOIOS_DEV_ID variable needs to be set to a code signing identity. The program detect.go attempts to detect suitable values for GOIOS_DEV_ID (along with GOIOS_APP_ID and GOIOS_TEAM_ID). Before this change, detect.go would use "security find-identity -p codesigning -v" to list all available identities for code signing and pick the first one with "iPhone Developer" in its name. However, that pick might be invalid since if it was replaced by an identity issued later. For example, on the mobile builder: $ security find-identity -p codesigning -v 1) 0E251DE41FE4490574E475AC320B47F58D6D3635 "lldb_codesign" 2) 0358588D07AA6A19478981BA405F40A97F95F187 "iPhone Developer: xxx@xxx (2754T98W8E)" 3) FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E "iPhone Developer: xxx@xxx (2754T98W8E)" 3 valid identities found In this case, the identity 0358588D07AA6A19478981BA405F40A97F95F187 is picked by detect.go even though it has been invalidated by FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E. Instead of attempting to find an identity from the "security find-identity" list, use the identity from the CommonName in the embedded certificate in the provisioning file. The CommonName only lists the identity name (iPhone Developer: xxx@xxx (2754T98W8E)), not the fingerprint (FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E), but fortunately the codesign tool accepts both. Identity names may not be unique, as demonstrated by the example, but that will result in an ambiguity error at codesigning instead of a more obscure error about an invalid identity when go_darwin_arm_exec.go runs a binary. The fix is then to delete the invalid identity from the system keychain. While here, find all connected devices instead of the first connected and only consider provision files that covers them all. This matters for the mobile builder where two devices are connected. Change-Id: I6beb59ace3fc5e071ba76222a20a607765943989 Reviewed-on: https://go-review.googlesource.com/105436 Run-TryBot: Elias Naur <elias.naur@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-04-07 09:22:43 -06:00
"crypto/x509"
"fmt"
"os"
"os/exec"
"strings"
)
func main() {
misc/ios: make detect.go more robust To enable the exec wrapper go_darwin_arm_exec.go to run binaries on iOS devices, the GOIOS_DEV_ID variable needs to be set to a code signing identity. The program detect.go attempts to detect suitable values for GOIOS_DEV_ID (along with GOIOS_APP_ID and GOIOS_TEAM_ID). Before this change, detect.go would use "security find-identity -p codesigning -v" to list all available identities for code signing and pick the first one with "iPhone Developer" in its name. However, that pick might be invalid since if it was replaced by an identity issued later. For example, on the mobile builder: $ security find-identity -p codesigning -v 1) 0E251DE41FE4490574E475AC320B47F58D6D3635 "lldb_codesign" 2) 0358588D07AA6A19478981BA405F40A97F95F187 "iPhone Developer: xxx@xxx (2754T98W8E)" 3) FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E "iPhone Developer: xxx@xxx (2754T98W8E)" 3 valid identities found In this case, the identity 0358588D07AA6A19478981BA405F40A97F95F187 is picked by detect.go even though it has been invalidated by FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E. Instead of attempting to find an identity from the "security find-identity" list, use the identity from the CommonName in the embedded certificate in the provisioning file. The CommonName only lists the identity name (iPhone Developer: xxx@xxx (2754T98W8E)), not the fingerprint (FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E), but fortunately the codesign tool accepts both. Identity names may not be unique, as demonstrated by the example, but that will result in an ambiguity error at codesigning instead of a more obscure error about an invalid identity when go_darwin_arm_exec.go runs a binary. The fix is then to delete the invalid identity from the system keychain. While here, find all connected devices instead of the first connected and only consider provision files that covers them all. This matters for the mobile builder where two devices are connected. Change-Id: I6beb59ace3fc5e071ba76222a20a607765943989 Reviewed-on: https://go-review.googlesource.com/105436 Run-TryBot: Elias Naur <elias.naur@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-04-07 09:22:43 -06:00
udids := getLines(exec.Command("idevice_id", "-l"))
if len(udids) == 0 {
fail("no udid found; is a device connected?")
}
misc/ios: make detect.go more robust To enable the exec wrapper go_darwin_arm_exec.go to run binaries on iOS devices, the GOIOS_DEV_ID variable needs to be set to a code signing identity. The program detect.go attempts to detect suitable values for GOIOS_DEV_ID (along with GOIOS_APP_ID and GOIOS_TEAM_ID). Before this change, detect.go would use "security find-identity -p codesigning -v" to list all available identities for code signing and pick the first one with "iPhone Developer" in its name. However, that pick might be invalid since if it was replaced by an identity issued later. For example, on the mobile builder: $ security find-identity -p codesigning -v 1) 0E251DE41FE4490574E475AC320B47F58D6D3635 "lldb_codesign" 2) 0358588D07AA6A19478981BA405F40A97F95F187 "iPhone Developer: xxx@xxx (2754T98W8E)" 3) FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E "iPhone Developer: xxx@xxx (2754T98W8E)" 3 valid identities found In this case, the identity 0358588D07AA6A19478981BA405F40A97F95F187 is picked by detect.go even though it has been invalidated by FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E. Instead of attempting to find an identity from the "security find-identity" list, use the identity from the CommonName in the embedded certificate in the provisioning file. The CommonName only lists the identity name (iPhone Developer: xxx@xxx (2754T98W8E)), not the fingerprint (FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E), but fortunately the codesign tool accepts both. Identity names may not be unique, as demonstrated by the example, but that will result in an ambiguity error at codesigning instead of a more obscure error about an invalid identity when go_darwin_arm_exec.go runs a binary. The fix is then to delete the invalid identity from the system keychain. While here, find all connected devices instead of the first connected and only consider provision files that covers them all. This matters for the mobile builder where two devices are connected. Change-Id: I6beb59ace3fc5e071ba76222a20a607765943989 Reviewed-on: https://go-review.googlesource.com/105436 Run-TryBot: Elias Naur <elias.naur@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-04-07 09:22:43 -06:00
mps := detectMobileProvisionFiles(udids)
if len(mps) == 0 {
misc/ios: make detect.go more robust To enable the exec wrapper go_darwin_arm_exec.go to run binaries on iOS devices, the GOIOS_DEV_ID variable needs to be set to a code signing identity. The program detect.go attempts to detect suitable values for GOIOS_DEV_ID (along with GOIOS_APP_ID and GOIOS_TEAM_ID). Before this change, detect.go would use "security find-identity -p codesigning -v" to list all available identities for code signing and pick the first one with "iPhone Developer" in its name. However, that pick might be invalid since if it was replaced by an identity issued later. For example, on the mobile builder: $ security find-identity -p codesigning -v 1) 0E251DE41FE4490574E475AC320B47F58D6D3635 "lldb_codesign" 2) 0358588D07AA6A19478981BA405F40A97F95F187 "iPhone Developer: xxx@xxx (2754T98W8E)" 3) FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E "iPhone Developer: xxx@xxx (2754T98W8E)" 3 valid identities found In this case, the identity 0358588D07AA6A19478981BA405F40A97F95F187 is picked by detect.go even though it has been invalidated by FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E. Instead of attempting to find an identity from the "security find-identity" list, use the identity from the CommonName in the embedded certificate in the provisioning file. The CommonName only lists the identity name (iPhone Developer: xxx@xxx (2754T98W8E)), not the fingerprint (FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E), but fortunately the codesign tool accepts both. Identity names may not be unique, as demonstrated by the example, but that will result in an ambiguity error at codesigning instead of a more obscure error about an invalid identity when go_darwin_arm_exec.go runs a binary. The fix is then to delete the invalid identity from the system keychain. While here, find all connected devices instead of the first connected and only consider provision files that covers them all. This matters for the mobile builder where two devices are connected. Change-Id: I6beb59ace3fc5e071ba76222a20a607765943989 Reviewed-on: https://go-review.googlesource.com/105436 Run-TryBot: Elias Naur <elias.naur@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-04-07 09:22:43 -06:00
fail("did not find mobile provision matching device udids %q", udids)
}
fmt.Println("# Available provisioning profiles below.")
fmt.Println("# NOTE: Any existing app on the device with the app id specified by GOIOS_APP_ID")
fmt.Println("# will be overwritten when running Go programs.")
for _, mp := range mps {
fmt.Println()
f, err := os.CreateTemp("", "go_ios_detect_")
check(err)
fname := f.Name()
defer os.Remove(fname)
out := output(parseMobileProvision(mp))
_, err = f.Write(out)
check(err)
check(f.Close())
misc/ios: make detect.go more robust To enable the exec wrapper go_darwin_arm_exec.go to run binaries on iOS devices, the GOIOS_DEV_ID variable needs to be set to a code signing identity. The program detect.go attempts to detect suitable values for GOIOS_DEV_ID (along with GOIOS_APP_ID and GOIOS_TEAM_ID). Before this change, detect.go would use "security find-identity -p codesigning -v" to list all available identities for code signing and pick the first one with "iPhone Developer" in its name. However, that pick might be invalid since if it was replaced by an identity issued later. For example, on the mobile builder: $ security find-identity -p codesigning -v 1) 0E251DE41FE4490574E475AC320B47F58D6D3635 "lldb_codesign" 2) 0358588D07AA6A19478981BA405F40A97F95F187 "iPhone Developer: xxx@xxx (2754T98W8E)" 3) FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E "iPhone Developer: xxx@xxx (2754T98W8E)" 3 valid identities found In this case, the identity 0358588D07AA6A19478981BA405F40A97F95F187 is picked by detect.go even though it has been invalidated by FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E. Instead of attempting to find an identity from the "security find-identity" list, use the identity from the CommonName in the embedded certificate in the provisioning file. The CommonName only lists the identity name (iPhone Developer: xxx@xxx (2754T98W8E)), not the fingerprint (FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E), but fortunately the codesign tool accepts both. Identity names may not be unique, as demonstrated by the example, but that will result in an ambiguity error at codesigning instead of a more obscure error about an invalid identity when go_darwin_arm_exec.go runs a binary. The fix is then to delete the invalid identity from the system keychain. While here, find all connected devices instead of the first connected and only consider provision files that covers them all. This matters for the mobile builder where two devices are connected. Change-Id: I6beb59ace3fc5e071ba76222a20a607765943989 Reviewed-on: https://go-review.googlesource.com/105436 Run-TryBot: Elias Naur <elias.naur@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-04-07 09:22:43 -06:00
cert, err := plistExtract(fname, "DeveloperCertificates:0")
check(err)
pcert, err := x509.ParseCertificate(cert)
check(err)
fmt.Printf("export GOIOS_DEV_ID=\"%s\"\n", pcert.Subject.CommonName)
appID, err := plistExtract(fname, "Entitlements:application-identifier")
check(err)
fmt.Printf("export GOIOS_APP_ID=%s\n", appID)
teamID, err := plistExtract(fname, "Entitlements:com.apple.developer.team-identifier")
check(err)
fmt.Printf("export GOIOS_TEAM_ID=%s\n", teamID)
}
}
misc/ios: make detect.go more robust To enable the exec wrapper go_darwin_arm_exec.go to run binaries on iOS devices, the GOIOS_DEV_ID variable needs to be set to a code signing identity. The program detect.go attempts to detect suitable values for GOIOS_DEV_ID (along with GOIOS_APP_ID and GOIOS_TEAM_ID). Before this change, detect.go would use "security find-identity -p codesigning -v" to list all available identities for code signing and pick the first one with "iPhone Developer" in its name. However, that pick might be invalid since if it was replaced by an identity issued later. For example, on the mobile builder: $ security find-identity -p codesigning -v 1) 0E251DE41FE4490574E475AC320B47F58D6D3635 "lldb_codesign" 2) 0358588D07AA6A19478981BA405F40A97F95F187 "iPhone Developer: xxx@xxx (2754T98W8E)" 3) FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E "iPhone Developer: xxx@xxx (2754T98W8E)" 3 valid identities found In this case, the identity 0358588D07AA6A19478981BA405F40A97F95F187 is picked by detect.go even though it has been invalidated by FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E. Instead of attempting to find an identity from the "security find-identity" list, use the identity from the CommonName in the embedded certificate in the provisioning file. The CommonName only lists the identity name (iPhone Developer: xxx@xxx (2754T98W8E)), not the fingerprint (FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E), but fortunately the codesign tool accepts both. Identity names may not be unique, as demonstrated by the example, but that will result in an ambiguity error at codesigning instead of a more obscure error about an invalid identity when go_darwin_arm_exec.go runs a binary. The fix is then to delete the invalid identity from the system keychain. While here, find all connected devices instead of the first connected and only consider provision files that covers them all. This matters for the mobile builder where two devices are connected. Change-Id: I6beb59ace3fc5e071ba76222a20a607765943989 Reviewed-on: https://go-review.googlesource.com/105436 Run-TryBot: Elias Naur <elias.naur@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-04-07 09:22:43 -06:00
func detectMobileProvisionFiles(udids [][]byte) []string {
cmd := exec.Command("mdfind", "-name", ".mobileprovision")
lines := getLines(cmd)
var files []string
for _, line := range lines {
if len(line) == 0 {
continue
}
xmlLines := getLines(parseMobileProvision(string(line)))
misc/ios: make detect.go more robust To enable the exec wrapper go_darwin_arm_exec.go to run binaries on iOS devices, the GOIOS_DEV_ID variable needs to be set to a code signing identity. The program detect.go attempts to detect suitable values for GOIOS_DEV_ID (along with GOIOS_APP_ID and GOIOS_TEAM_ID). Before this change, detect.go would use "security find-identity -p codesigning -v" to list all available identities for code signing and pick the first one with "iPhone Developer" in its name. However, that pick might be invalid since if it was replaced by an identity issued later. For example, on the mobile builder: $ security find-identity -p codesigning -v 1) 0E251DE41FE4490574E475AC320B47F58D6D3635 "lldb_codesign" 2) 0358588D07AA6A19478981BA405F40A97F95F187 "iPhone Developer: xxx@xxx (2754T98W8E)" 3) FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E "iPhone Developer: xxx@xxx (2754T98W8E)" 3 valid identities found In this case, the identity 0358588D07AA6A19478981BA405F40A97F95F187 is picked by detect.go even though it has been invalidated by FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E. Instead of attempting to find an identity from the "security find-identity" list, use the identity from the CommonName in the embedded certificate in the provisioning file. The CommonName only lists the identity name (iPhone Developer: xxx@xxx (2754T98W8E)), not the fingerprint (FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E), but fortunately the codesign tool accepts both. Identity names may not be unique, as demonstrated by the example, but that will result in an ambiguity error at codesigning instead of a more obscure error about an invalid identity when go_darwin_arm_exec.go runs a binary. The fix is then to delete the invalid identity from the system keychain. While here, find all connected devices instead of the first connected and only consider provision files that covers them all. This matters for the mobile builder where two devices are connected. Change-Id: I6beb59ace3fc5e071ba76222a20a607765943989 Reviewed-on: https://go-review.googlesource.com/105436 Run-TryBot: Elias Naur <elias.naur@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-04-07 09:22:43 -06:00
matches := 0
for _, udid := range udids {
for _, xmlLine := range xmlLines {
if bytes.Contains(xmlLine, udid) {
matches++
}
}
}
misc/ios: make detect.go more robust To enable the exec wrapper go_darwin_arm_exec.go to run binaries on iOS devices, the GOIOS_DEV_ID variable needs to be set to a code signing identity. The program detect.go attempts to detect suitable values for GOIOS_DEV_ID (along with GOIOS_APP_ID and GOIOS_TEAM_ID). Before this change, detect.go would use "security find-identity -p codesigning -v" to list all available identities for code signing and pick the first one with "iPhone Developer" in its name. However, that pick might be invalid since if it was replaced by an identity issued later. For example, on the mobile builder: $ security find-identity -p codesigning -v 1) 0E251DE41FE4490574E475AC320B47F58D6D3635 "lldb_codesign" 2) 0358588D07AA6A19478981BA405F40A97F95F187 "iPhone Developer: xxx@xxx (2754T98W8E)" 3) FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E "iPhone Developer: xxx@xxx (2754T98W8E)" 3 valid identities found In this case, the identity 0358588D07AA6A19478981BA405F40A97F95F187 is picked by detect.go even though it has been invalidated by FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E. Instead of attempting to find an identity from the "security find-identity" list, use the identity from the CommonName in the embedded certificate in the provisioning file. The CommonName only lists the identity name (iPhone Developer: xxx@xxx (2754T98W8E)), not the fingerprint (FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E), but fortunately the codesign tool accepts both. Identity names may not be unique, as demonstrated by the example, but that will result in an ambiguity error at codesigning instead of a more obscure error about an invalid identity when go_darwin_arm_exec.go runs a binary. The fix is then to delete the invalid identity from the system keychain. While here, find all connected devices instead of the first connected and only consider provision files that covers them all. This matters for the mobile builder where two devices are connected. Change-Id: I6beb59ace3fc5e071ba76222a20a607765943989 Reviewed-on: https://go-review.googlesource.com/105436 Run-TryBot: Elias Naur <elias.naur@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-04-07 09:22:43 -06:00
if matches == len(udids) {
files = append(files, string(line))
}
}
return files
}
func parseMobileProvision(fname string) *exec.Cmd {
return exec.Command("security", "cms", "-D", "-i", string(fname))
}
func plistExtract(fname string, path string) ([]byte, error) {
out, err := exec.Command("/usr/libexec/PlistBuddy", "-c", "Print "+path, fname).CombinedOutput()
if err != nil {
return nil, err
}
return bytes.TrimSpace(out), nil
}
func getLines(cmd *exec.Cmd) [][]byte {
out := output(cmd)
misc/ios: make detect.go more robust To enable the exec wrapper go_darwin_arm_exec.go to run binaries on iOS devices, the GOIOS_DEV_ID variable needs to be set to a code signing identity. The program detect.go attempts to detect suitable values for GOIOS_DEV_ID (along with GOIOS_APP_ID and GOIOS_TEAM_ID). Before this change, detect.go would use "security find-identity -p codesigning -v" to list all available identities for code signing and pick the first one with "iPhone Developer" in its name. However, that pick might be invalid since if it was replaced by an identity issued later. For example, on the mobile builder: $ security find-identity -p codesigning -v 1) 0E251DE41FE4490574E475AC320B47F58D6D3635 "lldb_codesign" 2) 0358588D07AA6A19478981BA405F40A97F95F187 "iPhone Developer: xxx@xxx (2754T98W8E)" 3) FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E "iPhone Developer: xxx@xxx (2754T98W8E)" 3 valid identities found In this case, the identity 0358588D07AA6A19478981BA405F40A97F95F187 is picked by detect.go even though it has been invalidated by FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E. Instead of attempting to find an identity from the "security find-identity" list, use the identity from the CommonName in the embedded certificate in the provisioning file. The CommonName only lists the identity name (iPhone Developer: xxx@xxx (2754T98W8E)), not the fingerprint (FC6D96F24A3223C98BF7A2C2C5194D82E04CD23E), but fortunately the codesign tool accepts both. Identity names may not be unique, as demonstrated by the example, but that will result in an ambiguity error at codesigning instead of a more obscure error about an invalid identity when go_darwin_arm_exec.go runs a binary. The fix is then to delete the invalid identity from the system keychain. While here, find all connected devices instead of the first connected and only consider provision files that covers them all. This matters for the mobile builder where two devices are connected. Change-Id: I6beb59ace3fc5e071ba76222a20a607765943989 Reviewed-on: https://go-review.googlesource.com/105436 Run-TryBot: Elias Naur <elias.naur@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-04-07 09:22:43 -06:00
lines := bytes.Split(out, []byte("\n"))
// Skip the empty line at the end.
if len(lines[len(lines)-1]) == 0 {
lines = lines[:len(lines)-1]
}
return lines
}
func output(cmd *exec.Cmd) []byte {
out, err := cmd.Output()
if err != nil {
fmt.Println(strings.Join(cmd.Args, "\n"))
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return out
}
func check(err error) {
if err != nil {
fail(err.Error())
}
}
func fail(msg string, v ...interface{}) {
fmt.Fprintf(os.Stderr, msg, v...)
fmt.Fprintln(os.Stderr)
os.Exit(1)
}