Search in sources :

Example 91 with ContextType

use of org.talend.designer.core.model.utils.emf.talendfile.ContextType in project tbd-studio-se by Talend.

the class GoogleDataprocInfoForm method initCommonProperties.

private void initCommonProperties(HadoopServiceProperties properties) {
    HadoopClusterConnection connection = getConnection();
    ContextType contextType = null;
    if (getConnection().isContextMode()) {
        contextType = ConnectionContextHelper.getContextTypeForContextMode(connection, connection.getContextName(), false);
    }
    properties.setContextType(contextType);
    properties.setRelativeHadoopClusterId(connectionItem.getProperty().getId());
    properties.setRelativeHadoopClusterLabel(connectionItem.getProperty().getLabel());
    properties.setDistribution(connection.getDistribution());
    properties.setVersion(connection.getDfVersion());
    properties.setUseKrb(connection.isEnableKerberos());
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) HadoopClusterConnection(org.talend.repository.model.hadoopcluster.HadoopClusterConnection)

Example 92 with ContextType

use of org.talend.designer.core.model.utils.emf.talendfile.ContextType in project tbd-studio-se by Talend.

the class KnoxInfoForm method initCommonProperties.

private void initCommonProperties(HadoopServiceProperties properties) {
    HadoopClusterConnection connection = getConnection();
    ContextType contextType = null;
    if (getConnection().isContextMode()) {
        contextType = ConnectionContextHelper.getContextTypeForContextMode(connection, connection.getContextName(), false);
    }
    properties.setContextType(contextType);
    properties.setRelativeHadoopClusterId(connectionItem.getProperty().getId());
    properties.setRelativeHadoopClusterLabel(connectionItem.getProperty().getLabel());
    properties.setDistribution(connection.getDistribution());
    properties.setVersion(connection.getDfVersion());
    properties.setUseKrb(connection.isEnableKerberos());
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) HadoopClusterConnection(org.talend.repository.model.hadoopcluster.HadoopClusterConnection)

Example 93 with ContextType

use of org.talend.designer.core.model.utils.emf.talendfile.ContextType in project tbd-studio-se by Talend.

the class MongoDBConnectionUtil method extractFromContext.

private static String extractFromContext(NoSQLConnection connection, String attr) {
    String attrVal = connection.getAttributes().get(attr);
    if (connection.isContextMode()) {
        ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connection);
        attrVal = ContextParameterUtils.getOriginalValue(contextType, attrVal);
    }
    return attrVal;
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType)

Example 94 with ContextType

use of org.talend.designer.core.model.utils.emf.talendfile.ContextType in project tbd-studio-se by Talend.

the class MongoDBConnectionUtil method getMongo4ConnStringVersion.

public static synchronized Object getMongo4ConnStringVersion(NoSQLConnection connection, ContextType contextType, ClassLoader classLoader, boolean requireAuth, boolean requireEncryption) throws NoSQLServerException {
    Object mongo = null;
    String user = connection.getAttributes().get(IMongoDBAttributes.USERNAME);
    String pass = connection.getAttributes().get(IMongoDBAttributes.PASSWORD);
    if (contextType != null) {
        user = ContextParameterUtils.getOriginalValue(contextType, user);
        pass = ContextParameterUtils.getOriginalValue(contextType, pass);
    } else {
        pass = connection.getValue(pass, false);
    }
    try {
        String connString = extractFromContext(connection, IMongoDBAttributes.CONN_STRING);
        Object clientSettingsBuilder = // $NON-NLS-1$ //$NON-NLS-2$
        NoSQLReflection.invokeStaticMethod(// $NON-NLS-1$ //$NON-NLS-2$
        "com.mongodb.MongoClientSettings", // $NON-NLS-1$ //$NON-NLS-2$
        "builder", new Object[0], classLoader);
        Object connectionString = NoSQLReflection.newInstance("com.mongodb.ConnectionString", new Object[] { connString }, classLoader, String.class);
        clientSettingsBuilder = NoSQLReflection.invokeMethod(clientSettingsBuilder, "applyConnectionString", new Object[] { connectionString }, connectionString.getClass());
        Object mongoCredential = getCredential(connection, contextType, user, pass, classLoader);
        clientSettingsBuilder = NoSQLReflection.invokeMethod(clientSettingsBuilder, "credential", new Object[] { mongoCredential }, Class.forName("com.mongodb.MongoCredential", true, classLoader));
        String authMechanism = connection.getAttributes().get(IMongoDBAttributes.AUTHENTICATION_MECHANISM);
        // 
        if ((requireAuth && authMechanism.equals(IMongoConstants.X509)) || requireEncryption) {
            // enable ssl & context
            // $NON-NLS-1$
            Class<?> blockClasszz = Class.forName("com.mongodb.Block", false, classLoader);
            Class[] interfaces = new Class[1];
            interfaces[0] = blockClasszz;
            Object block = Proxy.newProxyInstance(classLoader, interfaces, (proxy, method, args) -> {
                switch(method.getName()) {
                    case // $NON-NLS-1$
                    "apply":
                        if (args[0] != null) {
                            SSLContext sslContext = null;
                            if (requireAuth && authMechanism.equals(IMongoConstants.X509)) {
                                sslContext = mongoX509SSLContext(connection);
                            } else if (requireEncryption) {
                                sslContext = StudioSSLContextProvider.getContext();
                            }
                            // $NON-NLS-1$
                            NoSQLReflection.invokeMethod(// $NON-NLS-1$
                            args[0], // $NON-NLS-1$
                            "context", // $NON-NLS-1$
                            new Object[] { sslContext }, // $NON-NLS-1$
                            SSLContext.class);
                        }
                        return null;
                    default:
                        throw new NoSQLServerException(// $NON-NLS-1$
                        Messages.getString("MongoDBConnectionUtil.CannotFindMethod", method.getName()));
                }
            });
            clientSettingsBuilder = NoSQLReflection.invokeMethod(clientSettingsBuilder, "applyToSslSettings", new Object[] { block }, blockClasszz);
            // $NON-NLS-1$
            blockClasszz = Class.forName("com.mongodb.Block", false, classLoader);
            interfaces = new Class[1];
            interfaces[0] = blockClasszz;
            block = Proxy.newProxyInstance(classLoader, interfaces, (proxy, method, args) -> {
                switch(method.getName()) {
                    case // $NON-NLS-1$
                    "apply":
                        if (args[0] != null) {
                            // $NON-NLS-1$
                            NoSQLReflection.invokeMethod(// $NON-NLS-1$
                            args[0], // $NON-NLS-1$
                            "enabled", // $NON-NLS-1$
                            new Object[] { true }, boolean.class);
                        }
                        return null;
                    default:
                        throw new Exception(// $NON-NLS-1$
                        "MongoDBConnectionUtil.CannotFindMethod" + method.getName());
                }
            });
            clientSettingsBuilder = NoSQLReflection.invokeMethod(clientSettingsBuilder, "applyToSslSettings", new Object[] { block }, blockClasszz);
        }
        Object mongoClientSettings = NoSQLReflection.invokeMethod(clientSettingsBuilder, "build", new Object[0]);
        mongo = // $NON-NLS-1$ //$NON-NLS-2$
        NoSQLReflection.invokeStaticMethod(// $NON-NLS-1$ //$NON-NLS-2$
        "com.mongodb.client.MongoClients", // $NON-NLS-1$ //$NON-NLS-2$
        "create", new Object[] { mongoClientSettings }, classLoader, // $NON-NLS-1$
        Class.forName("com.mongodb.MongoClientSettings", true, classLoader));
        mongos.add(mongo);
    } catch (Exception e) {
        throw new NoSQLServerException(e);
    }
    return mongo;
}
Also used : X509Certificate(java.security.cert.X509Certificate) SSLContext(javax.net.ssl.SSLContext) StringUtils(org.apache.commons.lang.StringUtils) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) CertificateFactory(java.security.cert.CertificateFactory) JSONObject(org.talend.utils.json.JSONObject) NoSQLClassLoaderFactory(org.talend.repository.nosql.factory.NoSQLClassLoaderFactory) TalendQuoteUtils(org.talend.core.utils.TalendQuoteUtils) SecureRandom(java.security.SecureRandom) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException) Map(java.util.Map) IMongoConstants(org.talend.repository.nosql.db.common.mongodb.IMongoConstants) NoSQLReflection(org.talend.repository.nosql.reflection.NoSQLReflection) ASN1Object(org.bouncycastle.asn1.ASN1Object) ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ContextParameterUtils(org.talend.core.model.utils.ContextParameterUtils) Set(java.util.Set) KeyStore(java.security.KeyStore) NoSQLConnection(org.talend.repository.model.nosql.NoSQLConnection) KeyFactory(java.security.KeyFactory) Base64(java.util.Base64) List(java.util.List) Certificate(java.security.cert.Certificate) RSAPrivateKeyStructure(org.bouncycastle.asn1.pkcs.RSAPrivateKeyStructure) ConnectionContextHelper(org.talend.metadata.managment.ui.utils.ConnectionContextHelper) PrivateKey(java.security.PrivateKey) PluginUtil(org.talend.core.ui.utils.PluginUtil) Pattern(java.util.regex.Pattern) INoSQLCommonAttributes(org.talend.repository.nosql.constants.INoSQLCommonAttributes) Proxy(java.lang.reflect.Proxy) StudioEncryption(org.talend.utils.security.StudioEncryption) HashMap(java.util.HashMap) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StudioSSLContextProvider(org.talend.core.utils.StudioSSLContextProvider) ExceptionHandler(org.talend.commons.exception.ExceptionHandler) JSONArray(org.talend.utils.json.JSONArray) JSONException(org.talend.utils.json.JSONException) Properties(java.util.Properties) CoreRuntimePlugin(org.talend.core.runtime.CoreRuntimePlugin) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) Iterator(java.util.Iterator) Files(java.nio.file.Files) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) Messages(org.talend.repository.nosql.i18n.Messages) FileInputStream(java.io.FileInputStream) File(java.io.File) KeyManager(javax.net.ssl.KeyManager) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) Paths(java.nio.file.Paths) IMongoDBAttributes(org.talend.repository.nosql.db.common.mongodb.IMongoDBAttributes) SSLPreferenceConstants(org.talend.core.prefs.SSLPreferenceConstants) Collections(java.util.Collections) InputStream(java.io.InputStream) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) JSONObject(org.talend.utils.json.JSONObject) ASN1Object(org.bouncycastle.asn1.ASN1Object) SSLContext(javax.net.ssl.SSLContext) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) JSONException(org.talend.utils.json.JSONException)

Example 95 with ContextType

use of org.talend.designer.core.model.utils.emf.talendfile.ContextType in project tbd-studio-se by Talend.

the class MongoDBConnectionUtil method getDB.

public static synchronized Object getDB(NoSQLConnection connection, String dbName, Object mongoClient) throws NoSQLServerException {
    Object db = null;
    if (StringUtils.isEmpty(dbName)) {
        return db;
    }
    try {
        String requireAuthAttr = connection.getAttributes().get(IMongoDBAttributes.REQUIRED_AUTHENTICATION);
        boolean requireAuth = requireAuthAttr == null ? false : Boolean.valueOf(requireAuthAttr);
        // 
        String requireEncryptionAttr = connection.getAttributes().get(IMongoDBAttributes.REQUIRED_ENCRYPTION);
        boolean requireEncryption = requireEncryptionAttr == null ? false : Boolean.valueOf(requireEncryptionAttr);
        updateConfigProperties(requireEncryption);
        if (isUpgradeLatestVersion(connection)) {
            if (mongoClient == null) {
                mongoClient = getMongo(connection, requireAuth, requireEncryption);
            }
            db = // $NON-NLS-1$
            NoSQLReflection.invokeMethod(// $NON-NLS-1$
            mongoClient, // $NON-NLS-1$
            "getDatabase", new Object[] { dbName });
        } else if (isUpgradeVersion(connection)) {
            if (mongoClient == null) {
                mongoClient = getMongo(connection, requireAuth, requireEncryption);
            }
            db = // $NON-NLS-1$
            NoSQLReflection.invokeMethod(// $NON-NLS-1$
            mongoClient, // $NON-NLS-1$
            "getDB", new Object[] { dbName });
        } else {
            if (mongoClient == null) {
                mongoClient = getMongo(connection);
            }
            // $NON-NLS-1$
            db = NoSQLReflection.invokeMethod(mongoClient, "getDB", new Object[] { dbName });
            // Do authenticate
            if (requireAuth) {
                String userName = connection.getAttributes().get(IMongoDBAttributes.USERNAME);
                String password = connection.getValue(connection.getAttributes().get(IMongoDBAttributes.PASSWORD), false);
                if (connection.isContextMode()) {
                    ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connection);
                    userName = ContextParameterUtils.getOriginalValue(contextType, userName);
                    password = ContextParameterUtils.getOriginalValue(contextType, password);
                }
                if (userName != null && password != null) {
                    boolean authorized = (Boolean) NoSQLReflection.invokeMethod(db, "authenticate", // $NON-NLS-1$
                    new Object[] { userName, password.toCharArray() });
                    if (!authorized) {
                        // $NON-NLS-1$
                        throw new NoSQLServerException(Messages.getString("MongoDBConnectionUtil.ConnotLogon", dbName));
                    }
                }
            }
        }
    } catch (NoSQLReflectionException e) {
        throw new NoSQLServerException(e);
    }
    return db;
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) JSONObject(org.talend.utils.json.JSONObject) ASN1Object(org.bouncycastle.asn1.ASN1Object) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException)

Aggregations

ContextType (org.talend.designer.core.model.utils.emf.talendfile.ContextType)108 ContextParameterType (org.talend.designer.core.model.utils.emf.talendfile.ContextParameterType)27 ArrayList (java.util.ArrayList)26 ContextItem (org.talend.core.model.properties.ContextItem)21 PersistenceException (org.talend.commons.exception.PersistenceException)17 File (java.io.File)16 NoSQLServerException (org.talend.repository.nosql.exceptions.NoSQLServerException)15 HadoopClusterConnection (org.talend.repository.model.hadoopcluster.HadoopClusterConnection)13 NoSQLReflectionException (org.talend.repository.nosql.exceptions.NoSQLReflectionException)13 Test (org.junit.Test)12 HashMap (java.util.HashMap)10 List (java.util.List)10 ProcessItem (org.talend.core.model.properties.ProcessItem)9 IOException (java.io.IOException)8 Map (java.util.Map)8 EList (org.eclipse.emf.common.util.EList)7 FileNotFoundException (java.io.FileNotFoundException)6 FileOutputStream (java.io.FileOutputStream)6 Property (org.talend.core.model.properties.Property)6 HashSet (java.util.HashSet)5