Search in sources :

Example 1 with SaslExtensions

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"));
}
Also used : SaslExtensions(org.apache.kafka.common.security.auth.SaslExtensions) Test(org.junit.jupiter.api.Test)

Example 2 with SaslExtensions

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);
}
Also used : SaslExtensions(org.apache.kafka.common.security.auth.SaslExtensions) Subject(javax.security.auth.Subject) Test(org.junit.jupiter.api.Test)

Example 3 with SaslExtensions

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);
}
Also used : SaslExtensions(org.apache.kafka.common.security.auth.SaslExtensions) Subject(javax.security.auth.Subject) Test(org.junit.jupiter.api.Test)

Example 4 with SaslExtensions

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);
}
Also used : HashMap(java.util.HashMap) SaslExtensions(org.apache.kafka.common.security.auth.SaslExtensions) Test(org.junit.jupiter.api.Test)

Example 5 with SaslExtensions

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"));
}
Also used : HashMap(java.util.HashMap) SaslExtensions(org.apache.kafka.common.security.auth.SaslExtensions) Test(org.junit.jupiter.api.Test)

Aggregations

SaslExtensions (org.apache.kafka.common.security.auth.SaslExtensions)20 Test (org.junit.jupiter.api.Test)13 HashMap (java.util.HashMap)7 Subject (javax.security.auth.Subject)6 Map (java.util.Map)3 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)3 SaslException (javax.security.sasl.SaslException)3 IOException (java.io.IOException)2 ConfigException (org.apache.kafka.common.config.ConfigException)2 OAuthBearerToken (org.apache.kafka.common.security.oauthbearer.OAuthBearerToken)2 OAuthBearerTokenCallback (org.apache.kafka.common.security.oauthbearer.OAuthBearerTokenCallback)2 Callback (javax.security.auth.callback.Callback)1 NameCallback (javax.security.auth.callback.NameCallback)1 PasswordCallback (javax.security.auth.callback.PasswordCallback)1 AuthorizeCallback (javax.security.sasl.AuthorizeCallback)1 RealmCallback (javax.security.sasl.RealmCallback)1 IllegalSaslStateException (org.apache.kafka.common.errors.IllegalSaslStateException)1 SaslAuthenticationException (org.apache.kafka.common.errors.SaslAuthenticationException)1 SaslExtensionsCallback (org.apache.kafka.common.security.auth.SaslExtensionsCallback)1 OAuthBearerValidatorCallback (org.apache.kafka.common.security.oauthbearer.OAuthBearerValidatorCallback)1