public void copyTo(RunScriptOptions to) { if (this.getPort() != -1) to.blockOnPort(this.getPort(), this.getSeconds()); if (this.getTaskName() != null) to.nameTask(this.getTaskName()); to.runAsRoot(this.shouldRunAsRoot()); to.blockOnComplete(this.shouldBlockOnComplete()); to.wrapInInitScript(this.shouldWrapInInitScript()); if (this.hasLoginPasswordOption()) to.overrideLoginPassword(this.loginPassword); if (this.hasLoginPrivateKeyOption()) to.overrideLoginPrivateKey(this.loginPrivateKey); if (this.getLoginUser() != null) to.overrideLoginUser(this.getLoginUser()); if (this.shouldAuthenticateSudo() != null) { to.overrideAuthenticateSudo(this.shouldAuthenticateSudo()); } }
public static RunScriptOptions runAsRoot(boolean value) { RunScriptOptions options = new RunScriptOptions(); return options.runAsRoot(value); }
public static RunScriptOptions overrideLoginCredentials(LoginCredentials credentials) { RunScriptOptions options = new RunScriptOptions(); return options.overrideLoginCredentials(credentials); }
/** * @see RunScriptOptions#blockOnComplete(boolean) */ public static RunScriptOptions blockOnComplete(boolean value) { RunScriptOptions options = new RunScriptOptions(); return options.blockOnComplete(value); }
public static RunScriptOptions overrideAuthenticateSudo(boolean authenticateSudo) { RunScriptOptions options = new RunScriptOptions(); return options.overrideAuthenticateSudo(authenticateSudo); }
public void testNotRoot() { Statement command = exec("doFoo"); NodeMetadata node = new NodeMetadataBuilder().ids("id").status(Status.RUNNING).credentials( LoginCredentials.builder().user("tester").password("testpassword!").authenticateSudo(true).build()).build(); SshClient sshClient = createMock(SshClient.class); sshClient.connect(); sshClient.put("/tmp/init-jclouds-script-0", init.render(OsFamily.UNIX)); expect(sshClient.getUsername()).andReturn("tester").atLeastOnce(); expect(sshClient.getHostAddress()).andReturn("somewhere.example.com").atLeastOnce(); expect(sshClient.exec("chmod 755 /tmp/init-jclouds-script-0")).andReturn(new ExecResponse("", "", 0)); expect(sshClient.exec("ln -fs /tmp/init-jclouds-script-0 jclouds-script-0")).andReturn( new ExecResponse("", "", 0)); expect(sshClient.exec("/tmp/init-jclouds-script-0 init")).andReturn(new ExecResponse("", "", 0)); statusFactory, timeouts, Functions.forMap(ImmutableMap.of(node, sshClient)), eventBus, InitScriptConfigurationForTasks.create().appendIncrementingNumberToAnonymousTaskNames(), node, command, new RunScriptOptions().runAsRoot(false));
public void simpleTest() { RunScriptOnNodeUsingSsh testMe = new RunScriptOnNodeUsingSsh(sshFactory, eventBus, node, exec("echo $USER\necho $USER"), wrapInInitScript(false).runAsRoot(false)); testMe.init(); sshClient.connect(); expect(sshClient.getUsername()).andReturn("tester"); expect(sshClient.getHostAddress()).andReturn("somewhere.example.com"); expect(sshClient.exec("echo $USER\n" + "echo $USER\n")).andReturn(new ExecResponse("tester\ntester\n", null, 0)); sshClient.disconnect(); replay(sshClient); testMe.call(); }
@Test(expectedExceptions = IllegalStateException.class) public void testWithoutInitThrowsIllegalStateException() { Statement command = exec("doFoo"); NodeMetadata node = new NodeMetadataBuilder().ids("id").status(Status.RUNNING).credentials( LoginCredentials.builder().user("tester").password("notalot").build()).build(); SshClient sshClient = createMock(SshClient.class); replay(sshClient); RunScriptOnNodeAsInitScriptUsingSsh testMe = new RunScriptOnNodeAsInitScriptUsingSsh(Functions .forMap(ImmutableMap.of(node, sshClient)), eventBus, InitScriptConfigurationForTasks.create() .appendIncrementingNumberToAnonymousTaskNames(), node, command, new RunScriptOptions()); testMe.call(); }
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(); }
protected NodeMetadata updateNodeWithCredentialsIfPresent(NodeMetadata node, RunScriptOptions options) { checkNotNull(node, "node"); Builder builder = LoginCredentials.builder(node.getCredentials()); if (options.getLoginUser() != null) builder.user(options.getLoginUser()); if (options.hasLoginPasswordOption()) { if (options.hasLoginPassword()) { builder.password(options.getLoginPassword()); } else { builder.noPassword(); } } if (options.hasLoginPrivateKeyOption()) { if (options.hasLoginPrivateKey()) { builder.privateKey(options.getLoginPrivateKey()); } else { builder.noPrivateKey(); } } if (options.shouldAuthenticateSudo() != null) builder.authenticateSudo(true); return NodeMetadataBuilder.fromNodeMetadata(node).credentials(builder.build()).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 RunScriptOptions defaultRunScriptOptionsForSpec(ClusterSpec spec) { LoginCredentials credentials = LoginCredentials.builder() .user(spec.getClusterUser()).privateKey(spec.getPrivateKey()).build(); return overrideLoginCredentials(credentials).wrapInInitScript(false) .runAsRoot(false); }
@Test(enabled = false) protected void tryBadPassword(String group, Credentials good) throws AssertionError { try { Map<? extends NodeMetadata, ExecResponse> responses = client.runScriptOnNodesMatching( runningInGroup(group), "echo I put a bad password", wrapInInitScript(false).runAsRoot(false).overrideLoginCredentials( LoginCredentials.builder().user(good.identity).noPrivateKey().password("romeo").build())); assert responses.size() == 0 : "shouldn't pass with a bad password\n" + responses; } catch (AssertionError e) { throw e; } catch (RunScriptOnNodesException e) { assert Iterables.any(e.getNodeErrors().values(), Predicates.instanceOf(AuthorizationException.class)) : e + " not authexception!"; } }
public Map<? extends NodeMetadata, ExecResponse> runScriptOnNodesMatching( ClusterSpec spec, Predicate<NodeMetadata> condition, Statement statement, RunScriptOptions options) throws IOException, RunScriptOnNodesException { LoginCredentials credentials = LoginCredentials.builder() .user(spec.getClusterUser()).privateKey(spec.getPrivateKey()).build(); if (options == null) { options = defaultRunScriptOptionsForSpec(spec); } else if (options.getLoginUser() == null) { options = options.overrideLoginCredentials(credentials); } condition = Predicates .and(runningInGroup(spec.getClusterName()), condition); ComputeServiceContext context = getCompute().apply(spec); return context.getComputeService().runScriptOnNodesMatching(condition, statement, options); }
public Boolean call() { Statement statement = Statements.newStatementList(exec("hostname")); // NB this assumes passwordless sudo ! ExecResponse response = computeService.runScriptOnNode(nodeRef.getId(), statement, overrideLoginCredentials(expectedCredentialsRef).runAsRoot(false)); return response.getExitStatus() == 0; }}) .limitTimeTo(delayMs, MILLISECONDS)
public void testCanRunCommandAsCurrentUser() throws Exception { Map<? extends NodeMetadata, ExecResponse> responses = context.getComputeService().runScriptOnNodesMatching( Predicates.<NodeMetadata> alwaysTrue(), exec("id"), wrapInInitScript(false).runAsRoot(false)); for (Entry<? extends NodeMetadata, ExecResponse> response : responses.entrySet()) assert response.getValue().getOutput().trim().contains(System.getProperty("user.name")) : response.getKey() + ": " + response.getValue(); }
wrapInInitScript(false).runAsRoot(false).overrideLoginCredentials(good)).entrySet()) { checkResponseEqualsHostname(response.getValue(), response.getKey()); wrapInInitScript(false).runAsRoot(false)); checkResponseEqualsHostname(response, node); OperatingSystem os = node.getOperatingSystem(); assert response.getExitStatus() == 0 : node.getId() + ": " + response; assert response.getExitStatus() == 0 : node.getId() + ": " + response; response = client.runScriptOnNode(node.getId(), "echo $USER", wrapInInitScript(false).runAsRoot(false)); assert response.getOutput().trim().equals("foo") : node.getId() + ": " + response;
/** * Run set of queued commands now */ public static void executeOnNodes(List<Statement> commands, boolean runAsRoot, String clustername, ComputeService compute, String username, String sshKeyName) throws RunScriptOnNodesException, InterruptedException, ExecutionException, TimeoutException { compute.runScriptOnNodesMatching( NodePredicates.runningInGroup(clustername), new StatementList(commands), new RunScriptOptions() .nameTask("Setup") .overrideLoginCredentials(Tools.getPrivateKeyCredentials(username, sshKeyName)) .wrapInInitScript(true) .overrideLoginUser(username) .blockOnComplete(true) .runAsRoot(runAsRoot)); }
public static RunScriptOptions recipeScriptOptions(RunScriptOptions options) { return options.wrapInInitScript(true).runAsRoot(true); }