Search in sources :

Example 51 with RestClientConfigurationBuilder

use of org.infinispan.client.rest.configuration.RestClientConfigurationBuilder in project infinispan by infinispan.

the class RestAuthentication method testMalformedDigestHeader.

@Test
public void testMalformedDigestHeader() throws Exception {
    assumeTrue(mechanism.startsWith("DIGEST"));
    InfinispanServerDriver serverDriver = SERVERS.getServerDriver();
    InetSocketAddress serverAddress = serverDriver.getServerSocket(0, 11222);
    RestClientConfigurationBuilder builder = new RestClientConfigurationBuilder().followRedirects(false);
    builder.addServer().host(serverAddress.getHostName()).port(serverAddress.getPort());
    RestClient restClient = RestClient.forConfiguration(builder.build());
    RestResponse response = sync(restClient.raw().get("/rest/v2/caches"));
    assertEquals(401, response.getStatus());
    String auth = response.headers().get("Www-Authenticate").stream().filter(h -> h.startsWith("Digest")).findFirst().get();
    HashMap<String, byte[]> parameters = DigestUtil.parseResponse(auth.substring(7).getBytes(UTF_8), UTF_8, false, httpDigest);
    final String realm = new String(parameters.get("realm"), UTF_8);
    final String nonce = new String(parameters.get("nonce"), UTF_8);
    final String opaque = new String(parameters.get("opaque"), UTF_8);
    final String algorithm = new String(parameters.get("algorithm"), UTF_8);
    final String charset = StandardCharsets.ISO_8859_1.name();
    final MessageDigest digester = MessageDigest.getInstance(algorithm);
    final String nc = "00000001";
    final String cnonce = "00000000";
    final String username = "h4ck0rz";
    final String password = "letmein";
    final String uri = "/backdoor";
    final String s1 = username + ':' + realm + ':' + password;
    final String s2 = "GET:" + uri;
    final String hasha1 = toHexString(digester.digest(s1.getBytes(charset)));
    final String h2 = toHexString(digester.digest(s2.getBytes(charset)));
    final String digestValue = hasha1 + ':' + nonce + ':' + nc + ':' + cnonce + ":auth:" + h2;
    final String digest = toHexString(digester.digest(digestValue.getBytes(StandardCharsets.US_ASCII.toString())));
    String authz = String.format("Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\", qop=auth, nc=%s, cnonce=%s, algorithm=%s, opaque=\"%s\"", username, realm, nonce, uri, digest, nc, cnonce, algorithm, opaque);
    response = sync(restClient.raw().get("/rest/v2/caches", Collections.singletonMap("Authorization", authz)));
    assertEquals(400, response.getStatus());
}
Also used : RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) InetSocketAddress(java.net.InetSocketAddress) RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient) InfinispanServerDriver(org.infinispan.server.test.core.InfinispanServerDriver) Util.toHexString(org.infinispan.commons.util.Util.toHexString) MessageDigest(java.security.MessageDigest) Test(org.junit.Test)

Example 52 with RestClientConfigurationBuilder

use of org.infinispan.client.rest.configuration.RestClientConfigurationBuilder in project infinispan by infinispan.

the class AbstractAuthorization method assertXSiteOps.

private void assertXSiteOps(TestUser user, int status, int noContentStatus, int notModifiedStatus) {
    RestClientConfigurationBuilder userConfig = restBuilders.get(user);
    RestClient client = getServerTest().rest().withClientConfiguration(userConfig).get();
    RestCacheClient xsiteCache = client.cache("xsite");
    assertStatus(status, xsiteCache.takeSiteOffline("NYC"));
    assertStatus(status, xsiteCache.bringSiteOnline("NYC"));
    assertStatus(status, xsiteCache.cancelPushState("NYC"));
    assertStatus(status, xsiteCache.cancelReceiveState("NYC"));
    assertStatus(status, xsiteCache.clearPushStateStatus());
    assertStatus(status, xsiteCache.pushSiteState("NYC"));
    assertStatus(status, xsiteCache.pushStateStatus());
    assertStatus(status, xsiteCache.xsiteBackups());
    assertStatus(status, xsiteCache.backupStatus("NYC"));
    assertStatus(status, xsiteCache.getXSiteTakeOfflineConfig("NYC"));
    assertStatus(noContentStatus, xsiteCache.updateXSiteTakeOfflineConfig("NYC", 10, 1000));
    assertStatus(status, xsiteCache.xSiteStateTransferMode("NYC"));
    assertStatus(notModifiedStatus, xsiteCache.xSiteStateTransferMode("NYC", XSiteStateTransferMode.MANUAL));
    RestCacheManagerClient xsiteCacheManager = client.cacheManager("default");
    assertStatus(status, xsiteCacheManager.bringBackupOnline("NYC"));
    assertStatus(status, xsiteCacheManager.takeOffline("NYC"));
    assertStatus(status, xsiteCacheManager.backupStatuses());
}
Also used : RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) RestCacheManagerClient(org.infinispan.client.rest.RestCacheManagerClient) RestClient(org.infinispan.client.rest.RestClient) RestCacheClient(org.infinispan.client.rest.RestCacheClient)

Example 53 with RestClientConfigurationBuilder

use of org.infinispan.client.rest.configuration.RestClientConfigurationBuilder in project infinispan by infinispan.

the class AuthorizationKerberosIT method addClientBuilders.

@Override
protected void addClientBuilders(TestUser user) {
    ConfigurationBuilder hotRodBuilder = new ConfigurationBuilder();
    RestClientConfigurationBuilder restBuilder = new RestClientConfigurationBuilder();
    if (user != TestUser.ANONYMOUS) {
        Subject subject = Common.createSubject(user.getUser(), "INFINISPAN.ORG", user.getPassword().toCharArray());
        hotRodBuilder.security().authentication().saslMechanism("GSSAPI").serverName("datagrid").realm("default").callbackHandler(new VoidCallbackHandler()).clientSubject(subject);
        restBuilder.security().authentication().mechanism("SPNEGO").clientSubject(subject);
    }
    hotRodBuilders.put(user, hotRodBuilder);
    restBuilders.put(user, restBuilder);
}
Also used : ConfigurationBuilder(org.infinispan.client.hotrod.configuration.ConfigurationBuilder) RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) VoidCallbackHandler(org.infinispan.client.hotrod.security.VoidCallbackHandler) Subject(javax.security.auth.Subject)

Example 54 with RestClientConfigurationBuilder

use of org.infinispan.client.rest.configuration.RestClientConfigurationBuilder in project infinispan by infinispan.

the class AbstractAuthenticationKeyCloak method testRestReadWrite.

@Test
public void testRestReadWrite() {
    String token = getToken();
    RestClientConfigurationBuilder builder = new RestClientConfigurationBuilder();
    builder.security().authentication().mechanism("BEARER_TOKEN").username(token);
    RestClient client = getServerTest().rest().withClientConfiguration(builder).create();
    RestResponse response = sync(client.cache(getServerTest().getMethodName()).post("k1", "v1"));
    assertEquals(204, response.getStatus());
    response = sync(client.cache(getServerTest().getMethodName()).get("k1"));
    assertEquals(200, response.getStatus());
    assertEquals("v1", response.getBody());
}
Also used : RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient) Test(org.junit.Test)

Example 55 with RestClientConfigurationBuilder

use of org.infinispan.client.rest.configuration.RestClientConfigurationBuilder in project infinispan by infinispan.

the class AuthenticationImplicitIT method testRest.

public void testRest(Protocol protocol) {
    RestClientConfigurationBuilder builder = new RestClientConfigurationBuilder();
    if (!mechanism.isEmpty()) {
        builder.protocol(protocol).security().authentication().mechanism(mechanism).realm("default").username("all_user").password("all");
    }
    if (mechanism.isEmpty() || "BASIC".equals(mechanism)) {
        Exceptions.expectException(SecurityException.class, () -> SERVER_TEST.rest().withClientConfiguration(builder).create());
    } else {
        RestClient client = SERVER_TEST.rest().withClientConfiguration(builder).create();
        RestResponse response = sync(client.cache(SERVER_TEST.getMethodName()).post("k1", "v1"));
        assertEquals(204, response.getStatus());
        assertEquals(protocol, response.getProtocol());
        response = sync(client.cache(SERVER_TEST.getMethodName()).get("k1"));
        assertEquals(200, response.getStatus());
        assertEquals(protocol, response.getProtocol());
        assertEquals("v1", response.getBody());
    }
}
Also used : RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient)

Aggregations

RestClientConfigurationBuilder (org.infinispan.client.rest.configuration.RestClientConfigurationBuilder)56 Test (org.junit.Test)26 RestClient (org.infinispan.client.rest.RestClient)25 RestResponse (org.infinispan.client.rest.RestResponse)22 RestClientConfiguration (org.infinispan.client.rest.configuration.RestClientConfiguration)8 RestServerHelper (org.infinispan.rest.helper.RestServerHelper)6 ConfigurationBuilder (org.infinispan.client.hotrod.configuration.ConfigurationBuilder)5 InetSocketAddress (java.net.InetSocketAddress)4 RestCacheClient (org.infinispan.client.rest.RestCacheClient)4 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)4 RestServer (org.infinispan.rest.RestServer)4 RestServerConfigurationBuilder (org.infinispan.rest.configuration.RestServerConfigurationBuilder)4 AbstractRestResourceTest (org.infinispan.rest.resources.AbstractRestResourceTest)4 DummyServerManagement (org.infinispan.server.core.DummyServerManagement)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 RestEntity (org.infinispan.client.rest.RestEntity)3 Json (org.infinispan.commons.dataconversion.internal.Json)3 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)3 Router (org.infinispan.server.router.Router)3