private static LoginCredentials getLoginForCommandExecution(final Action action) { try { String user = System.getProperty("user.name"); String privateKey = Files.toString(new File(System.getProperty("user.home") + "/.ssh/id_rsa"), UTF_8); return LoginCredentials.builder().user(user).privateKey(privateKey) .authenticateSudo(true).build(); } catch (Exception e) { System.err.println("error reading ssh key " + e.getMessage()); System.exit(1); return null; } }
public void testWhenCredentialsNotPresentAndProviderPropertyHasUserAndPasswordAndSudo() { @SuppressWarnings("unchecked") Map<String, Credentials> credstore = createMock(Map.class); LoginCredentials expected = LoginCredentials.builder().user("ubuntu").password("password").authenticateSudo(true) .build(); ValueOfConfigurationKeyOrNull config = createMock(ValueOfConfigurationKeyOrNull.class); expect(credstore.containsKey("image")).andReturn(false); expect(config.apply("provider.image.login-user")).andReturn("ubuntu:password"); expect(config.apply("provider.image.authenticate-sudo")).andReturn("true"); expect(credstore.put("image", expected)).andReturn(null); replay(config); replay(credstore); GetLoginForProviderFromPropertiesAndStoreCredentialsOrReturnNull fn = new GetLoginForProviderFromPropertiesAndStoreCredentialsOrReturnNull( "provider", config, credstore); assertEquals(fn.get(), expected); verify(config); verify(credstore); }
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.getOptionalPassword().isPresent()) builder.password(credsFromParameters.getOptionalPassword().get()); if (credsFromParameters.getOptionalPrivateKey().isPresent()) builder.privateKey(credsFromParameters.getOptionalPrivateKey().get()); if (credsFromParameters.shouldAuthenticateSudo()) builder.authenticateSudo(true); creds = builder.build(); } return creds; }
private LoginCredentials resolveNodeCredentials(Template template) { TemplateOptions options = template.getOptions(); LoginCredentials.Builder credentials = LoginCredentials.builder(template.getImage().getDefaultCredentials()); if (!Strings.isNullOrEmpty(options.getLoginUser())) { credentials.user(options.getLoginUser()); } if (!Strings.isNullOrEmpty(options.getLoginPrivateKey())) { credentials.privateKey(options.getLoginPrivateKey()); } if (!Strings.isNullOrEmpty(options.getLoginPassword())) { credentials.password(options.getLoginPassword()); } if (options.shouldAuthenticateSudo() != null) { credentials.authenticateSudo(options.shouldAuthenticateSudo()); } return credentials.build(); }
@Override public Credentials apply(InputStream from) { try { String creds = Strings2.toStringAndClose(checkNotNull(from)); if (creds.indexOf("\"user\":") == -1) { return json.fromJson(creds, Credentials.class); } else { JsonLoginCredentials val = json.fromJson(creds, JsonLoginCredentials.class); return LoginCredentials.builder().user(val.user).password(val.password).privateKey(val.privateKey) .authenticateSudo(Boolean.TRUE.equals(val.authenticateSudo)).build(); } } catch (Exception e) { logger.warn(e, "ignoring problem retrieving credentials"); return null; } } }
@Override public Credentials apply(InputStream from) { try { String creds = Strings2.toStringAndClose(checkNotNull(from)); if (creds.indexOf("\"user\":") == -1) { return json.fromJson(creds, Credentials.class); } else { JsonLoginCredentials val = json.fromJson(creds, JsonLoginCredentials.class); return LoginCredentials.builder().user(val.user).password(val.password).privateKey(val.privateKey) .authenticateSudo(Boolean.TRUE.equals(val.authenticateSudo)).build(); } } catch (Exception e) { logger.warn(e, "ignoring problem retrieving credentials"); return null; } } }
@Override public Credentials apply(InputStream from) { try { String creds = Strings2.toStringAndClose(checkNotNull(from)); if (creds.indexOf("\"user\":") == -1) { return json.fromJson(creds, Credentials.class); } else { JsonLoginCredentials val = json.fromJson(creds, JsonLoginCredentials.class); return LoginCredentials.builder().user(val.user).password(val.password).privateKey(val.privateKey) .authenticateSudo(Boolean.TRUE.equals(val.authenticateSudo)).build(); } } catch (Exception e) { logger.warn(e, "ignoring problem retrieving credentials"); return null; } } }
public void simpleRootTestWithSudoPassword() { node = createMock(NodeMetadata.class); expect(node.getCredentials()).andReturn(LoginCredentials.builder().user("tester").password("testpassword!").authenticateSudo(true).build()) .atLeastOnce(); replay(node); RunScriptOnNodeUsingSsh testMe = new RunScriptOnNodeUsingSsh(sshFactory, eventBus, node, exec("echo $USER\necho $USER"), wrapInInitScript(false).runAsRoot(true)); testMe.init(); sshClient.connect(); expect(sshClient.getUsername()).andReturn("tester"); expect(sshClient.getHostAddress()).andReturn("somewhere.example.com"); expect( sshClient.exec("sudo -S sh <<'RUN_SCRIPT_AS_ROOT_SSH'\n" + "testpassword!\n" + "echo $USER\n" + "echo $USER\n" + "RUN_SCRIPT_AS_ROOT_SSH\n")).andReturn(new ExecResponse("root\nroot\n", null, 0)); sshClient.disconnect(); replay(sshClient); testMe.call(); }
public void testCredentialsToByteSourceConversion() throws Exception { Function<Credentials, ByteSource> toBytesFunc = getCredentialsToByteStoreFunction(createInjector()); Function<ByteSource, Credentials> fromBytesFunc = getByteStoreToCredentialsFunction(createInjector()); LoginCredentials creds = LoginCredentials.builder().user("myuser").password("mypass").authenticateSudo(true).build(); ByteSource bytes = toBytesFunc.apply(creds); LoginCredentials deserializedCreds = (LoginCredentials) fromBytesFunc.apply(bytes); String json = bytes.asCharSource(Charsets.UTF_8).read(); assertEquals(json, "{\"user\":\"myuser\",\"password\":\"mypass\",\"authenticateSudo\":true}"); assertEquals(deserializedCreds.identity, creds.identity); assertEquals(deserializedCreds.credential, creds.credential); assertEquals(deserializedCreds.getUser(), creds.getUser()); assertEquals(deserializedCreds.getOptionalPassword(), creds.getOptionalPassword()); assertEquals(deserializedCreds.getOptionalPrivateKey(), creds.getOptionalPrivateKey()); assertEquals(deserializedCreds.shouldAuthenticateSudo(), creds.shouldAuthenticateSudo()); }
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; }
public static LoginCredentials overrideDefaultCredentialsWithOptionsIfPresent( @Nullable LoginCredentials defaultCreds, RunScriptOptions options) { Builder builder = LoginCredentials.builder(defaultCreds); if (options.getLoginUser() != null) builder.user(options.getLoginUser()); if (options.getLoginPassword() != null) builder.password(options.getLoginPassword()); if (options.getLoginPrivateKey() != null) builder.privateKey(options.getLoginPrivateKey()); if (options.shouldAuthenticateSudo() != null) builder.authenticateSudo(true); return builder.build(); }
public LoginCredentials apply(Template template, LoginCredentials fromNode) { RunScriptOptions options = checkNotNull(template.getOptions(), "template options are required"); LoginCredentials.Builder builder = LoginCredentials.builder(fromNode); if (options.getLoginUser() != null) builder.user(template.getOptions().getLoginUser()); if (options.getLoginPassword() != null) builder.password(options.getLoginPassword()); if (options.getLoginPrivateKey() != null) builder.privateKey(options.getLoginPrivateKey()); if (options.shouldAuthenticateSudo() != null && options.shouldAuthenticateSudo()) builder.authenticateSudo(true); return builder.build(); } }
public LoginCredentials apply(Template template, LoginCredentials fromNode) { RunScriptOptions options = checkNotNull(template.getOptions(), "template options are required"); LoginCredentials.Builder builder = LoginCredentials.builder(fromNode); if (options.getLoginUser() != null) builder.user(template.getOptions().getLoginUser()); if (options.getLoginPassword() != null) builder.password(options.getLoginPassword()); if (options.getLoginPrivateKey() != null) builder.privateKey(options.getLoginPrivateKey()); if (options.shouldAuthenticateSudo() != null && options.shouldAuthenticateSudo()) builder.authenticateSudo(true); return builder.build(); } }
public static LoginCredentials overrideDefaultCredentialsWithOptionsIfPresent( @Nullable LoginCredentials defaultCreds, RunScriptOptions options) { Builder builder = LoginCredentials.builder(defaultCreds); if (options.getLoginUser() != null) builder.user(options.getLoginUser()); if (options.getLoginPassword() != null) builder.password(options.getLoginPassword()); if (options.getLoginPrivateKey() != null) builder.privateKey(options.getLoginPrivateKey()); if (options.shouldAuthenticateSudo() != null) builder.authenticateSudo(true); return builder.build(); }
public static LoginCredentials overrideDefaultCredentialsWithOptionsIfPresent( @Nullable LoginCredentials defaultCreds, RunScriptOptions options) { Builder builder = LoginCredentials.builder(defaultCreds); if (options.getLoginUser() != null) builder.user(options.getLoginUser()); if (options.getLoginPassword() != null) builder.password(options.getLoginPassword()); if (options.getLoginPrivateKey() != null) builder.privateKey(options.getLoginPrivateKey()); if (options.shouldAuthenticateSudo() != null) builder.authenticateSudo(true); return builder.build(); }
@Override public LoginCredentials apply(Object image) { return LoginCredentials.builder() .authenticateSudo(templateBuilder.getAuthenticateSudo() != null ? templateBuilder.getAuthenticateSudo() : false) .privateKey(compoundKey) .user(templateBuilder.getLoginUser()).build(); }
public void testLoginDifferentAcrossMultipleInjectorsAndLooksNiceWithSudo() throws IOException { Injector injector = createInjector(); Map<String, ByteSource> map = getMap(injector); LoginCredentials creds = LoginCredentials.builder().user("user").password("pass").authenticateSudo(true).build(); Map<String, Credentials> store = getStore(injector); put(map, store, "test", creds); checkConsistent(map, store, "test", creds, "{\"user\":\"user\",\"password\":\"pass\",\"authenticateSudo\":true}"); checkConsistent(map, store, "test", creds, "{\"user\":\"user\",\"password\":\"pass\",\"authenticateSudo\":true}"); remove(map, store, "test"); }
public static LoginCredentials overrideDefaultCredentialsWithOptionsIfPresent( @Nullable LoginCredentials defaultCreds, RunScriptOptions options) { Builder builder = LoginCredentials.builder(defaultCreds); if (options.getLoginUser() != null) builder.user(options.getLoginUser()); if (options.getLoginPassword() != null) builder.password(options.getLoginPassword()); if (options.getLoginPrivateKey() != null) builder.privateKey(options.getLoginPrivateKey()); if (options.shouldAuthenticateSudo() != null) builder.authenticateSudo(true); return builder.build(); }
@Override public LoginCredentials apply(Object image) { return LoginCredentials.builder() .authenticateSudo(templateBuilder.getAuthenticateSudo() != null ? templateBuilder.getAuthenticateSudo() : false) .privateKey(compoundKey) .user(templateBuilder.getLoginUser()).build(); }
public void testLoginConsistentAcrossMultipleInjectorsAndLooksNiceWithSudo() throws IOException { Map<String, InputStream> map = getMap(createInjector()); LoginCredentials creds = LoginCredentials.builder().user("user").password("pass").authenticateSudo(true).build(); put(map, getStore(createInjector()), "test", creds); checkConsistent(map, getStore(createInjector()), "test", creds, "{\"user\":\"user\",\"password\":\"pass\",\"authenticateSudo\":true}"); checkConsistent(map, getStore(createInjector()), "test", creds, "{\"user\":\"user\",\"password\":\"pass\",\"authenticateSudo\":true}"); remove(map, getStore(createInjector()), "test"); }