use of org.pentaho.di.repository.KettleAuthenticationException in project pentaho-kettle by pentaho.
the class ExecuteJobServletTest method testExecuteJobServletTestWithUnauthorizedUser.
@Test
public void testExecuteJobServletTestWithUnauthorizedUser() throws KettleException, IOException, ServletException {
doReturn(ExecuteJobServlet.CONTEXT_PATH).when(mockHttpServletRequest).getContextPath();
doReturn(REPOSITORY_NAME).when(mockHttpServletRequest).getParameter("rep");
doReturn(UNAUTHORIZED_USER).when(mockHttpServletRequest).getParameter("user");
doReturn(PASSWORD).when(mockHttpServletRequest).getParameter("pass");
doReturn(JOB_NAME).when(mockHttpServletRequest).getParameter("job");
doReturn(LEVEL).when(mockHttpServletRequest).getParameter("level");
PowerMockito.mockStatic(Encr.class);
when(Encr.decryptPasswordOptionallyEncrypted(PASSWORD)).thenReturn(PASSWORD);
KettleAuthenticationException kae = new KettleAuthenticationException();
ExecutionException ee = new ExecutionException(kae);
KettleException ke = new KettleException(ee);
doThrow(ke).when(spyExecuteJobServlet).openRepository(REPOSITORY_NAME, UNAUTHORIZED_USER, PASSWORD);
StringWriter out = mockWriter();
spyExecuteJobServlet.doGet(mockHttpServletRequest, spyHttpServletResponse);
String message = BaseMessages.getString(PKG, "ExecuteJobServlet.Error.Authentication", ExecuteJobServlet.CONTEXT_PATH);
assertTrue(out.toString().contains(message));
}
use of org.pentaho.di.repository.KettleAuthenticationException in project pentaho-kettle by pentaho.
the class ExecuteJobServlet method doGet.
/**
* <div id="mindtouch">
* <h1>/kettle/executeJob</h1>
* <a name="GET"></a>
* <h2>GET</h2>
* <p>Executes job from the specified repository.
* Connects to the repository provided as a parameter, loads the job from it and executes it.
* Empty response is returned or response contains output of an error happened during the job execution.
* Response contains <code>ERROR</code> result if error happened during job execution.</p>
*
* <p><b>Example Request:</b><br />
* <pre function="syntax.xml">
* GET /kettle/executeJob/?rep=my_repository&user=my_user&pass=my_password&job=my_job&level=INFO
* </pre>
*
* </p>
* <h3>Parameters</h3>
* <table class="pentaho-table">
* <tbody>
* <tr>
* <th>name</th>
* <th>description</th>
* <th>type</th>
* </tr>
* <tr>
* <td>rep</td>
* <td>Repository id to connect to.</td>
* <td>query</td>
* </tr>
* <tr>
* <td>user</td>
* <td>User name to be used to connect to repository.</td>
* <td>query</td>
* </tr>
* <tr>
* <td>pass</td>
* <td>User password to be used to connect to repository.</td>
* <td>query</td>
* </tr>
* <tr>
* <td>job</td>
* <td>Job name to be loaded and executed.</td>
* <td>query</td>
* </tr>
* <tr>
* <td>level</td>
* <td>Logging level to be used for job execution (i.e. Debug).</td>
* <td>query</td>
* </tr>
* <tr>
* <td>*any name*</td>
* <td>All the other parameters will be sent to the job for using as variables.
* When necessary you can add custom parameters to the request.
* They will be used to set the job variables values.</td>
* <td>query</td>
* </tr>
* </tbody>
* </table>
*
* <h3>Response Body</h3>
*
* <table class="pentaho-table">
* <tbody>
* <tr>
* <td align="right">element:</td>
* <td>(custom)</td>
* </tr>
* <tr>
* <td align="right">media types:</td>
* <td>application/xml</td>
* </tr>
* </tbody>
* </table>
* <p>Response contains error output of the job executed or Carte object Id
* if the execution was successful.</p>
*
* <p><b>Example Error Response:</b></p>
* <pre function="syntax.xml">
* <webresult>
* <result>OK</result>
* <message>Job started</message>
* <id>74d96aa6-f29a-4bac-a26a-06a8c8f107e5</id>
* </webresult>
* </pre>
*
* <h3>Status Codes</h3>
* <table class="pentaho-table">
* <tbody>
* <tr>
* <th>code</th>
* <th>description</th>
* </tr>
* <tr>
* <td>200</td>
* <td>Request was processed.</td>
* </tr>
* <tr>
* <td>500</td>
* <td>Internal server error occurs during request processing.</td>
* </tr>
* </tbody>
*</table>
*</div>
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (isJettyMode() && !request.getContextPath().startsWith(CONTEXT_PATH)) {
return;
}
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "ExecuteJobServlet.Log.ExecuteJobRequested"));
}
// Options taken from PAN
//
String[] knownOptions = new String[] { "rep", "user", "pass", "job", "level" };
String repOption = request.getParameter("rep");
String userOption = request.getParameter("user");
String passOption = Encr.decryptPasswordOptionallyEncrypted(request.getParameter("pass"));
String jobOption = request.getParameter("job");
String levelOption = request.getParameter("level");
PrintWriter out = response.getWriter();
Repository repository;
try {
repository = openRepository(repOption, userOption, passOption);
} catch (KettleRepositoryNotFoundException krnfe) {
// Repository not found.
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
String message = BaseMessages.getString(PKG, "ExecuteJobServlet.Error.UnableToFindRepository", repOption);
out.println(new WebResult(WebResult.STRING_ERROR, message));
return;
} catch (KettleException ke) {
// Authentication Error.
if (ke.getCause() instanceof ExecutionException) {
ExecutionException ee = (ExecutionException) ke.getCause();
if (ee.getCause() instanceof KettleAuthenticationException) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
String message = BaseMessages.getString(PKG, "ExecuteJobServlet.Error.Authentication", getContextPath());
out.println(new WebResult(WebResult.STRING_ERROR, message));
return;
}
}
// Something unexpected occurred.
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
String message = BaseMessages.getString(PKG, "ExecuteJobServlet.Error.UnexpectedError", Const.CR + Const.getStackTracker(ke));
out.println(new WebResult(WebResult.STRING_ERROR, message));
return;
}
String encoding = System.getProperty("KETTLE_DEFAULT_SERVLET_ENCODING", null);
if (encoding != null && !Utils.isEmpty(encoding.trim())) {
response.setCharacterEncoding(encoding);
response.setContentType("text/html; charset=" + encoding);
}
JobMeta jobMeta;
try {
jobMeta = loadJob(repository, jobOption);
} catch (KettleException ke) {
// Job not found in repository.
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
out.println(new WebResult(WebResult.STRING_ERROR, ke.getMessage()));
return;
}
// Set the servlet parameters as variables in the job
//
String[] parameters = jobMeta.listParameters();
Enumeration<?> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String parameter = (String) parameterNames.nextElement();
String[] values = request.getParameterValues(parameter);
//
if (Const.indexOfString(parameter, knownOptions) < 0) {
//
if (Const.indexOfString(parameter, parameters) < 0) {
jobMeta.setVariable(parameter, values[0]);
} else {
try {
jobMeta.setParameterValue(parameter, values[0]);
} catch (UnknownParamException upe) {
// Unknown parameter is unexpected.
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
String message = BaseMessages.getString(PKG, "ExecuteJobServlet.Error.UnexpectedError", Const.CR + Const.getStackTracker(upe));
out.println(new WebResult(WebResult.STRING_ERROR, message));
}
}
}
}
JobExecutionConfiguration jobExecutionConfiguration = new JobExecutionConfiguration();
LogLevel logLevel = LogLevel.getLogLevelForCode(levelOption);
jobExecutionConfiguration.setLogLevel(logLevel);
JobConfiguration jobConfiguration = new JobConfiguration(jobMeta, jobExecutionConfiguration);
String carteObjectId = UUID.randomUUID().toString();
SimpleLoggingObject servletLoggingObject = new SimpleLoggingObject(CONTEXT_PATH, LoggingObjectType.CARTE, null);
servletLoggingObject.setContainerObjectId(carteObjectId);
servletLoggingObject.setLogLevel(logLevel);
// Create the job and store in the list...
//
final Job job = new Job(repository, jobMeta, servletLoggingObject);
job.setRepository(repository);
job.setSocketRepository(getSocketRepository());
getJobMap().addJob(jobMeta.getName(), carteObjectId, job, jobConfiguration);
job.setContainerObjectId(carteObjectId);
if (repository != null) {
// The repository connection is open: make sure we disconnect from the repository once we
// are done with this job.
//
Repository finalRepository = repository;
job.addJobListener(new JobAdapter() {
@Override
public void jobFinished(Job job) {
finalRepository.disconnect();
}
});
}
try {
runJob(job);
WebResult webResult = new WebResult(WebResult.STRING_OK, "Job started", carteObjectId);
out.println(webResult.getXML());
out.flush();
} catch (Exception executionException) {
// Something went wrong while running the job.
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
String logging = KettleLogStore.getAppender().getBuffer(job.getLogChannelId(), false).toString();
String message = BaseMessages.getString(PKG, "ExecuteJobServlet.Error.WhileExecutingJob", jobOption, logging);
out.println(new WebResult(WebResult.STRING_ERROR, message));
return;
}
// Everything went well till the end.
response.setStatus(HttpServletResponse.SC_OK);
}
use of org.pentaho.di.repository.KettleAuthenticationException in project pentaho-kettle by pentaho.
the class ExecuteTransServletTest method doGetRepositoryAuthenticationFailTest.
@Test
public void doGetRepositoryAuthenticationFailTest() throws Exception {
HttpServletRequest mockHttpServletRequest = mock(HttpServletRequest.class);
HttpServletResponse mockHttpServletResponse = mock(HttpServletResponse.class);
RepositoriesMeta repositoriesMeta = mock(RepositoriesMeta.class);
RepositoryMeta repositoryMeta = mock(RepositoryMeta.class);
Repository repository = mock(Repository.class);
PluginRegistry pluginRegistry = mock(PluginRegistry.class);
KettleException kettleException = mock(KettleException.class);
ExecutionException executionException = mock(ExecutionException.class);
KettleLogStore.init();
StringWriter out = new StringWriter();
PrintWriter printWriter = new PrintWriter(out);
when(mockHttpServletRequest.getParameter("rep")).thenReturn("Repo");
when(mockHttpServletRequest.getParameter("trans")).thenReturn("Trans");
when(mockHttpServletRequest.getParameter("user")).thenReturn("wrongUser");
when(mockHttpServletRequest.getParameter("pass")).thenReturn("wrongPass");
when(mockHttpServletResponse.getWriter()).thenReturn(printWriter);
whenNew(RepositoriesMeta.class).withNoArguments().thenReturn(repositoriesMeta);
when(repositoriesMeta.findRepository("Repo")).thenReturn(repositoryMeta);
mockStatic(PluginRegistry.class);
when(PluginRegistry.getInstance()).thenReturn(pluginRegistry);
when(pluginRegistry.loadClass(RepositoryPluginType.class, repositoryMeta, Repository.class)).thenReturn(repository);
doThrow(kettleException).when(repository).connect("wrongUser", "wrongPass");
when(kettleException.getCause()).thenReturn(executionException);
when(executionException.getCause()).thenReturn(new KettleAuthenticationException());
when(repository.isConnected()).thenReturn(false);
executeTransServlet.doGet(mockHttpServletRequest, mockHttpServletResponse);
verify(mockHttpServletResponse).setStatus(HttpServletResponse.SC_OK);
verify(mockHttpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
}
use of org.pentaho.di.repository.KettleAuthenticationException in project pentaho-kettle by pentaho.
the class ExecuteTransServlet method doGet.
/**
* <div id="mindtouch">
* <h1>/kettle/executeTrans</h1>
* <a name="GET"></a>
* <h2>GET</h2>
* <p>Executes transformation from the specified repository.
* Connects to the repository provided as a parameter, loads the transformation from it and executes it.
* Empty response is returned or response contains output of an error happened during the transformation execution.
* Response contains <code>ERROR</code> result if error happened during transformation execution.</p>
*
* <p><b>Example Request:</b><br />
* <pre function="syntax.xml">
* GET /kettle/executeTrans/?rep=my_repository&user=my_user&pass=my_password&trans=my_trans&level=INFO
* </pre>
*
* </p>
* <h3>Parameters</h3>
* <table class="pentaho-table">
* <tbody>
* <tr>
* <th>name</th>
* <th>description</th>
* <th>type</th>
* </tr>
* <tr>
* <td>rep</td>
* <td>Repository id to connect to.</td>
* <td>query</td>
* </tr>
* <tr>
* <td>user</td>
* <td>User name to be used to connect to repository.</td>
* <td>query</td>
* </tr>
* <tr>
* <td>pass</td>
* <td>User password to be used to connect to repository.</td>
* <td>query</td>
* </tr>
* <tr>
* <td>trans</td>
* <td>Transfromation name to be loaded and executed.</td>
* <td>query</td>
* </tr>
* <tr>
* <td>level</td>
* <td>Logging level to be used for transformation execution (i.e. Debug).</td>
* <td>query</td>
* </tr>
* <tr>
* <td>*any name*</td>
* <td>All the other parameters will be sent to the transformation for using as variables.
* When necessary you can add custom parameters to the request.
* They will be used to set the transformation variables values..</td>
* <td>query</td>
* </tr>
* </tbody>
* </table>
*
* <h3>Response Body</h3>
*
* <table class="pentaho-table">
* <tbody>
* <tr>
* <td align="right">element:</td>
* <td>(custom)</td>
* </tr>
* <tr>
* <td align="right">media types:</td>
* <td>application/xml</td>
* </tr>
* </tbody>
* </table>
* <p>Response contains error output of the transformation executed or nothing
* if the execution was successful.</p>
*
* <p><b>Example Error Response:</b></p>
* <pre function="syntax.xml">
* <webresult>
* <result>ERROR</result>
* <message>Unexpected error executing the transformation:
* 
org.pentaho.di.core.exception.KettleException:
* 
Unable to find transformation 'dummy-trans.ktr' in directory
* :/home/admin

 at
* org.pentaho.di.www.ExecuteTransServlet.loadTransformation(ExecuteTransServlet.java:214)

* at org.pentaho.di.www.ExecuteTransServlet.doGet(ExecuteTransServlet.java:104)

* at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)

* at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)

* at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)

* at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)

* at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)

* at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)

* at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)

* at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)

* at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)

* at org.mortbay.jetty.Server.handle(Server.java:326)

* at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:536)

* at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:915)

* at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:539)

* at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)

* at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:405)

* at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228)

* at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

* </message>
* <id/>
* </webresult>
* </pre>
*
* <h3>Status Codes</h3>
* <table class="pentaho-table">
* <tbody>
* <tr>
* <th>code</th>
* <th>description</th>
* </tr>
* <tr>
* <td>200</td>
* <td>Request was processed.</td>
* </tr>
* <tr>
* <td>400</td>
* <td>When missing mandatory params repo and trans</td>
* </tr>
* <tr>
* <td>401</td>
* <td>When authentication to repository fails</td>
* </tr>
* <tr>
* <td>404</td>
* <td>When transformation is not found</td>
* </tr>
* <tr>
* <td>500</td>
* <td>Internal server error occurs during request processing.</td>
* </tr>
* </tbody>
*</table>
*</div>
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (isJettyMode() && !request.getContextPath().startsWith(CONTEXT_PATH)) {
return;
}
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "ExecuteTransServlet.Log.ExecuteTransRequested"));
}
// Options taken from PAN
//
String[] knownOptions = new String[] { REP, USER, PASS, TRANS, LEVEL };
String repOption = request.getParameter(REP);
String userOption = request.getParameter(USER);
String passOption = Encr.decryptPasswordOptionallyEncrypted(request.getParameter(PASS));
String transOption = request.getParameter(TRANS);
String levelOption = request.getParameter(LEVEL);
response.setStatus(HttpServletResponse.SC_OK);
String encoding = System.getProperty("KETTLE_DEFAULT_SERVLET_ENCODING", null);
if (encoding != null && !Utils.isEmpty(encoding.trim())) {
response.setCharacterEncoding(encoding);
response.setContentType("text/html; charset=" + encoding);
}
PrintWriter out = response.getWriter();
if (repOption == null || transOption == null) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "ExecuteTransServlet.Error.MissingMandatoryParameter", repOption == null ? REP : TRANS)));
return;
}
try {
final Repository repository = openRepository(repOption, userOption, passOption);
final TransMeta transMeta = loadTransformation(repository, transOption);
// Set the servlet parameters as variables in the transformation
//
String[] parameters = transMeta.listParameters();
Enumeration<?> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String parameter = (String) parameterNames.nextElement();
String[] values = request.getParameterValues(parameter);
//
if (Const.indexOfString(parameter, knownOptions) < 0) {
//
if (Const.indexOfString(parameter, parameters) < 0) {
transMeta.setVariable(parameter, values[0]);
} else {
transMeta.setParameterValue(parameter, values[0]);
}
}
}
TransExecutionConfiguration transExecutionConfiguration = new TransExecutionConfiguration();
LogLevel logLevel = LogLevel.getLogLevelForCode(levelOption);
transExecutionConfiguration.setLogLevel(logLevel);
TransConfiguration transConfiguration = new TransConfiguration(transMeta, transExecutionConfiguration);
String carteObjectId = UUID.randomUUID().toString();
SimpleLoggingObject servletLoggingObject = new SimpleLoggingObject(CONTEXT_PATH, LoggingObjectType.CARTE, null);
servletLoggingObject.setContainerObjectId(carteObjectId);
servletLoggingObject.setLogLevel(logLevel);
// Create the transformation and store in the list...
//
final Trans trans = new Trans(transMeta, servletLoggingObject);
trans.setRepository(repository);
trans.setSocketRepository(getSocketRepository());
getTransformationMap().addTransformation(transMeta.getName(), carteObjectId, trans, transConfiguration);
trans.setContainerObjectId(carteObjectId);
if (repository != null) {
// The repository connection is open: make sure we disconnect from the repository once we
// are done with this transformation.
//
trans.addTransListener(new TransAdapter() {
@Override
public void transFinished(Trans trans) {
repository.disconnect();
}
});
}
// Pass the servlet print writer to the transformation...
//
trans.setServletPrintWriter(out);
trans.setServletReponse(response);
trans.setServletRequest(request);
try {
// Execute the transformation...
//
executeTrans(trans);
String logging = KettleLogStore.getAppender().getBuffer(trans.getLogChannelId(), false).toString();
if (trans.isFinishedOrStopped() && trans.getErrors() > 0) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "ExecuteTransServlet.Error.ErrorExecutingTrans", logging)));
}
out.flush();
} catch (Exception executionException) {
String logging = KettleLogStore.getAppender().getBuffer(trans.getLogChannelId(), false).toString();
throw new KettleException(BaseMessages.getString(PKG, "ExecuteTransServlet.Error.ErrorExecutingTrans", logging), executionException);
}
} catch (Exception ex) {
// When we get to this point KettleAuthenticationException has already been wrapped in an Execution Exception
// and that in a KettleException
Throwable kettleExceptionCause = ex.getCause();
if (kettleExceptionCause != null && kettleExceptionCause instanceof ExecutionException) {
Throwable executionExceptionCause = kettleExceptionCause.getCause();
if (executionExceptionCause != null && executionExceptionCause instanceof KettleAuthenticationException) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "ExecuteTransServlet.Error.Authentication", getContextPath())));
}
} else if (ex.getMessage().contains(UNABLE_TO_FIND_TRANS)) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "ExecuteTransServlet.Error.UnableToFindTransformation", transOption)));
} else {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "ExecuteTransServlet.Error.UnexpectedError", Const.CR + Const.getStackTracker(ex))));
}
}
}
Aggregations