Search in sources :

Example 1 with RemoteAuthenticator

use of io.cdap.cdap.security.spi.authenticator.RemoteAuthenticator 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)

Example 2 with RemoteAuthenticator

use of io.cdap.cdap.security.spi.authenticator.RemoteAuthenticator in project cdap by caskdata.

the class DefaultRemoteAuthenticatorProvider method get.

/**
 * Retrieves the current {@link RemoteAuthenticator} from the extension loader using the authenticator name or
 * {@code null} if there is no current authenticator available.
 */
@Override
public RemoteAuthenticator get() {
    if (defaultAuthenticatorName == null) {
        return new NoOpRemoteAuthenticator();
    }
    // Try to get the current authenticator from the extension loader.
    RemoteAuthenticator extensionRemoteAuthenticator = remoteAuthenticatorExtensionLoader.get(defaultAuthenticatorName);
    if (extensionRemoteAuthenticator != null) {
        return extensionRemoteAuthenticator;
    }
    // Once a local remote authenticator has been instantiated, return that instead.
    if (localRemoteAuthenticator != null) {
        return localRemoteAuthenticator;
    }
    // Check if remote authenticator binding is a local authenticator.
    RemoteAuthenticator localAuthenticator = getLocalAuthenticator(defaultAuthenticatorName);
    if (localAuthenticator != null) {
        localRemoteAuthenticator = localAuthenticator;
        return localRemoteAuthenticator;
    }
    // If no remote authenticator was found, return a NoOpRemoteAuthenticator.
    localRemoteAuthenticator = new NoOpRemoteAuthenticator();
    return localRemoteAuthenticator;
}
Also used : RemoteAuthenticator(io.cdap.cdap.security.spi.authenticator.RemoteAuthenticator)

Aggregations

RemoteAuthenticator (io.cdap.cdap.security.spi.authenticator.RemoteAuthenticator)2 NoOpRemoteAuthenticator (io.cdap.cdap.common.internal.remote.NoOpRemoteAuthenticator)1 RemoteClient (io.cdap.cdap.common.internal.remote.RemoteClient)1 RemoteClientFactory (io.cdap.cdap.common.internal.remote.RemoteClientFactory)1 Credential (io.cdap.cdap.proto.security.Credential)1 HttpRequestConfig (io.cdap.common.http.HttpRequestConfig)1 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1 HttpURLConnection (java.net.HttpURLConnection)1 Test (org.junit.Test)1