Search in sources :

Example 56 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project knox by apache.

the class GatewayBasicFuncTest method oozieQueryJobStatus.

/* GET /oozie/v1/jobs?filter=user%3Dbansalm&offset=1&len=50 (body JSON; contains URL)
    HTTP/1.1 200 OK
    Content-Type: application/json;charset=UTF-8
    Content-Length: 46
    Server: Apache-Coyote/1.1
    Date: Thu, 14 Feb 2013 16:10:25 GMT
  */
/* GET /oozie/v1/job/0000000-130214094519989-oozie-oozi-W
    HTTP/1.1 200 OK
    Content-Type: application/json;charset=UTF-8
    Content-Length: 2611
    Server: Apache-Coyote/1.1
    Date: Thu, 14 Feb 2013 17:39:36 GMT
  */
/* http://192.168.56.101:11000/oozie/v1/job/0000000-130214094519989-oozie-oozi-W?action=start&user.name=sandbox
    HTTP/1.1 200 OK
    Date: Thu, 14 Feb 2013 17:52:13 GMT
    Content-Length: 0
    Server: Apache-Coyote/1.1
    Set-Cookie: hadoop.auth="u=sandbox&p=sandbox&t=simple&e=1360900333149&s=AU/GeHDNBuK9RBRaBJfrqatjfz8="; Version=1; Path=/
  */
/* PUT /oozie/v1/job/job-3?action=rerun (request body XML, contains URL)
    HTTP/1.1 200 OK
    Date: Thu, 14 Feb 2013 18:07:45 GMT
    Content-Length: 0
    Server: Apache-Coyote/1.1
    Set-Cookie: hadoop.auth="u=sandbox&p=sandbox&t=simple&e=1360901264892&s=DCOczPqn9mcisCeOb5x2C7LIRc8="; Version=1; Path=/
  */
/* GET /oozie/v1/job/0000000-130214094519989-oozie-oozi-W?show=info (body JSON, contains URL)
    HTTP/1.1 200 OK
    Content-Type: application/json;charset=UTF-8
    Content-Length: 2611
    Server: Apache-Coyote/1.1
    Date: Thu, 14 Feb 2013 17:45:23 GMT
  */
private String oozieQueryJobStatus(String user, String password, String id, int status) throws Exception {
    driver.getMock("OOZIE").expect().method("GET").pathInfo("/v1/job/" + id).respond().status(HttpStatus.SC_OK).content(driver.getResourceBytes("oozie-job-show-info.json")).contentType("application/json");
    // NOTE:  For some reason REST-assured doesn't like this and ends up failing with Content-Length issues.
    URL url = new URL(driver.getUrl("OOZIE") + "/v1/job/" + id + (driver.isUseGateway() ? "" : "?user.name=" + user));
    HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
    HttpClientBuilder builder = HttpClientBuilder.create();
    CloseableHttpClient client = builder.build();
    HttpClientContext context = HttpClientContext.create();
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost), new UsernamePasswordCredentials(user, password));
    context.setCredentialsProvider(credsProvider);
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    // Add AuthCache to the execution context
    context.setAuthCache(authCache);
    HttpGet request = new HttpGet(url.toURI());
    request.setHeader("X-XSRF-Header", "ksdhfjkhdsjkf");
    HttpResponse response = client.execute(targetHost, request, context);
    assertThat(response.getStatusLine().getStatusCode(), Matchers.is(status));
    String json = EntityUtils.toString(response.getEntity());
    String jobStatus = JsonPath.from(json).getString("status");
    return jobStatus;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpGet(org.apache.http.client.methods.HttpGet) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpResponse(org.apache.http.HttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) IsEmptyString.isEmptyString(org.hamcrest.text.IsEmptyString.isEmptyString) Matchers.containsString(org.hamcrest.Matchers.containsString) URL(java.net.URL) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope)

Example 57 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project knox by apache.

the class GatewayMultiFuncTest method testPostWithContentTypeKnox681.

@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testPostWithContentTypeKnox681() throws Exception {
    LOG_ENTER();
    MockServer mock = new MockServer("REPEAT", true);
    params = new Properties();
    params.put("MOCK_SERVER_PORT", mock.getPort());
    params.put("LDAP_URL", driver.getLdapUrl());
    String topoStr = TestUtils.merge(DAT, "topologies/test-knox678-utf8-chars-topology.xml", params);
    File topoFile = new File(config.getGatewayTopologyDir(), "knox681.xml");
    FileUtils.writeStringToFile(topoFile, topoStr);
    topos.reloadTopologies();
    mock.expect().method("PUT").pathInfo("/repeat-context/").respond().status(HttpStatus.SC_CREATED).content("{\"name\":\"value\"}".getBytes()).contentLength(-1).contentType("application/json; charset=UTF-8").header("Location", gatewayUrl + "/knox681/repeat");
    String uname = "guest";
    String pword = uname + "-password";
    HttpHost targetHost = new HttpHost("localhost", gatewayPort, "http");
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(uname, pword));
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPut request = new HttpPut(gatewayUrl + "/knox681/repeat");
    request.addHeader("X-XSRF-Header", "jksdhfkhdsf");
    request.addHeader("Content-Type", "application/json");
    CloseableHttpResponse response = client.execute(request, context);
    assertThat(response.getStatusLine().getStatusCode(), is(HttpStatus.SC_CREATED));
    assertThat(response.getFirstHeader("Location").getValue(), endsWith("/gateway/knox681/repeat"));
    assertThat(response.getFirstHeader("Content-Type").getValue(), is("application/json; charset=UTF-8"));
    String body = new String(IOUtils.toByteArray(response.getEntity().getContent()), Charset.forName("UTF-8"));
    assertThat(body, is("{\"name\":\"value\"}"));
    response.close();
    client.close();
    mock.expect().method("PUT").pathInfo("/repeat-context/").respond().status(HttpStatus.SC_CREATED).content("<test-xml/>".getBytes()).contentType("application/xml; charset=UTF-8").header("Location", gatewayUrl + "/knox681/repeat");
    client = HttpClients.createDefault();
    request = new HttpPut(gatewayUrl + "/knox681/repeat");
    request.addHeader("X-XSRF-Header", "jksdhfkhdsf");
    request.addHeader("Content-Type", "application/xml");
    response = client.execute(request, context);
    assertThat(response.getStatusLine().getStatusCode(), is(HttpStatus.SC_CREATED));
    assertThat(response.getFirstHeader("Location").getValue(), endsWith("/gateway/knox681/repeat"));
    assertThat(response.getFirstHeader("Content-Type").getValue(), is("application/xml; charset=UTF-8"));
    body = new String(IOUtils.toByteArray(response.getEntity().getContent()), Charset.forName("UTF-8"));
    assertThat(the(body), hasXPath("/test-xml"));
    response.close();
    client.close();
    mock.stop();
    LOG_EXIT();
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) Properties(java.util.Properties) HttpPut(org.apache.http.client.methods.HttpPut) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) MockServer(org.apache.knox.test.mock.MockServer) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) File(java.io.File) ReleaseTest(org.apache.knox.test.category.ReleaseTest) Test(org.junit.Test)

Example 58 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project knox by apache.

the class GatewaySslFuncTest method testKnox674SslCipherSuiteConfig.

@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testKnox674SslCipherSuiteConfig() throws Exception {
    LOG_ENTER();
    String topoStr = TestUtils.merge(DAT, "test-admin-topology.xml", params);
    File topoFile = new File(config.getGatewayTopologyDir(), "test-topology.xml");
    FileUtils.writeStringToFile(topoFile, topoStr);
    topos.reloadTopologies();
    String username = "guest";
    String password = "guest-password";
    String serviceUrl = gatewayUrl + "/test-topology/api/v1/version";
    HttpHost targetHost = new HttpHost("localhost", gatewayPort, gatewayScheme);
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(username, password));
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);
    CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(new SSLConnectionSocketFactory(createInsecureSslContext(), new String[] { "TLSv1.2" }, new String[] { "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" }, new TrustAllHosts())).build();
    HttpGet request = new HttpGet(serviceUrl);
    CloseableHttpResponse response = client.execute(request, context);
    assertThat(the(new StreamSource(response.getEntity().getContent())), hasXPath("/ServerVersion/version"));
    response.close();
    client.close();
    gateway.stop();
    config.setExcludedSSLCiphers(Arrays.asList(new String[] { "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" }));
    config.setIncludedSSLCiphers(Arrays.asList(new String[] { "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" }));
    startGatewayServer();
    serviceUrl = gatewayUrl + "/test-topology/api/v1/version";
    try {
        client = HttpClients.custom().setSSLSocketFactory(new SSLConnectionSocketFactory(createInsecureSslContext(), new String[] { "TLSv1.2" }, new String[] { "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" }, new TrustAllHosts())).build();
        request = new HttpGet(serviceUrl);
        client.execute(request, context);
        fail("Expected SSLHandshakeException");
    } catch (SSLHandshakeException e) {
        // Expected.
        client.close();
    }
    client = HttpClients.custom().setSSLSocketFactory(new SSLConnectionSocketFactory(createInsecureSslContext(), new String[] { "TLSv1.2" }, new String[] { "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" }, new TrustAllHosts())).build();
    request = new HttpGet(serviceUrl);
    response = client.execute(request, context);
    assertThat(the(new StreamSource(response.getEntity().getContent())), hasXPath("/ServerVersion/version"));
    response.close();
    client.close();
    LOG_EXIT();
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpGet(org.apache.http.client.methods.HttpGet) StreamSource(javax.xml.transform.stream.StreamSource) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) File(java.io.File) ReleaseTest(org.apache.knox.test.category.ReleaseTest) Test(org.junit.Test)

Example 59 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project jackrabbit by apache.

the class WebDAVTestBase method setUp.

protected void setUp() throws Exception {
    super.setUp();
    File home = new File("target/jackrabbit-repository");
    if (!home.exists()) {
        home.mkdirs();
    }
    File config = new File(home, "repository.xml");
    if (!config.exists()) {
        createDefaultConfiguration(config);
    }
    File keystore = new File(home, KEYSTORE);
    if (!keystore.exists()) {
        createKeystore(keystore);
    }
    if (repoContext == null) {
        repoContext = RepositoryContext.create(RepositoryConfig.create(config.toURI(), home.getPath()));
    }
    if (server == null) {
        server = new Server();
        ServletHolder simple = new ServletHolder(new SimpleWebdavServlet() {

            private static final long serialVersionUID = 8638589328461138178L;

            public Repository getRepository() {
                return repoContext.getRepository();
            }
        });
        simple.setInitParameter(SimpleWebdavServlet.INIT_PARAM_RESOURCE_CONFIG, "/config.xml");
        ServletHolder remoting = new ServletHolder(new JcrRemotingServlet() {

            private static final long serialVersionUID = -2969534124090379387L;

            public Repository getRepository() {
                return repoContext.getRepository();
            }
        });
        remoting.setInitParameter(JcrRemotingServlet.INIT_PARAM_RESOURCE_PATH_PREFIX, "/remoting");
        ServletContextHandler schandler = new ServletContextHandler(server, "/");
        schandler.addServlet(simple, SIMPLE_WEBDAV_SERVLET_PATH_MAPPING);
        schandler.addServlet(remoting, REMOTING_WEBDAV_SERVLET_PATH_MAPPING);
        schandler.setBaseResource(Resource.newClassPathResource("/"));
        server.setHandler(schandler);
    }
    if (httpConnector == null) {
        httpConnector = new ServerConnector(server);
        httpConnector.setHost("localhost");
        httpConnector.setPort(0);
        server.addConnector(httpConnector);
    }
    if (httpsConnector == null) {
        SslContextFactory sslContextFactory = new SslContextFactory();
        sslContextFactory.setKeyStorePath(keystore.getPath());
        sslContextFactory.setKeyStorePassword(KEYSTOREPW);
        sslContextFactory.setKeyManagerPassword(KEYSTOREPW);
        sslContextFactory.setTrustStorePath(keystore.getPath());
        sslContextFactory.setTrustStorePassword(KEYSTOREPW);
        SslConnectionFactory cfac = new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString());
        httpsConnector = new ServerConnector(server, cfac, new HttpConnectionFactory(new HttpConfiguration()));
        httpsConnector.setHost("localhost");
        httpsConnector.setPort(0);
        server.addConnector(httpsConnector);
    }
    if (!server.isStarted()) {
        try {
            server.start();
        } catch (Exception e) {
            throw new RepositoryStubException(e);
        }
    }
    this.uri = new URI("http", null, "localhost", httpConnector.getLocalPort(), "/default/", null, null);
    this.remotingUri = new URI("http", null, "localhost", httpConnector.getLocalPort(), REMOTING_PREFIX + "/", null, null);
    this.httpsUri = new URI("https", null, "localhost", httpsConnector.getLocalPort(), "/default/", null, null);
    this.root = this.uri.toASCIIString();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    // cm.setMaxTotal(100);
    HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("admin", "admin"));
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    // Add AuthCache to the execution context
    this.context = HttpClientContext.create();
    this.context.setCredentialsProvider(credsProvider);
    this.context.setAuthCache(authCache);
    this.client = HttpClients.custom().setConnectionManager(cm).build();
    super.setUp();
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) RepositoryStubException(org.apache.jackrabbit.test.RepositoryStubException) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) URI(java.net.URI) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SimpleWebdavServlet(org.apache.jackrabbit.webdav.simple.SimpleWebdavServlet) HttpHost(org.apache.http.HttpHost) BasicScheme(org.apache.http.impl.auth.BasicScheme) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) JcrRemotingServlet(org.apache.jackrabbit.server.remoting.davex.JcrRemotingServlet) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) RepositoryStubException(org.apache.jackrabbit.test.RepositoryStubException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Repository(javax.jcr.Repository) AuthScope(org.apache.http.auth.AuthScope) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) File(java.io.File)

Example 60 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project tutorials by eugenp.

the class HttpClientAuthLiveTest method context.

private HttpContext context() {
    final HttpHost targetHost = new HttpHost("localhost", 8080, "http");
    final CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS));
    // Create AuthCache instance
    final AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    authCache.put(targetHost, new BasicScheme());
    // Add AuthCache to the execution context
    final HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);
    return context;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

BasicScheme (org.apache.http.impl.auth.BasicScheme)82 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)57 CredentialsProvider (org.apache.http.client.CredentialsProvider)48 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)47 AuthCache (org.apache.http.client.AuthCache)46 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)46 HttpHost (org.apache.http.HttpHost)45 AuthScope (org.apache.http.auth.AuthScope)38 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)32 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)25 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)24 HttpGet (org.apache.http.client.methods.HttpGet)23 URI (java.net.URI)20 Test (org.junit.Test)18 IOException (java.io.IOException)13 Credentials (org.apache.http.auth.Credentials)13 HttpResponse (org.apache.http.HttpResponse)9 HttpPost (org.apache.http.client.methods.HttpPost)9 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)9 Header (org.apache.http.Header)8