/** 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(); }
public void testPreferPublicKeyDataOverFileAndNoPrivateKeyRequired() throws Exception { ConfigBag config = ConfigBag.newInstance(); config.put(LocationConfigKeys.PUBLIC_KEY_DATA, "mydata"); config.put(LocationConfigKeys.PUBLIC_KEY_FILE, SSH_PUBLIC_KEY_FILE); config.put(LocationConfigKeys.PRIVATE_KEY_FILE, ""); LocationConfigUtils.OsCredential creds = LocationConfigUtils.getOsCredential(config); String data = creds.getPublicKeyData(); assertEquals(data, "mydata"); Assert.assertNull(creds.getPreferredCredential()); Assert.assertFalse(creds.hasPassword()); Assert.assertFalse(creds.hasKey()); // and not even any warnings here Assert.assertTrue(creds.getWarningMessages().isEmpty()); }
/** if a password is available, and either this is preferred over a key or there is no key */ public boolean isUsingPassword() { return hasPassword() && (!hasKey() || preferPassword); }
/** returns either the key or password or null; if both a key and a password this prefers the key unless otherwise set * via {@link #preferPassword()} */ public synchronized String getPreferredCredential() { infer(); if (isUsingPassword()) return password; if (hasKey()) return privateKeyData; return null; }
/** returns the un-passphrased key-pair info if a key is being used, or else null */ public KeyPair findKeyPair() { OsCredential creds = LocationConfigUtils.getOsCredential(config().getBag()); if (creds.hasKey()) { String data = creds.getPrivateKeyData(); return SecureKeys.readPem(data.getBytes(), getConfig(SshTool.PROP_PRIVATE_KEY_PASSPHRASE)); } else { return null; } }
/** if there is no credential (ignores public key) */ public boolean isEmpty() { return !hasKey() && !hasPassword(); } public boolean hasKey() {
public void testPreferPrivateKeyDataOverFile() throws Exception { ConfigBag config = ConfigBag.newInstance(); config.put(LocationConfigKeys.PRIVATE_KEY_DATA, "mydata"); config.put(LocationConfigKeys.PRIVATE_KEY_FILE, SSH_PRIVATE_KEY_FILE); LocationConfigUtils.OsCredential creds = LocationConfigUtils.getOsCredential(config).doKeyValidation(false); Assert.assertTrue(creds.hasKey()); // warnings, as it is malformed Assert.assertFalse(creds.getWarningMessages().isEmpty()); String data = creds.getPrivateKeyData(); assertEquals(data, "mydata"); }