Search in sources :

Example 6 with CommunicationException

use of org.teiid.net.CommunicationException in project teiid by teiid.

the class OioOjbectChannelFactory method createObjectChannel.

@Override
public ObjectChannel createObjectChannel(HostInfo info) throws CommunicationException, IOException {
    final Socket socket;
    if (info.isSsl()) {
        if (this.sslSocketFactory == null) {
            try {
                sslSocketFactory = SocketUtil.getSSLSocketFactory(props);
            } catch (GeneralSecurityException e) {
                throw new CommunicationException(JDBCPlugin.Event.TEIID20027, e, e.getMessage());
            }
        }
        socket = sslSocketFactory.getSocket(info.getHostName(), info.getPortNumber());
    } else {
        socket = new Socket(info.getInetAddress(), info.getPortNumber());
    }
    if (receiveBufferSize > 0) {
        socket.setReceiveBufferSize(receiveBufferSize);
    }
    if (sendBufferSize > 0) {
        socket.setSendBufferSize(sendBufferSize);
    }
    // enable Nagle's algorithm to conserve bandwidth
    socket.setTcpNoDelay(!conserveBandwidth);
    socket.setSoTimeout(soTimeout);
    return new OioObjectChannel(socket, maxObjectSize);
}
Also used : CommunicationException(org.teiid.net.CommunicationException) GeneralSecurityException(java.security.GeneralSecurityException) Socket(java.net.Socket)

Example 7 with CommunicationException

use of org.teiid.net.CommunicationException in project teiid by teiid.

the class TestSocketRemoting method testMethodInvocation.

@Test
public void testMethodInvocation() throws Exception {
    ClientServiceRegistryImpl csr = new ClientServiceRegistryImpl() {

        @Override
        public ClassLoader getCallerClassloader() {
            return getClass().getClassLoader();
        }
    };
    csr.registerClientService(ILogon.class, new ILogon() {

        public ResultsFuture<?> logoff() throws InvalidSessionException {
            ResultsFuture<?> result = new ResultsFuture<Void>();
            // $NON-NLS-1$
            result.getResultsReceiver().exceptionOccurred(new TeiidComponentException("some exception"));
            return result;
        }

        public LogonResult logon(Properties connectionProperties) throws LogonException, TeiidComponentException {
            return new LogonResult();
        }

        // tests asynch where we don't care about the result
        public ResultsFuture<?> ping() throws InvalidSessionException, TeiidComponentException {
            return null;
        }

        @Override
        public ResultsFuture<?> ping(Collection<String> sessions) throws TeiidComponentException, CommunicationException {
            return null;
        }

        @Override
        public void assertIdentity(SessionToken sessionId) throws InvalidSessionException, TeiidComponentException {
        }

        @Override
        public LogonResult neogitiateGssLogin(Properties connectionProperties, byte[] serviceToken, boolean createSession) throws LogonException {
            return null;
        }
    }, // $NON-NLS-1$
    "foo");
    // $NON-NLS-1$
    csr.registerClientService(FakeService.class, new FakeServiceImpl(), "foo");
    final FakeClientServerInstance serverInstance = new FakeClientServerInstance(csr);
    SocketServerConnection connection = createFakeConnection(serverInstance);
    ILogon logon = connection.getService(ILogon.class);
    Future<?> result = logon.ping();
    assertNull(result.get(0, TimeUnit.MILLISECONDS));
    result = logon.logoff();
    try {
        result.get(0, TimeUnit.MICROSECONDS);
        // $NON-NLS-1$
        fail("exception expected");
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof TeiidComponentException);
    }
    FakeService service = connection.getService(FakeService.class);
    Future<Integer> asynchInteger = service.asynchResult();
    assertEquals(new Integer(5), asynchInteger.get(0, TimeUnit.MILLISECONDS));
    try {
        service.exceptionMethod();
        // $NON-NLS-1$
        fail("exception expected");
    } catch (TeiidProcessingException e) {
    }
    DQP dqp = connection.getService(DQP.class);
    try {
        ResultsFuture<?> future = dqp.begin();
        future.get();
        // $NON-NLS-1$
        fail("exception expected");
    } catch (Exception e) {
        // $NON-NLS-1$
        assertTrue(e.getMessage().indexOf("Component not found:") != -1);
    }
}
Also used : LogonResult(org.teiid.client.security.LogonResult) Properties(java.util.Properties) TeiidProcessingException(org.teiid.core.TeiidProcessingException) LogonException(org.teiid.client.security.LogonException) ExecutionException(java.util.concurrent.ExecutionException) InvalidSessionException(org.teiid.client.security.InvalidSessionException) DQP(org.teiid.client.DQP) CommunicationException(org.teiid.net.CommunicationException) SessionToken(org.teiid.client.security.SessionToken) ILogon(org.teiid.client.security.ILogon) InvalidSessionException(org.teiid.client.security.InvalidSessionException) TeiidComponentException(org.teiid.core.TeiidComponentException) LogonException(org.teiid.client.security.LogonException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) CommunicationException(org.teiid.net.CommunicationException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) ConnectionException(org.teiid.net.ConnectionException) ResultsFuture(org.teiid.client.util.ResultsFuture) TeiidComponentException(org.teiid.core.TeiidComponentException) SocketServerConnection(org.teiid.net.socket.SocketServerConnection) Test(org.junit.Test)

Example 8 with CommunicationException

use of org.teiid.net.CommunicationException in project teiid by teiid.

the class GssAction method authenticate.

public static LogonResult authenticate(ILogon logon, Properties props) throws LogonException, TeiidComponentException, CommunicationException {
    if (logger.isLoggable(Level.FINE)) {
        // $NON-NLS-1$
        logger.fine("GSS Authentication Request");
    }
    Object result = null;
    StringBuilder errors = new StringBuilder();
    String jaasApplicationName = props.getProperty(TeiidURL.CONNECTION.JAAS_NAME);
    // $NON-NLS-1$
    String nl = System.getProperty("line.separator");
    if (jaasApplicationName == null) {
        // $NON-NLS-1$
        jaasApplicationName = "Teiid";
    }
    String kerberosPrincipalName = props.getProperty(TeiidURL.CONNECTION.KERBEROS_SERVICE_PRINCIPLE_NAME);
    if (kerberosPrincipalName == null) {
        try {
            TeiidURL url = new TeiidURL(props.getProperty(TeiidURL.CONNECTION.SERVER_URL));
            // $NON-NLS-1$
            kerberosPrincipalName = "TEIID/" + url.getHostInfo().get(0).getHostName();
        } catch (Exception e) {
        // Ignore exception
        }
        if (kerberosPrincipalName == null) {
            // $NON-NLS-1$
            errors.append(JDBCPlugin.Util.getString("client_prop_missing", TeiidURL.CONNECTION.KERBEROS_SERVICE_PRINCIPLE_NAME));
            errors.append(nl);
        }
    }
    // $NON-NLS-1$
    String krb5 = System.getProperty("java.security.krb5.conf");
    // $NON-NLS-1$
    String realm = System.getProperty("java.security.krb5.realm");
    // $NON-NLS-1$
    String kdc = System.getProperty("java.security.krb5.kdc");
    if (krb5 == null && realm == null && kdc == null) {
        // $NON-NLS-1$
        errors.append(JDBCPlugin.Util.getString("no_gss_selection"));
        errors.append(nl);
    } else if (krb5 != null && (realm != null || kdc != null)) {
        // $NON-NLS-1$
        errors.append(JDBCPlugin.Util.getString("ambigious_gss_selection"));
        errors.append(nl);
    } else if ((realm != null && kdc == null) || (realm == null && kdc != null)) {
        // krb5 is null here..
        if (realm == null) {
            // $NON-NLS-1$ //$NON-NLS-2$
            errors.append(JDBCPlugin.Util.getString("system_prop_missing", "java.security.krb5.realm"));
            errors.append(nl);
        }
        if (kdc == null) {
            // $NON-NLS-1$ //$NON-NLS-2$
            errors.append(JDBCPlugin.Util.getString("system_prop_missing", "java.security.krb5.kdc"));
            errors.append(nl);
        }
    }
    // $NON-NLS-1$
    String config = System.getProperty("java.security.auth.login.config");
    if (config == null) {
        // $NON-NLS-1$ //$NON-NLS-2$
        errors.append(JDBCPlugin.Util.getString("system_prop_missing", "java.security.auth.login.config"));
        errors.append(nl);
    }
    try {
        String user = props.getProperty(TeiidURL.CONNECTION.USER_NAME);
        String password = props.getProperty(TeiidURL.CONNECTION.PASSWORD);
        boolean performAuthentication = true;
        GSSCredential gssCredential = null;
        Subject sub = Subject.getSubject(AccessController.getContext());
        if (sub != null) {
            Set<GSSCredential> gssCreds = sub.getPrivateCredentials(GSSCredential.class);
            if (gssCreds != null && gssCreds.size() > 0) {
                gssCredential = gssCreds.iterator().next();
                performAuthentication = false;
                if (logger.isLoggable(Level.FINE)) {
                    // $NON-NLS-1$
                    logger.fine("GSS Authentication using delegated credential");
                }
            } else {
                if (logger.isLoggable(Level.FINE)) {
                    // $NON-NLS-1$
                    logger.fine("No delegation credential found in the subject");
                }
            }
        }
        if (performAuthentication) {
            if (errors.length() > 0) {
                throw new LogonException(JDBCPlugin.Event.TEIID20005, errors.toString());
            }
            LoginContext lc = new LoginContext(jaasApplicationName, new GSSCallbackHandler(user, password));
            lc.login();
            sub = lc.getSubject();
        }
        PrivilegedAction action = new GssAction(logon, kerberosPrincipalName, props, user, gssCredential);
        result = Subject.doAs(sub, action);
    } catch (Exception e) {
        throw new LogonException(JDBCPlugin.Event.TEIID20005, e, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20005));
    }
    if (result instanceof LogonException) {
        throw (LogonException) result;
    } else if (result instanceof TeiidComponentException) {
        throw (TeiidComponentException) result;
    } else if (result instanceof CommunicationException) {
        throw (CommunicationException) result;
    } else if (result instanceof Exception) {
        throw new LogonException(JDBCPlugin.Event.TEIID20005, (Exception) result, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20005));
    }
    return (LogonResult) result;
}
Also used : CommunicationException(org.teiid.net.CommunicationException) TeiidURL(org.teiid.net.TeiidURL) LogonResult(org.teiid.client.security.LogonResult) TeiidComponentException(org.teiid.core.TeiidComponentException) CommunicationException(org.teiid.net.CommunicationException) LogonException(org.teiid.client.security.LogonException) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) Subject(javax.security.auth.Subject) LoginContext(javax.security.auth.login.LoginContext) PrivilegedAction(java.security.PrivilegedAction) LogonException(org.teiid.client.security.LogonException) TeiidComponentException(org.teiid.core.TeiidComponentException)

Example 9 with CommunicationException

use of org.teiid.net.CommunicationException in project teiid by teiid.

the class LocalServerConnection method authenticate.

public synchronized void authenticate() throws ConnectionException, CommunicationException {
    Object previousSecurityContext = workContext.getSecurityHelper().associateSecurityContext(workContext.getSession().getSecurityContext());
    try {
        logoff();
    } finally {
        workContext.getSecurityHelper().associateSecurityContext(previousSecurityContext);
    }
    workContext.setSecurityContext(previousSecurityContext);
    try {
        this.result = this.getService(ILogon.class).logon(this.connectionProperties);
        AuthenticationType type = (AuthenticationType) this.result.getProperty(ILogon.AUTH_TYPE);
        if (type != null) {
            // server has issued an additional challenge
            if (type == AuthenticationType.GSS) {
                try {
                    this.result = MakeGSS.authenticate(this.getService(ILogon.class), this.connectionProperties);
                } catch (LogonException e) {
                    if (!passthrough) {
                        throw new LogonException(RuntimePlugin.Event.TEIID40150, e, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40150));
                    }
                    throw e;
                }
            } else {
                throw new LogonException(JDBCPlugin.Event.TEIID20034, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20034, type));
            }
        }
    } catch (LogonException e) {
        // to give to the user
        throw new ConnectionException(e);
    } catch (TeiidComponentException e) {
        if (e.getCause() instanceof CommunicationException) {
            throw (CommunicationException) e.getCause();
        }
        throw new CommunicationException(RuntimePlugin.Event.TEIID40069, e);
    }
}
Also used : CommunicationException(org.teiid.net.CommunicationException) LogonException(org.teiid.client.security.LogonException) TeiidComponentException(org.teiid.core.TeiidComponentException) ConnectionException(org.teiid.net.ConnectionException) AuthenticationType(org.teiid.net.socket.AuthenticationType)

Example 10 with CommunicationException

use of org.teiid.net.CommunicationException in project teiid by teiid.

the class TestSocketServerConnection method createConnection.

private SocketServerConnection createConnection(final Throwable t, final HostInfo hostInfo, Properties p) throws CommunicationException, ConnectionException {
    ServerDiscovery discovery = new UrlServerDiscovery(new TeiidURL(hostInfo.getHostName(), hostInfo.getPortNumber(), false));
    SocketServerInstanceFactory instanceFactory = new SocketServerInstanceFactory() {

        FakeILogon logon = new FakeILogon(t);

        @Override
        public SocketServerInstance getServerInstance(HostInfo info) throws CommunicationException, IOException {
            SocketServerInstance instance = Mockito.mock(SocketServerInstance.class);
            Mockito.stub(instance.getCryptor()).toReturn(new NullCryptor());
            Mockito.stub(instance.getHostInfo()).toReturn(hostInfo);
            Mockito.stub(instance.getService(ILogon.class)).toReturn(logon);
            Mockito.stub(instance.getServerVersion()).toReturn("07.03");
            if (t != null) {
                try {
                    Mockito.doAnswer(new Answer<Void>() {

                        @Override
                        public Void answer(InvocationOnMock invocation) throws Throwable {
                            if (logon.t == null) {
                                return null;
                            }
                            throw logon.t;
                        }
                    }).when(instance).send((Message) Mockito.anyObject(), (ResultsReceiver<Object>) Mockito.anyObject(), (Serializable) Mockito.anyObject());
                } catch (Exception e) {
                }
            }
            Mockito.stub(instance.isOpen()).toReturn(true);
            return instance;
        }

        @Override
        public void connected(SocketServerInstance instance, SessionToken session) {
        }

        @Override
        public void disconnected(SocketServerInstance instance, SessionToken session) {
        }
    };
    SocketServerConnection connection = new SocketServerConnection(instanceFactory, false, discovery, p);
    return connection;
}
Also used : SessionToken(org.teiid.client.security.SessionToken) TeiidURL(org.teiid.net.TeiidURL) NullCryptor(org.teiid.core.crypto.NullCryptor) InvalidSessionException(org.teiid.client.security.InvalidSessionException) TeiidComponentException(org.teiid.core.TeiidComponentException) CommunicationException(org.teiid.net.CommunicationException) IOException(java.io.IOException) LogonException(org.teiid.client.security.LogonException) ConnectionException(org.teiid.net.ConnectionException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HostInfo(org.teiid.net.HostInfo)

Aggregations

CommunicationException (org.teiid.net.CommunicationException)20 Test (org.junit.Test)9 ConnectionException (org.teiid.net.ConnectionException)9 IOException (java.io.IOException)7 ILogon (org.teiid.client.security.ILogon)6 LogonException (org.teiid.client.security.LogonException)6 TeiidComponentException (org.teiid.core.TeiidComponentException)6 Properties (java.util.Properties)5 TeiidException (org.teiid.core.TeiidException)5 UnknownHostException (java.net.UnknownHostException)4 InvalidSessionException (org.teiid.client.security.InvalidSessionException)4 SocketException (java.net.SocketException)3 SocketTimeoutException (java.net.SocketTimeoutException)3 CryptoException (org.teiid.core.crypto.CryptoException)3 NullCryptor (org.teiid.core.crypto.NullCryptor)3 TeiidURL (org.teiid.net.TeiidURL)3 SocketServerConnection (org.teiid.net.socket.SocketServerConnection)3 ConnectException (java.net.ConnectException)2 MalformedURLException (java.net.MalformedURLException)2 DQP (org.teiid.client.DQP)2