Search in sources :

Example 1 with SaslExtensionsCallback

use of org.apache.kafka.common.security.auth.SaslExtensionsCallback in project kafka by apache.

the class OAuthBearerLoginCallbackHandlerTest method testHandleSaslExtensionsCallback.

@Test
public void testHandleSaslExtensionsCallback() throws Exception {
    OAuthBearerLoginCallbackHandler handler = new OAuthBearerLoginCallbackHandler();
    Map<String, ?> configs = getSaslConfigs(SASL_OAUTHBEARER_TOKEN_ENDPOINT_URL, "http://www.example.com");
    Map<String, Object> jaasConfig = new HashMap<>();
    jaasConfig.put(CLIENT_ID_CONFIG, "an ID");
    jaasConfig.put(CLIENT_SECRET_CONFIG, "a secret");
    jaasConfig.put("extension_foo", "1");
    jaasConfig.put("extension_bar", 2);
    jaasConfig.put("EXTENSION_baz", "3");
    configureHandler(handler, configs, jaasConfig);
    try {
        SaslExtensionsCallback callback = new SaslExtensionsCallback();
        handler.handle(new Callback[] { callback });
        assertNotNull(callback.extensions());
        Map<String, String> extensions = callback.extensions().map();
        assertEquals("1", extensions.get("foo"));
        assertEquals("2", extensions.get("bar"));
        assertNull(extensions.get("baz"));
        assertEquals(2, extensions.size());
    } finally {
        handler.close();
    }
}
Also used : SaslExtensionsCallback(org.apache.kafka.common.security.auth.SaslExtensionsCallback) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 2 with SaslExtensionsCallback

use of org.apache.kafka.common.security.auth.SaslExtensionsCallback in project kafka by apache.

the class OAuthBearerLoginCallbackHandlerTest method testHandleSaslExtensionsCallbackWithInvalidExtension.

@Test
public void testHandleSaslExtensionsCallbackWithInvalidExtension() {
    String illegalKey = "extension_" + OAuthBearerClientInitialResponse.AUTH_KEY;
    OAuthBearerLoginCallbackHandler handler = new OAuthBearerLoginCallbackHandler();
    Map<String, ?> configs = getSaslConfigs(SASL_OAUTHBEARER_TOKEN_ENDPOINT_URL, "http://www.example.com");
    Map<String, Object> jaasConfig = new HashMap<>();
    jaasConfig.put(CLIENT_ID_CONFIG, "an ID");
    jaasConfig.put(CLIENT_SECRET_CONFIG, "a secret");
    jaasConfig.put(illegalKey, "this key isn't allowed per OAuthBearerClientInitialResponse.validateExtensions");
    configureHandler(handler, configs, jaasConfig);
    try {
        SaslExtensionsCallback callback = new SaslExtensionsCallback();
        assertThrowsWithMessage(ConfigException.class, () -> handler.handle(new Callback[] { callback }), "Extension name " + OAuthBearerClientInitialResponse.AUTH_KEY + " is invalid");
    } finally {
        handler.close();
    }
}
Also used : SaslExtensionsCallback(org.apache.kafka.common.security.auth.SaslExtensionsCallback) OAuthBearerTokenCallback(org.apache.kafka.common.security.oauthbearer.OAuthBearerTokenCallback) SaslExtensionsCallback(org.apache.kafka.common.security.auth.SaslExtensionsCallback) Callback(javax.security.auth.callback.Callback) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 3 with SaslExtensionsCallback

use of org.apache.kafka.common.security.auth.SaslExtensionsCallback in project kafka by apache.

the class OAuthBearerUnsecuredLoginCallbackHandlerTest method addsExtensions.

@Test
public void addsExtensions() throws IOException, UnsupportedCallbackException {
    Map<String, String> options = new HashMap<>();
    options.put("unsecuredLoginExtension_testId", "1");
    OAuthBearerUnsecuredLoginCallbackHandler callbackHandler = createCallbackHandler(options, new MockTime());
    SaslExtensionsCallback callback = new SaslExtensionsCallback();
    callbackHandler.handle(new Callback[] { callback });
    assertEquals("1", callback.extensions().map().get("testId"));
}
Also used : SaslExtensionsCallback(org.apache.kafka.common.security.auth.SaslExtensionsCallback) HashMap(java.util.HashMap) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 4 with SaslExtensionsCallback

use of org.apache.kafka.common.security.auth.SaslExtensionsCallback in project kafka by apache.

the class OAuthBearerUnsecuredLoginCallbackHandlerTest method throwsErrorOnInvalidExtensionValue.

@Test
public void throwsErrorOnInvalidExtensionValue() {
    Map<String, String> options = new HashMap<>();
    options.put("unsecuredLoginExtension_testId", "Çalifornia");
    OAuthBearerUnsecuredLoginCallbackHandler callbackHandler = createCallbackHandler(options, new MockTime());
    SaslExtensionsCallback callback = new SaslExtensionsCallback();
    assertThrows(IOException.class, () -> callbackHandler.handle(new Callback[] { callback }));
}
Also used : SaslExtensionsCallback(org.apache.kafka.common.security.auth.SaslExtensionsCallback) OAuthBearerTokenCallback(org.apache.kafka.common.security.oauthbearer.OAuthBearerTokenCallback) SaslExtensionsCallback(org.apache.kafka.common.security.auth.SaslExtensionsCallback) Callback(javax.security.auth.callback.Callback) HashMap(java.util.HashMap) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 5 with SaslExtensionsCallback

use of org.apache.kafka.common.security.auth.SaslExtensionsCallback in project kafka by apache.

the class OAuthBearerLoginModule method identifyExtensions.

/**
 * Attaches SASL extensions to the Subject
 */
private void identifyExtensions() throws LoginException {
    SaslExtensionsCallback extensionsCallback = new SaslExtensionsCallback();
    try {
        callbackHandler.handle(new Callback[] { extensionsCallback });
        extensionsRequiringCommit = extensionsCallback.extensions();
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        throw new LoginException("An internal error occurred while retrieving SASL extensions from callback handler");
    } catch (UnsupportedCallbackException e) {
        extensionsRequiringCommit = EMPTY_EXTENSIONS;
        log.debug("CallbackHandler {} does not support SASL extensions. No extensions will be added", callbackHandler.getClass().getName());
    }
    if (extensionsRequiringCommit == null) {
        log.error("SASL Extensions cannot be null. Check whether your callback handler is explicitly setting them as null.");
        throw new LoginException("Extensions cannot be null.");
    }
}
Also used : SaslExtensionsCallback(org.apache.kafka.common.security.auth.SaslExtensionsCallback) LoginException(javax.security.auth.login.LoginException) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Aggregations

SaslExtensionsCallback (org.apache.kafka.common.security.auth.SaslExtensionsCallback)7 HashMap (java.util.HashMap)5 Test (org.junit.jupiter.api.Test)5 Callback (javax.security.auth.callback.Callback)4 OAuthBearerTokenCallback (org.apache.kafka.common.security.oauthbearer.OAuthBearerTokenCallback)3 MockTime (org.apache.kafka.common.utils.MockTime)3 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)2 IOException (java.io.IOException)1 Map (java.util.Map)1 Subject (javax.security.auth.Subject)1 NameCallback (javax.security.auth.callback.NameCallback)1 PasswordCallback (javax.security.auth.callback.PasswordCallback)1 LoginException (javax.security.auth.login.LoginException)1 AuthorizeCallback (javax.security.sasl.AuthorizeCallback)1 RealmCallback (javax.security.sasl.RealmCallback)1 SaslExtensions (org.apache.kafka.common.security.auth.SaslExtensions)1 ScramExtensionsCallback (org.apache.kafka.common.security.scram.ScramExtensionsCallback)1