/** throws if there are any problems */ public OsCredential checkNotEmpty() { checkNoErrors(); if (!hasKey() && !hasPassword()) { if (warningMessages.size()>0) throw new IllegalStateException("Could not find credentials: "+warningMessages); else throw new IllegalStateException("Could not find credentials"); } return this; }
@Test(expectedExceptions=IllegalStateException.class) public void testInvalidKeyData() throws Exception { ConfigBag config = ConfigBag.newInstance(); config.put(LocationConfigKeys.PRIVATE_KEY_DATA, "mydata"); LocationConfigUtils.OsCredential creds = LocationConfigUtils.getOsCredential(config).doKeyValidation(false); Assert.assertTrue(creds.hasKey()); Assert.assertFalse(creds.getWarningMessages().isEmpty()); creds.checkNoErrors(); }
@Test(groups="Integration") public void testReadsPrivateKeyFileWithPassphrase() throws Exception { ConfigBag config = ConfigBag.newInstance(); config.put(LocationConfigKeys.PRIVATE_KEY_FILE, SSH_PRIVATE_KEY_FILE_WITH_PASSPHRASE); LocationConfigUtils.OsCredential cred = LocationConfigUtils.getOsCredential(config).doKeyValidation(false); String data = cred.getPreferredCredential(); assertTrue(data != null && data.length() > 0); Assert.assertFalse(data.isEmpty()); cred.doKeyValidation(true); try { cred.checkNoErrors(); Assert.fail("check should fail as passphrase needed"); } catch (IllegalStateException exception) { } config.put(LocationConfigKeys.PRIVATE_KEY_PASSPHRASE, "passphrase"); cred.checkNoErrors(); config.put(LocationConfigKeys.PRIVATE_KEY_PASSPHRASE, "wrong_passphrase"); try { cred.checkNoErrors(); Assert.fail("check should fail as passphrase needed"); } catch (IllegalStateException exception) { } }