Skip to content

Commit

Permalink
[BACKLOG-40063] - VFS Connection Manager : Allow permission to be app…
Browse files Browse the repository at this point in the history
…lied on the VFS connections created from PUC
  • Loading branch information
soagarwal1 committed Apr 24, 2024
1 parent 37fde4d commit 58b5c48
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 26 deletions.
8 changes: 8 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<xmlunit.version>1.5</xmlunit.version>
<joda.version>2.10.2</joda.version>
<encryption-support.version>10.2.0.0-SNAPSHOT</encryption-support.version>
<com.github.spotbugs.annotations.version>4.2.3</com.github.spotbugs.annotations.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -506,6 +507,13 @@
<artifactId>pentaho-encryption-support</artifactId>
<version>${encryption-support.version}</version>
</dependency>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
<version>${com.github.spotbugs.annotations.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,24 @@ public static <E extends Exception> void transformFields( Object object, Transfo

private static List<Field> getFields( Object connectionDetails ) {
List<Field> fields = new ArrayList<>();
for ( Field field : connectionDetails.getClass().getDeclaredFields() ) {
Annotation annotation = Arrays.stream( field.getAnnotations() ).filter(
Encrypted.class::isInstance ).findAny().orElse( null );
if ( annotation != null ) {
fields.add( field );
Class<?> clazz = connectionDetails.getClass();
while ( clazz != Object.class ) {
for ( Field field : clazz.getDeclaredFields() ) {
Annotation annotation = Arrays.stream( field.getAnnotations() ).filter(
Encrypted.class::isInstance ).findAny().orElse( null );
if ( annotation != null ) {
fields.add( field );
}
}
clazz = clazz.getSuperclass();
}
return fields;
}

public static void setValue( Object object, Field field, String value ) {
try {
Method setMethod =
object.getClass().getMethod( SET_PREFIX + StringUtils.capitalize( field.getName() ), String.class );
Method setMethod = getDeclaredMethod( object.getClass(),
SET_PREFIX + StringUtils.capitalize( field.getName() ), String.class );
setMethod.invoke( object, value );
} catch ( NoSuchMethodException | InvocationTargetException | IllegalAccessException ignore ) {
// ignore
Expand All @@ -110,11 +114,23 @@ public static void setValue( Object object, Field field, String value ) {

public static String getValue( Object object, Field field ) {
try {
Method getMethod =
object.getClass().getMethod( GET_PREFIX + StringUtils.capitalize( field.getName() ) );
Method getMethod = getDeclaredMethod( object.getClass(),
GET_PREFIX + StringUtils.capitalize( field.getName() ) );
return (String) getMethod.invoke( object );
} catch ( NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
return null;
}
}

private static Method getDeclaredMethod( Class<?> parentClass, String name, Class<?>... parameterTypes ) throws NoSuchMethodException {
if ( parentClass == Object.class ) {
throw new NoSuchMethodException();
}
try {
return parentClass.getDeclaredMethod( name, parameterTypes );
} catch ( NoSuchMethodException | SecurityException e ) {
parentClass = parentClass.getSuperclass();
return getDeclaredMethod( parentClass, name, parameterTypes );
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*! ******************************************************************************
*
* 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.vfs;

import edu.umd.cs.findbugs.annotations.NonNull;
import org.pentaho.metastore.persist.MetaStoreAttribute;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class BaseVFSConnectionDetails implements VFSConnectionDetails {

@NonNull
@MetaStoreAttribute
private List<String> baRoles = new ArrayList<>();

@NonNull
@Override
public List<String> getBaRoles() {
return baRoles;
}

@Override
public Map<String, String> getProperties() {
Map<String, String> props = new HashMap<>();
fillProperties( props );
return props;
}

/**
* Adds base/default properties to properties of connection instance.
* <p>
* @param props The properties map
*/
protected void fillProperties( Map<String, String> props ) {
props.put( "baRoles", getBaRoles().toString() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Pentaho Data Integration
*
* Copyright (C) 2024 by Hitachi Vantara : http://www.pentaho.com
* Copyright (C) 2019-2024 by Hitachi Vantara : http://www.pentaho.com
*
*******************************************************************************
*
Expand All @@ -24,6 +24,9 @@

import org.pentaho.di.connections.ConnectionDetails;

import java.util.Collections;
import java.util.List;

/**
* Created by bmorrise on 2/13/19.
*/
Expand Down Expand Up @@ -69,5 +72,17 @@ default String getRootPath() {
*
* @param rootPath The root path
*/
default void setRootPath(String rootPath ) {}
default void setRootPath( String rootPath ) { }

/**
* Gets the list of roles with access to the connection from the Business Analytics product.
* <p>
* Access control does not distinguish between types of access operations (such as read, write, delete).
* Access is granted to either all or none of the operations.
* @return A non-null list of roles.
*/
default List<String> getBaRoles() {
// remove after implementation
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Pentaho Data Integration
*
* Copyright (C) 2019-2022 by Hitachi Vantara : http://www.pentaho.com
* Copyright (C) 2019-2024 by Hitachi Vantara : http://www.pentaho.com
*
*******************************************************************************
*
Expand All @@ -22,7 +22,7 @@

package org.pentaho.di.connections.vfs.providers.other;

import org.pentaho.di.connections.vfs.VFSConnectionDetails;
import org.pentaho.di.connections.vfs.BaseVFSConnectionDetails;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.metastore.persist.MetaStoreAttribute;
import org.pentaho.metastore.persist.MetaStoreElementType;
Expand All @@ -33,7 +33,7 @@
@MetaStoreElementType(
name = "Other VFS Connection",
description = "Defines the connection details for a generic vfs connection" )
public class OtherConnectionDetails implements VFSConnectionDetails {
public class OtherConnectionDetails extends BaseVFSConnectionDetails {

public VariableSpace space;
public static final String TYPE = "other";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.junit.Test;
import org.pentaho.di.connections.common.bucket.TestConnectionDetails;
import org.pentaho.di.connections.common.bucket.TestConnectionProvider;
import org.pentaho.di.connections.vfs.VFSConnectionDetails;
import org.pentaho.di.connections.vfs.VFSHelper;
import org.pentaho.di.connections.vfs.VFSLookupFilter;
import org.pentaho.di.core.KettleClientEnvironment;
Expand All @@ -50,6 +51,8 @@ public class ConnectionManagerTest {
private static String CONNECTION_NAME = "Connection Name";
private static String PASSWORD = "testpassword";
private static String PASSWORD2 = "testpassword2";
private static String ROLE1 = "role1";
private static String ROLE2 = "role2";

private ConnectionManager connectionManager;

Expand Down Expand Up @@ -235,6 +238,23 @@ public void testNullConnectionName() {
Assert.assertNull( fileSystemOptions );
}

@Test
public void testBaRolesNotNull() {
addOne();
TestConnectionDetails connectionDetails = (TestConnectionDetails) connectionManager.getConnectionDetails( CONNECTION_NAME );
Assert.assertNotNull( connectionDetails );
Assert.assertNotNull( connectionDetails.getBaRoles() );
}

@Test
public void testDefaultPropertiesNotNull() {
addOne();
TestConnectionDetails connectionDetails = (TestConnectionDetails) connectionManager.getConnectionDetails( CONNECTION_NAME );
Assert.assertNotNull( connectionDetails );
Assert.assertNotNull( connectionDetails.getProperties() );
Assert.assertNotNull( connectionDetails.getProperties().get( "baRoles" ) );
}

private void addProvider() {
TestConnectionProvider testConnectionProvider = new TestConnectionProvider( connectionManager );
connectionManager.addConnectionProvider( TestConnectionProvider.SCHEME, testConnectionProvider );
Expand All @@ -247,6 +267,8 @@ private void addOne() {
testConnectionDetails.setName( CONNECTION_NAME );
testConnectionDetails.setPassword( PASSWORD );
testConnectionDetails.setPassword1( PASSWORD2 );
testConnectionDetails.getBaRoles().add( ROLE1 );
testConnectionDetails.getBaRoles().add( ROLE2 );
connectionManager.save( testConnectionDetails );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2022 by Hitachi Vantara : http://www.pentaho.com
* Copyright (C) 2002-2024 by Hitachi Vantara : http://www.pentaho.com
*
*******************************************************************************
*
Expand All @@ -22,18 +22,16 @@

package org.pentaho.di.connections.common.bucket;

import org.pentaho.di.connections.ConnectionDetails;
import org.pentaho.di.connections.annotations.Encrypted;
import org.pentaho.di.connections.vfs.VFSConnectionDetails;
import org.pentaho.di.connections.vfs.BaseVFSConnectionDetails;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.metastore.persist.MetaStoreAttribute;
import org.pentaho.metastore.persist.MetaStoreElement;
import org.pentaho.metastore.persist.MetaStoreElementType;

@MetaStoreElementType(
name = "Test VFS Connection",
description = "Defines the connection details for a test vfs connection" )
public class TestConnectionDetails implements VFSConnectionDetails {
public class TestConnectionDetails extends BaseVFSConnectionDetails {

private static String TYPE = "test";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2022 by Hitachi Vantara : http://www.pentaho.com
* Copyright (C) 2002-2024 by Hitachi Vantara : http://www.pentaho.com
*
*******************************************************************************
*
Expand All @@ -23,15 +23,15 @@
package org.pentaho.di.connections.common.domain;

import org.pentaho.di.connections.annotations.Encrypted;
import org.pentaho.di.connections.vfs.VFSConnectionDetails;
import org.pentaho.di.connections.vfs.BaseVFSConnectionDetails;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.metastore.persist.MetaStoreAttribute;
import org.pentaho.metastore.persist.MetaStoreElementType;

@MetaStoreElementType(
name = "Test VFS Connection With Domain",
description = "Defines the connection details for a test vfs connection" )
public class TestConnectionWithDomainDetails implements VFSConnectionDetails {
public class TestConnectionWithDomainDetails extends BaseVFSConnectionDetails {

private static String TYPE = "test2";
private VariableSpace space;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Pentaho Data Integration
*
* Copyright (C) 2020 by Hitachi Vantara : http://www.pentaho.com
* Copyright (C) 2020-2024 by Hitachi Vantara : http://www.pentaho.com
*
*******************************************************************************
*
Expand All @@ -23,15 +23,15 @@
package org.pentaho.di.connections.ui;

import org.pentaho.di.connections.annotations.Encrypted;
import org.pentaho.di.connections.vfs.VFSConnectionDetails;
import org.pentaho.di.connections.vfs.BaseVFSConnectionDetails;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.metastore.persist.MetaStoreAttribute;
import org.pentaho.metastore.persist.MetaStoreElementType;

@MetaStoreElementType(
name = "Test VFS Connection",
description = "Defines the connection details for a test vfs connection" )
public class TestConnectionDetails implements VFSConnectionDetails {
public class TestConnectionDetails extends BaseVFSConnectionDetails {

private static String TYPE = "test";
private VariableSpace space;
Expand Down Expand Up @@ -93,4 +93,4 @@ public String getPassword1() {
public void setPassword1( String password1 ) {
this.password1 = password1;
}
}
}

0 comments on commit 58b5c48

Please sign in to comment.