From cdedc58ec1bd8bb966087ab324da94b9034d7416 Mon Sep 17 00:00:00 2001
From: Paulo Castro <paulo@balena.io>
Date: Thu, 15 Aug 2019 15:38:50 +0100
Subject: [PATCH] Fix failing registry secrets authentication under certain
 conditions ('balena build' and 'balena deploy')

Change-type: patch
Signed-off-by: Paulo Castro <paulo@balena.io>
---
 doc/cli.markdown          | 24 ++++++++++++++++++------
 lib/actions/build.coffee  | 14 ++++++--------
 lib/actions/deploy.coffee | 19 ++++++++-----------
 lib/utils/messages.ts     |  8 ++++++--
 4 files changed, 38 insertions(+), 27 deletions(-)

diff --git a/doc/cli.markdown b/doc/cli.markdown
index da0bb230..9e731b02 100644
--- a/doc/cli.markdown
+++ b/doc/cli.markdown
@@ -1447,8 +1447,12 @@ Sample registry-secrets YAML file:
 		username: '_json_key'
 		password: '{escaped contents of the GCR keyfile.json file}'
 
-If an option is not specified, and a secrets.yml or secrets.json file exists in
-the balena directory (usually $HOME/.balena), this file will be used instead.
+For a sample project using registry secrets with the Google Container Registry,
+check: https://github.com/balena-io-playground/sample-gcr-registry-secrets
+
+If the --registry-secrets option is not specified, and a secrets.yml or
+secrets.json file exists in the balena directory (usually $HOME/.balena),
+this file will be used instead.
 
 Examples:
 
@@ -1598,8 +1602,12 @@ Sample registry-secrets YAML file:
 		username: '_json_key'
 		password: '{escaped contents of the GCR keyfile.json file}'
 
-If an option is not specified, and a secrets.yml or secrets.json file exists in
-the balena directory (usually $HOME/.balena), this file will be used instead.
+For a sample project using registry secrets with the Google Container Registry,
+check: https://github.com/balena-io-playground/sample-gcr-registry-secrets
+
+If the --registry-secrets option is not specified, and a secrets.yml or
+secrets.json file exists in the balena directory (usually $HOME/.balena),
+this file will be used instead.
 
 Examples:
 
@@ -1722,8 +1730,12 @@ Sample registry-secrets YAML file:
 		username: '_json_key'
 		password: '{escaped contents of the GCR keyfile.json file}'
 
-If an option is not specified, and a secrets.yml or secrets.json file exists in
-the balena directory (usually $HOME/.balena), this file will be used instead.
+For a sample project using registry secrets with the Google Container Registry,
+check: https://github.com/balena-io-playground/sample-gcr-registry-secrets
+
+If the --registry-secrets option is not specified, and a secrets.yml or
+secrets.json file exists in the balena directory (usually $HOME/.balena),
+this file will be used instead.
 
 Examples:
 
diff --git a/lib/actions/build.coffee b/lib/actions/build.coffee
index 44f4d0ad..a40f49df 100644
--- a/lib/actions/build.coffee
+++ b/lib/actions/build.coffee
@@ -109,17 +109,15 @@ module.exports =
 		Logger = require('../utils/logger')
 
 		logger = new Logger()
-
 		logger.logDebug('Parsing input...')
 
-		Promise.try ->
-			# `build` accepts `[source]` as a parameter, but compose expects it
-			# as an option. swap them here
-			options.source ?= params.source
-			delete params.source
-
-			validateComposeOptions(sdk, options)
+		# `build` accepts `[source]` as a parameter, but compose expects it
+		# as an option. swap them here
+		options.source ?= params.source
+		delete params.source
 
+		Promise.resolve(validateComposeOptions(sdk, options))
+		.then ->
 			{ application, arch, deviceType } = options
 
 			if (not (arch? and deviceType?) and not application?) or (application? and (arch? or deviceType?))
diff --git a/lib/actions/deploy.coffee b/lib/actions/deploy.coffee
index a34340e1..81c3e8ba 100644
--- a/lib/actions/deploy.coffee
+++ b/lib/actions/deploy.coffee
@@ -186,21 +186,18 @@ module.exports =
 		Logger = require('../utils/logger')
 
 		logger = new Logger()
-
 		logger.logDebug('Parsing input...')
 
-		appName = undefined
-		Promise.try ->
-			# when Capitano converts a positional parameter (but not an option)
-			# to a number, the original value is preserved with the _raw suffix
-			{ appName, appName_raw, image } = params
+		# when Capitano converts a positional parameter (but not an option)
+		# to a number, the original value is preserved with the _raw suffix
+		{ appName, appName_raw, image } = params
 
-			# look into "balena build" options if appName isn't given
-			appName = appName_raw || appName || options.application
-			delete options.application
-
-			validateComposeOptions(sdk, options)
+		# look into "balena build" options if appName isn't given
+		appName = appName_raw || appName || options.application
+		delete options.application
 
+		Promise.resolve(validateComposeOptions(sdk, options))
+		.then ->
 			if not appName?
 				throw new Error('Please specify the name of the application to deploy')
 
diff --git a/lib/utils/messages.ts b/lib/utils/messages.ts
index 0357785b..6c9035e1 100644
--- a/lib/utils/messages.ts
+++ b/lib/utils/messages.ts
@@ -45,5 +45,9 @@ Sample registry-secrets YAML file:
 		username: '_json_key'
 		password: '{escaped contents of the GCR keyfile.json file}'
 
-If an option is not specified, and a secrets.yml or secrets.json file exists in
-the balena directory (usually $HOME/.balena), this file will be used instead.`;
+For a sample project using registry secrets with the Google Container Registry,
+check: https://github.com/balena-io-playground/sample-gcr-registry-secrets
+
+If the --registry-secrets option is not specified, and a secrets.yml or
+secrets.json file exists in the balena directory (usually $HOME/.balena),
+this file will be used instead.`;