-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KAFKA-17921: Support SASL_PLAINTEXT protocol with java.security.auth.…
…login.config Signed-off-by: PoAn Yang <[email protected]>
- Loading branch information
1 parent
55577e7
commit 535eedb
Showing
7 changed files
with
234 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
test-common/src/main/java/org/apache/kafka/common/test/JaasModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.kafka.common.test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class JaasModule { | ||
|
||
public static JaasModule plainLoginModule(String username, String password, boolean debug, Map<String, String> validUsers) { | ||
String name = "org.apache.kafka.common.security.plain.PlainLoginModule"; | ||
|
||
Map<String, String> entries = new HashMap<>(); | ||
entries.put("username", username); | ||
entries.put("password", password); | ||
validUsers.forEach((user, pass) -> entries.put("user_" + user, pass)); | ||
|
||
return new JaasModule( | ||
name, | ||
debug, | ||
entries | ||
); | ||
} | ||
|
||
private final String name; | ||
|
||
private final boolean debug; | ||
|
||
private final Map<String, String> entries; | ||
|
||
private JaasModule(String name, boolean debug, Map<String, String> entries) { | ||
this.name = name; | ||
this.debug = debug; | ||
this.entries = entries; | ||
} | ||
|
||
public String name() { | ||
return name; | ||
} | ||
|
||
public boolean debug() { | ||
return debug; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s required%n debug=%b%n %s;%n", name, debug, entries.entrySet().stream() | ||
.map(e -> e.getKey() + "=\"" + e.getValue() + "\"") | ||
.collect(Collectors.joining("\n "))); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
test-common/src/main/java/org/apache/kafka/common/test/JaasTestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.kafka.common.test; | ||
|
||
import org.apache.kafka.common.security.JaasUtils; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStreamWriter; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.security.auth.login.Configuration; | ||
|
||
public class JaasTestUtils { | ||
public static class JaasSection { | ||
private final String contextName; | ||
private final List<JaasModule> modules; | ||
|
||
public JaasSection(String contextName, List<JaasModule> modules) { | ||
this.contextName = contextName; | ||
this.modules = modules; | ||
} | ||
|
||
public List<JaasModule> getModules() { | ||
return modules; | ||
} | ||
|
||
public String getContextName() { | ||
return contextName; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s {%n %s%n};%n", | ||
contextName, | ||
modules.stream().map(Object::toString).collect(Collectors.joining("\n "))); | ||
} | ||
} | ||
|
||
public static final String KAFKA_SERVER_CONTEXT_NAME = "KafkaServer"; | ||
|
||
public static final String KAFKA_PLAIN_USER = "plain-user"; | ||
public static final String KAFKA_PLAIN_PASSWORD = "plain-user-secret"; | ||
public static final String KAFKA_PLAIN_ADMIN = "plain-admin"; | ||
public static final String KAFKA_PLAIN_ADMIN_PASSWORD = "plain-admin-secret"; | ||
|
||
public static File writeJaasContextsToFile(List<JaasSection> jaasSections) throws IOException { | ||
File jaasFile = TestUtils.tempFile(); | ||
try (FileOutputStream fileStream = new FileOutputStream(jaasFile); | ||
OutputStreamWriter writer = new OutputStreamWriter(fileStream, StandardCharsets.UTF_8);) { | ||
writer.write(String.join("", jaasSections.stream().map(Object::toString).toArray(String[]::new))); | ||
} | ||
return jaasFile; | ||
} | ||
|
||
public static void refreshJavaLoginConfigParam(File file) { | ||
System.setProperty(JaasUtils.JAVA_LOGIN_CONFIG_PARAM, file.getAbsolutePath()); | ||
// This will cause a reload of the Configuration singleton when `getConfiguration` is called | ||
Configuration.setConfiguration(null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters