Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ public class ProfileServlet extends HttpServlet {
private static final String ACCESS_CONTROL_ALLOW_METHODS =
"Access-Control-Allow-Methods";
private static final String ALLOWED_METHODS = "GET";
private static final String ACCESS_CONTROL_ALLOW_ORIGIN =
"Access-Control-Allow-Origin";
private static final String CONTENT_TYPE_TEXT = "text/plain; charset=utf-8";
private static final String ASYNC_PROFILER_HOME_ENV = "ASYNC_PROFILER_HOME";
private static final String ASYNC_PROFILER_HOME_SYSTEM_PROPERTY =
Expand Down Expand Up @@ -462,7 +460,6 @@ private Output getOutput(final HttpServletRequest req) {

private void setResponseHeader(final HttpServletResponse response) {
response.setHeader(ACCESS_CONTROL_ALLOW_METHODS, ALLOWED_METHODS);
response.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
response.setContentType(CONTENT_TYPE_TEXT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.ozone.csi.CsiServer.CsiConfig;
import org.slf4j.Logger;
Expand All @@ -54,11 +58,16 @@ public NodeService(CsiConfig configuration) {
this.mountCommand = configuration.getMountCommand();
}

private static final Pattern SAFE_PATH_PATTERN =
Pattern.compile("^[a-zA-Z0-9/_\\:. -]+$");

@Override
public void nodePublishVolume(NodePublishVolumeRequest request,
StreamObserver<NodePublishVolumeResponse> responseObserver) {

try {
validatePath(request.getTargetPath());
validatePath(request.getVolumeId());
Files.createDirectories(Paths.get(request.getTargetPath()));
String command =
String.format(mountCommand,
Expand All @@ -82,9 +91,25 @@ public void nodePublishVolume(NodePublishVolumeRequest request,

}

private static void validatePath(String path) throws IOException {
if (path == null || path.isEmpty()) {
throw new IOException("Path must not be empty");
}
if (!SAFE_PATH_PATTERN.matcher(path).matches()) {
throw new IOException(
"Path contains disallowed characters: " + path);
}
try {
Paths.get(path);
} catch (InvalidPathException e) {
throw new IOException("Invalid path: " + path, e);
}
}

private void executeCommand(String command)
throws IOException, InterruptedException {
Process exec = Runtime.getRuntime().exec(command);
List<String> args = Arrays.asList(command.split(" "));
Process exec = new ProcessBuilder(args).start();
exec.waitFor(10, TimeUnit.SECONDS);

LOG.info("Command is executed with stdout: {}, stderr: {}",
Expand All @@ -100,12 +125,18 @@ private void executeCommand(String command)
@Override
public void nodeUnpublishVolume(NodeUnpublishVolumeRequest request,
StreamObserver<NodeUnpublishVolumeResponse> responseObserver) {
String umountCommand =
String.format("fusermount -u %s", request.getTargetPath());
LOG.info("Executing {}", umountCommand);
try {
validatePath(request.getTargetPath());
} catch (IOException e) {
responseObserver.onError(e);
return;
}
List<String> umountArgs = Arrays.asList(
"fusermount", "-u", request.getTargetPath());
LOG.info("Executing {}", umountArgs);

try {
executeCommand(umountCommand);
executeCommand(String.join(" ", umountArgs));

responseObserver.onNext(NodeUnpublishVolumeResponse.newBuilder()
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.regex.Pattern;
import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.DefaultValue;
Expand All @@ -47,6 +48,9 @@
@Path("/metrics")
public class MetricsProxyEndpoint {

private static final Pattern SAFE_API_PATTERN =
Pattern.compile("^[a-zA-Z0-9_]+$");

private MetricsServiceProvider metricsServiceProvider;

@Inject
Expand All @@ -67,6 +71,11 @@ public void getMetricsResponse(
@Context UriInfo uriInfo,
@Context HttpServletResponse httpServletResponse
) throws Exception {
if (!SAFE_API_PATTERN.matcher(api).matches()) {
httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST,
"Invalid metrics API name.");
return;
}
if (metricsServiceProvider != null) {
HttpURLConnection connection = metricsServiceProvider.getMetricsResponse(
api, uriInfo.getRequestUri().getQuery());
Expand Down