Search in sources :

Example 11 with Password

use of org.apache.kafka.common.config.types.Password in project kafka by apache.

the class LoginManagerTest method setUp.

@BeforeEach
public void setUp() {
    dynamicPlainContext = new Password(PlainLoginModule.class.getName() + " required user=\"plainuser\" password=\"plain-secret\";");
    dynamicDigestContext = new Password(TestDigestLoginModule.class.getName() + " required user=\"digestuser\" password=\"digest-secret\";");
    TestJaasConfig.createConfiguration("SCRAM-SHA-256", Collections.singletonList("SCRAM-SHA-256"));
}
Also used : PlainLoginModule(org.apache.kafka.common.security.plain.PlainLoginModule) Password(org.apache.kafka.common.config.types.Password) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 12 with Password

use of org.apache.kafka.common.config.types.Password in project kafka by apache.

the class TestSslUtils method exportPrivateKey.

public static Password exportPrivateKey(String storePath, Password storePassword, Password keyPassword, String storeType, Password pemKeyPassword) throws Exception {
    try (FileInputStream in = new FileInputStream(storePath)) {
        KeyStore ks = KeyStore.getInstance(storeType);
        ks.load(in, storePassword.value().toCharArray());
        String alias = ks.aliases().nextElement();
        return new Password(pem((PrivateKey) ks.getKey(alias, keyPassword.value().toCharArray()), pemKeyPassword));
    }
}
Also used : PrivateKey(java.security.PrivateKey) DEROctetString(org.bouncycastle.asn1.DEROctetString) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) Password(org.apache.kafka.common.config.types.Password)

Example 13 with Password

use of org.apache.kafka.common.config.types.Password in project kafka by apache.

the class TestSslUtils method convertToPem.

public static void convertToPem(Map<String, Object> sslProps, boolean writeToFile, boolean encryptPrivateKey) throws Exception {
    String tsPath = (String) sslProps.get(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG);
    String tsType = (String) sslProps.get(SslConfigs.SSL_TRUSTSTORE_TYPE_CONFIG);
    Password tsPassword = (Password) sslProps.remove(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG);
    Password trustCerts = (Password) sslProps.remove(SslConfigs.SSL_TRUSTSTORE_CERTIFICATES_CONFIG);
    if (trustCerts == null && tsPath != null) {
        trustCerts = exportCertificates(tsPath, tsPassword, tsType);
    }
    if (trustCerts != null) {
        if (tsPath == null) {
            tsPath = File.createTempFile("truststore", ".pem").getPath();
            sslProps.put(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, tsPath);
        }
        sslProps.put(SslConfigs.SSL_TRUSTSTORE_TYPE_CONFIG, PEM_TYPE);
        if (writeToFile)
            writeToFile(tsPath, trustCerts);
        else {
            sslProps.put(SslConfigs.SSL_TRUSTSTORE_CERTIFICATES_CONFIG, trustCerts);
            sslProps.remove(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG);
        }
    }
    String ksPath = (String) sslProps.get(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG);
    Password certChain = (Password) sslProps.remove(SslConfigs.SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG);
    Password key = (Password) sslProps.remove(SslConfigs.SSL_KEYSTORE_KEY_CONFIG);
    if (certChain == null && ksPath != null) {
        String ksType = (String) sslProps.get(SslConfigs.SSL_KEYSTORE_TYPE_CONFIG);
        Password ksPassword = (Password) sslProps.remove(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG);
        Password keyPassword = (Password) sslProps.get(SslConfigs.SSL_KEY_PASSWORD_CONFIG);
        certChain = exportCertificates(ksPath, ksPassword, ksType);
        Password pemKeyPassword = encryptPrivateKey ? keyPassword : null;
        key = exportPrivateKey(ksPath, ksPassword, keyPassword, ksType, pemKeyPassword);
        if (!encryptPrivateKey)
            sslProps.remove(SslConfigs.SSL_KEY_PASSWORD_CONFIG);
    }
    if (certChain != null) {
        if (ksPath == null) {
            ksPath = File.createTempFile("keystore", ".pem").getPath();
            sslProps.put(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG, ksPath);
        }
        sslProps.put(SslConfigs.SSL_KEYSTORE_TYPE_CONFIG, PEM_TYPE);
        if (writeToFile)
            writeToFile(ksPath, key, certChain);
        else {
            sslProps.put(SslConfigs.SSL_KEYSTORE_KEY_CONFIG, key);
            sslProps.put(SslConfigs.SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG, certChain);
            sslProps.remove(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG);
        }
    }
}
Also used : DEROctetString(org.bouncycastle.asn1.DEROctetString) Password(org.apache.kafka.common.config.types.Password)

Example 14 with Password

use of org.apache.kafka.common.config.types.Password in project kafka by apache.

the class AbstractConfigTest method testValuesWithSecondaryPrefix.

@Test
public void testValuesWithSecondaryPrefix() {
    String prefix = "listener.name.listener1.";
    Password saslJaasConfig1 = new Password("test.myLoginModule1 required;");
    Password saslJaasConfig2 = new Password("test.myLoginModule2 required;");
    Password saslJaasConfig3 = new Password("test.myLoginModule3 required;");
    Properties props = new Properties();
    props.put("listener.name.listener1.test-mechanism.sasl.jaas.config", saslJaasConfig1.value());
    props.put("test-mechanism.sasl.jaas.config", saslJaasConfig2.value());
    props.put("sasl.jaas.config", saslJaasConfig3.value());
    props.put("listener.name.listener1.gssapi.sasl.kerberos.kinit.cmd", "/usr/bin/kinit2");
    props.put("listener.name.listener1.gssapi.sasl.kerberos.service.name", "testkafka");
    props.put("listener.name.listener1.gssapi.sasl.kerberos.min.time.before.relogin", "60000");
    props.put("ssl.provider", "TEST");
    TestSecurityConfig config = new TestSecurityConfig(props);
    Map<String, Object> valuesWithPrefixOverride = config.valuesWithPrefixOverride(prefix);
    // prefix with mechanism overrides global
    assertTrue(config.unused().contains("listener.name.listener1.test-mechanism.sasl.jaas.config"));
    assertTrue(config.unused().contains("test-mechanism.sasl.jaas.config"));
    assertEquals(saslJaasConfig1, valuesWithPrefixOverride.get("test-mechanism.sasl.jaas.config"));
    assertEquals(saslJaasConfig3, valuesWithPrefixOverride.get("sasl.jaas.config"));
    assertFalse(config.unused().contains("listener.name.listener1.test-mechanism.sasl.jaas.config"));
    assertFalse(config.unused().contains("test-mechanism.sasl.jaas.config"));
    assertFalse(config.unused().contains("sasl.jaas.config"));
    // prefix with mechanism overrides default
    assertFalse(config.unused().contains("sasl.kerberos.kinit.cmd"));
    assertTrue(config.unused().contains("listener.name.listener1.gssapi.sasl.kerberos.kinit.cmd"));
    assertFalse(config.unused().contains("gssapi.sasl.kerberos.kinit.cmd"));
    assertFalse(config.unused().contains("sasl.kerberos.kinit.cmd"));
    assertEquals("/usr/bin/kinit2", valuesWithPrefixOverride.get("gssapi.sasl.kerberos.kinit.cmd"));
    assertFalse(config.unused().contains("listener.name.listener1.sasl.kerberos.kinit.cmd"));
    // prefix override for mechanism with no default
    assertFalse(config.unused().contains("sasl.kerberos.service.name"));
    assertTrue(config.unused().contains("listener.name.listener1.gssapi.sasl.kerberos.service.name"));
    assertFalse(config.unused().contains("gssapi.sasl.kerberos.service.name"));
    assertFalse(config.unused().contains("sasl.kerberos.service.name"));
    assertEquals("testkafka", valuesWithPrefixOverride.get("gssapi.sasl.kerberos.service.name"));
    assertFalse(config.unused().contains("listener.name.listener1.gssapi.sasl.kerberos.service.name"));
    // unset with no default
    assertTrue(config.unused().contains("ssl.provider"));
    assertNull(valuesWithPrefixOverride.get("gssapi.ssl.provider"));
    assertTrue(config.unused().contains("ssl.provider"));
}
Also used : TestSecurityConfig(org.apache.kafka.common.security.TestSecurityConfig) Properties(java.util.Properties) Password(org.apache.kafka.common.config.types.Password) Test(org.junit.jupiter.api.Test)

Example 15 with Password

use of org.apache.kafka.common.config.types.Password in project kafka by apache.

the class ConfigDefTest method testBasicTypes.

@Test
public void testBasicTypes() {
    ConfigDef def = new ConfigDef().define("a", Type.INT, 5, Range.between(0, 14), Importance.HIGH, "docs").define("b", Type.LONG, Importance.HIGH, "docs").define("c", Type.STRING, "hello", Importance.HIGH, "docs").define("d", Type.LIST, Importance.HIGH, "docs").define("e", Type.DOUBLE, Importance.HIGH, "docs").define("f", Type.CLASS, Importance.HIGH, "docs").define("g", Type.BOOLEAN, Importance.HIGH, "docs").define("h", Type.BOOLEAN, Importance.HIGH, "docs").define("i", Type.BOOLEAN, Importance.HIGH, "docs").define("j", Type.PASSWORD, Importance.HIGH, "docs");
    Properties props = new Properties();
    props.put("a", "1   ");
    props.put("b", 2);
    props.put("d", " a , b, c");
    props.put("e", 42.5d);
    props.put("f", String.class.getName());
    props.put("g", "true");
    props.put("h", "FalSE");
    props.put("i", "TRUE");
    props.put("j", "password");
    Map<String, Object> vals = def.parse(props);
    assertEquals(1, vals.get("a"));
    assertEquals(2L, vals.get("b"));
    assertEquals("hello", vals.get("c"));
    assertEquals(asList("a", "b", "c"), vals.get("d"));
    assertEquals(42.5d, vals.get("e"));
    assertEquals(String.class, vals.get("f"));
    assertEquals(true, vals.get("g"));
    assertEquals(false, vals.get("h"));
    assertEquals(true, vals.get("i"));
    assertEquals(new Password("password"), vals.get("j"));
    assertEquals(Password.HIDDEN, vals.get("j").toString());
}
Also used : CaseInsensitiveValidString(org.apache.kafka.common.config.ConfigDef.CaseInsensitiveValidString) ValidString(org.apache.kafka.common.config.ConfigDef.ValidString) Properties(java.util.Properties) Password(org.apache.kafka.common.config.types.Password) Test(org.junit.jupiter.api.Test)

Aggregations

Password (org.apache.kafka.common.config.types.Password)41 HashMap (java.util.HashMap)9 Properties (java.util.Properties)7 Test (org.junit.Test)7 TestSecurityConfig (org.apache.kafka.common.security.TestSecurityConfig)6 PlainLoginModule (org.apache.kafka.common.security.plain.PlainLoginModule)6 Test (org.junit.jupiter.api.Test)6 InetSocketAddress (java.net.InetSocketAddress)5 SecurityProtocol (org.apache.kafka.common.security.auth.SecurityProtocol)5 DEROctetString (org.bouncycastle.asn1.DEROctetString)5 KeyStore (java.security.KeyStore)4 AppConfigurationEntry (javax.security.auth.login.AppConfigurationEntry)4 ValidString (org.apache.kafka.common.config.ConfigDef.ValidString)4 X509Certificate (java.security.cert.X509Certificate)3 LogContext (org.apache.kafka.common.utils.LogContext)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ArgumentsSource (org.junit.jupiter.params.provider.ArgumentsSource)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2