@VisibleForTesting public String execAsRoot(String command) { if (node.getCredentials().identity.equals("root")) { } else if (node.getCredentials().shouldAuthenticateSudo()) { command = String.format("sudo -S sh <<'%s'\n%s\n%s%s\n", MARKER, node.getCredentials().getPassword(), command, MARKER); } else { command = String.format("sudo sh <<'%s'\n%s%s\n", MARKER, command, MARKER); } return command; }
@VisibleForTesting public String execAsRoot(String command) { if (node.getCredentials().identity.equals("root")) { } else if (node.getCredentials().shouldAuthenticateSudo()) { command = String.format("sudo -S sh <<'%s'\n%s\n%s%s\n", MARKER, node.getCredentials().getPassword(), command, MARKER); } else { command = String.format("sudo sh <<'%s'\n%s%s\n", MARKER, command, MARKER); } return command; }
ExecResponse runCommand(String command) { String statement = String.format("[%s] as %s@%s", command.replace( node.getCredentials().getPassword() != null ? node.getCredentials().getPassword() : "XXXXX", "XXXXX"), ssh .getUsername(), ssh.getHostAddress()); if (command.endsWith("status") || command.endsWith("stdout") || command.endsWith("stderr")) logger.trace(">> running %s", statement); else computeLogger.debug(">> running " + statement); ExecResponse returnVal = ssh.exec(command); if (!command.endsWith("status")) checkState(returnVal.getExitStatus() == 0, "error running %s; returnVal !=0: %s", statement, returnVal); return returnVal; }
@VisibleForTesting String execScriptAsRoot(String action) { String command; if (node.getCredentials().identity.equals("root")) { command = initFile + " " + action; } else if (node.getCredentials().shouldAuthenticateSudo()) { command = String.format("echo '%s'|sudo -S %s %s", node.getCredentials().getPassword(), initFile, action); } else { command = "sudo " + initFile + " " + action; } return command; }
ExecResponse runCommand(String command) { String statement = String.format("[%s] as %s@%s", command.replace( node.getCredentials().getPassword() != null ? node.getCredentials().getPassword() : "XXXXX", "XXXXX"), ssh .getUsername(), ssh.getHostAddress()); if (command.endsWith("status") || command.endsWith("stdout") || command.endsWith("stderr")) logger.trace(">> running %s", statement); else computeLogger.debug(">> running " + statement); ExecResponse returnVal = ssh.exec(command); if (!command.endsWith("status")) checkState(returnVal.getExitStatus() == 0, "error running %s; returnVal !=0: %s", statement, returnVal); return returnVal; }
@Override public void authenticate(Connection connection) throws IOException { String u = credentials.getUser(); if(!connection.authenticateWithPublicKey(u, credentials.getPrivateKey().toCharArray(), credentials.getPassword())){ throw new IOException(String.format("Public key authentication failed: trying to login as %s@%s with %s", u, connection.getHostname(), keyPair.privateKey)); } }
@VisibleForTesting String execScriptAsRoot(String action) { String command; if (node.getCredentials().identity.equals("root")) { command = initFile + " " + action; } else if (node.getCredentials().shouldAuthenticateSudo()) { command = String.format("echo '%s'|sudo -S %s %s", node.getCredentials().getPassword(), initFile, action); } else { command = "sudo " + initFile + " " + action; } return command; }
protected ExecResponse runCommand(String command) { ExecResponse returnVal; logger.debug(">> running [%s] as %s@%s", command.replace(node.getCredentials().getPassword() != null ? node .getCredentials().getPassword() : "XXXXX", "XXXXX"), ssh.getUsername(), ssh.getHostAddress()); returnVal = ssh.exec(command); return returnVal; }
protected ExecResponse runCommand(String command) { ExecResponse returnVal; logger.debug(">> running [%s] as %s@%s", command.replace(node.getCredentials().getPassword() != null ? node .getCredentials().getPassword() : "XXXXX", "XXXXX"), ssh.getUsername(), ssh.getHostAddress()); returnVal = ssh.exec(command); return returnVal; }
public JschSshClient(ProxyConfig proxyConfig, BackoffLimitedRetryHandler backoffLimitedRetryHandler, HostAndPort socket, LoginCredentials loginCredentials, int timeout) { this.user = checkNotNull(loginCredentials, "loginCredentials").getUser(); this.host = checkNotNull(socket, "socket").getHostText(); checkArgument(socket.getPort() > 0, "ssh port must be greater then zero" + socket.getPort()); checkArgument(loginCredentials.getPassword() != null || loginCredentials.getPrivateKey() != null, "you must specify a password or a key"); this.backoffLimitedRetryHandler = checkNotNull(backoffLimitedRetryHandler, "backoffLimitedRetryHandler"); if (loginCredentials.getPrivateKey() == null) { this.toString = String.format("%s:pw[%s]@%s:%d", loginCredentials.getUser(), base16().lowerCase().encode(md5().hashString(loginCredentials.getPassword(), UTF_8).asBytes()), host, socket.getPort()); } else { String fingerPrint = fingerprintPrivateKey(loginCredentials.getPrivateKey()); String sha1 = sha1PrivateKey(loginCredentials.getPrivateKey()); this.toString = String.format("%s:rsa[fingerprint(%s),sha1(%s)]@%s:%d", loginCredentials.getUser(), fingerPrint, sha1, host, socket.getPort()); } sessionConnection = SessionConnection.builder().hostAndPort(HostAndPort.fromParts(host, socket.getPort())).loginCredentials( loginCredentials).proxy(checkNotNull(proxyConfig, "proxyConfig")).connectTimeout(timeout).sessionTimeout(timeout).build(); }
public LoginCredentials apply(Template template, LoginCredentials fromNode) { LoginCredentials creds = fromNode; LoginCredentials credsFromParameters = credentialsFromImageOrTemplateOptions.apply(template); if (credsFromParameters != null) { Builder builder = LoginCredentials.builder(creds); if (credsFromParameters.getUser() != null) builder.user(credsFromParameters.getUser()); if (credsFromParameters.getPassword() != null) builder.password(credsFromParameters.getPassword()); if (credsFromParameters.getPrivateKey() != null) builder.privateKey(credsFromParameters.getPrivateKey()); if (credsFromParameters.shouldAuthenticateSudo()) builder.authenticateSudo(true); creds = builder.build(); } return creds; }
@Override public InputStream apply(Credentials from) { checkNotNull(from, "inputCredentials"); if (from instanceof LoginCredentials) { LoginCredentials login = LoginCredentials.class.cast(from); JsonLoginCredentials val = new JsonLoginCredentials(); val.user = login.getUser(); val.password = login.getPassword(); val.privateKey = login.getPrivateKey(); if (login.shouldAuthenticateSudo()) val.authenticateSudo = login.shouldAuthenticateSudo(); return Strings2.toInputStream(json.toJson(val)); } return Strings2.toInputStream(json.toJson(from)); } }
public LoginCredentials apply(Template template, LoginCredentials fromNode) { LoginCredentials creds = fromNode; LoginCredentials credsFromParameters = credentialsFromImageOrTemplateOptions.apply(template); if (credsFromParameters != null) { Builder builder = LoginCredentials.builder(creds); if (credsFromParameters.getUser() != null) builder.user(credsFromParameters.getUser()); if (credsFromParameters.getPassword() != null) builder.password(credsFromParameters.getPassword()); if (credsFromParameters.getPrivateKey() != null) builder.privateKey(credsFromParameters.getPrivateKey()); if (credsFromParameters.shouldAuthenticateSudo()) builder.authenticateSudo(true); creds = builder.build(); } return creds; }
@Override public InputStream apply(Credentials from) { checkNotNull(from, "inputCredentials"); if (from instanceof LoginCredentials) { LoginCredentials login = LoginCredentials.class.cast(from); JsonLoginCredentials val = new JsonLoginCredentials(); val.user = login.getUser(); val.password = login.getPassword(); val.privateKey = login.getPrivateKey(); if (login.shouldAuthenticateSudo()) val.authenticateSudo = login.shouldAuthenticateSudo(); return Strings2.toInputStream(json.toJson(val)); } return Strings2.toInputStream(json.toJson(from)); } }
@Override public InputStream apply(Credentials from) { checkNotNull(from, "inputCredentials"); if (from instanceof LoginCredentials) { LoginCredentials login = LoginCredentials.class.cast(from); JsonLoginCredentials val = new JsonLoginCredentials(); val.user = login.getUser(); val.password = login.getPassword(); val.privateKey = login.getPrivateKey(); if (login.shouldAuthenticateSudo()) val.authenticateSudo = login.shouldAuthenticateSudo(); return Strings2.toInputStream(json.toJson(val)); } return Strings2.toInputStream(json.toJson(from)); } }
@Test public void testApply() throws Exception { WindowsLoginCredentialsFromEncryptedData f = new WindowsLoginCredentialsFromEncryptedData(new JCECrypto()); LoginCredentials credentials = f.apply(new EncryptedPasswordAndPrivateKey(ENCRYPTED_PASSWORD, PRIVATE_KEY)); assertEquals(credentials.getUser(), "Administrator"); assertEquals(credentials.getPassword(), "u4.y9mb;nR."); assertFalse(credentials.getOptionalPrivateKey().isPresent()); } }
@Override public LoginCredentials apply(@Nullable org.jclouds.domain.LoginCredentials input) { if (input == null) { return null; } return LoginCredentials.builder().username(input.getUser()).password(input.getPassword()) .privateKey(input.getPrivateKey()).authenticateSudo(input.shouldAuthenticateSudo()) .build(); } }
protected Map<String,Object> extractSshConfig(ConfigBag setup, NodeMetadata node) { ConfigBag nodeConfig = new ConfigBag(); if (node!=null) { nodeConfig.putIfNotNull(PASSWORD, node.getCredentials().getPassword()); nodeConfig.putIfNotNull(PRIVATE_KEY_DATA, node.getCredentials().getPrivateKey()); } return extractSshConfig(setup, nodeConfig).getAllConfigRaw(); }
@Test public void testApply() throws Exception { WindowsLoginCredentialsFromEncryptedData f = new WindowsLoginCredentialsFromEncryptedData(new JCECrypto()); PasswordData passwordData = PasswordData.builder() .instanceId("i-2574e22a") .timestamp(dateService.iso8601DateParse("2012-07-30T07:27:23.000+0000")) .passwordData(ENCRYPTED_PASSWORD).build(); LoginCredentials credentials = f.apply(new PasswordDataAndPrivateKey(passwordData, PRIVATE_KEY)); assertEquals(credentials.getUser(), "Administrator"); assertEquals(credentials.getPassword(), "u4.y9mb;nR."); assertFalse(credentials.getOptionalPrivateKey().isPresent()); } }
@Test public void testWindowsAdminWorks() throws Exception { String group = "winadm"; // Spin up a new node. Make sure to open the RDP port 3389 Template template = view.getComputeService().templateBuilder().from(windowsTemplate).options(inboundPorts(3389)) .build(); try { NodeMetadata node = Iterables.getOnlyElement(view.getComputeService().createNodesInGroup(group, 1, template)); assertEquals(node.getCredentials().getUser(), "Administrator"); assertFalse(Strings.isNullOrEmpty(node.getCredentials().getPassword())); } finally { view.getComputeService().destroyNodesMatching(NodePredicates.inGroup(group)); } }