misc/ios: read codesign info from environment variables

Use environment variables to allow set-and-forget.

Add a script to attempt to autodetect codesign info.

Change-Id: Ic56b9c5f097b1a4117ebb89c408bc333d91f581d
Reviewed-on: https://go-review.googlesource.com/8910
Reviewed-by: David Crawshaw <crawshaw@golang.org>
diff --git a/misc/ios/go_darwin_arm_exec.go b/misc/ios/go_darwin_arm_exec.go
index 5b044f2..917d9a3 100644
--- a/misc/ios/go_darwin_arm_exec.go
+++ b/misc/ios/go_darwin_arm_exec.go
@@ -11,6 +11,12 @@
 // the remote lldb session. This flag is appended to the end of the
 // script's arguments and is not passed through to the underlying
 // binary.
+//
+// This script requires that three environment variables be set:
+// 	GOIOS_DEV_ID: The codesigning developer id or certificate identifier
+// 	GOIOS_APP_ID: The provisioning app id prefix. Must support wildcard app ids.
+// 	GOIOS_TEAM_ID: The team id that owns the app id prefix.
+// $GOROOT/misc/ios contains a script, detect.go, that attempts to autodetect these.
 package main
 
 import (
@@ -37,6 +43,12 @@
 
 var tmpdir string
 
+var (
+	devID  string
+	appID  string
+	teamID string
+)
+
 func main() {
 	log.SetFlags(0)
 	log.SetPrefix("go_darwin_arm_exec: ")
@@ -47,6 +59,10 @@
 		log.Fatal("usage: go_darwin_arm_exec a.out")
 	}
 
+	devID = getenv("GOIOS_DEV_ID")
+	appID = getenv("GOIOS_APP_ID")
+	teamID = getenv("GOIOS_TEAM_ID")
+
 	var err error
 	tmpdir, err = ioutil.TempDir("", "go_darwin_arm_exec_")
 	if err != nil {
@@ -77,6 +93,14 @@
 	}
 }
 
+func getenv(envvar string) string {
+	s := os.Getenv(envvar)
+	if s == "" {
+		log.Fatalf("%s not set\nrun $GOROOT/misc/ios/detect.go to attempt to autodetect", s)
+	}
+	return s
+}
+
 func run(bin string, args []string) (err error) {
 	appdir := filepath.Join(tmpdir, "gotest.app")
 	os.RemoveAll(appdir)
@@ -89,7 +113,7 @@
 	}
 
 	entitlementsPath := filepath.Join(tmpdir, "Entitlements.plist")
-	if err := ioutil.WriteFile(entitlementsPath, []byte(entitlementsPlist), 0744); err != nil {
+	if err := ioutil.WriteFile(entitlementsPath, []byte(entitlementsPlist()), 0744); err != nil {
 		return err
 	}
 	if err := ioutil.WriteFile(filepath.Join(appdir, "Info.plist"), []byte(infoPlist), 0744); err != nil {
@@ -107,7 +131,7 @@
 	cmd := exec.Command(
 		"codesign",
 		"-f",
-		"-s", "E8BMC3FE2Z", // certificate associated with golang.org
+		"-s", devID,
 		"--entitlements", entitlementsPath,
 		appdir,
 	)
@@ -592,22 +616,22 @@
 </plist>
 `
 
-const devID = `YE84DJ86AZ`
-
-const entitlementsPlist = `<?xml version="1.0" encoding="UTF-8"?>
+func entitlementsPlist() string {
+	return `<?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>keychain-access-groups</key>
-	<array><string>` + devID + `.golang.gotest</string></array>
+	<array><string>` + teamID + `.golang.gotest</string></array>
 	<key>get-task-allow</key>
 	<true/>
 	<key>application-identifier</key>
-	<string>` + devID + `.golang.gotest</string>
+	<string>` + teamID + `.golang.gotest</string>
 	<key>com.apple.developer.team-identifier</key>
-	<string>` + devID + `</string>
+	<string>` + teamID + `</string>
 </dict>
 </plist>`
+}
 
 const resourceRules = `<?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">