Repository: typesafehub/conductr-cli

Issue:
Credentials file can only contain one section
My credentials file are defined as follows:

$ cat .lightbend/commercial.credentials 
realm = Bintray
host = dl.bintray.com
user = [my username]
password = [my password]
realm = Bintray API Realm
host = api.bintray.com
user = [csp user]
password = [csp password]

I took the template from https://github.com/yuchaoran2011/csp-provisioning/blob/master/csp-environment/ansible/roles/install_conductr_cli/templates/commercial.credentials.j2

But when I ran:
`sandbox run 2.1.1 --feature visualization`,
I got the following error:

|------------------------------------------------|
| Starting ConductR                              |
|------------------------------------------------|
Bintray credentials loaded from /home/ubuntu/.lightbend/commercial.credentials
Error: Unable to fetch ConductR core artifact 2.1.1 from Bintray.
Error: Please specify a valid ConductR version.
Error: The latest version can be found on: https://www.lightbend.com/product/conductr/developer

Removing the last 4 lines of the credentials file solved the problem for me.

Discussion:

Patch:
diff --git a/1 b/1
new file mode 100644
index 0000000..e69de29
diff --git a/conductr_cli/resolvers/bintray_resolver.py b/conductr_cli/resolvers/bintray_resolver.py
index 4501b87..b01da9e 100644
--- a/conductr_cli/resolvers/bintray_resolver.py
+++ b/conductr_cli/resolvers/bintray_resolver.py
@@ -15,7 +15,7 @@ BINTRAY_API_BASE_URL = 'https://api.bintray.com'
 BINTRAY_DOWNLOAD_BASE_URL = 'https://dl.bintray.com'
 BINTRAY_DOWNLOAD_REALM = 'Bintray'
 BINTRAY_CREDENTIAL_FILE_PATH = '{}/.lightbend/commercial.credentials'.format(os.path.expanduser('~'))
-BINTRAY_PROPERTIES_RE = re.compile('^\s*(\S+)\s*=\s*([\S]+)\s*$')
+BINTRAY_PROPERTIES_RE = re.compile('^\s*(\S+)\s*=\s*((\S|\S+\s+\S+)+)\s*$')
 BINTRAY_LIGHTBEND_ORG = 'lightbend'
 BINTRAY_CONDUCTR_COMMERCIAL_REPO = 'commercial-releases'
 BINTRAY_CONDUCTR_GENERIC_REPO = 'generic'
@@ -168,12 +168,16 @@ def load_bintray_credentials(raise_error=True, disable_instructions=False):
         with open(BINTRAY_CREDENTIAL_FILE_PATH, 'r') as cred_file:
             lines = [line.replace('\n', '') for line in cred_file.readlines()]
             data = dict()
+            realm = BINTRAY_DOWNLOAD_REALM
             for line in lines:
                 match = BINTRAY_PROPERTIES_RE.match(line)
                 if match is not None:
                     try:
                         key, value = match.group(1, 2)
-                        data[key] = value
+                        if key == 'realm':
+                            realm = value
+                        elif realm == BINTRAY_DOWNLOAD_REALM:
+                            data[key] = value
                     except IndexError:
                         pass
 


Test patch:
diff --git a/conductr_cli/resolvers/test/test_bintray_resolver.py b/conductr_cli/resolvers/test/test_bintray_resolver.py
index 4790058..9cc48c3 100644
--- a/conductr_cli/resolvers/test/test_bintray_resolver.py
+++ b/conductr_cli/resolvers/test/test_bintray_resolver.py
@@ -1018,6 +1018,29 @@ class TestLoadBintrayCredentials(TestCase):
         exists_mock.assert_called_with('{}/.lightbend/commercial.credentials'.format(os.path.expanduser('~')))
         open_mock.assert_called_with('{}/.lightbend/commercial.credentials'.format(os.path.expanduser('~')), 'r')
 
+    def test_success_multiple_realms(self):
+        bintray_credential_file = (
+            'realm = Bintray\n'
+            'user = user1\n'
+            'password = sec=ret\n'
+            'realm = Bintray API Realm\n'
+            'user = user2\n'
+            'password = sec=ret2\n'
+        )
+
+        exists_mock = MagicMock(return_value=True)
+        open_mock = MagicMock(return_value=io.StringIO(bintray_credential_file))
+
+        with patch('os.path.exists', exists_mock), \
+                patch('builtins.open', open_mock):
+            realm, username, password = bintray_resolver.load_bintray_credentials()
+            self.assertEqual('Bintray', realm)
+            self.assertEqual('user1', username)
+            self.assertEqual('sec=ret', password)
+
+        exists_mock.assert_called_with('{}/.lightbend/commercial.credentials'.format(os.path.expanduser('~')))
+        open_mock.assert_called_with('{}/.lightbend/commercial.credentials'.format(os.path.expanduser('~')), 'r')
+
     def test_credential_file_not_having_username_password(self):
         bintray_credential_file = strip_margin(
             """|dummy = yes