Skip to content

Commit

Permalink
[PDI-15652] - Several steps / jobs don't work with encrypted password…
Browse files Browse the repository at this point in the history
… in a parameter
  • Loading branch information
YuryBY committed Sep 22, 2016
1 parent 3d26a01 commit c18a83c
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 18 deletions.
25 changes: 24 additions & 1 deletion core/src/org/pentaho/di/core/util/Utils.java
Expand Up @@ -22,6 +22,9 @@

package org.pentaho.di.core.util;

import org.pentaho.di.core.encryption.Encr;
import org.pentaho.di.core.variables.VariableSpace;

import java.util.List;

/* Levenshtein in Java, originally from Josh Drew's code at
Expand Down Expand Up @@ -107,7 +110,7 @@ public static int getDamerauLevenshteinDistance( String s, String t ) {
/**
* Check if the CharSequence supplied is empty. A CharSequence is empty when it is null or when the length is 0
*
* @param string
* @param val
* The stringBuffer to check
* @return true if the stringBuffer supplied is empty
*/
Expand Down Expand Up @@ -149,5 +152,25 @@ public static boolean isEmpty( List<?> list ) {
return list == null || list.size() == 0;
}

/**
* Resolves password from variable if it's necessary and decrypts if the password was encrypted
*
*
* @param variables
* VariableSpace is used for resolving
* @param password
* the password for resolving and decrypting
* @return resolved decrypted password
*/
public static String resolvePassword( VariableSpace variables, String password ) {
String resolvedPassword = variables.environmentSubstitute( password );
if ( resolvedPassword != null ) {
// returns resolved decrypted password
return Encr.decryptPasswordOptionallyEncrypted( resolvedPassword );
} else {
// actually null
return resolvedPassword;
}
}

}
Expand Up @@ -716,8 +716,12 @@ public String getPassword() {
return password;
}

/**
* @return Returns resolved decrypted password or null
* in case of {@link #getPassword()} returns null.
*/
public String getRealPassword() {
return environmentSubstitute( getPassword() );
return Utils.resolvePassword( variables, getPassword() );
}

public String getAttachmentFolder() {
Expand Down
Expand Up @@ -359,7 +359,7 @@ public void setSenderServiceName( String senderServiceName ) {
}

/**
* @param user
* @param password
* The password to set.
*/
public void setPassword( String password ) {
Expand Down Expand Up @@ -458,7 +458,7 @@ public Result execute( Result previousResult, int nr ) {

// Target
String realServername = environmentSubstitute( serverName );
String realPassword = environmentSubstitute( password );
String realPassword = Utils.resolvePassword( variables, password );
int realPort = Const.toInt( environmentSubstitute( port ), DEFAULT_PORT );
int realResponseTimeOut = Const.toInt( environmentSubstitute( responseTimeOut ), DEFAULT_RESPONSE_TIME_OUT );
int realConnectionTimeOut =
Expand Down
3 changes: 2 additions & 1 deletion engine/src/org/pentaho/di/job/entries/sftp/JobEntrySFTP.java
Expand Up @@ -628,9 +628,10 @@ public Result execute( Result previousResult, int nr ) {
String realProxyHost = environmentSubstitute( getProxyHost() );
if ( !Utils.isEmpty( realProxyHost ) ) {
// Set proxy
String password = Utils.resolvePassword( variables, getProxyPassword() );
sftpclient.setProxy(
realProxyHost, environmentSubstitute( getProxyPort() ), environmentSubstitute( getProxyUsername() ),
environmentSubstitute( getProxyPassword() ), getProxyType() );
password, getProxyType() );
}

// login to ftp host ...
Expand Down
2 changes: 1 addition & 1 deletion engine/src/org/pentaho/di/trans/step/BaseStep.java
Expand Up @@ -146,7 +146,7 @@
public class BaseStep implements VariableSpace, StepInterface, LoggingObjectInterface, ExtensionDataInterface {
private static Class<?> PKG = BaseStep.class; // for i18n purposes, needed by Translator2!!

private VariableSpace variables = new Variables();
protected VariableSpace variables = new Variables();

private TransMeta transMeta;

Expand Down
Expand Up @@ -557,7 +557,8 @@ public boolean openNewFile() {
if ( meta.isSheetProtected() ) {
// Protect Sheet by setting password
data.sheet.getSettings().setProtected( true );
data.sheet.getSettings().setPassword( environmentSubstitute( meta.getPassword() ) );
String realPassword = Utils.resolvePassword( variables, meta.getPassword() );
data.sheet.getSettings().setPassword( realPassword );
}

// Set the initial position...
Expand Down
Expand Up @@ -932,7 +932,7 @@ public boolean init( StepMetaInterface smi, StepDataInterface sdi ) {
data.realTemplateSheetName = environmentSubstitute( meta.getTemplateSheetName() );
data.realTemplateFileName = environmentSubstitute( meta.getTemplateFileName() );
data.realStartingCell = environmentSubstitute( meta.getStartingCell() );
data.realPassword = environmentSubstitute( meta.getPassword() );
data.realPassword = Utils.resolvePassword( variables, meta.getPassword() );
data.realProtectedBy = environmentSubstitute( meta.getProtectedBy() );

data.shiftExistingCells = ExcelWriterStepMeta.ROW_WRITE_PUSH_DOWN.equals( meta.getRowWritingMethod() );
Expand Down
2 changes: 1 addition & 1 deletion engine/src/org/pentaho/di/trans/steps/http/HTTP.java
Expand Up @@ -399,7 +399,7 @@ public boolean init( StepMetaInterface smi, StepDataInterface sdi ) {
data.realProxyHost = environmentSubstitute( meta.getProxyHost() );
data.realProxyPort = Const.toInt( environmentSubstitute( meta.getProxyPort() ), 8080 );
data.realHttpLogin = environmentSubstitute( meta.getHttpLogin() );
data.realHttpPassword = environmentSubstitute( meta.getHttpPassword() );
data.realHttpPassword = Utils.resolvePassword( variables, meta.getHttpPassword() );

data.realSocketTimeout = Const.toInt( environmentSubstitute( meta.getSocketTimeout() ), -1 );
data.realConnectionTimeout = Const.toInt( environmentSubstitute( meta.getSocketTimeout() ), -1 );
Expand Down
Expand Up @@ -494,7 +494,7 @@ public boolean init( StepMetaInterface smi, StepDataInterface sdi ) {
data.realProxyHost = environmentSubstitute( meta.getProxyHost() );
data.realProxyPort = Const.toInt( environmentSubstitute( meta.getProxyPort() ), 8080 );
data.realHttpLogin = environmentSubstitute( meta.getHttpLogin() );
data.realHttpPassword = environmentSubstitute( meta.getHttpPassword() );
data.realHttpPassword = Utils.resolvePassword( variables, meta.getHttpPassword() );

data.realSocketTimeout = Const.toInt( environmentSubstitute( meta.getSocketTimeout() ), -1 );
data.realConnectionTimeout = Const.toInt( environmentSubstitute( meta.getSocketTimeout() ), -1 );
Expand Down
2 changes: 1 addition & 1 deletion engine/src/org/pentaho/di/trans/steps/mail/Mail.java
Expand Up @@ -270,7 +270,7 @@ public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws

// cache the position of the Authentication password field
if ( data.indexOfAuthenticationPass < 0 ) {
String realAuthenticationPassword = meta.getAuthenticationPassword();
String realAuthenticationPassword = Utils.resolvePassword( variables, meta.getAuthenticationPassword() );
data.indexOfAuthenticationPass = data.previousRowMeta.indexOfValue( realAuthenticationPassword );
if ( data.indexOfAuthenticationPass < 0 ) {
throw new KettleException( BaseMessages.getString(
Expand Down
Expand Up @@ -404,7 +404,7 @@ public boolean init( StepMetaInterface smi, StepDataInterface sdi ) {
}

String realusername = environmentSubstitute( meta.getUserName() );
String realpassword = environmentSubstitute( meta.getPassword() );
String realpassword = Utils.resolvePassword( variables, meta.getPassword() );
int realport = Const.toInt( environmentSubstitute( meta.getPort() ), -1 );
String realProxyUsername = environmentSubstitute( meta.getProxyUsername() );
if ( !meta.isDynamicFolder() ) {
Expand Down
Expand Up @@ -121,8 +121,10 @@ public void openQuery() throws KettleDatabaseException {
if ( !Utils.isEmpty( databaseMeta.getUsername() ) ) {
connectString += "JdbcUser=" + space.environmentSubstitute( databaseMeta.getUsername() ) + ";";
}
if ( !Utils.isEmpty( databaseMeta.getPassword() ) ) {
connectString += "JdbcPassword=" + space.environmentSubstitute( databaseMeta.getPassword() ) + ";";
String password = databaseMeta.getPassword();
if ( !Utils.isEmpty( password ) ) {
String realPassword = Utils.resolvePassword( space, password );
connectString += "JdbcPassword=" + space.environmentSubstitute( realPassword ) + ";";
}

if ( !Utils.isEmpty( realRole ) ) {
Expand Down
Expand Up @@ -120,7 +120,7 @@ public boolean execute( MonetDBBulkLoaderMeta meta, boolean wait ) throws Kettle
DatabaseMeta dm = meta.getDatabaseMeta();

String user = environmentSubstitute( Const.NVL( dm.getUsername(), "" ) );
String password = environmentSubstitute( Const.NVL( dm.getPassword(), "" ) );
String password = Utils.resolvePassword( variables, Const.NVL( dm.getPassword(), "" ) );

MapiSocket mserver = getMonetDBConnection();
data.mserver = mserver;
Expand Down Expand Up @@ -616,7 +616,7 @@ protected MapiSocket getMonetDBConnection() throws Exception {
String hostname = environmentSubstitute( Const.NVL( dm.getHostname(), "" ) );
String portnum = environmentSubstitute( Const.NVL( dm.getDatabasePortNumberString(), "" ) );
String user = environmentSubstitute( Const.NVL( dm.getUsername(), "" ) );
String password = environmentSubstitute( Const.NVL( dm.getPassword(), "" ) );
String password = Utils.resolvePassword( variables, Const.NVL( dm.getPassword(), "" ) );
String db = environmentSubstitute( Const.NVL( dm.getDatabaseName(), "" ) );

MapiSocket mserver = getMonetDBConnection( hostname, Integer.valueOf( portnum ), user, password, db, log );
Expand Down
3 changes: 1 addition & 2 deletions engine/src/org/pentaho/di/trans/steps/ssh/SSH.java
Expand Up @@ -24,7 +24,6 @@

import org.pentaho.di.core.Const;
import org.pentaho.di.core.util.Utils;
import org.pentaho.di.core.encryption.Encr;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.row.RowMeta;
import org.pentaho.di.core.row.RowMetaInterface;
Expand Down Expand Up @@ -209,7 +208,7 @@ public boolean init( StepMetaInterface smi, StepDataInterface sdi ) {
String servername = environmentSubstitute( meta.getServerName() );
int nrPort = Const.toInt( environmentSubstitute( meta.getPort() ), 22 );
String username = environmentSubstitute( meta.getuserName() );
String password = Encr.decryptPasswordOptionallyEncrypted( environmentSubstitute( meta.getpassword() ) );
String password = Utils.resolvePassword( variables, meta.getpassword() );
String keyFilename = environmentSubstitute( meta.getKeyFileName() );
String passphrase = environmentSubstitute( meta.getPassphrase() );
int timeOut = Const.toInt( environmentSubstitute( meta.getTimeOut() ), 0 );
Expand Down

0 comments on commit c18a83c

Please sign in to comment.