Propagate Google API keys into the chroot.

These are a new type of credential baked into chrome/chromium for
developers without internal copies of ChromeOS, and not building internal
versions of Chrome.

We automatically move .googleapikeys into the chroot each time.

We don't overwrite the destination, so that people can configure keys the
way they want. If they just don't want to be bothered, the best thing happens
the easiest way.  Get Keys, put them in home.  Keep working.

BUG=chromium-os:34438
TEST=local

Reviewed-on: https://gerrit.chromium.org/gerrit/33771
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Ready: Peter Mayo <petermayo@chromium.org>
Tested-by: Peter Mayo <petermayo@chromium.org>

Change-Id: I1b5b766db79d4c022f2ab5872fe08725581472f5
Reviewed-on: https://gerrit.chromium.org/gerrit/34136
Reviewed-by: David James <davidjames@chromium.org>
Tested-by: Peter Mayo <petermayo@chromium.org>
diff --git a/sdk_lib/enter_chroot.sh b/sdk_lib/enter_chroot.sh
index 8e76444..619529d 100755
--- a/sdk_lib/enter_chroot.sh
+++ b/sdk_lib/enter_chroot.sh
@@ -56,6 +56,8 @@
 Otherwise, provides an interactive shell.
 "
 
+CROS_LOG_PREFIX=cros_sdk:enter_chroot
+
 # Version of info from common.sh that only echos if --verbose is set.
 debug() {
   if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then
@@ -212,6 +214,35 @@
   cp "$1" "${FLAGS_chroot}/$2"
 }
 
+# Usage: promote_api_keys
+# This takes care of getting the developer API keys into the chroot where
+# chrome can build with them.  It needs to take it from the places a dev
+# is likely to put them, and recognize that older chroots may or may not
+# have been used since the concept of keys got added, as well as before
+# and after the developer decding to grab his own keys.
+promote_api_keys() {
+  local destination="${FLAGS_chroot}/home/${USER}/.googleapikeys"
+  # Don't disturb existing keys.  They could be set differently
+  if [[ -s "${destination}" ]]; then
+    return 0
+  fi
+  if [[ -r "${HOME}/.googleapikeys" ]]; then
+    cp "${HOME}/.googleapikeys" "${destination}"
+    if [[ -s "${destination}" ]] ; then
+      info "Copied Google API keys into chroot."
+    fi
+  elif [[ -r "${HOME}/.gyp/include.gypi" ]]; then
+    local NAME="('google_(api_key|default_client_(id|secret))')"
+    local WS="[[:space:]]*"
+    local CONTENTS="('[^\\\\']*')"
+    sed -nr -e "/^${WS}${NAME}${WS}[:=]${WS}${CONTENTS}.*/{s//\1: \4,/;p;}" \
+         "${HOME}/.gyp/include.gypi" >"${destination}"
+    if [[ -s "${destination}" ]]; then
+      info "Put discovered Google API keys into chroot."
+    fi
+  fi
+}
+
 setup_env() {
   # Validate sudo timestamp before entering the critical section so that we
   # don't stall for a password while we have the lockfile.
@@ -433,6 +464,7 @@
     for fn in "${FILES_TO_COPY_TO_CHROOT[@]}"; do
       copy_into_chroot_if_exists "${HOME}/${fn}" "/home/${USER}/${fn}"
     done
+    promote_api_keys
 
     # Make sure user's requested locales are available
     # http://crosbug.com/19139
diff --git a/sdk_lib/make_chroot.sh b/sdk_lib/make_chroot.sh
index 6f957c9..94dfe82 100755
--- a/sdk_lib/make_chroot.sh
+++ b/sdk_lib/make_chroot.sh
@@ -52,7 +52,7 @@
 eval set -- "${FLAGS_ARGV}"
 check_flags_only_and_allow_null_arg "$@" && set --
 
-CROS_LOG_PREFIX=cros_sdk
+CROS_LOG_PREFIX=cros_sdk:make_chroot
 
 assert_not_root_user
 # Set the right umask for chroot creation.