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();
}
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();
}
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();
}
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();
}
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);
}
Aggregations