Search in sources :

Example 1 with MetricsService

use of org.apache.knox.gateway.services.metrics.MetricsService in project knox by apache.

the class GatewayServlet method createFilter.

private static GatewayFilter createFilter(InputStream stream, ServletContext servletContext) throws ServletException {
    try {
        GatewayFilter filter = null;
        if (stream != null) {
            try {
                GatewayDescriptor descriptor = GatewayDescriptorFactory.load("xml", new InputStreamReader(stream));
                filter = GatewayFactory.create(descriptor);
            } finally {
                stream.close();
            }
        }
        GatewayConfig gatewayConfig = (GatewayConfig) servletContext.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
        if (gatewayConfig.isMetricsEnabled()) {
            GatewayServices gatewayServices = (GatewayServices) servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
            MetricsService metricsService = gatewayServices.getService(GatewayServices.METRICS_SERVICE);
            if (metricsService != null) {
                GatewayFilter instrumentedFilter = metricsService.getInstrumented(filter);
                if (instrumentedFilter != null) {
                    filter = instrumentedFilter;
                }
            }
        }
        return filter;
    } catch (IOException e) {
        throw new ServletException(e);
    } catch (URISyntaxException e) {
        throw new ServletException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) GatewayServices(org.apache.knox.gateway.services.GatewayServices) InputStreamReader(java.io.InputStreamReader) MetricsService(org.apache.knox.gateway.services.metrics.MetricsService) GatewayDescriptor(org.apache.knox.gateway.descriptor.GatewayDescriptor) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) AbstractGatewayFilter(org.apache.knox.gateway.filter.AbstractGatewayFilter) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig)

Example 2 with MetricsService

use of org.apache.knox.gateway.services.metrics.MetricsService in project knox by apache.

the class DefaultHttpClientFactory method createHttpClient.

@Override
public HttpClient createHttpClient(FilterConfig filterConfig) {
    HttpClientBuilder builder = null;
    GatewayConfig gatewayConfig = (GatewayConfig) filterConfig.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
    GatewayServices services = (GatewayServices) filterConfig.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
    if (gatewayConfig != null && gatewayConfig.isMetricsEnabled()) {
        MetricsService metricsService = services.getService(GatewayServices.METRICS_SERVICE);
        builder = metricsService.getInstrumented(HttpClientBuilder.class);
    } else {
        builder = HttpClients.custom();
    }
    if (Boolean.parseBoolean(filterConfig.getInitParameter("useTwoWaySsl"))) {
        char[] keypass = null;
        MasterService ms = services.getService("MasterService");
        AliasService as = services.getService(GatewayServices.ALIAS_SERVICE);
        try {
            keypass = as.getGatewayIdentityPassphrase();
        } catch (AliasServiceException e) {
        // nop - default passphrase will be used
        }
        if (keypass == null) {
            // there has been no alias created for the key - let's assume it is the same as the keystore password
            keypass = ms.getMasterSecret();
        }
        KeystoreService ks = services.getService(GatewayServices.KEYSTORE_SERVICE);
        final SSLContext sslcontext;
        try {
            KeyStore keystoreForGateway = ks.getKeystoreForGateway();
            sslcontext = SSLContexts.custom().loadTrustMaterial(keystoreForGateway, new TrustSelfSignedStrategy()).loadKeyMaterial(keystoreForGateway, keypass).build();
        } catch (Exception e) {
            throw new IllegalArgumentException("Unable to create SSLContext", e);
        }
        builder.setSSLSocketFactory(new SSLConnectionSocketFactory(sslcontext));
    }
    if ("true".equals(System.getProperty(GatewayConfig.HADOOP_KERBEROS_SECURED))) {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UseJaasCredentials());
        Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new KnoxSpnegoAuthSchemeFactory(true)).build();
        builder = builder.setDefaultAuthSchemeRegistry(authSchemeRegistry).setDefaultCookieStore(new HadoopAuthCookieStore()).setDefaultCredentialsProvider(credentialsProvider);
    } else {
        builder = builder.setDefaultCookieStore(new NoCookieStore());
    }
    builder.setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE);
    builder.setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE);
    builder.setRedirectStrategy(new NeverRedirectStrategy());
    builder.setRetryHandler(new NeverRetryHandler());
    int maxConnections = getMaxConnections(filterConfig);
    builder.setMaxConnTotal(maxConnections);
    builder.setMaxConnPerRoute(maxConnections);
    builder.setDefaultRequestConfig(getRequestConfig(filterConfig));
    HttpClient client = builder.build();
    return client;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AliasServiceException(org.apache.knox.gateway.services.security.AliasServiceException) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) KeystoreService(org.apache.knox.gateway.services.security.KeystoreService) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) AliasService(org.apache.knox.gateway.services.security.AliasService) MetricsService(org.apache.knox.gateway.services.metrics.MetricsService) SSLContext(javax.net.ssl.SSLContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) MasterService(org.apache.knox.gateway.services.security.MasterService) KeyStore(java.security.KeyStore) ProtocolException(org.apache.http.ProtocolException) IOException(java.io.IOException) AliasServiceException(org.apache.knox.gateway.services.security.AliasServiceException) HttpClient(org.apache.http.client.HttpClient) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider)

Aggregations

IOException (java.io.IOException)2 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)2 GatewayServices (org.apache.knox.gateway.services.GatewayServices)2 MetricsService (org.apache.knox.gateway.services.metrics.MetricsService)2 InputStreamReader (java.io.InputStreamReader)1 URISyntaxException (java.net.URISyntaxException)1 KeyStore (java.security.KeyStore)1 SSLContext (javax.net.ssl.SSLContext)1 ServletException (javax.servlet.ServletException)1 ProtocolException (org.apache.http.ProtocolException)1 AuthSchemeProvider (org.apache.http.auth.AuthSchemeProvider)1 CredentialsProvider (org.apache.http.client.CredentialsProvider)1 HttpClient (org.apache.http.client.HttpClient)1 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)1 TrustSelfSignedStrategy (org.apache.http.conn.ssl.TrustSelfSignedStrategy)1 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)1 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)1 GatewayDescriptor (org.apache.knox.gateway.descriptor.GatewayDescriptor)1 AbstractGatewayFilter (org.apache.knox.gateway.filter.AbstractGatewayFilter)1 AliasService (org.apache.knox.gateway.services.security.AliasService)1