Search in sources :

Example 6 with IAsyncResult

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

the class BasicMutualAuthTest method shouldFailWhenGatewayDoesNotTrustApi.

/**
 * Scenario:
 *   - no CA inherited trust
 *   - gateway does <em>not</em> trust the API
 *   - API trusts gateway certificate
 */
@Test
public void shouldFailWhenGatewayDoesNotTrustApi() {
    config.put(TLSOptions.TLS_TRUSTSTORE, getResourcePath("2waytest/basic_mutual_auth_2/gateway_ts.jks"));
    config.put(TLSOptions.TLS_TRUSTSTOREPASSWORD, "changeme");
    config.put(TLSOptions.TLS_KEYSTORE, getResourcePath("2waytest/basic_mutual_auth_2/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)

Example 7 with IAsyncResult

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

the class BasicMutualAuthTest method shouldFailWithDevModeAndNoClientKeys.

/**
 * Scenario:
 *   - Development mode TLS pass-through. Gateway accepts anything.
 *   - Server should still refuse on basis of requiring client auth.
 */
@Test
public void shouldFailWithDevModeAndNoClientKeys() {
    config.put(TLSOptions.TLS_DEVMODE, "true");
    HttpConnectorFactory factory = new HttpConnectorFactory(config);
    IApiConnector connector = factory.createConnector(request, api, RequiredAuthType.DEFAULT, false, new ConnectorConfigImpl());
    IApiConnection connection = connector.connect(request, (IAsyncResult<IApiConnectionResponse> result) -> {
        Assert.assertTrue(result.isError());
        System.out.println(result.getError());
    });
    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 8 with IAsyncResult

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

the class AliasedCertTest method shouldFallbackWhenMultipleAliasesAvailable.

/**
 * Scenario:
 *   - First alias invalid, second valid.
 *   - Mutual trust exists between gateway and API.
 *   - We must fall back to the valid alias.
 * @throws CertificateException the certificate exception
 * @throws IOException the IO exception
 */
@Test
public void shouldFallbackWhenMultipleAliasesAvailable() throws CertificateException, IOException {
    config.put(TLSOptions.TLS_TRUSTSTORE, getResourcePath("2waytest/aliased_keys/gateway_ts.jks"));
    config.put(TLSOptions.TLS_TRUSTSTOREPASSWORD, "changeme");
    config.put(TLSOptions.TLS_KEYSTORE, getResourcePath("2waytest/aliased_keys/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");
    // Only gateway2 is valid. `unrelated` is real but not trusted by API. others don't exist.
    config.put(TLSOptions.TLS_KEYALIASES, "unrelated, owt, or, nowt, gateway, sonorous, unrelated");
    X509Certificate expectedCert;
    try (InputStream inStream = new FileInputStream(getResourcePath("2waytest/aliased_keys/gateway.cer"))) {
        expectedCert = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(inStream);
    }
    HttpConnectorFactory factory = new HttpConnectorFactory(config);
    IApiConnector connector = factory.createConnector(request, api, RequiredAuthType.MTLS, false, new ConnectorConfigImpl());
    IApiConnection connection = connector.connect(request, (IAsyncResult<IApiConnectionResponse> result) -> {
        if (result.isError())
            throw new RuntimeException(result.getError());
        Assert.assertTrue(result.isSuccess());
        // Assert that the expected certificate (associated with the private key by virtue)
        // was the one used.
        Assert.assertEquals(expectedCert.getSerialNumber(), clientSerial);
    });
    connection.end();
}
Also used : IApiConnection(io.apiman.gateway.engine.IApiConnection) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) 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) X509Certificate(java.security.cert.X509Certificate) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 9 with IAsyncResult

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

the class AliasedCertTest method shouldSucceedWhenValidKeyAlias.

/**
 * Scenario:
 *   - Select client key alias `gateway2`.
 *   - Mutual trust exists between gateway and API
 *   - We must use the `gateway2` cert NOT `gateway`.
 * @throws CertificateException the certificate exception
 * @throws IOException the IO exception
 */
@Test
public void shouldSucceedWhenValidKeyAlias() throws CertificateException, IOException {
    config.put(TLSOptions.TLS_TRUSTSTORE, getResourcePath("2waytest/aliased_keys/gateway_ts.jks"));
    config.put(TLSOptions.TLS_TRUSTSTOREPASSWORD, "changeme");
    config.put(TLSOptions.TLS_KEYSTORE, getResourcePath("2waytest/aliased_keys/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");
    config.put(TLSOptions.TLS_KEYALIASES, "gatewayalias");
    X509Certificate expectedCert;
    try (InputStream inStream = new FileInputStream(getResourcePath("2waytest/aliased_keys/gatewayalias.cer"))) {
        expectedCert = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(inStream);
    }
    HttpConnectorFactory factory = new HttpConnectorFactory(config);
    IApiConnector connector = factory.createConnector(request, api, RequiredAuthType.MTLS, false, new ConnectorConfigImpl());
    IApiConnection connection = connector.connect(request, (IAsyncResult<IApiConnectionResponse> result) -> {
        if (result.isError())
            throw new RuntimeException(result.getError());
        Assert.assertTrue(result.isSuccess());
        // Assert that the expected certificate (associated with the private key by virtue)
        // was the one used.
        Assert.assertEquals(expectedCert.getSerialNumber(), clientSerial);
    });
    connection.end();
}
Also used : IApiConnection(io.apiman.gateway.engine.IApiConnection) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) 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) X509Certificate(java.security.cert.X509Certificate) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 10 with IAsyncResult

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

the class JDBCIdentityValidator method validate.

/**
 * @param connection
 * @param query
 * @param username
 * @param context
 * @param password
 * @param config
 * @param handler
 */
protected void validate(final IJdbcConnection connection, final String query, final String username, final String password, final IPolicyContext context, final JDBCIdentitySource config, final IAsyncResultHandler<Boolean> handler) {
    IAsyncResultHandler<IJdbcResultSet> queryHandler = new IAsyncResultHandler<IJdbcResultSet>() {

        @Override
        public void handle(IAsyncResult<IJdbcResultSet> result) {
            if (result.isError()) {
                closeQuietly(connection);
                handler.handle(AsyncResultImpl.create(result.getError(), Boolean.class));
            } else {
                boolean validated = false;
                IJdbcResultSet resultSet = result.getResult();
                if (resultSet.next()) {
                    validated = true;
                }
                resultSet.close();
                if (validated && config.isExtractRoles()) {
                    extractRoles(connection, username, context, config, handler);
                } else {
                    closeQuietly(connection);
                    handler.handle(AsyncResultImpl.create(validated));
                }
            }
        }
    };
    connection.query(queryHandler, query, username, password);
}
Also used : IJdbcResultSet(io.apiman.gateway.engine.components.jdbc.IJdbcResultSet) IAsyncResultHandler(io.apiman.gateway.engine.async.IAsyncResultHandler) IAsyncResult(io.apiman.gateway.engine.async.IAsyncResult)

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