Search in sources :

Example 1 with RemoteClient

use of io.cdap.cdap.common.internal.remote.RemoteClient in project cdap by caskdata.

the class ArtifactCacheHttpHandlerInternal method fetchArtifact.

@GET
@Path("peers/{peer}/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}")
public void fetchArtifact(HttpRequest request, HttpResponder responder, @PathParam("peer") String peer, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion) throws Exception {
    ArtifactId artifactId = new ArtifactId(namespaceId, artifactName, artifactVersion);
    try {
        String endpoint = tetheringStore.getPeer(peer).getEndpoint();
        RemoteClientFactory factory = new RemoteClientFactory(new NoOpDiscoveryServiceClient(endpoint), new NoOpInternalAuthenticator(), remoteAuthenticator);
        HttpRequestConfig config = new DefaultHttpRequestConfig(true);
        RemoteClient remoteClient = factory.createRemoteClient("", config, Constants.Gateway.INTERNAL_API_VERSION_3);
        File artifactPath = cache.getArtifact(artifactId, peer, remoteClient);
        Location artifactLocation = Locations.toLocation(artifactPath);
        responder.sendContent(HttpResponseStatus.OK, new LocationBodyProducer(artifactLocation), new DefaultHttpHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM));
    } catch (Exception ex) {
        if (ex instanceof HttpErrorStatusProvider) {
            HttpResponseStatus status = HttpResponseStatus.valueOf(((HttpErrorStatusProvider) ex).getStatusCode());
            responder.sendString(status, exceptionToJson(ex));
        } else {
            responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, exceptionToJson(ex));
        }
    }
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) NoOpInternalAuthenticator(io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator) HttpErrorStatusProvider(io.cdap.cdap.api.common.HttpErrorStatusProvider) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) LocationBodyProducer(io.cdap.cdap.common.http.LocationBodyProducer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) File(java.io.File) Location(org.apache.twill.filesystem.Location) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with RemoteClient

use of io.cdap.cdap.common.internal.remote.RemoteClient in project cdap by caskdata.

the class RuntimeServiceRoutingTest method testGetAndDelete.

@Test
public void testGetAndDelete() throws IOException, UnauthorizedException {
    ProgramRunId programRunId = NamespaceId.DEFAULT.app("app", "1.0").workflow("workflow").run(RunIds.generate());
    mockRemoteAuthenticatorProvider.setAuthenticator(new MockRemoteAuthenticator(programRunId));
    RemoteClient remoteClient = injector.getInstance(RemoteClientFactory.class).createRemoteClient(Constants.Service.RUNTIME, DefaultHttpRequestConfig.DEFAULT, Constants.Gateway.INTERNAL_API_VERSION_3 + "/runtime/namespaces");
    for (HttpMethod method : EnumSet.of(HttpMethod.GET, HttpMethod.DELETE)) {
        for (int status : Arrays.asList(200, 400, 404, 500)) {
            io.cdap.common.http.HttpRequest request = remoteClient.requestBuilder(method, String.format("%s/apps/%s/versions/%s/%s/%s/runs/%s/services/%s/mock/%s/%d", programRunId.getNamespace(), programRunId.getApplication(), programRunId.getVersion(), programRunId.getType().getCategoryName(), programRunId.getProgram(), programRunId.getRun(), MOCK_SERVICE, method.name().toLowerCase(), status)).build();
            HttpResponse response = remoteClient.execute(request);
            Assert.assertEquals(status, response.getResponseCode());
        }
    }
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) HttpResponse(io.cdap.common.http.HttpResponse) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) HttpMethod(io.cdap.common.http.HttpMethod) Test(org.junit.Test)

Example 3 with RemoteClient

use of io.cdap.cdap.common.internal.remote.RemoteClient in project cdap by caskdata.

the class RemoteClientAuthenticatorTest method testRemoteClientWithoutInternalAuthInjectsNoAuthenticationContext.

@Test
public void testRemoteClientWithoutInternalAuthInjectsNoAuthenticationContext() throws Exception {
    CConfiguration cConf = injector.getInstance(CConfiguration.class);
    cConf.setBoolean(Constants.Security.INTERNAL_AUTH_ENABLED, false);
    RemoteClientFactory remoteClientFactory = injector.getInstance(RemoteClientFactory.class);
    RemoteClient remoteClient = remoteClientFactory.createRemoteClient(TEST_SERVICE, new HttpRequestConfig(15000, 15000, false), "/");
    HttpURLConnection conn = remoteClient.openConnection(HttpMethod.GET, "");
    int responseCode = conn.getResponseCode();
    // Verify that the request received the expected headers.
    HttpHeaders headers = testHttpHandler.getRequest().headers();
    Assert.assertEquals(HttpResponseStatus.OK.code(), responseCode);
    Assert.assertFalse(headers.contains(Constants.Security.Headers.USER_ID));
    Assert.assertFalse(headers.contains(Constants.Security.Headers.RUNTIME_TOKEN));
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) HttpURLConnection(java.net.HttpURLConnection) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) Test(org.junit.Test)

Example 4 with RemoteClient

use of io.cdap.cdap.common.internal.remote.RemoteClient in project cdap by caskdata.

the class RemoteClientAuthenticatorTest method testRemoteClientWithInternalAuthInjectsAuthenticationContext.

@Test
public void testRemoteClientWithInternalAuthInjectsAuthenticationContext() throws Exception {
    CConfiguration cConf = injector.getInstance(CConfiguration.class);
    cConf.setBoolean(Constants.Security.INTERNAL_AUTH_ENABLED, true);
    RemoteClientFactory remoteClientFactory = injector.getInstance(RemoteClientFactory.class);
    RemoteClient remoteClient = remoteClientFactory.createRemoteClient(TEST_SERVICE, new HttpRequestConfig(15000, 15000, false), "/");
    // Set authentication context principal.
    String expectedName = "somebody";
    String expectedCredValue = "credential";
    Credential.CredentialType expectedCredType = Credential.CredentialType.EXTERNAL;
    System.setProperty("user.name", expectedName);
    System.setProperty("user.credential.value", expectedCredValue);
    System.setProperty("user.credential.type", expectedCredType.name());
    HttpURLConnection conn = remoteClient.openConnection(HttpMethod.GET, "");
    int responseCode = conn.getResponseCode();
    // Verify that the request received the expected headers.
    HttpHeaders headers = testHttpHandler.getRequest().headers();
    Assert.assertEquals(HttpResponseStatus.OK.code(), responseCode);
    Assert.assertEquals(expectedName, headers.get(Constants.Security.Headers.USER_ID));
    Assert.assertEquals(String.format("%s %s", expectedCredType.getQualifiedName(), expectedCredValue), headers.get(Constants.Security.Headers.RUNTIME_TOKEN));
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) Credential(io.cdap.cdap.proto.security.Credential) HttpURLConnection(java.net.HttpURLConnection) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) Test(org.junit.Test)

Example 5 with RemoteClient

use of io.cdap.cdap.common.internal.remote.RemoteClient in project cdap by caskdata.

the class RemoteClientAuthenticatorTest method testRemoteClientWithRemoteAuthenticatorIncludesAuthorizationHeader.

@Test
public void testRemoteClientWithRemoteAuthenticatorIncludesAuthorizationHeader() throws Exception {
    String mockAuthenticatorName = "mock-remote-authenticator";
    Credential expectedCredential = new Credential("test-credential", Credential.CredentialType.EXTERNAL_BEARER);
    RemoteAuthenticator mockRemoteAuthenticator = mock(RemoteAuthenticator.class);
    when(mockRemoteAuthenticator.getName()).thenReturn(mockAuthenticatorName);
    when(mockRemoteAuthenticator.getCredentials()).thenReturn(expectedCredential);
    mockRemoteAuthenticatorProvider.setAuthenticator(mockRemoteAuthenticator);
    RemoteClientFactory remoteClientFactory = injector.getInstance(RemoteClientFactory.class);
    RemoteClient remoteClient = remoteClientFactory.createRemoteClient(TEST_SERVICE, new HttpRequestConfig(15000, 15000, false), "/");
    HttpURLConnection conn = remoteClient.openConnection(HttpMethod.GET, "");
    int responseCode = conn.getResponseCode();
    // Verify that the request received the expected headers.
    HttpHeaders headers = testHttpHandler.getRequest().headers();
    Assert.assertEquals(HttpResponseStatus.OK.code(), responseCode);
    Assert.assertEquals(String.format("%s %s", expectedCredential.getType().getQualifiedName(), expectedCredential.getValue()), headers.get(javax.ws.rs.core.HttpHeaders.AUTHORIZATION));
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) Credential(io.cdap.cdap.proto.security.Credential) NoOpRemoteAuthenticator(io.cdap.cdap.common.internal.remote.NoOpRemoteAuthenticator) RemoteAuthenticator(io.cdap.cdap.security.spi.authenticator.RemoteAuthenticator) HttpURLConnection(java.net.HttpURLConnection) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) Test(org.junit.Test)

Aggregations

RemoteClient (io.cdap.cdap.common.internal.remote.RemoteClient)9 RemoteClientFactory (io.cdap.cdap.common.internal.remote.RemoteClientFactory)9 Test (org.junit.Test)7 HttpRequestConfig (io.cdap.common.http.HttpRequestConfig)5 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)3 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)3 HttpResponse (io.cdap.common.http.HttpResponse)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 HttpURLConnection (java.net.HttpURLConnection)3 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)2 NoOpInternalAuthenticator (io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator)2 Credential (io.cdap.cdap.proto.security.Credential)2 HttpMethod (io.cdap.common.http.HttpMethod)2 File (java.io.File)2 Location (org.apache.twill.filesystem.Location)2 HttpErrorStatusProvider (io.cdap.cdap.api.common.HttpErrorStatusProvider)1 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)1 LocationBodyProducer (io.cdap.cdap.common.http.LocationBodyProducer)1 Id (io.cdap.cdap.common.id.Id)1 NoOpRemoteAuthenticator (io.cdap.cdap.common.internal.remote.NoOpRemoteAuthenticator)1