Search in sources :

Example 11 with SPNegoSchemeFactory

use of org.apache.http.impl.auth.SPNegoSchemeFactory in project lucene-solr by apache.

the class Krb5HttpClientBuilder method getBuilder.

public SolrHttpClientBuilder getBuilder(SolrHttpClientBuilder builder) {
    if (System.getProperty(LOGIN_CONFIG_PROP) != null) {
        String configValue = System.getProperty(LOGIN_CONFIG_PROP);
        if (configValue != null) {
            logger.info("Setting up SPNego auth with config: " + configValue);
            final String useSubjectCredsProp = "javax.security.auth.useSubjectCredsOnly";
            String useSubjectCredsVal = System.getProperty(useSubjectCredsProp);
            // authentication mechanism can load the credentials from the JAAS configuration.
            if (useSubjectCredsVal == null) {
                System.setProperty(useSubjectCredsProp, "false");
            } else if (!useSubjectCredsVal.toLowerCase(Locale.ROOT).equals("false")) {
                // Don't overwrite the prop value if it's already been written to something else,
                // but log because it is likely the Credentials won't be loaded correctly.
                logger.warn("System Property: " + useSubjectCredsProp + " set to: " + useSubjectCredsVal + " not false.  SPNego authentication may not be successful.");
            }
            javax.security.auth.login.Configuration.setConfiguration(jaasConfig);
            //Enable only SPNEGO authentication scheme.
            builder.setAuthSchemeRegistryProvider(() -> {
                Lookup<AuthSchemeProvider> authProviders = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, false)).build();
                return authProviders;
            });
            // Get the credentials from the JAAS configuration rather than here
            Credentials useJaasCreds = new Credentials() {

                public String getPassword() {
                    return null;
                }

                public Principal getUserPrincipal() {
                    return null;
                }
            };
            HttpClientUtil.setCookiePolicy(SolrPortAwareCookieSpecFactory.POLICY_NAME);
            builder.setCookieSpecRegistryProvider(() -> {
                SolrPortAwareCookieSpecFactory cookieFactory = new SolrPortAwareCookieSpecFactory();
                Lookup<CookieSpecProvider> cookieRegistry = RegistryBuilder.<CookieSpecProvider>create().register(SolrPortAwareCookieSpecFactory.POLICY_NAME, cookieFactory).build();
                return cookieRegistry;
            });
            builder.setDefaultCredentialsProvider(() -> {
                CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                credentialsProvider.setCredentials(AuthScope.ANY, useJaasCreds);
                return credentialsProvider;
            });
            HttpClientUtil.addRequestInterceptor(bufferedEntityInterceptor);
        }
    } else {
        logger.warn("{} is configured without specifying system property '{}'", getClass().getName(), LOGIN_CONFIG_PROP);
    }
    return builder;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CookieSpecProvider(org.apache.http.cookie.CookieSpecProvider) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials)

Example 12 with SPNegoSchemeFactory

use of org.apache.http.impl.auth.SPNegoSchemeFactory in project ovirt-engine-sdk-java by oVirt.

the class ConnectionBuilder42 method createHttpClient.

/**
 * Creates HttpClient
 */
@Override
protected HttpClient createHttpClient() {
    int port = getPort();
    Credentials credentials = null;
    AuthSchemeRegistry schemeRegistry = new AuthSchemeRegistry();
    AuthScope authScope = new AuthScope(getHost(), port, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME);
    // Create credentials:
    if (user != null && user.length() > 0) {
        schemeRegistry.register(AuthPolicy.BASIC, new BasicSchemeFactory());
        credentials = new UsernamePasswordCredentials(user, password);
    } else if (kerberos) {
        schemeRegistry.register(AuthPolicy.SPNEGO, new SPNegoSchemeFactory(true));
        credentials = new Credentials() {

            @Override
            public Principal getUserPrincipal() {
                return null;
            }

            @Override
            public String getPassword() {
                return null;
            }
        };
    }
    // Create http client:
    DefaultHttpClient client = new DefaultHttpClient(new PoolingClientConnectionManager(createConnectionSocketFactoryRegistry()));
    client.setAuthSchemes(schemeRegistry);
    client.getCredentialsProvider().setCredentials(authScope, credentials);
    client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);
    // Set request timeout:
    if (timeout != -1) {
        HttpConnectionParams.setSoTimeout(client.getParams(), timeout);
    }
    // Compress/decompress entities if compressing enabled:
    if (compress) {
        return new HttpClient42(new DecompressingHttpClient(client));
    }
    return new HttpClient42(client);
}
Also used : PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) BasicSchemeFactory(org.apache.http.impl.auth.BasicSchemeFactory) AuthSchemeRegistry(org.apache.http.auth.AuthSchemeRegistry) AuthScope(org.apache.http.auth.AuthScope) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) DecompressingHttpClient(org.apache.http.impl.client.DecompressingHttpClient) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 13 with SPNegoSchemeFactory

use of org.apache.http.impl.auth.SPNegoSchemeFactory in project ovirt-engine-sdk-java by oVirt.

the class ConnectionBuilder45 method createHttpClient.

/**
 * Creates HttpClient
 */
@Override
protected HttpClient createHttpClient() {
    int port = getPort();
    Lookup<AuthSchemeProvider> authSchemeProvider = null;
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    AuthScope authScope = new AuthScope(getHost(), port, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME);
    if (user != null && user.length() > 0) {
        credsProvider.setCredentials(authScope, new UsernamePasswordCredentials(user, password));
    } else if (kerberos) {
        authSchemeProvider = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true)).build();
        credsProvider.setCredentials(authScope, new Credentials() {

            @Override
            public Principal getUserPrincipal() {
                return null;
            }

            @Override
            public String getPassword() {
                return null;
            }
        });
    }
    RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).setConnectTimeout(timeout).build();
    HttpClientBuilder clientBuilder = HttpClientBuilder.create().setConnectionManager(new PoolingHttpClientConnectionManager(createConnectionSocketFactoryRegistry())).setDefaultRequestConfig(globalConfig).setDefaultCredentialsProvider(credsProvider).setDefaultAuthSchemeRegistry(authSchemeProvider);
    if (!compress) {
        clientBuilder.disableContentCompression();
    }
    return new HttpClient45(clientBuilder.build());
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) AuthScope(org.apache.http.auth.AuthScope) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 14 with SPNegoSchemeFactory

use of org.apache.http.impl.auth.SPNegoSchemeFactory in project ranger by apache.

the class ElasticSearchMgr method getRestClientBuilder.

public static RestClientBuilder getRestClientBuilder(String urls, String protocol, String user, String password, int port) {
    RestClientBuilder restClientBuilder = RestClient.builder(MiscUtil.toArray(urls, ",").stream().map(x -> new HttpHost(x, port, protocol)).<HttpHost>toArray(i -> new HttpHost[i]));
    if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password) && !user.equalsIgnoreCase("NONE") && !password.equalsIgnoreCase("NONE")) {
        if (password.contains("keytab") && new File(password).exists()) {
            final KerberosCredentialsProvider credentialsProvider = CredentialsProviderUtil.getKerberosCredentials(user, password);
            Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()).build();
            restClientBuilder.setHttpClientConfigCallback(clientBuilder -> {
                clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                clientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
                return clientBuilder;
            });
        } else {
            final CredentialsProvider credentialsProvider = CredentialsProviderUtil.getBasicCredentials(user, password);
            restClientBuilder.setHttpClientConfigCallback(clientBuilder -> clientBuilder.setDefaultCredentialsProvider(credentialsProvider));
        }
    } else {
        logger.error("ElasticSearch Credentials not provided!!");
        final CredentialsProvider credentialsProvider = null;
        restClientBuilder.setHttpClientConfigCallback(clientBuilder -> clientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    }
    return restClientBuilder;
}
Also used : RestClient(org.elasticsearch.client.RestClient) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) StringUtils(org.apache.commons.lang.StringUtils) RegistryBuilder(org.apache.http.config.RegistryBuilder) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) ElasticSearchAuditDestination(org.apache.ranger.audit.destination.ElasticSearchAuditDestination) AuthSchemes(org.apache.http.client.config.AuthSchemes) KerberosCredentialsProvider(org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider) Locale(java.util.Locale) Lookup(org.apache.http.config.Lookup) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) PrivilegedActionException(java.security.PrivilegedActionException) Logger(org.slf4j.Logger) KerberosTicket(javax.security.auth.kerberos.KerberosTicket) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) Subject(javax.security.auth.Subject) File(java.io.File) Component(org.springframework.stereotype.Component) MiscUtil(org.apache.ranger.audit.provider.MiscUtil) PropertiesUtil(org.apache.ranger.common.PropertiesUtil) CredentialsProviderUtil(org.apache.ranger.authorization.credutils.CredentialsProviderUtil) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpHost(org.apache.http.HttpHost) KerberosCredentialsProvider(org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider) HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) KerberosCredentialsProvider(org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) File(java.io.File)

Example 15 with SPNegoSchemeFactory

use of org.apache.http.impl.auth.SPNegoSchemeFactory in project calcite-avatica by apache.

the class AvaticaCommonsHttpClientImpl method setGSSCredential.

public void setGSSCredential(GSSCredential credential) {
    this.authRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(STRIP_PORT_ON_SERVER_LOOKUP, USE_CANONICAL_HOSTNAME)).build();
    this.credentialsProvider = new BasicCredentialsProvider();
    if (null != credential) {
        // Non-null credential should be used directly with KerberosCredentials.
        // This is never set by the JDBC driver, nor the tests
        this.credentialsProvider.setCredentials(AuthScope.ANY, new KerberosCredentials(credential));
    } else {
        // A null credential implies that the user is logged in via JAAS using the
        // java.security.auth.login.config system property
        this.credentialsProvider.setCredentials(AuthScope.ANY, EmptyCredentials.INSTANCE);
    }
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) KerberosCredentials(org.apache.http.auth.KerberosCredentials) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory)

Aggregations

SPNegoSchemeFactory (org.apache.http.impl.auth.SPNegoSchemeFactory)19 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)12 AuthSchemeProvider (org.apache.http.auth.AuthSchemeProvider)10 CredentialsProvider (org.apache.http.client.CredentialsProvider)8 HttpHost (org.apache.http.HttpHost)7 Lookup (org.apache.http.config.Lookup)7 Subject (javax.security.auth.Subject)6 KerberosTicket (javax.security.auth.kerberos.KerberosTicket)6 KerberosCredentials (org.apache.http.auth.KerberosCredentials)6 Principal (java.security.Principal)5 AuthScope (org.apache.http.auth.AuthScope)5 Credentials (org.apache.http.auth.Credentials)5 File (java.io.File)4 GSSCredential (org.ietf.jgss.GSSCredential)4 GSSManager (org.ietf.jgss.GSSManager)4 GSSName (org.ietf.jgss.GSSName)4 Oid (org.ietf.jgss.Oid)4 IOException (java.io.IOException)3 Locale (java.util.Locale)3 StringUtils (org.apache.commons.lang.StringUtils)3