Skip to content

Commit

Permalink
Merge pull request #20021 from vespa-engine/freva/exec-as
Browse files Browse the repository at this point in the history
Require UnixUser to ContainerEngine::execute
  • Loading branch information
freva authored Nov 15, 2021
2 parents c33b0e4 + 15fb3e9 commit 9cd877a
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.yahoo.vespa.hosted.node.admin.container.image.Image;
import com.yahoo.vespa.hosted.node.admin.nodeagent.ContainerData;
import com.yahoo.vespa.hosted.node.admin.nodeagent.NodeAgentContext;
import com.yahoo.vespa.hosted.node.admin.task.util.file.UnixUser;
import com.yahoo.vespa.hosted.node.admin.task.util.process.CommandResult;

import java.time.Duration;
Expand Down Expand Up @@ -40,8 +41,8 @@ public interface ContainerEngine {
/** Returns the network interface used by container in given context */
String networkInterface(NodeAgentContext context);

/** Execute command inside container as root. Ignores non-zero exit code */
CommandResult executeAsRoot(NodeAgentContext context, Duration timeout, String... command);
/** Execute command inside container as given user. Ignores non-zero exit code */
CommandResult execute(NodeAgentContext context, UnixUser user, Duration timeout, String... command);

/** Execute command inside the container's network namespace. Throws on non-zero exit code */
CommandResult executeInNetworkNamespace(NodeAgentContext context, String... command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.yahoo.vespa.hosted.node.admin.container.image.ContainerImagePruner;
import com.yahoo.vespa.hosted.node.admin.nodeagent.ContainerData;
import com.yahoo.vespa.hosted.node.admin.nodeagent.NodeAgentContext;
import com.yahoo.vespa.hosted.node.admin.task.util.file.UnixUser;
import com.yahoo.vespa.hosted.node.admin.task.util.process.CommandLine;
import com.yahoo.vespa.hosted.node.admin.task.util.process.CommandResult;

Expand Down Expand Up @@ -66,13 +67,13 @@ public boolean pullImageAsyncIfNeeded(TaskContext context, DockerImage dockerIma
}

/** Executes a command inside container identified by given context. Does NOT throw on non-zero exit code */
public CommandResult executeCommandInContainerAsRoot(NodeAgentContext context, String... command) {
return executeCommandInContainerAsRoot(context, CommandLine.DEFAULT_TIMEOUT.toSeconds(), command);
public CommandResult executeCommandInContainer(NodeAgentContext context, UnixUser user, String... command) {
return executeCommandInContainer(context, user, CommandLine.DEFAULT_TIMEOUT, command);
}

/** Execute command inside container identified by given context. Does NOT throw on non-zero exit code */
public CommandResult executeCommandInContainerAsRoot(NodeAgentContext context, Long timeoutSeconds, String... command) {
return containerEngine.executeAsRoot(context, Duration.ofSeconds(timeoutSeconds), command);
public CommandResult executeCommandInContainer(NodeAgentContext context, UnixUser user, Duration timeout, String... command) {
return containerEngine.execute(context, user, timeout, command);
}

/** Execute command in inside containers network namespace, identified by given context. Throws on non-zero exit code */
Expand Down Expand Up @@ -142,7 +143,7 @@ public boolean deleteUnusedContainerImages(TaskContext context, List<DockerImage

private String executeNodeCtlInContainer(NodeAgentContext context, String program) {
String[] command = new String[] {context.paths().underVespaHome("bin/vespa-nodectl").pathInContainer(), program};
return executeCommandInContainerAsRoot(context, command).getOutput();
return executeCommandInContainer(context, context.users().vespa(), command).getOutput();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public class CoreCollector {
static final Map<String, Object> JAVA_HEAP_DUMP_METADATA =
Map.of("bin_path", "java", "backtrace", List.of("Heap dump, no backtrace available"));

private final ContainerOperations docker;
private final ContainerOperations container;

public CoreCollector(ContainerOperations docker) {
this.docker = docker;
public CoreCollector(ContainerOperations container) {
this.container = container;
}

String getGdbPath(NodeAgentContext context) {
Expand All @@ -47,7 +47,7 @@ String getGdbPath(NodeAgentContext context) {
String readBinPathFallback(NodeAgentContext context, ContainerPath coredumpPath) {
String command = getGdbPath(context) + " -n -batch -core " + coredumpPath.pathInContainer() + " | grep \'^Core was generated by\'";
String[] wrappedCommand = {"/bin/sh", "-c", command};
CommandResult result = docker.executeCommandInContainerAsRoot(context, wrappedCommand);
CommandResult result = container.executeCommandInContainer(context, context.users().root(), wrappedCommand);

Matcher matcher = CORE_GENERATOR_PATH_PATTERN.matcher(result.getOutput());
if (! matcher.find()) {
Expand All @@ -60,7 +60,7 @@ String readBinPathFallback(NodeAgentContext context, ContainerPath coredumpPath)
String readBinPath(NodeAgentContext context, ContainerPath coredumpPath) {
String[] command = {"file", coredumpPath.pathInContainer()};
try {
CommandResult result = docker.executeCommandInContainerAsRoot(context, command);
CommandResult result = container.executeCommandInContainer(context, context.users().root(), command);
if (result.getExitCode() != 0) {
throw new ConvergenceException("file command failed with " + asString(result));
}
Expand All @@ -86,7 +86,7 @@ List<String> readBacktrace(NodeAgentContext context, ContainerPath coredumpPath,
String threads = allThreads ? "thread apply all bt" : "bt";
String[] command = {getGdbPath(context), "-n", "-ex", threads, "-batch", binPath, coredumpPath.pathInContainer()};

CommandResult result = docker.executeCommandInContainerAsRoot(context, command);
CommandResult result = container.executeCommandInContainer(context, context.users().root(), command);
if (result.getExitCode() != 0)
throw new ConvergenceException("Failed to read backtrace " + asString(result) + ", Command: " + Arrays.toString(command));

Expand All @@ -96,7 +96,7 @@ List<String> readBacktrace(NodeAgentContext context, ContainerPath coredumpPath,
List<String> readJstack(NodeAgentContext context, ContainerPath coredumpPath, String binPath) {
String[] command = {"jhsdb", "jstack", "--exe", binPath, "--core", coredumpPath.pathInContainer()};

CommandResult result = docker.executeCommandInContainerAsRoot(context, command);
CommandResult result = container.executeCommandInContainer(context, context.users().root(), command);
if (result.getExitCode() != 0)
throw new ConvergenceException("Failed to read jstack " + asString(result) + ", Command: " + Arrays.toString(command));

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public int servicePid() {

@Override
public CommandResult executeCommandInNode(List<String> command, boolean logOutput) {
CommandResult result = container.executeCommandInContainerAsRoot(nodeAgentCtx, command.toArray(new String[0]));
CommandResult result = container.executeCommandInContainer(nodeAgentCtx, nodeAgentCtx.users().vespa(), command.toArray(new String[0]));
String cmdString = command.stream().map(s -> "'" + s + "'").collect(Collectors.joining(" ", "\"", "\""));
int exitCode = result.getExitCode();
String output = result.getOutput().trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.yahoo.vespa.hosted.node.admin.container.image.Image;
import com.yahoo.vespa.hosted.node.admin.nodeagent.ContainerData;
import com.yahoo.vespa.hosted.node.admin.nodeagent.NodeAgentContext;
import com.yahoo.vespa.hosted.node.admin.task.util.file.UnixUser;
import com.yahoo.vespa.hosted.node.admin.task.util.process.CommandResult;

import java.time.Duration;
Expand Down Expand Up @@ -109,7 +110,7 @@ public String networkInterface(NodeAgentContext context) {
}

@Override
public CommandResult executeAsRoot(NodeAgentContext context, Duration timeout, String... command) {
public CommandResult execute(NodeAgentContext context, UnixUser user, Duration timeout, String... command) {
return new CommandResult(null, 0, "");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private void mockExec(String[] cmd, String output, String error) {
}

private void mockExec(NodeAgentContext context, String[] cmd, String output, String error) {
when(docker.executeCommandInContainerAsRoot(context, cmd))
when(docker.executeCommandInContainer(context, context.users().root(), cmd))
.thenReturn(new CommandResult(null, error.isEmpty() ? 0 : 1, error.isEmpty() ? output : error));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.maintenance.servicedump;

import com.yahoo.yolean.concurrent.Sleeper;
import com.yahoo.test.ManualClock;
import com.yahoo.vespa.hosted.node.admin.configserver.noderepository.NodeSpec;
import com.yahoo.vespa.hosted.node.admin.configserver.noderepository.NodeState;
Expand All @@ -12,6 +11,7 @@
import com.yahoo.vespa.hosted.node.admin.nodeagent.NodeAgentContextImpl;
import com.yahoo.vespa.hosted.node.admin.task.util.process.CommandResult;
import com.yahoo.vespa.test.file.TestFileSystem;
import com.yahoo.yolean.concurrent.Sleeper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -62,7 +62,7 @@ void creates_valid_dump_id_from_dump_request() {
void invokes_perf_commands_when_generating_perf_report() {
// Setup mocks
ContainerOperations operations = mock(ContainerOperations.class);
when(operations.executeCommandInContainerAsRoot(any(), any()))
when(operations.executeCommandInContainer(any(), any(), any()))
.thenReturn(new CommandResult(null, 0, "12345"))
.thenReturn(new CommandResult(null, 0, ""))
.thenReturn(new CommandResult(null, 0, ""));
Expand All @@ -78,13 +78,13 @@ void invokes_perf_commands_when_generating_perf_report() {
.build();
reporter.processServiceDumpRequest(context);

verify(operations).executeCommandInContainerAsRoot(
context, "/opt/vespa/libexec/vespa/find-pid", "default/container.1");
verify(operations).executeCommandInContainerAsRoot(
context, "perf", "record", "-g", "--output=/opt/vespa/tmp/vespa-service-dump/perf-record.bin",
verify(operations).executeCommandInContainer(
context, context.users().vespa(), "/opt/vespa/libexec/vespa/find-pid", "default/container.1");
verify(operations).executeCommandInContainer(
context, context.users().vespa(), "perf", "record", "-g", "--output=/opt/vespa/tmp/vespa-service-dump/perf-record.bin",
"--pid=12345", "sleep", "45");
verify(operations).executeCommandInContainerAsRoot(
context, "bash", "-c", "perf report --input=/opt/vespa/tmp/vespa-service-dump/perf-record.bin" +
verify(operations).executeCommandInContainer(
context, context.users().vespa(), "bash", "-c", "perf report --input=/opt/vespa/tmp/vespa-service-dump/perf-record.bin" +
" > /opt/vespa/tmp/vespa-service-dump/perf-report.txt");

String expectedJson = "{\"createdMillis\":1600000000000,\"startedAt\":1600001000000,\"completedAt\":1600001000000," +
Expand All @@ -103,7 +103,7 @@ void invokes_perf_commands_when_generating_perf_report() {
void invokes_jcmd_commands_when_creating_jfr_recording() {
// Setup mocks
ContainerOperations operations = mock(ContainerOperations.class);
when(operations.executeCommandInContainerAsRoot(any(), any()))
when(operations.executeCommandInContainer(any(), any(), any()))
.thenReturn(new CommandResult(null, 0, "12345"))
.thenReturn(new CommandResult(null, 0, "ok"))
.thenReturn(new CommandResult(null, 0, "name=host-admin success"));
Expand All @@ -120,12 +120,12 @@ void invokes_jcmd_commands_when_creating_jfr_recording() {
.build();
reporter.processServiceDumpRequest(context);

verify(operations).executeCommandInContainerAsRoot(
context, "/opt/vespa/libexec/vespa/find-pid", "default/container.1");
verify(operations).executeCommandInContainerAsRoot(
context, "jcmd", "12345", "JFR.start", "name=host-admin", "path-to-gc-roots=true", "settings=profile",
verify(operations).executeCommandInContainer(
context, context.users().vespa(), "/opt/vespa/libexec/vespa/find-pid", "default/container.1");
verify(operations).executeCommandInContainer(
context, context.users().vespa(), "jcmd", "12345", "JFR.start", "name=host-admin", "path-to-gc-roots=true", "settings=profile",
"filename=/opt/vespa/tmp/vespa-service-dump/recording.jfr", "duration=30s");
verify(operations).executeCommandInContainerAsRoot(context, "jcmd", "12345", "JFR.check", "name=host-admin");
verify(operations).executeCommandInContainer(context, context.users().vespa(), "jcmd", "12345", "JFR.check", "name=host-admin");

String expectedJson = "{\"createdMillis\":1600000000000,\"startedAt\":1600001000000," +
"\"completedAt\":1600001000000," +
Expand All @@ -142,7 +142,7 @@ void invokes_jcmd_commands_when_creating_jfr_recording() {
void handles_multiple_artifact_types() {
// Setup mocks
ContainerOperations operations = mock(ContainerOperations.class);
when(operations.executeCommandInContainerAsRoot(any(), any()))
when(operations.executeCommandInContainer(any(), any(), any()))
// For perf report:
.thenReturn(new CommandResult(null, 0, "12345"))
.thenReturn(new CommandResult(null, 0, ""))
Expand Down

0 comments on commit 9cd877a

Please sign in to comment.