You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@zeppelin.apache.org by GitBox <gi...@apache.org> on 2020/02/02 10:45:36 UTC

[GitHub] [zeppelin] alexott commented on a change in pull request #3612: [ZEPPELIN-4571] Add MongoDB interpreter for Zeppelin

alexott commented on a change in pull request #3612: [ZEPPELIN-4571] Add MongoDB interpreter for Zeppelin
URL: https://github.com/apache/zeppelin/pull/3612#discussion_r373835890
 
 

 ##########
 File path: mongodb/src/main/java/org/apache/zeppelin/mongodb/MongoDbInterpreter.java
 ##########
 @@ -0,0 +1,209 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.zeppelin.mongodb;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Scanner;
+
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.DefaultExecutor;
+import org.apache.commons.exec.ExecuteException;
+import org.apache.commons.exec.ExecuteWatchdog;
+import org.apache.commons.exec.Executor;
+import org.apache.commons.exec.PumpStreamHandler;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
+import org.apache.zeppelin.interpreter.Interpreter;
+import org.apache.zeppelin.interpreter.InterpreterContext;
+import org.apache.zeppelin.interpreter.InterpreterResult;
+import org.apache.zeppelin.interpreter.InterpreterResult.Code;
+import org.apache.zeppelin.scheduler.Scheduler;
+import org.apache.zeppelin.scheduler.SchedulerFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * MongoDB interpreter. It uses the mongo shell to interpret the commands.
+ */
+public class MongoDbInterpreter extends Interpreter {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(MongoDbInterpreter.class);
+
+  private static final String SHELL_EXTENSION =
+      new Scanner(MongoDbInterpreter.class.getResourceAsStream("/shell_extension.js"), "UTF-8")
+      .useDelimiter("\\A").next();
+
+  private static final int SIGTERM_CODE = 143;
+
+  private long commandTimeout = 60000;
+
+  private String dbAddress;
+
+  private int maxConcurrency = 10;
+
+  private Map<String, Executor> runningProcesses =  new HashMap<>();
+
+  public MongoDbInterpreter(Properties property) {
+    super(property);
+  }
+
+  @Override
+  public void open() {
+    commandTimeout = Long.parseLong(getProperty("mongo.shell.command.timeout"));
+    maxConcurrency = Integer.parseInt(getProperty("mongo.interpreter.concurrency.max"));
+    dbAddress = getProperty("mongo.server.host") + ":"
+      + getProperty("mongo.server.port") + "/"
+      + getProperty("mongo.server.database");
+  }
+
+  @Override
+  public void close() {
+    runningProcesses.clear();
+    runningProcesses = null;
+  }
+
+  @Override
+  public FormType getFormType() {
+    return FormType.SIMPLE;
+  }
+
+  @Override
+  public InterpreterResult interpret(String script, InterpreterContext context) {
+    LOGGER.debug("Run MongoDB script: {}", script);
+
+    if (StringUtils.isEmpty(script)) {
+      return new InterpreterResult(Code.SUCCESS);
+    }
+
+    String paragraphId = context.getParagraphId();
+    // Write script in a temporary file
+    // The script is enriched with extensions
+    final File scriptFile = new File(getScriptFileName(paragraphId));
+    try {
+      FileUtils.write(scriptFile,
+          SHELL_EXTENSION
+          .replace("__ZEPPELIN_TABLE_LIMIT__", getProperty("mongo.shell.command.table.limit")) +
+          script);
+    }
+    catch (IOException e) {
+      LOGGER.error("Can not write script in temp file", e);
+      return new InterpreterResult(Code.ERROR, e.getMessage());
+    }
+
+    InterpreterResult result = new InterpreterResult(InterpreterResult.Code.SUCCESS);
+
+    final DefaultExecutor executor = new DefaultExecutor();
+    final ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
+
+    executor.setStreamHandler(new PumpStreamHandler(context.out, errorStream));
+    executor.setWatchdog(new ExecuteWatchdog(commandTimeout));
+
+    final CommandLine cmdLine = CommandLine.parse(getProperty("mongo.shell.path"));
+    cmdLine.addArgument("--quiet", false);
+
+    if (!StringUtils.isEmpty(getProperty("mongo.server.username"))) {
+      cmdLine.addArgument("-u", false);
+      cmdLine.addArgument(getProperty("mongo.server.username"), false);
+      cmdLine.addArgument("-p", false);
+      cmdLine.addArgument(getProperty("mongo.server.password"), false);
 
 Review comment:
   my point is that people with shell access, for example, via shell interpreter, could do `ps -aef|grep mongo` and get the full command line with username & password.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services