Search in sources :

Example 1 with TeiidURL

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

the class SocketServerConnectionFactory method getConnection.

/**
 * @param connectionProperties will be updated with additional information before logon
 */
public SocketServerConnection getConnection(Properties connectionProperties) throws CommunicationException, ConnectionException {
    TeiidURL url;
    try {
        url = new TeiidURL(connectionProperties.getProperty(TeiidURL.CONNECTION.SERVER_URL));
    } catch (MalformedURLException e1) {
        throw new ConnectionException(JDBCPlugin.Event.TEIID20014, e1, e1.getMessage());
    }
    String discoveryStrategyName = connectionProperties.getProperty(TeiidURL.CONNECTION.DISCOVERY_STRATEGY, URL);
    ServerDiscovery discovery;
    if (URL.equalsIgnoreCase(discoveryStrategyName)) {
        discovery = new UrlServerDiscovery();
    } else {
        try {
            discovery = (ServerDiscovery) ReflectionHelper.create(discoveryStrategyName, null, this.getClass().getClassLoader());
        } catch (TeiidException e) {
            throw new ConnectionException(e);
        }
    }
    discovery.init(url, connectionProperties);
    return new SocketServerConnection(this, url.isUsingSSL(), discovery, connectionProperties);
}
Also used : MalformedURLException(java.net.MalformedURLException) TeiidURL(org.teiid.net.TeiidURL) ConnectionException(org.teiid.net.ConnectionException) TeiidException(org.teiid.core.TeiidException)

Example 2 with TeiidURL

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

the class TestSocketServerConnection method testLogonFailsWithMultipleHosts.

@Test
public void testLogonFailsWithMultipleHosts() throws Exception {
    Properties p = new Properties();
    SocketServerInstanceFactory instanceFactory = Mockito.mock(SocketServerInstanceFactory.class);
    Mockito.stub(instanceFactory.getServerInstance((HostInfo) Mockito.anyObject())).toThrow(new SingleInstanceCommunicationException());
    // $NON-NLS-1$
    ServerDiscovery discovery = new UrlServerDiscovery(new TeiidURL("mm://host1:1,host2:2"));
    try {
        new SocketServerConnection(instanceFactory, false, discovery, p);
        // $NON-NLS-1$
        fail("exception expected");
    } catch (CommunicationException e) {
        // $NON-NLS-1$
        assertEquals("TEIID20021 No valid host available. Attempted connections to: [host1:1, host2:2]", e.getMessage());
    }
}
Also used : CommunicationException(org.teiid.net.CommunicationException) TeiidURL(org.teiid.net.TeiidURL) Properties(java.util.Properties) Test(org.junit.Test)

Example 3 with TeiidURL

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

the class TestCommSockets method testFailedConnect.

@Test(expected = CommunicationException.class)
public void testFailedConnect() throws Exception {
    SSLConfiguration config = new SSLConfiguration();
    listener = new SocketListener(addr, 1024, 1024, 1, config, null, BufferManagerFactory.getStandaloneBufferManager());
    Properties p = new Properties();
    String url = new TeiidURL(addr.getHostName(), listener.getPort() - 1, false).getAppServerURL();
    // wrong port
    p.setProperty(TeiidURL.CONNECTION.SERVER_URL, url);
    SocketServerConnectionFactory.getInstance().getConnection(p);
}
Also used : TeiidURL(org.teiid.net.TeiidURL) Properties(java.util.Properties) Test(org.junit.Test)

Example 4 with TeiidURL

use of org.teiid.net.TeiidURL 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 5 with TeiidURL

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

the class TeiidDataSource method buildServerURL.

protected String buildServerURL() throws TeiidSQLException {
    if (serverName == null) {
        return null;
    }
    if (this.alternateServers == null || this.alternateServers.length() == 0) {
        // Format:  "mm://server:port"
        return new TeiidURL(this.serverName, this.portNumber, this.secure).getAppServerURL();
    }
    // Format: "mm://server1:port,server2:port,..."
    String serverURL = this.secure ? TeiidURL.SECURE_PROTOCOL : TeiidURL.DEFAULT_PROTOCOL;
    if (this.serverName.indexOf(':') != -1 && !this.serverName.startsWith("[")) {
        // $NON-NLS-1$
        // $NON-NLS-1$ //$NON-NLS-2$
        serverURL += "[" + this.serverName + "]";
    } else {
        serverURL += this.serverName;
    }
    serverURL += TeiidURL.COLON_DELIMITER + this.portNumber;
    // add in the port number if not specified
    String[] as = this.alternateServers.split(TeiidURL.COMMA_DELIMITER);
    for (int i = 0; i < as.length; i++) {
        String server = as[i].trim();
        // ipv6 without port
        if (server.startsWith("[") && server.endsWith("]")) {
            // $NON-NLS-1$ //$NON-NLS-2$
            String msg = reasonWhyInvalidServerName(server.substring(1, server.length() - 1));
            if (msg != null) {
                // $NON-NLS-1$
                throw createConnectionError(JDBCPlugin.Util.getString("MMDataSource.alternateServer_is_invalid", msg));
            }
            serverURL += (TeiidURL.COMMA_DELIMITER + as[i] + TeiidURL.COLON_DELIMITER + this.portNumber);
        } else {
            String[] serverParts = server.split(TeiidURL.COLON_DELIMITER, 2);
            String msg = reasonWhyInvalidServerName(serverParts[0]);
            if (msg != null) {
                // $NON-NLS-1$
                throw createConnectionError(JDBCPlugin.Util.getString("MMDataSource.alternateServer_is_invalid", msg));
            }
            serverURL += (TeiidURL.COMMA_DELIMITER + serverParts[0] + TeiidURL.COLON_DELIMITER);
            if (serverParts.length > 1) {
                try {
                    TeiidURL.validatePort(serverParts[1]);
                } catch (MalformedURLException e) {
                    // $NON-NLS-1$
                    throw createConnectionError(JDBCPlugin.Util.getString("MMDataSource.alternateServer_is_invalid", e.getMessage()));
                }
                serverURL += serverParts[1];
            } else {
                serverURL += this.portNumber;
            }
        }
    }
    try {
        return new TeiidURL(serverURL).getAppServerURL();
    } catch (MalformedURLException e) {
        throw TeiidSQLException.create(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) TeiidURL(org.teiid.net.TeiidURL)

Aggregations

TeiidURL (org.teiid.net.TeiidURL)8 Properties (java.util.Properties)4 CommunicationException (org.teiid.net.CommunicationException)3 MalformedURLException (java.net.MalformedURLException)2 Test (org.junit.Test)2 LogonException (org.teiid.client.security.LogonException)2 TeiidComponentException (org.teiid.core.TeiidComponentException)2 ConnectionException (org.teiid.net.ConnectionException)2 HostInfo (org.teiid.net.HostInfo)2 SocketServerConnectionFactory (org.teiid.net.socket.SocketServerConnectionFactory)2 UrlServerDiscovery (org.teiid.net.socket.UrlServerDiscovery)2 IOException (java.io.IOException)1 PrivilegedAction (java.security.PrivilegedAction)1 Subject (javax.security.auth.Subject)1 LoginContext (javax.security.auth.login.LoginContext)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 InvalidSessionException (org.teiid.client.security.InvalidSessionException)1 LogonResult (org.teiid.client.security.LogonResult)1 SessionToken (org.teiid.client.security.SessionToken)1 MemoryStorageManager (org.teiid.common.buffer.impl.MemoryStorageManager)1