Search in sources :

Example 1 with InternalRequestSignature

use of org.apache.kafka.connect.runtime.rest.InternalRequestSignature in project kafka by apache.

the class DistributedHerderTest method testPutTaskConfigsInvalidSignature.

@Test
public void testPutTaskConfigsInvalidSignature() {
    Callback<Void> taskConfigCb = EasyMock.mock(Callback.class);
    Capture<Throwable> errorCapture = Capture.newInstance();
    taskConfigCb.onCompletion(capture(errorCapture), EasyMock.eq(null));
    EasyMock.expectLastCall().once();
    EasyMock.expect(member.currentProtocolVersion()).andReturn(CONNECT_PROTOCOL_V2).anyTimes();
    InternalRequestSignature signature = EasyMock.mock(InternalRequestSignature.class);
    EasyMock.expect(signature.keyAlgorithm()).andReturn("HmacSHA256").anyTimes();
    EasyMock.expect(signature.isValid(EasyMock.anyObject())).andReturn(false).anyTimes();
    PowerMock.replayAll(taskConfigCb, signature);
    herder.putTaskConfigs(CONN1, TASK_CONFIGS, taskConfigCb, signature);
    PowerMock.verifyAll();
    assertTrue(errorCapture.getValue() instanceof ConnectRestException);
    assertEquals(FORBIDDEN.getStatusCode(), ((ConnectRestException) errorCapture.getValue()).statusCode());
}
Also used : InternalRequestSignature(org.apache.kafka.connect.runtime.rest.InternalRequestSignature) ConnectRestException(org.apache.kafka.connect.runtime.rest.errors.ConnectRestException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with InternalRequestSignature

use of org.apache.kafka.connect.runtime.rest.InternalRequestSignature in project kafka by apache.

the class DistributedHerderTest method testPutTaskConfigsValidRequiredSignature.

@Test
public void testPutTaskConfigsValidRequiredSignature() {
    Callback<Void> taskConfigCb = EasyMock.mock(Callback.class);
    member.wakeup();
    EasyMock.expectLastCall().once();
    EasyMock.expect(member.currentProtocolVersion()).andReturn(CONNECT_PROTOCOL_V2).anyTimes();
    InternalRequestSignature signature = EasyMock.mock(InternalRequestSignature.class);
    EasyMock.expect(signature.keyAlgorithm()).andReturn("HmacSHA256").anyTimes();
    EasyMock.expect(signature.isValid(EasyMock.anyObject())).andReturn(true).anyTimes();
    PowerMock.replayAll(taskConfigCb, signature);
    herder.putTaskConfigs(CONN1, TASK_CONFIGS, taskConfigCb, signature);
    PowerMock.verifyAll();
}
Also used : InternalRequestSignature(org.apache.kafka.connect.runtime.rest.InternalRequestSignature) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with InternalRequestSignature

use of org.apache.kafka.connect.runtime.rest.InternalRequestSignature in project kafka by apache.

the class ConnectorsResourceTest method testPutConnectorTaskConfigsWithInternalRequestSignature.

@Test
public void testPutConnectorTaskConfigsWithInternalRequestSignature() throws Throwable {
    final String signatureAlgorithm = "HmacSHA256";
    final String encodedSignature = "Kv1/OSsxzdVIwvZ4e30avyRIVrngDfhzVUm/kAZEKc4=";
    final Capture<Callback<Void>> cb = Capture.newInstance();
    final Capture<InternalRequestSignature> signatureCapture = Capture.newInstance();
    herder.putTaskConfigs(EasyMock.eq(CONNECTOR_NAME), EasyMock.eq(TASK_CONFIGS), EasyMock.capture(cb), EasyMock.capture(signatureCapture));
    expectAndCallbackResult(cb, null);
    HttpHeaders headers = EasyMock.mock(HttpHeaders.class);
    EasyMock.expect(headers.getHeaderString(InternalRequestSignature.SIGNATURE_ALGORITHM_HEADER)).andReturn(signatureAlgorithm).once();
    EasyMock.expect(headers.getHeaderString(InternalRequestSignature.SIGNATURE_HEADER)).andReturn(encodedSignature).once();
    PowerMock.replayAll(headers);
    connectorsResource.putTaskConfigs(CONNECTOR_NAME, headers, FORWARD, serializeAsBytes(TASK_CONFIGS));
    PowerMock.verifyAll();
    InternalRequestSignature expectedSignature = new InternalRequestSignature(serializeAsBytes(TASK_CONFIGS), Mac.getInstance(signatureAlgorithm), Base64.getDecoder().decode(encodedSignature));
    assertEquals(expectedSignature, signatureCapture.getValue());
}
Also used : InternalRequestSignature(org.apache.kafka.connect.runtime.rest.InternalRequestSignature) HttpHeaders(javax.ws.rs.core.HttpHeaders) Callback(org.apache.kafka.connect.util.Callback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with InternalRequestSignature

use of org.apache.kafka.connect.runtime.rest.InternalRequestSignature in project kafka by apache.

the class DistributedHerderTest method testPutTaskConfigsDisallowedSignatureAlgorithm.

@Test
public void testPutTaskConfigsDisallowedSignatureAlgorithm() {
    Callback<Void> taskConfigCb = EasyMock.mock(Callback.class);
    Capture<Throwable> errorCapture = Capture.newInstance();
    taskConfigCb.onCompletion(capture(errorCapture), EasyMock.eq(null));
    EasyMock.expectLastCall().once();
    EasyMock.expect(member.currentProtocolVersion()).andReturn(CONNECT_PROTOCOL_V2).anyTimes();
    InternalRequestSignature signature = EasyMock.mock(InternalRequestSignature.class);
    EasyMock.expect(signature.keyAlgorithm()).andReturn("HmacSHA489").anyTimes();
    PowerMock.replayAll(taskConfigCb, signature);
    herder.putTaskConfigs(CONN1, TASK_CONFIGS, taskConfigCb, signature);
    PowerMock.verifyAll();
    assertTrue(errorCapture.getValue() instanceof BadRequestException);
}
Also used : InternalRequestSignature(org.apache.kafka.connect.runtime.rest.InternalRequestSignature) BadRequestException(org.apache.kafka.connect.runtime.rest.errors.BadRequestException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

InternalRequestSignature (org.apache.kafka.connect.runtime.rest.InternalRequestSignature)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 HttpHeaders (javax.ws.rs.core.HttpHeaders)1 BadRequestException (org.apache.kafka.connect.runtime.rest.errors.BadRequestException)1 ConnectRestException (org.apache.kafka.connect.runtime.rest.errors.ConnectRestException)1 Callback (org.apache.kafka.connect.util.Callback)1