Search in sources :

Example 11 with IAsyncResult

use of io.apiman.gateway.engine.async.IAsyncResult in project apiman by apiman.

the class LDAPIdentityValidator method handleLdapSearch.

private void handleLdapSearch(final ILdapClientConnection connection, List<ILdapSearchEntry> searchEntries, LDAPIdentitySource config, LdapConfigBean ldapConfigBean, ILdapComponent ldapComponent, IPolicyContext context, String username, String password, final IAsyncResultHandler<Boolean> handler) {
    if (searchEntries.size() > 1) {
        // $NON-NLS-1$
        NamingException ex = new NamingException("Found multiple entries for the same username: " + username);
        handler.handle(AsyncResultImpl.<Boolean>create(ex));
    } else if (searchEntries.isEmpty()) {
        handler.handle(AsyncResultImpl.create(Boolean.FALSE));
    } else {
        // Just one result
        // First entry
        String userDn = searchEntries.get(0).getDn();
        if (userDn != null) {
            ldapConfigBean.setBindDn(userDn);
            ldapConfigBean.setBindPassword(password);
            bind(config, ldapConfigBean, ldapComponent, context, new IAsyncResultHandler<ILdapResult>() {

                @Override
                public void handle(IAsyncResult<ILdapResult> result) {
                    if (result.isError()) {
                        if (result.getError() instanceof LdapException) {
                            LdapException ex = (LdapException) result.getError();
                            if (ex.getResultCode().isAuthFailure()) {
                                handler.handle(AsyncResultImpl.create(Boolean.FALSE));
                            } else {
                                handler.handle(AsyncResultImpl.<Boolean>create(ex));
                            }
                            connection.close(ex);
                        } else {
                            handler.handle(AsyncResultImpl.<Boolean>create(result.getError()));
                            connection.close();
                        }
                    } else {
                        LdapResultCode resultCode = result.getResult().getResultCode();
                        if (LdapResultCode.isSuccess(resultCode)) {
                            handler.handle(AsyncResultImpl.create(Boolean.TRUE));
                        } else {
                            // TODO handle errors better?
                            handler.handle(AsyncResultImpl.create(Boolean.FALSE));
                        }
                        connection.close();
                    }
                }
            });
        } else {
            handler.handle(AsyncResultImpl.create(Boolean.FALSE));
        }
    }
}
Also used : IAsyncResultHandler(io.apiman.gateway.engine.async.IAsyncResultHandler) LdapResultCode(io.apiman.gateway.engine.components.ldap.result.LdapResultCode) NamingException(javax.naming.NamingException) IAsyncResult(io.apiman.gateway.engine.async.IAsyncResult) LdapException(io.apiman.gateway.engine.components.ldap.result.LdapException)

Example 12 with IAsyncResult

use of io.apiman.gateway.engine.async.IAsyncResult in project apiman by apiman.

the class SimpleSharedStatePolicy method apply.

/**
 * @see io.apiman.gateway.engine.policy.IPolicy#apply(io.apiman.gateway.engine.beans.ApiRequest, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object, io.apiman.gateway.engine.policy.IPolicyChain)
 */
@Override
public void apply(final ApiRequest request, final IPolicyContext context, final Object config, final IPolicyChain<ApiRequest> chain) {
    final ISharedStateComponent sharedState = context.getComponent(ISharedStateComponent.class);
    final String namespace = "urn:" + getClass().getName();
    final String propName = "test-property";
    sharedState.getProperty(namespace, propName, "", new IAsyncResultHandler<String>() {

        @Override
        public void handle(IAsyncResult<String> result) {
            String propVal = result.getResult();
            String newVal = propVal + "+";
            sharedState.setProperty(namespace, propName, newVal, new IAsyncResultHandler<Void>() {

                @Override
                public void handle(IAsyncResult<Void> result) {
                    chain.doApply(request);
                }
            });
        }
    });
}
Also used : ISharedStateComponent(io.apiman.gateway.engine.components.ISharedStateComponent) IAsyncResultHandler(io.apiman.gateway.engine.async.IAsyncResultHandler) IAsyncResult(io.apiman.gateway.engine.async.IAsyncResult)

Example 13 with IAsyncResult

use of io.apiman.gateway.engine.async.IAsyncResult in project apiman by apiman.

the class DefaultPluginRegistry method downloadPlugin.

/**
 * Downloads the plugin via its maven GAV information.  This will first look in the local
 * .m2 directory.  If the plugin is not found there, then it will try to download the
 * plugin from one of the configured remote maven repositories.
 */
protected void downloadPlugin(final PluginCoordinates coordinates, final IAsyncResultHandler<File> handler) {
    if (pluginRepositories.isEmpty()) {
        // Didn't find it - no repositories configured!
        handler.handle(AsyncResultImpl.create((File) null));
        return;
    }
    final Iterator<URI> iterator = pluginRepositories.iterator();
    URI repoUrl = iterator.next();
    final IAsyncResultHandler<File> handler2 = new IAsyncResultHandler<File>() {

        @Override
        public void handle(IAsyncResult<File> result) {
            // If result is bad : No success or result empty And other repo exist
            if ((!result.isSuccess() || result.getResult() == null) && iterator.hasNext()) {
                downloadFromMavenRepo(coordinates, iterator.next(), this);
            } else {
                // If result is Good or all repo tried
                handler.handle(result);
            }
        }
    };
    downloadFromMavenRepo(coordinates, repoUrl, handler2);
}
Also used : IAsyncResultHandler(io.apiman.gateway.engine.async.IAsyncResultHandler) IAsyncResult(io.apiman.gateway.engine.async.IAsyncResult) File(java.io.File) URI(java.net.URI)

Example 14 with IAsyncResult

use of io.apiman.gateway.engine.async.IAsyncResult in project apiman by apiman.

the class BasicMutualAuthTest method shouldSucceedWhenAllowedSelfSigned.

/**
 * Scenario:
 *   - no CA inherited trust
 *   - gateway does not explicitly trust the API, but automatically validates against self-signed
 *   - API trusts gateway certificate
 */
@Test
public void shouldSucceedWhenAllowedSelfSigned() {
    config.put(TLSOptions.TLS_TRUSTSTORE, getResourcePath("2waytest/basic_mutual_auth/gateway_ts.jks"));
    config.put(TLSOptions.TLS_TRUSTSTOREPASSWORD, "changeme");
    config.put(TLSOptions.TLS_KEYSTORE, getResourcePath("2waytest/basic_mutual_auth/gateway_ks.jks"));
    config.put(TLSOptions.TLS_KEYSTOREPASSWORD, "changeme");
    config.put(TLSOptions.TLS_KEYPASSWORD, "changeme");
    config.put(TLSOptions.TLS_ALLOWANYHOST, "true");
    config.put(TLSOptions.TLS_ALLOWSELFSIGNED, "true");
    HttpConnectorFactory factory = new HttpConnectorFactory(config);
    IApiConnector connector = factory.createConnector(request, api, RequiredAuthType.MTLS, false, new ConnectorConfigImpl());
    IApiConnection connection = connector.connect(request, (IAsyncResult<IApiConnectionResponse> result) -> {
        Assert.assertTrue(result.isSuccess());
    });
    connection.end();
}
Also used : IApiConnection(io.apiman.gateway.engine.IApiConnection) HttpConnectorFactory(io.apiman.gateway.platforms.servlet.connectors.HttpConnectorFactory) ConnectorConfigImpl(io.apiman.gateway.platforms.servlet.connectors.ConnectorConfigImpl) IAsyncResult(io.apiman.gateway.engine.async.IAsyncResult) IApiConnector(io.apiman.gateway.engine.IApiConnector) Test(org.junit.Test)

Example 15 with IAsyncResult

use of io.apiman.gateway.engine.async.IAsyncResult in project apiman by apiman.

the class BasicMutualAuthTest method shouldFailWhenApiDoesNotTrustGateway.

/**
 * Scenario:
 *   - no CA inherited trust
 *   - gateway does trust the API
 *   - API does <em>not</em> trust gateway
 */
@Test
public void shouldFailWhenApiDoesNotTrustGateway() {
    config.put(TLSOptions.TLS_TRUSTSTORE, getResourcePath("2waytest/service_not_trust_gw/gateway_ts.jks"));
    config.put(TLSOptions.TLS_TRUSTSTOREPASSWORD, "changeme");
    config.put(TLSOptions.TLS_KEYSTORE, getResourcePath("2waytest/service_not_trust_gw/gateway_ks.jks"));
    config.put(TLSOptions.TLS_KEYSTOREPASSWORD, "changeme");
    config.put(TLSOptions.TLS_KEYPASSWORD, "changeme");
    config.put(TLSOptions.TLS_ALLOWANYHOST, "true");
    config.put(TLSOptions.TLS_ALLOWSELFSIGNED, "false");
    HttpConnectorFactory factory = new HttpConnectorFactory(config);
    IApiConnector connector = factory.createConnector(request, api, RequiredAuthType.MTLS, false, new ConnectorConfigImpl());
    IApiConnection connection = connector.connect(request, (IAsyncResult<IApiConnectionResponse> result) -> {
        Assert.assertTrue(result.isError());
        System.out.println(result.getError());
        Assert.assertTrue(result.getError() instanceof ConnectorException);
    // Would like to assert on SSL error, but is sun specific info
    // TODO improve connector to handle this situation better
    });
    connection.end();
}
Also used : IApiConnection(io.apiman.gateway.engine.IApiConnection) HttpConnectorFactory(io.apiman.gateway.platforms.servlet.connectors.HttpConnectorFactory) ConnectorException(io.apiman.gateway.engine.beans.exceptions.ConnectorException) ConnectorConfigImpl(io.apiman.gateway.platforms.servlet.connectors.ConnectorConfigImpl) IAsyncResult(io.apiman.gateway.engine.async.IAsyncResult) IApiConnector(io.apiman.gateway.engine.IApiConnector) Test(org.junit.Test)

Aggregations

IAsyncResult (io.apiman.gateway.engine.async.IAsyncResult)18 IApiConnector (io.apiman.gateway.engine.IApiConnector)9 IApiConnection (io.apiman.gateway.engine.IApiConnection)8 ConnectorConfigImpl (io.apiman.gateway.platforms.servlet.connectors.ConnectorConfigImpl)8 HttpConnectorFactory (io.apiman.gateway.platforms.servlet.connectors.HttpConnectorFactory)8 Test (org.junit.Test)8 IAsyncResultHandler (io.apiman.gateway.engine.async.IAsyncResultHandler)6 ArrayList (java.util.ArrayList)3 ConnectorException (io.apiman.gateway.engine.beans.exceptions.ConnectorException)2 IJdbcResultSet (io.apiman.gateway.engine.components.jdbc.IJdbcResultSet)2 PolicyWithConfiguration (io.apiman.gateway.engine.policy.PolicyWithConfiguration)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 X509Certificate (java.security.cert.X509Certificate)2 HashSet (java.util.HashSet)2 Plugin (io.apiman.common.plugin.Plugin)1 IConnectorConfig (io.apiman.gateway.engine.IConnectorConfig)1 IEngineResult (io.apiman.gateway.engine.IEngineResult)1 ApiContract (io.apiman.gateway.engine.beans.ApiContract)1