use of org.apache.kafka.common.security.auth.SaslExtensions in project kafka by apache.
the class SaslExtensionsTest method testCannotAddValueToMapReferenceAndGetFromExtensions.
@Test
public void testCannotAddValueToMapReferenceAndGetFromExtensions() {
SaslExtensions extensions = new SaslExtensions(this.map);
assertNull(extensions.map().get("hello"));
this.map.put("hello", "42");
assertNull(extensions.map().get("hello"));
}
use of org.apache.kafka.common.security.auth.SaslExtensions in project kafka by apache.
the class OAuthBearerLoginModuleTest method login1Commit1Logout1Login2Commit2Logout2.
@Test
public void login1Commit1Logout1Login2Commit2Logout2() throws LoginException {
/*
* Invoke login()/commit() on loginModule1; invoke logout() on loginModule1;
* invoke login()/commit() on loginModule2; invoke logout() on loginModule2
*/
Subject subject = new Subject();
Set<Object> privateCredentials = subject.getPrivateCredentials();
Set<Object> publicCredentials = subject.getPublicCredentials();
// Create callback handler
OAuthBearerToken[] tokens = new OAuthBearerToken[] { mock(OAuthBearerToken.class), mock(OAuthBearerToken.class) };
SaslExtensions[] extensions = new SaslExtensions[] { mock(SaslExtensions.class), mock(SaslExtensions.class) };
TestCallbackHandler testTokenCallbackHandler = new TestCallbackHandler(tokens, extensions);
// Create login modules
OAuthBearerLoginModule loginModule1 = new OAuthBearerLoginModule();
loginModule1.initialize(subject, testTokenCallbackHandler, Collections.emptyMap(), Collections.emptyMap());
OAuthBearerLoginModule loginModule2 = new OAuthBearerLoginModule();
loginModule2.initialize(subject, testTokenCallbackHandler, Collections.emptyMap(), Collections.emptyMap());
// Should start with nothing
assertEquals(0, privateCredentials.size());
assertEquals(0, publicCredentials.size());
loginModule1.login();
// Should still have nothing until commit() is called
assertEquals(0, privateCredentials.size());
assertEquals(0, publicCredentials.size());
loginModule1.commit();
// Now we should have the first token
assertEquals(1, privateCredentials.size());
assertEquals(1, publicCredentials.size());
assertSame(tokens[0], privateCredentials.iterator().next());
assertSame(extensions[0], publicCredentials.iterator().next());
loginModule1.logout();
// Should have nothing again
assertEquals(0, privateCredentials.size());
assertEquals(0, publicCredentials.size());
loginModule2.login();
// Should still have nothing until commit() is called
assertEquals(0, privateCredentials.size());
assertEquals(0, publicCredentials.size());
loginModule2.commit();
// Now we should have the second token
assertEquals(1, privateCredentials.size());
assertEquals(1, publicCredentials.size());
assertSame(tokens[1], privateCredentials.iterator().next());
assertSame(extensions[1], publicCredentials.iterator().next());
loginModule2.logout();
// Should have nothing again
assertEquals(0, privateCredentials.size());
assertEquals(0, publicCredentials.size());
verifyNoInteractions((Object[]) tokens);
verifyNoInteractions((Object[]) extensions);
}
use of org.apache.kafka.common.security.auth.SaslExtensions in project kafka by apache.
the class OAuthBearerLoginModuleTest method login1Commit1Login2Abort2Login3Commit3Logout3.
@Test
public void login1Commit1Login2Abort2Login3Commit3Logout3() throws LoginException {
/*
* Invoke login()/commit() on loginModule1; invoke login()/abort() on
* loginModule2; invoke login()/commit()/logout() on loginModule3
*/
Subject subject = new Subject();
Set<Object> privateCredentials = subject.getPrivateCredentials();
Set<Object> publicCredentials = subject.getPublicCredentials();
// Create callback handler
OAuthBearerToken[] tokens = new OAuthBearerToken[] { mock(OAuthBearerToken.class), mock(OAuthBearerToken.class), mock(OAuthBearerToken.class) };
SaslExtensions[] extensions = new SaslExtensions[] { mock(SaslExtensions.class), mock(SaslExtensions.class), mock(SaslExtensions.class) };
TestCallbackHandler testTokenCallbackHandler = new TestCallbackHandler(tokens, extensions);
// Create login modules
OAuthBearerLoginModule loginModule1 = new OAuthBearerLoginModule();
loginModule1.initialize(subject, testTokenCallbackHandler, Collections.emptyMap(), Collections.emptyMap());
OAuthBearerLoginModule loginModule2 = new OAuthBearerLoginModule();
loginModule2.initialize(subject, testTokenCallbackHandler, Collections.emptyMap(), Collections.emptyMap());
OAuthBearerLoginModule loginModule3 = new OAuthBearerLoginModule();
loginModule3.initialize(subject, testTokenCallbackHandler, Collections.emptyMap(), Collections.emptyMap());
// Should start with nothing
assertEquals(0, privateCredentials.size());
assertEquals(0, publicCredentials.size());
loginModule1.login();
// Should still have nothing until commit() is called
assertEquals(0, privateCredentials.size());
assertEquals(0, publicCredentials.size());
loginModule1.commit();
// Now we should have the first token
assertEquals(1, privateCredentials.size());
assertEquals(1, publicCredentials.size());
assertSame(tokens[0], privateCredentials.iterator().next());
assertSame(extensions[0], publicCredentials.iterator().next());
// Now go get the second token
loginModule2.login();
// Should still have first token
assertEquals(1, privateCredentials.size());
assertEquals(1, publicCredentials.size());
assertSame(tokens[0], privateCredentials.iterator().next());
assertSame(extensions[0], publicCredentials.iterator().next());
loginModule2.abort();
// Should still have just the first token because we aborted
assertEquals(1, privateCredentials.size());
assertSame(tokens[0], privateCredentials.iterator().next());
assertEquals(1, publicCredentials.size());
assertSame(extensions[0], publicCredentials.iterator().next());
// Now go get the third token
loginModule2.login();
// Should still have first token
assertEquals(1, privateCredentials.size());
assertSame(tokens[0], privateCredentials.iterator().next());
assertEquals(1, publicCredentials.size());
assertSame(extensions[0], publicCredentials.iterator().next());
loginModule2.commit();
// Should have first and third tokens at this point
assertEquals(2, privateCredentials.size());
Iterator<Object> iterator = privateCredentials.iterator();
assertNotSame(tokens[1], iterator.next());
assertNotSame(tokens[1], iterator.next());
assertEquals(2, publicCredentials.size());
Iterator<Object> publicIterator = publicCredentials.iterator();
assertNotSame(extensions[1], publicIterator.next());
assertNotSame(extensions[1], publicIterator.next());
loginModule1.logout();
// Now we should have just the third token
assertEquals(1, privateCredentials.size());
assertSame(tokens[2], privateCredentials.iterator().next());
assertEquals(1, publicCredentials.size());
assertSame(extensions[2], publicCredentials.iterator().next());
verifyNoInteractions((Object[]) tokens);
verifyNoInteractions((Object[]) extensions);
}
use of org.apache.kafka.common.security.auth.SaslExtensions in project kafka by apache.
the class OAuthBearerClientInitialResponseTest method testBuildClientResponseToBytes.
/*
Test how a client would build a response
*/
@Test
public void testBuildClientResponseToBytes() throws Exception {
String expectedMesssage = "n,,\u0001auth=Bearer 123.345.567\u0001nineteen=42\u0001\u0001";
Map<String, String> extensions = new HashMap<>();
extensions.put("nineteen", "42");
OAuthBearerClientInitialResponse response = new OAuthBearerClientInitialResponse("123.345.567", new SaslExtensions(extensions));
String message = new String(response.toBytes(), StandardCharsets.UTF_8);
assertEquals(expectedMesssage, message);
}
use of org.apache.kafka.common.security.auth.SaslExtensions in project kafka by apache.
the class OAuthBearerExtensionsValidatorCallbackTest method testUnvalidatedExtensionsAreIgnored.
/**
* Extensions that are neither validated or invalidated must not be present in either maps
*/
@Test
public void testUnvalidatedExtensionsAreIgnored() {
Map<String, String> extensions = new HashMap<>();
extensions.put("valid", "valid");
extensions.put("error", "error");
extensions.put("nothing", "nothing");
OAuthBearerExtensionsValidatorCallback callback = new OAuthBearerExtensionsValidatorCallback(TOKEN, new SaslExtensions(extensions));
callback.error("error", "error");
callback.valid("valid");
assertFalse(callback.validatedExtensions().containsKey("nothing"));
assertFalse(callback.invalidExtensions().containsKey("nothing"));
assertEquals("nothing", callback.ignoredExtensions().get("nothing"));
}
Aggregations