Skip to content

Commit

Permalink
Merge pull request pentaho#9303 from soagarwal1/BACKLOG-40063
Browse files Browse the repository at this point in the history
[BACKLOG-40063] - Address review comments
  • Loading branch information
singletonc authored May 1, 2024
2 parents 6dfb63c + b117946 commit f4363fe
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ public static String getValue( Object object, Field field ) {
}
}

/**
* Unlike Class#getDeclaredMethod which returns all declared
* private, protected and public methods declared in the specific Class
*
* EncryptUtils#getDeclaredMethod returns all declared
* private, protected and public methods declared in the specific Class and its Parent Classes
*
* @param parentClass The class whose declared methods are needed.
* @param name The field/method name.
* @param parameterTypes Return Type of the field/method
*/
private static Method getDeclaredMethod( Class<?> parentClass, String name, Class<?>... parameterTypes ) throws NoSuchMethodException {
if ( parentClass == Object.class ) {
throw new NoSuchMethodException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import java.util.List;
import java.util.Map;

/**
* A class to put common fields for all types of VFS Connection Details to avoid code duplication
*/
public abstract class BaseVFSConnectionDetails implements VFSConnectionDetails {

@NonNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2024 by Hitachi Vantara : http://www.pentaho.com
*
*******************************************************************************
*
* Licensed 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.pentaho.di.connections.common.bucket;

import org.pentaho.metastore.persist.MetaStoreAttribute;

/**
* Child class to validate encrypted fields in Parent Classes getting encrypted without failure
*/
public class TestConnectionDetailsChild extends TestConnectionDetails {

@MetaStoreAttribute
private String password3;

public void setPassword3( String password3 ) {
this.password3 = password3;
}

public String getPassword3() {
return password3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2024 by Hitachi Vantara : http://www.pentaho.com
*
*******************************************************************************
*
* Licensed 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.pentaho.di.connections.utils;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.pentaho.di.connections.common.bucket.TestConnectionDetailsChild;
import org.pentaho.di.core.KettleClientEnvironment;
import org.pentaho.di.core.encryption.Encr;

public class EncryptUtilsTest {

private static String PASSWORD = "testpassword";
private static String PASSWORD2 = "testpassword2";
private static String PASSWORD3 = "testpassword3";


@Before
public void setUp() throws Exception {
KettleClientEnvironment.init();
}

@Test
public void testTransformFields() {
TestConnectionDetailsChild testConnectionDetails = new TestConnectionDetailsChild();
testConnectionDetails.setPassword( PASSWORD );
testConnectionDetails.setPassword3( PASSWORD3 );
EncryptUtils.transformFields( testConnectionDetails, Encr::encryptPasswordIfNotUsingVariables );
Assert.assertTrue( testConnectionDetails.getPassword().startsWith( "Encrypted " ) );
}

@Test
public void testGetValue() throws NoSuchFieldException {
TestConnectionDetailsChild testConnectionDetails = new TestConnectionDetailsChild();
testConnectionDetails.setPassword( PASSWORD );
testConnectionDetails.setPassword3( PASSWORD3 );
String value = EncryptUtils.getValue( testConnectionDetails,
testConnectionDetails.getClass().getDeclaredField( "password3" ) );
Assert.assertNotNull( value );
Assert.assertEquals( PASSWORD3, value );
}

@Test
public void testSetValue() throws NoSuchFieldException {
TestConnectionDetailsChild testConnectionDetails = new TestConnectionDetailsChild();
testConnectionDetails.setPassword( PASSWORD );
testConnectionDetails.setPassword3( PASSWORD3 );
EncryptUtils.setValue( testConnectionDetails,
testConnectionDetails.getClass().getDeclaredField( "password3" ), PASSWORD2 );
Assert.assertEquals( testConnectionDetails.getPassword3(), PASSWORD2 );
}
}

0 comments on commit f4363fe

Please sign in to comment.